xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision f1cc607e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 /*
26  * Copyright (c) 2011 Bayard G. Bell.  All rights reserved.
27  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
28  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
29  * Copyright 2017 Nexenta Systems, Inc.
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_write_cache_changeable	ssd_get_write_cache_changeable
932 #define	sd_get_nv_sup			ssd_get_nv_sup
933 #define	sd_make_device			ssd_make_device
934 #define	sdopen				ssdopen
935 #define	sdclose				ssdclose
936 #define	sd_ready_and_valid		ssd_ready_and_valid
937 #define	sdmin				ssdmin
938 #define	sdread				ssdread
939 #define	sdwrite				ssdwrite
940 #define	sdaread				ssdaread
941 #define	sdawrite			ssdawrite
942 #define	sdstrategy			ssdstrategy
943 #define	sdioctl				ssdioctl
944 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
945 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
946 #define	sd_checksum_iostart		ssd_checksum_iostart
947 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
948 #define	sd_pm_iostart			ssd_pm_iostart
949 #define	sd_core_iostart			ssd_core_iostart
950 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
951 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
952 #define	sd_checksum_iodone		ssd_checksum_iodone
953 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
954 #define	sd_pm_iodone			ssd_pm_iodone
955 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
956 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
957 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
958 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
959 #define	sd_buf_iodone			ssd_buf_iodone
960 #define	sd_uscsi_strategy		ssd_uscsi_strategy
961 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
962 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
963 #define	sd_uscsi_iodone			ssd_uscsi_iodone
964 #define	sd_xbuf_strategy		ssd_xbuf_strategy
965 #define	sd_xbuf_init			ssd_xbuf_init
966 #define	sd_pm_entry			ssd_pm_entry
967 #define	sd_pm_exit			ssd_pm_exit
968 
969 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
970 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
971 
972 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
973 #define	sdintr				ssdintr
974 #define	sd_start_cmds			ssd_start_cmds
975 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
976 #define	sd_bioclone_alloc		ssd_bioclone_alloc
977 #define	sd_bioclone_free		ssd_bioclone_free
978 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
979 #define	sd_shadow_buf_free		ssd_shadow_buf_free
980 #define	sd_print_transport_rejected_message	\
981 					ssd_print_transport_rejected_message
982 #define	sd_retry_command		ssd_retry_command
983 #define	sd_set_retry_bp			ssd_set_retry_bp
984 #define	sd_send_request_sense_command	ssd_send_request_sense_command
985 #define	sd_start_retry_command		ssd_start_retry_command
986 #define	sd_start_direct_priority_command	\
987 					ssd_start_direct_priority_command
988 #define	sd_return_failed_command	ssd_return_failed_command
989 #define	sd_return_failed_command_no_restart	\
990 					ssd_return_failed_command_no_restart
991 #define	sd_return_command		ssd_return_command
992 #define	sd_sync_with_callback		ssd_sync_with_callback
993 #define	sdrunout			ssdrunout
994 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
995 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
996 #define	sd_reduce_throttle		ssd_reduce_throttle
997 #define	sd_restore_throttle		ssd_restore_throttle
998 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
999 #define	sd_init_cdb_limits		ssd_init_cdb_limits
1000 #define	sd_pkt_status_good		ssd_pkt_status_good
1001 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
1002 #define	sd_pkt_status_busy		ssd_pkt_status_busy
1003 #define	sd_pkt_status_reservation_conflict	\
1004 					ssd_pkt_status_reservation_conflict
1005 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
1006 #define	sd_handle_request_sense		ssd_handle_request_sense
1007 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
1008 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
1009 #define	sd_validate_sense_data		ssd_validate_sense_data
1010 #define	sd_decode_sense			ssd_decode_sense
1011 #define	sd_print_sense_msg		ssd_print_sense_msg
1012 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
1013 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
1014 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
1015 #define	sd_sense_key_medium_or_hardware_error	\
1016 					ssd_sense_key_medium_or_hardware_error
1017 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
1018 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
1019 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
1020 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
1021 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
1022 #define	sd_sense_key_default		ssd_sense_key_default
1023 #define	sd_print_retry_msg		ssd_print_retry_msg
1024 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
1025 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
1026 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
1027 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
1028 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
1029 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
1030 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
1031 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
1032 #define	sd_pkt_reason_default		ssd_pkt_reason_default
1033 #define	sd_reset_target			ssd_reset_target
1034 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
1035 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
1036 #define	sd_taskq_create			ssd_taskq_create
1037 #define	sd_taskq_delete			ssd_taskq_delete
1038 #define	sd_target_change_task		ssd_target_change_task
1039 #define	sd_log_dev_status_event		ssd_log_dev_status_event
1040 #define	sd_log_lun_expansion_event	ssd_log_lun_expansion_event
1041 #define	sd_log_eject_request_event	ssd_log_eject_request_event
1042 #define	sd_media_change_task		ssd_media_change_task
1043 #define	sd_handle_mchange		ssd_handle_mchange
1044 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
1045 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
1046 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
1047 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
1048 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
1049 					sd_send_scsi_feature_GET_CONFIGURATION
1050 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
1051 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
1052 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
1053 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
1054 					ssd_send_scsi_PERSISTENT_RESERVE_IN
1055 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
1056 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
1057 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
1058 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
1059 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1060 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
1061 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
1062 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
1063 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
1064 #define	sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION	\
1065 				ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1066 #define	sd_gesn_media_data_valid	ssd_gesn_media_data_valid
1067 #define	sd_alloc_rqs			ssd_alloc_rqs
1068 #define	sd_free_rqs			ssd_free_rqs
1069 #define	sd_dump_memory			ssd_dump_memory
1070 #define	sd_get_media_info_com		ssd_get_media_info_com
1071 #define	sd_get_media_info		ssd_get_media_info
1072 #define	sd_get_media_info_ext		ssd_get_media_info_ext
1073 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
1074 #define	sd_nvpair_str_decode		ssd_nvpair_str_decode
1075 #define	sd_strtok_r			ssd_strtok_r
1076 #define	sd_set_properties		ssd_set_properties
1077 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
1078 #define	sd_setup_next_xfer		ssd_setup_next_xfer
1079 #define	sd_dkio_get_temp		ssd_dkio_get_temp
1080 #define	sd_check_mhd			ssd_check_mhd
1081 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1082 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1083 #define	sd_sname			ssd_sname
1084 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1085 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1086 #define	sd_take_ownership		ssd_take_ownership
1087 #define	sd_reserve_release		ssd_reserve_release
1088 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1089 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1090 #define	sd_persistent_reservation_in_read_keys	\
1091 					ssd_persistent_reservation_in_read_keys
1092 #define	sd_persistent_reservation_in_read_resv	\
1093 					ssd_persistent_reservation_in_read_resv
1094 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1095 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1096 #define	sd_mhdioc_release		ssd_mhdioc_release
1097 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1098 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1099 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1100 #define	sr_change_blkmode		ssr_change_blkmode
1101 #define	sr_change_speed			ssr_change_speed
1102 #define	sr_atapi_change_speed		ssr_atapi_change_speed
1103 #define	sr_pause_resume			ssr_pause_resume
1104 #define	sr_play_msf			ssr_play_msf
1105 #define	sr_play_trkind			ssr_play_trkind
1106 #define	sr_read_all_subcodes		ssr_read_all_subcodes
1107 #define	sr_read_subchannel		ssr_read_subchannel
1108 #define	sr_read_tocentry		ssr_read_tocentry
1109 #define	sr_read_tochdr			ssr_read_tochdr
1110 #define	sr_read_cdda			ssr_read_cdda
1111 #define	sr_read_cdxa			ssr_read_cdxa
1112 #define	sr_read_mode1			ssr_read_mode1
1113 #define	sr_read_mode2			ssr_read_mode2
1114 #define	sr_read_cd_mode2		ssr_read_cd_mode2
1115 #define	sr_sector_mode			ssr_sector_mode
1116 #define	sr_eject			ssr_eject
1117 #define	sr_ejected			ssr_ejected
1118 #define	sr_check_wp			ssr_check_wp
1119 #define	sd_watch_request_submit		ssd_watch_request_submit
1120 #define	sd_check_media			ssd_check_media
1121 #define	sd_media_watch_cb		ssd_media_watch_cb
1122 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1123 #define	sr_volume_ctrl			ssr_volume_ctrl
1124 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1125 #define	sd_log_page_supported		ssd_log_page_supported
1126 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1127 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1128 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1129 #define	sd_range_lock			ssd_range_lock
1130 #define	sd_get_range			ssd_get_range
1131 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1132 #define	sd_range_unlock			ssd_range_unlock
1133 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1134 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1135 
1136 #define	sd_iostart_chain		ssd_iostart_chain
1137 #define	sd_iodone_chain			ssd_iodone_chain
1138 #define	sd_initpkt_map			ssd_initpkt_map
1139 #define	sd_destroypkt_map		ssd_destroypkt_map
1140 #define	sd_chain_type_map		ssd_chain_type_map
1141 #define	sd_chain_index_map		ssd_chain_index_map
1142 
1143 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1144 #define	sd_failfast_flushq		ssd_failfast_flushq
1145 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1146 
1147 #define	sd_is_lsi			ssd_is_lsi
1148 #define	sd_tg_rdwr			ssd_tg_rdwr
1149 #define	sd_tg_getinfo			ssd_tg_getinfo
1150 #define	sd_rmw_msg_print_handler	ssd_rmw_msg_print_handler
1151 
1152 #endif	/* #if (defined(__fibre)) */
1153 
1154 
1155 int _init(void);
1156 int _fini(void);
1157 int _info(struct modinfo *modinfop);
1158 
1159 /*PRINTFLIKE3*/
1160 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1161 /*PRINTFLIKE3*/
1162 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1163 /*PRINTFLIKE3*/
1164 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1165 
1166 static int sdprobe(dev_info_t *devi);
1167 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1168     void **result);
1169 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1170     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1171 
1172 /*
1173  * Smart probe for parallel scsi
1174  */
1175 static void sd_scsi_probe_cache_init(void);
1176 static void sd_scsi_probe_cache_fini(void);
1177 static void sd_scsi_clear_probe_cache(void);
1178 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1179 
1180 /*
1181  * Attached luns on target for parallel scsi
1182  */
1183 static void sd_scsi_target_lun_init(void);
1184 static void sd_scsi_target_lun_fini(void);
1185 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1186 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1187 
1188 static int	sd_spin_up_unit(sd_ssc_t *ssc);
1189 
1190 /*
1191  * Using sd_ssc_init to establish sd_ssc_t struct
1192  * Using sd_ssc_send to send uscsi internal command
1193  * Using sd_ssc_fini to free sd_ssc_t struct
1194  */
1195 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1196 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1197     int flag, enum uio_seg dataspace, int path_flag);
1198 static void sd_ssc_fini(sd_ssc_t *ssc);
1199 
1200 /*
1201  * Using sd_ssc_assessment to set correct type-of-assessment
1202  * Using sd_ssc_post to post ereport & system log
1203  *       sd_ssc_post will call sd_ssc_print to print system log
1204  *       sd_ssc_post will call sd_ssd_ereport_post to post ereport
1205  */
1206 static void sd_ssc_assessment(sd_ssc_t *ssc,
1207     enum sd_type_assessment tp_assess);
1208 
1209 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1210 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1211 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1212     enum sd_driver_assessment drv_assess);
1213 
1214 /*
1215  * Using sd_ssc_set_info to mark an un-decodable-data error.
1216  * Using sd_ssc_extract_info to transfer information from internal
1217  *       data structures to sd_ssc_t.
1218  */
1219 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1220     const char *fmt, ...);
1221 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1222     struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1223 
1224 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1225     enum uio_seg dataspace, int path_flag);
1226 
1227 #ifdef _LP64
1228 static void	sd_enable_descr_sense(sd_ssc_t *ssc);
1229 static void	sd_reenable_dsense_task(void *arg);
1230 #endif /* _LP64 */
1231 
1232 static void	sd_set_mmc_caps(sd_ssc_t *ssc);
1233 
1234 static void sd_read_unit_properties(struct sd_lun *un);
1235 static int  sd_process_sdconf_file(struct sd_lun *un);
1236 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1237 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1238 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1239 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1240     int *data_list, sd_tunables *values);
1241 static void sd_process_sdconf_table(struct sd_lun *un);
1242 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1243 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1244 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1245 	int list_len, char *dataname_ptr);
1246 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1247     sd_tunables *prop_list);
1248 
1249 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1250     int reservation_flag);
1251 static int  sd_get_devid(sd_ssc_t *ssc);
1252 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1253 static int  sd_write_deviceid(sd_ssc_t *ssc);
1254 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1255 static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
1256 
1257 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1258 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1259 
1260 static int  sd_ddi_suspend(dev_info_t *devi);
1261 static int  sd_ddi_resume(dev_info_t *devi);
1262 static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
1263 static int  sdpower(dev_info_t *devi, int component, int level);
1264 
1265 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1266 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1267 static int  sd_unit_attach(dev_info_t *devi);
1268 static int  sd_unit_detach(dev_info_t *devi);
1269 
1270 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1271 static void sd_create_errstats(struct sd_lun *un, int instance);
1272 static void sd_set_errstats(struct sd_lun *un);
1273 static void sd_set_pstats(struct sd_lun *un);
1274 
1275 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1276 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1277 static int  sd_send_polled_RQS(struct sd_lun *un);
1278 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1279 
1280 #if (defined(__fibre))
1281 /*
1282  * Event callbacks (photon)
1283  */
1284 static void sd_init_event_callbacks(struct sd_lun *un);
1285 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1286 #endif
1287 
1288 /*
1289  * Defines for sd_cache_control
1290  */
1291 
1292 #define	SD_CACHE_ENABLE		1
1293 #define	SD_CACHE_DISABLE	0
1294 #define	SD_CACHE_NOCHANGE	-1
1295 
1296 static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1297 static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1298 static void  sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
1299 static void  sd_get_nv_sup(sd_ssc_t *ssc);
1300 static dev_t sd_make_device(dev_info_t *devi);
1301 static void  sd_check_solid_state(sd_ssc_t *ssc);
1302 static void  sd_check_emulation_mode(sd_ssc_t *ssc);
1303 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1304 	uint64_t capacity);
1305 
1306 /*
1307  * Driver entry point functions.
1308  */
1309 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1310 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1311 static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
1312 
1313 static void sdmin(struct buf *bp);
1314 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1315 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1316 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1317 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1318 
1319 static int sdstrategy(struct buf *bp);
1320 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1321 
1322 /*
1323  * Function prototypes for layering functions in the iostart chain.
1324  */
1325 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1326 	struct buf *bp);
1327 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1328 	struct buf *bp);
1329 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1330 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1331 	struct buf *bp);
1332 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1333 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1334 
1335 /*
1336  * Function prototypes for layering functions in the iodone chain.
1337  */
1338 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1339 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1340 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1341 	struct buf *bp);
1342 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1343 	struct buf *bp);
1344 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1345 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1346 	struct buf *bp);
1347 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1348 
1349 /*
1350  * Prototypes for functions to support buf(9S) based IO.
1351  */
1352 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1353 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1354 static void sd_destroypkt_for_buf(struct buf *);
1355 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1356 	struct buf *bp, int flags,
1357 	int (*callback)(caddr_t), caddr_t callback_arg,
1358 	diskaddr_t lba, uint32_t blockcount);
1359 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1360 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1361 
1362 /*
1363  * Prototypes for functions to support USCSI IO.
1364  */
1365 static int sd_uscsi_strategy(struct buf *bp);
1366 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1367 static void sd_destroypkt_for_uscsi(struct buf *);
1368 
1369 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1370 	uchar_t chain_type, void *pktinfop);
1371 
1372 static int  sd_pm_entry(struct sd_lun *un);
1373 static void sd_pm_exit(struct sd_lun *un);
1374 
1375 static void sd_pm_idletimeout_handler(void *arg);
1376 
1377 /*
1378  * sd_core internal functions (used at the sd_core_io layer).
1379  */
1380 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1381 static void sdintr(struct scsi_pkt *pktp);
1382 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1383 
1384 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1385 	enum uio_seg dataspace, int path_flag);
1386 
1387 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1388 	daddr_t blkno, int (*func)(struct buf *));
1389 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1390 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1391 static void sd_bioclone_free(struct buf *bp);
1392 static void sd_shadow_buf_free(struct buf *bp);
1393 
1394 static void sd_print_transport_rejected_message(struct sd_lun *un,
1395 	struct sd_xbuf *xp, int code);
1396 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1397     void *arg, int code);
1398 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1399     void *arg, int code);
1400 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1401     void *arg, int code);
1402 
1403 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1404 	int retry_check_flag,
1405 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1406 		int c),
1407 	void *user_arg, int failure_code,  clock_t retry_delay,
1408 	void (*statp)(kstat_io_t *));
1409 
1410 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1411 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1412 
1413 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1414 	struct scsi_pkt *pktp);
1415 static void sd_start_retry_command(void *arg);
1416 static void sd_start_direct_priority_command(void *arg);
1417 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1418 	int errcode);
1419 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1420 	struct buf *bp, int errcode);
1421 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1422 static void sd_sync_with_callback(struct sd_lun *un);
1423 static int sdrunout(caddr_t arg);
1424 
1425 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1426 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1427 
1428 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1429 static void sd_restore_throttle(void *arg);
1430 
1431 static void sd_init_cdb_limits(struct sd_lun *un);
1432 
1433 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1434 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1435 
1436 /*
1437  * Error handling functions
1438  */
1439 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1440 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1441 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1442 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1443 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1444 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1445 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1446 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1447 
1448 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1449 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1450 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1451 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1452 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1453 	struct sd_xbuf *xp, size_t actual_len);
1454 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1455 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1456 
1457 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1458 	void *arg, int code);
1459 
1460 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1461 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1462 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1463 	uint8_t *sense_datap,
1464 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1465 static void sd_sense_key_not_ready(struct sd_lun *un,
1466 	uint8_t *sense_datap,
1467 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1468 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1469 	uint8_t *sense_datap,
1470 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1471 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1472 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1473 static void sd_sense_key_unit_attention(struct sd_lun *un,
1474 	uint8_t *sense_datap,
1475 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1476 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1477 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1478 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1479 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1480 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1481 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1482 static void sd_sense_key_default(struct sd_lun *un,
1483 	uint8_t *sense_datap,
1484 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1485 
1486 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1487 	void *arg, int flag);
1488 
1489 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1490 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1491 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1492 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1493 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1494 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1495 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1496 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1497 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1498 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1499 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1500 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1501 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1502 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1503 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1504 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1505 
1506 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1507 
1508 static void sd_start_stop_unit_callback(void *arg);
1509 static void sd_start_stop_unit_task(void *arg);
1510 
1511 static void sd_taskq_create(void);
1512 static void sd_taskq_delete(void);
1513 static void sd_target_change_task(void *arg);
1514 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1515 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1516 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1517 static void sd_media_change_task(void *arg);
1518 
1519 static int sd_handle_mchange(struct sd_lun *un);
1520 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1521 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1522 	uint32_t *lbap, int path_flag);
1523 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1524 	uint32_t *lbap, uint32_t *psp, int path_flag);
1525 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1526 	int flag, int path_flag);
1527 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1528 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1529 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1530 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1531 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1532 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1533 	uchar_t usr_cmd, uchar_t *usr_bufp);
1534 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1535 	struct dk_callback *dkc);
1536 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1537 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1538 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1539 	uchar_t *bufaddr, uint_t buflen, int path_flag);
1540 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1541 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1542 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1543 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1544 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1545 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1546 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1547 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1548 	size_t buflen, daddr_t start_block, int path_flag);
1549 #define	sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag)	\
1550 	sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1551 	path_flag)
1552 #define	sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1553 	sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1554 	path_flag)
1555 
1556 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1557 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1558 	uint16_t param_ptr, int path_flag);
1559 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1560 	uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1561 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1562 
1563 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1564 static void sd_free_rqs(struct sd_lun *un);
1565 
1566 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1567 	uchar_t *data, int len, int fmt);
1568 static void sd_panic_for_res_conflict(struct sd_lun *un);
1569 
1570 /*
1571  * Disk Ioctl Function Prototypes
1572  */
1573 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1574 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1575 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1576 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1577 
1578 /*
1579  * Multi-host Ioctl Prototypes
1580  */
1581 static int sd_check_mhd(dev_t dev, int interval);
1582 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1583 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1584 static char *sd_sname(uchar_t status);
1585 static void sd_mhd_resvd_recover(void *arg);
1586 static void sd_resv_reclaim_thread();
1587 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1588 static int sd_reserve_release(dev_t dev, int cmd);
1589 static void sd_rmv_resv_reclaim_req(dev_t dev);
1590 static void sd_mhd_reset_notify_cb(caddr_t arg);
1591 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1592 	mhioc_inkeys_t *usrp, int flag);
1593 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1594 	mhioc_inresvs_t *usrp, int flag);
1595 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1596 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1597 static int sd_mhdioc_release(dev_t dev);
1598 static int sd_mhdioc_register_devid(dev_t dev);
1599 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1600 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1601 
1602 /*
1603  * SCSI removable prototypes
1604  */
1605 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1606 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1607 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1608 static int sr_pause_resume(dev_t dev, int mode);
1609 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1610 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1611 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1612 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1613 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1614 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1615 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1616 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1617 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1618 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1619 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1620 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1621 static int sr_eject(dev_t dev);
1622 static void sr_ejected(register struct sd_lun *un);
1623 static int sr_check_wp(dev_t dev);
1624 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1625 static int sd_check_media(dev_t dev, enum dkio_state state);
1626 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1627 static void sd_delayed_cv_broadcast(void *arg);
1628 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1629 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1630 
1631 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1632 
1633 /*
1634  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1635  */
1636 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1637 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1638 static void sd_wm_cache_destructor(void *wm, void *un);
1639 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1640 	daddr_t endb, ushort_t typ);
1641 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1642 	daddr_t endb);
1643 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1644 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1645 static void sd_read_modify_write_task(void * arg);
1646 static int
1647 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1648 	struct buf **bpp);
1649 
1650 
1651 /*
1652  * Function prototypes for failfast support.
1653  */
1654 static void sd_failfast_flushq(struct sd_lun *un);
1655 static int sd_failfast_flushq_callback(struct buf *bp);
1656 
1657 /*
1658  * Function prototypes to check for lsi devices
1659  */
1660 static void sd_is_lsi(struct sd_lun *un);
1661 
1662 /*
1663  * Function prototypes for partial DMA support
1664  */
1665 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1666 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1667 
1668 
1669 /* Function prototypes for cmlb */
1670 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1671     diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1672 
1673 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1674 
1675 /*
1676  * For printing RMW warning message timely
1677  */
1678 static void sd_rmw_msg_print_handler(void *arg);
1679 
1680 /*
1681  * Constants for failfast support:
1682  *
1683  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1684  * failfast processing being performed.
1685  *
1686  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1687  * failfast processing on all bufs with B_FAILFAST set.
1688  */
1689 
1690 #define	SD_FAILFAST_INACTIVE		0
1691 #define	SD_FAILFAST_ACTIVE		1
1692 
1693 /*
1694  * Bitmask to control behavior of buf(9S) flushes when a transition to
1695  * the failfast state occurs. Optional bits include:
1696  *
1697  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1698  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1699  * be flushed.
1700  *
1701  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1702  * driver, in addition to the regular wait queue. This includes the xbuf
1703  * queues. When clear, only the driver's wait queue will be flushed.
1704  */
1705 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1706 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1707 
1708 /*
1709  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1710  * to flush all queues within the driver.
1711  */
1712 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1713 
1714 
1715 /*
1716  * SD Testing Fault Injection
1717  */
1718 #ifdef SD_FAULT_INJECTION
1719 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1720 static void sd_faultinjection(struct scsi_pkt *pktp);
1721 static void sd_injection_log(char *buf, struct sd_lun *un);
1722 #endif
1723 
1724 /*
1725  * Device driver ops vector
1726  */
1727 static struct cb_ops sd_cb_ops = {
1728 	sdopen,			/* open */
1729 	sdclose,		/* close */
1730 	sdstrategy,		/* strategy */
1731 	nodev,			/* print */
1732 	sddump,			/* dump */
1733 	sdread,			/* read */
1734 	sdwrite,		/* write */
1735 	sdioctl,		/* ioctl */
1736 	nodev,			/* devmap */
1737 	nodev,			/* mmap */
1738 	nodev,			/* segmap */
1739 	nochpoll,		/* poll */
1740 	sd_prop_op,		/* cb_prop_op */
1741 	0,			/* streamtab  */
1742 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1743 	CB_REV,			/* cb_rev */
1744 	sdaread, 		/* async I/O read entry point */
1745 	sdawrite		/* async I/O write entry point */
1746 };
1747 
1748 struct dev_ops sd_ops = {
1749 	DEVO_REV,		/* devo_rev, */
1750 	0,			/* refcnt  */
1751 	sdinfo,			/* info */
1752 	nulldev,		/* identify */
1753 	sdprobe,		/* probe */
1754 	sdattach,		/* attach */
1755 	sddetach,		/* detach */
1756 	nodev,			/* reset */
1757 	&sd_cb_ops,		/* driver operations */
1758 	NULL,			/* bus operations */
1759 	sdpower,		/* power */
1760 	ddi_quiesce_not_needed,		/* quiesce */
1761 };
1762 
1763 /*
1764  * This is the loadable module wrapper.
1765  */
1766 #include <sys/modctl.h>
1767 
1768 #ifndef XPV_HVM_DRIVER
1769 static struct modldrv modldrv = {
1770 	&mod_driverops,		/* Type of module. This one is a driver */
1771 	SD_MODULE_NAME,		/* Module name. */
1772 	&sd_ops			/* driver ops */
1773 };
1774 
1775 static struct modlinkage modlinkage = {
1776 	MODREV_1, &modldrv, NULL
1777 };
1778 
1779 #else /* XPV_HVM_DRIVER */
1780 static struct modlmisc modlmisc = {
1781 	&mod_miscops,		/* Type of module. This one is a misc */
1782 	"HVM " SD_MODULE_NAME,		/* Module name. */
1783 };
1784 
1785 static struct modlinkage modlinkage = {
1786 	MODREV_1, &modlmisc, NULL
1787 };
1788 
1789 #endif /* XPV_HVM_DRIVER */
1790 
1791 static cmlb_tg_ops_t sd_tgops = {
1792 	TG_DK_OPS_VERSION_1,
1793 	sd_tg_rdwr,
1794 	sd_tg_getinfo
1795 };
1796 
1797 static struct scsi_asq_key_strings sd_additional_codes[] = {
1798 	0x81, 0, "Logical Unit is Reserved",
1799 	0x85, 0, "Audio Address Not Valid",
1800 	0xb6, 0, "Media Load Mechanism Failed",
1801 	0xB9, 0, "Audio Play Operation Aborted",
1802 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1803 	0x53, 2, "Medium removal prevented",
1804 	0x6f, 0, "Authentication failed during key exchange",
1805 	0x6f, 1, "Key not present",
1806 	0x6f, 2, "Key not established",
1807 	0x6f, 3, "Read without proper authentication",
1808 	0x6f, 4, "Mismatched region to this logical unit",
1809 	0x6f, 5, "Region reset count error",
1810 	0xffff, 0x0, NULL
1811 };
1812 
1813 
1814 /*
1815  * Struct for passing printing information for sense data messages
1816  */
1817 struct sd_sense_info {
1818 	int	ssi_severity;
1819 	int	ssi_pfa_flag;
1820 };
1821 
1822 /*
1823  * Table of function pointers for iostart-side routines. Separate "chains"
1824  * of layered function calls are formed by placing the function pointers
1825  * sequentially in the desired order. Functions are called according to an
1826  * incrementing table index ordering. The last function in each chain must
1827  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1828  * in the sd_iodone_chain[] array.
1829  *
1830  * Note: It may seem more natural to organize both the iostart and iodone
1831  * functions together, into an array of structures (or some similar
1832  * organization) with a common index, rather than two separate arrays which
1833  * must be maintained in synchronization. The purpose of this division is
1834  * to achieve improved performance: individual arrays allows for more
1835  * effective cache line utilization on certain platforms.
1836  */
1837 
1838 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1839 
1840 
1841 static sd_chain_t sd_iostart_chain[] = {
1842 
1843 	/* Chain for buf IO for disk drive targets (PM enabled) */
1844 	sd_mapblockaddr_iostart,	/* Index: 0 */
1845 	sd_pm_iostart,			/* Index: 1 */
1846 	sd_core_iostart,		/* Index: 2 */
1847 
1848 	/* Chain for buf IO for disk drive targets (PM disabled) */
1849 	sd_mapblockaddr_iostart,	/* Index: 3 */
1850 	sd_core_iostart,		/* Index: 4 */
1851 
1852 	/*
1853 	 * Chain for buf IO for removable-media or large sector size
1854 	 * disk drive targets with RMW needed (PM enabled)
1855 	 */
1856 	sd_mapblockaddr_iostart,	/* Index: 5 */
1857 	sd_mapblocksize_iostart,	/* Index: 6 */
1858 	sd_pm_iostart,			/* Index: 7 */
1859 	sd_core_iostart,		/* Index: 8 */
1860 
1861 	/*
1862 	 * Chain for buf IO for removable-media or large sector size
1863 	 * disk drive targets with RMW needed (PM disabled)
1864 	 */
1865 	sd_mapblockaddr_iostart,	/* Index: 9 */
1866 	sd_mapblocksize_iostart,	/* Index: 10 */
1867 	sd_core_iostart,		/* Index: 11 */
1868 
1869 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1870 	sd_mapblockaddr_iostart,	/* Index: 12 */
1871 	sd_checksum_iostart,		/* Index: 13 */
1872 	sd_pm_iostart,			/* Index: 14 */
1873 	sd_core_iostart,		/* Index: 15 */
1874 
1875 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1876 	sd_mapblockaddr_iostart,	/* Index: 16 */
1877 	sd_checksum_iostart,		/* Index: 17 */
1878 	sd_core_iostart,		/* Index: 18 */
1879 
1880 	/* Chain for USCSI commands (all targets) */
1881 	sd_pm_iostart,			/* Index: 19 */
1882 	sd_core_iostart,		/* Index: 20 */
1883 
1884 	/* Chain for checksumming USCSI commands (all targets) */
1885 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1886 	sd_pm_iostart,			/* Index: 22 */
1887 	sd_core_iostart,		/* Index: 23 */
1888 
1889 	/* Chain for "direct" USCSI commands (all targets) */
1890 	sd_core_iostart,		/* Index: 24 */
1891 
1892 	/* Chain for "direct priority" USCSI commands (all targets) */
1893 	sd_core_iostart,		/* Index: 25 */
1894 
1895 	/*
1896 	 * Chain for buf IO for large sector size disk drive targets
1897 	 * with RMW needed with checksumming (PM enabled)
1898 	 */
1899 	sd_mapblockaddr_iostart,	/* Index: 26 */
1900 	sd_mapblocksize_iostart,	/* Index: 27 */
1901 	sd_checksum_iostart,		/* Index: 28 */
1902 	sd_pm_iostart,			/* Index: 29 */
1903 	sd_core_iostart,		/* Index: 30 */
1904 
1905 	/*
1906 	 * Chain for buf IO for large sector size disk drive targets
1907 	 * with RMW needed with checksumming (PM disabled)
1908 	 */
1909 	sd_mapblockaddr_iostart,	/* Index: 31 */
1910 	sd_mapblocksize_iostart,	/* Index: 32 */
1911 	sd_checksum_iostart,		/* Index: 33 */
1912 	sd_core_iostart,		/* Index: 34 */
1913 
1914 };
1915 
1916 /*
1917  * Macros to locate the first function of each iostart chain in the
1918  * sd_iostart_chain[] array. These are located by the index in the array.
1919  */
1920 #define	SD_CHAIN_DISK_IOSTART			0
1921 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1922 #define	SD_CHAIN_MSS_DISK_IOSTART		5
1923 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1924 #define	SD_CHAIN_MSS_DISK_IOSTART_NO_PM		9
1925 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1926 #define	SD_CHAIN_CHKSUM_IOSTART			12
1927 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1928 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1929 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1930 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1931 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1932 #define	SD_CHAIN_MSS_CHKSUM_IOSTART		26
1933 #define	SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM	31
1934 
1935 
1936 /*
1937  * Table of function pointers for the iodone-side routines for the driver-
1938  * internal layering mechanism.  The calling sequence for iodone routines
1939  * uses a decrementing table index, so the last routine called in a chain
1940  * must be at the lowest array index location for that chain.  The last
1941  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1942  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1943  * of the functions in an iodone side chain must correspond to the ordering
1944  * of the iostart routines for that chain.  Note that there is no iodone
1945  * side routine that corresponds to sd_core_iostart(), so there is no
1946  * entry in the table for this.
1947  */
1948 
1949 static sd_chain_t sd_iodone_chain[] = {
1950 
1951 	/* Chain for buf IO for disk drive targets (PM enabled) */
1952 	sd_buf_iodone,			/* Index: 0 */
1953 	sd_mapblockaddr_iodone,		/* Index: 1 */
1954 	sd_pm_iodone,			/* Index: 2 */
1955 
1956 	/* Chain for buf IO for disk drive targets (PM disabled) */
1957 	sd_buf_iodone,			/* Index: 3 */
1958 	sd_mapblockaddr_iodone,		/* Index: 4 */
1959 
1960 	/*
1961 	 * Chain for buf IO for removable-media or large sector size
1962 	 * disk drive targets with RMW needed (PM enabled)
1963 	 */
1964 	sd_buf_iodone,			/* Index: 5 */
1965 	sd_mapblockaddr_iodone,		/* Index: 6 */
1966 	sd_mapblocksize_iodone,		/* Index: 7 */
1967 	sd_pm_iodone,			/* Index: 8 */
1968 
1969 	/*
1970 	 * Chain for buf IO for removable-media or large sector size
1971 	 * disk drive targets with RMW needed (PM disabled)
1972 	 */
1973 	sd_buf_iodone,			/* Index: 9 */
1974 	sd_mapblockaddr_iodone,		/* Index: 10 */
1975 	sd_mapblocksize_iodone,		/* Index: 11 */
1976 
1977 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1978 	sd_buf_iodone,			/* Index: 12 */
1979 	sd_mapblockaddr_iodone,		/* Index: 13 */
1980 	sd_checksum_iodone,		/* Index: 14 */
1981 	sd_pm_iodone,			/* Index: 15 */
1982 
1983 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1984 	sd_buf_iodone,			/* Index: 16 */
1985 	sd_mapblockaddr_iodone,		/* Index: 17 */
1986 	sd_checksum_iodone,		/* Index: 18 */
1987 
1988 	/* Chain for USCSI commands (non-checksum targets) */
1989 	sd_uscsi_iodone,		/* Index: 19 */
1990 	sd_pm_iodone,			/* Index: 20 */
1991 
1992 	/* Chain for USCSI commands (checksum targets) */
1993 	sd_uscsi_iodone,		/* Index: 21 */
1994 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1995 	sd_pm_iodone,			/* Index: 22 */
1996 
1997 	/* Chain for "direct" USCSI commands (all targets) */
1998 	sd_uscsi_iodone,		/* Index: 24 */
1999 
2000 	/* Chain for "direct priority" USCSI commands (all targets) */
2001 	sd_uscsi_iodone,		/* Index: 25 */
2002 
2003 	/*
2004 	 * Chain for buf IO for large sector size disk drive targets
2005 	 * with checksumming (PM enabled)
2006 	 */
2007 	sd_buf_iodone,			/* Index: 26 */
2008 	sd_mapblockaddr_iodone,		/* Index: 27 */
2009 	sd_mapblocksize_iodone,		/* Index: 28 */
2010 	sd_checksum_iodone,		/* Index: 29 */
2011 	sd_pm_iodone,			/* Index: 30 */
2012 
2013 	/*
2014 	 * Chain for buf IO for large sector size disk drive targets
2015 	 * with checksumming (PM disabled)
2016 	 */
2017 	sd_buf_iodone,			/* Index: 31 */
2018 	sd_mapblockaddr_iodone,		/* Index: 32 */
2019 	sd_mapblocksize_iodone,		/* Index: 33 */
2020 	sd_checksum_iodone,		/* Index: 34 */
2021 };
2022 
2023 
2024 /*
2025  * Macros to locate the "first" function in the sd_iodone_chain[] array for
2026  * each iodone-side chain. These are located by the array index, but as the
2027  * iodone side functions are called in a decrementing-index order, the
2028  * highest index number in each chain must be specified (as these correspond
2029  * to the first function in the iodone chain that will be called by the core
2030  * at IO completion time).
2031  */
2032 
2033 #define	SD_CHAIN_DISK_IODONE			2
2034 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
2035 #define	SD_CHAIN_RMMEDIA_IODONE			8
2036 #define	SD_CHAIN_MSS_DISK_IODONE		8
2037 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
2038 #define	SD_CHAIN_MSS_DISK_IODONE_NO_PM		11
2039 #define	SD_CHAIN_CHKSUM_IODONE			15
2040 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
2041 #define	SD_CHAIN_USCSI_CMD_IODONE		20
2042 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
2043 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
2044 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
2045 #define	SD_CHAIN_MSS_CHKSUM_IODONE		30
2046 #define	SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM	34
2047 
2048 
2049 
2050 /*
2051  * Array to map a layering chain index to the appropriate initpkt routine.
2052  * The redundant entries are present so that the index used for accessing
2053  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2054  * with this table as well.
2055  */
2056 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2057 
2058 static sd_initpkt_t	sd_initpkt_map[] = {
2059 
2060 	/* Chain for buf IO for disk drive targets (PM enabled) */
2061 	sd_initpkt_for_buf,		/* Index: 0 */
2062 	sd_initpkt_for_buf,		/* Index: 1 */
2063 	sd_initpkt_for_buf,		/* Index: 2 */
2064 
2065 	/* Chain for buf IO for disk drive targets (PM disabled) */
2066 	sd_initpkt_for_buf,		/* Index: 3 */
2067 	sd_initpkt_for_buf,		/* Index: 4 */
2068 
2069 	/*
2070 	 * Chain for buf IO for removable-media or large sector size
2071 	 * disk drive targets (PM enabled)
2072 	 */
2073 	sd_initpkt_for_buf,		/* Index: 5 */
2074 	sd_initpkt_for_buf,		/* Index: 6 */
2075 	sd_initpkt_for_buf,		/* Index: 7 */
2076 	sd_initpkt_for_buf,		/* Index: 8 */
2077 
2078 	/*
2079 	 * Chain for buf IO for removable-media or large sector size
2080 	 * disk drive targets (PM disabled)
2081 	 */
2082 	sd_initpkt_for_buf,		/* Index: 9 */
2083 	sd_initpkt_for_buf,		/* Index: 10 */
2084 	sd_initpkt_for_buf,		/* Index: 11 */
2085 
2086 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2087 	sd_initpkt_for_buf,		/* Index: 12 */
2088 	sd_initpkt_for_buf,		/* Index: 13 */
2089 	sd_initpkt_for_buf,		/* Index: 14 */
2090 	sd_initpkt_for_buf,		/* Index: 15 */
2091 
2092 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2093 	sd_initpkt_for_buf,		/* Index: 16 */
2094 	sd_initpkt_for_buf,		/* Index: 17 */
2095 	sd_initpkt_for_buf,		/* Index: 18 */
2096 
2097 	/* Chain for USCSI commands (non-checksum targets) */
2098 	sd_initpkt_for_uscsi,		/* Index: 19 */
2099 	sd_initpkt_for_uscsi,		/* Index: 20 */
2100 
2101 	/* Chain for USCSI commands (checksum targets) */
2102 	sd_initpkt_for_uscsi,		/* Index: 21 */
2103 	sd_initpkt_for_uscsi,		/* Index: 22 */
2104 	sd_initpkt_for_uscsi,		/* Index: 22 */
2105 
2106 	/* Chain for "direct" USCSI commands (all targets) */
2107 	sd_initpkt_for_uscsi,		/* Index: 24 */
2108 
2109 	/* Chain for "direct priority" USCSI commands (all targets) */
2110 	sd_initpkt_for_uscsi,		/* Index: 25 */
2111 
2112 	/*
2113 	 * Chain for buf IO for large sector size disk drive targets
2114 	 * with checksumming (PM enabled)
2115 	 */
2116 	sd_initpkt_for_buf,		/* Index: 26 */
2117 	sd_initpkt_for_buf,		/* Index: 27 */
2118 	sd_initpkt_for_buf,		/* Index: 28 */
2119 	sd_initpkt_for_buf,		/* Index: 29 */
2120 	sd_initpkt_for_buf,		/* Index: 30 */
2121 
2122 	/*
2123 	 * Chain for buf IO for large sector size disk drive targets
2124 	 * with checksumming (PM disabled)
2125 	 */
2126 	sd_initpkt_for_buf,		/* Index: 31 */
2127 	sd_initpkt_for_buf,		/* Index: 32 */
2128 	sd_initpkt_for_buf,		/* Index: 33 */
2129 	sd_initpkt_for_buf,		/* Index: 34 */
2130 };
2131 
2132 
2133 /*
2134  * Array to map a layering chain index to the appropriate destroypktpkt routine.
2135  * The redundant entries are present so that the index used for accessing
2136  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2137  * with this table as well.
2138  */
2139 typedef void (*sd_destroypkt_t)(struct buf *);
2140 
2141 static sd_destroypkt_t	sd_destroypkt_map[] = {
2142 
2143 	/* Chain for buf IO for disk drive targets (PM enabled) */
2144 	sd_destroypkt_for_buf,		/* Index: 0 */
2145 	sd_destroypkt_for_buf,		/* Index: 1 */
2146 	sd_destroypkt_for_buf,		/* Index: 2 */
2147 
2148 	/* Chain for buf IO for disk drive targets (PM disabled) */
2149 	sd_destroypkt_for_buf,		/* Index: 3 */
2150 	sd_destroypkt_for_buf,		/* Index: 4 */
2151 
2152 	/*
2153 	 * Chain for buf IO for removable-media or large sector size
2154 	 * disk drive targets (PM enabled)
2155 	 */
2156 	sd_destroypkt_for_buf,		/* Index: 5 */
2157 	sd_destroypkt_for_buf,		/* Index: 6 */
2158 	sd_destroypkt_for_buf,		/* Index: 7 */
2159 	sd_destroypkt_for_buf,		/* Index: 8 */
2160 
2161 	/*
2162 	 * Chain for buf IO for removable-media or large sector size
2163 	 * disk drive targets (PM disabled)
2164 	 */
2165 	sd_destroypkt_for_buf,		/* Index: 9 */
2166 	sd_destroypkt_for_buf,		/* Index: 10 */
2167 	sd_destroypkt_for_buf,		/* Index: 11 */
2168 
2169 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2170 	sd_destroypkt_for_buf,		/* Index: 12 */
2171 	sd_destroypkt_for_buf,		/* Index: 13 */
2172 	sd_destroypkt_for_buf,		/* Index: 14 */
2173 	sd_destroypkt_for_buf,		/* Index: 15 */
2174 
2175 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2176 	sd_destroypkt_for_buf,		/* Index: 16 */
2177 	sd_destroypkt_for_buf,		/* Index: 17 */
2178 	sd_destroypkt_for_buf,		/* Index: 18 */
2179 
2180 	/* Chain for USCSI commands (non-checksum targets) */
2181 	sd_destroypkt_for_uscsi,	/* Index: 19 */
2182 	sd_destroypkt_for_uscsi,	/* Index: 20 */
2183 
2184 	/* Chain for USCSI commands (checksum targets) */
2185 	sd_destroypkt_for_uscsi,	/* Index: 21 */
2186 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2187 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2188 
2189 	/* Chain for "direct" USCSI commands (all targets) */
2190 	sd_destroypkt_for_uscsi,	/* Index: 24 */
2191 
2192 	/* Chain for "direct priority" USCSI commands (all targets) */
2193 	sd_destroypkt_for_uscsi,	/* Index: 25 */
2194 
2195 	/*
2196 	 * Chain for buf IO for large sector size disk drive targets
2197 	 * with checksumming (PM disabled)
2198 	 */
2199 	sd_destroypkt_for_buf,		/* Index: 26 */
2200 	sd_destroypkt_for_buf,		/* Index: 27 */
2201 	sd_destroypkt_for_buf,		/* Index: 28 */
2202 	sd_destroypkt_for_buf,		/* Index: 29 */
2203 	sd_destroypkt_for_buf,		/* Index: 30 */
2204 
2205 	/*
2206 	 * Chain for buf IO for large sector size disk drive targets
2207 	 * with checksumming (PM enabled)
2208 	 */
2209 	sd_destroypkt_for_buf,		/* Index: 31 */
2210 	sd_destroypkt_for_buf,		/* Index: 32 */
2211 	sd_destroypkt_for_buf,		/* Index: 33 */
2212 	sd_destroypkt_for_buf,		/* Index: 34 */
2213 };
2214 
2215 
2216 
2217 /*
2218  * Array to map a layering chain index to the appropriate chain "type".
2219  * The chain type indicates a specific property/usage of the chain.
2220  * The redundant entries are present so that the index used for accessing
2221  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2222  * with this table as well.
2223  */
2224 
2225 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2226 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2227 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2228 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2229 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2230 						/* (for error recovery) */
2231 
2232 static int sd_chain_type_map[] = {
2233 
2234 	/* Chain for buf IO for disk drive targets (PM enabled) */
2235 	SD_CHAIN_BUFIO,			/* Index: 0 */
2236 	SD_CHAIN_BUFIO,			/* Index: 1 */
2237 	SD_CHAIN_BUFIO,			/* Index: 2 */
2238 
2239 	/* Chain for buf IO for disk drive targets (PM disabled) */
2240 	SD_CHAIN_BUFIO,			/* Index: 3 */
2241 	SD_CHAIN_BUFIO,			/* Index: 4 */
2242 
2243 	/*
2244 	 * Chain for buf IO for removable-media or large sector size
2245 	 * disk drive targets (PM enabled)
2246 	 */
2247 	SD_CHAIN_BUFIO,			/* Index: 5 */
2248 	SD_CHAIN_BUFIO,			/* Index: 6 */
2249 	SD_CHAIN_BUFIO,			/* Index: 7 */
2250 	SD_CHAIN_BUFIO,			/* Index: 8 */
2251 
2252 	/*
2253 	 * Chain for buf IO for removable-media or large sector size
2254 	 * disk drive targets (PM disabled)
2255 	 */
2256 	SD_CHAIN_BUFIO,			/* Index: 9 */
2257 	SD_CHAIN_BUFIO,			/* Index: 10 */
2258 	SD_CHAIN_BUFIO,			/* Index: 11 */
2259 
2260 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2261 	SD_CHAIN_BUFIO,			/* Index: 12 */
2262 	SD_CHAIN_BUFIO,			/* Index: 13 */
2263 	SD_CHAIN_BUFIO,			/* Index: 14 */
2264 	SD_CHAIN_BUFIO,			/* Index: 15 */
2265 
2266 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2267 	SD_CHAIN_BUFIO,			/* Index: 16 */
2268 	SD_CHAIN_BUFIO,			/* Index: 17 */
2269 	SD_CHAIN_BUFIO,			/* Index: 18 */
2270 
2271 	/* Chain for USCSI commands (non-checksum targets) */
2272 	SD_CHAIN_USCSI,			/* Index: 19 */
2273 	SD_CHAIN_USCSI,			/* Index: 20 */
2274 
2275 	/* Chain for USCSI commands (checksum targets) */
2276 	SD_CHAIN_USCSI,			/* Index: 21 */
2277 	SD_CHAIN_USCSI,			/* Index: 22 */
2278 	SD_CHAIN_USCSI,			/* Index: 23 */
2279 
2280 	/* Chain for "direct" USCSI commands (all targets) */
2281 	SD_CHAIN_DIRECT,		/* Index: 24 */
2282 
2283 	/* Chain for "direct priority" USCSI commands (all targets) */
2284 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2285 
2286 	/*
2287 	 * Chain for buf IO for large sector size disk drive targets
2288 	 * with checksumming (PM enabled)
2289 	 */
2290 	SD_CHAIN_BUFIO,			/* Index: 26 */
2291 	SD_CHAIN_BUFIO,			/* Index: 27 */
2292 	SD_CHAIN_BUFIO,			/* Index: 28 */
2293 	SD_CHAIN_BUFIO,			/* Index: 29 */
2294 	SD_CHAIN_BUFIO,			/* Index: 30 */
2295 
2296 	/*
2297 	 * Chain for buf IO for large sector size disk drive targets
2298 	 * with checksumming (PM disabled)
2299 	 */
2300 	SD_CHAIN_BUFIO,			/* Index: 31 */
2301 	SD_CHAIN_BUFIO,			/* Index: 32 */
2302 	SD_CHAIN_BUFIO,			/* Index: 33 */
2303 	SD_CHAIN_BUFIO,			/* Index: 34 */
2304 };
2305 
2306 
2307 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2308 #define	SD_IS_BUFIO(xp)			\
2309 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2310 
2311 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2312 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2313 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2314 
2315 
2316 
2317 /*
2318  * Struct, array, and macros to map a specific chain to the appropriate
2319  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2320  *
2321  * The sd_chain_index_map[] array is used at attach time to set the various
2322  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2323  * chain to be used with the instance. This allows different instances to use
2324  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2325  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2326  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2327  * dynamically & without the use of locking; and (2) a layer may update the
2328  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2329  * to allow for deferred processing of an IO within the same chain from a
2330  * different execution context.
2331  */
2332 
2333 struct sd_chain_index {
2334 	int	sci_iostart_index;
2335 	int	sci_iodone_index;
2336 };
2337 
2338 static struct sd_chain_index	sd_chain_index_map[] = {
2339 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2340 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2341 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2342 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2343 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2344 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2345 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2346 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2347 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2348 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2349 	{ SD_CHAIN_MSS_CHKSUM_IOSTART,		SD_CHAIN_MSS_CHKSUM_IODONE },
2350 	{ SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2351 
2352 };
2353 
2354 
2355 /*
2356  * The following are indexes into the sd_chain_index_map[] array.
2357  */
2358 
2359 /* un->un_buf_chain_type must be set to one of these */
2360 #define	SD_CHAIN_INFO_DISK		0
2361 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2362 #define	SD_CHAIN_INFO_RMMEDIA		2
2363 #define	SD_CHAIN_INFO_MSS_DISK		2
2364 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2365 #define	SD_CHAIN_INFO_MSS_DSK_NO_PM	3
2366 #define	SD_CHAIN_INFO_CHKSUM		4
2367 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2368 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM	10
2369 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM	11
2370 
2371 /* un->un_uscsi_chain_type must be set to one of these */
2372 #define	SD_CHAIN_INFO_USCSI_CMD		6
2373 /* USCSI with PM disabled is the same as DIRECT */
2374 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2375 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2376 
2377 /* un->un_direct_chain_type must be set to one of these */
2378 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2379 
2380 /* un->un_priority_chain_type must be set to one of these */
2381 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2382 
2383 /* size for devid inquiries */
2384 #define	MAX_INQUIRY_SIZE		0xF0
2385 
2386 /*
2387  * Macros used by functions to pass a given buf(9S) struct along to the
2388  * next function in the layering chain for further processing.
2389  *
2390  * In the following macros, passing more than three arguments to the called
2391  * routines causes the optimizer for the SPARC compiler to stop doing tail
2392  * call elimination which results in significant performance degradation.
2393  */
2394 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2395 	((*(sd_iostart_chain[index]))(index, un, bp))
2396 
2397 #define	SD_BEGIN_IODONE(index, un, bp)	\
2398 	((*(sd_iodone_chain[index]))(index, un, bp))
2399 
2400 #define	SD_NEXT_IOSTART(index, un, bp)				\
2401 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2402 
2403 #define	SD_NEXT_IODONE(index, un, bp)				\
2404 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2405 
2406 /*
2407  *    Function: _init
2408  *
2409  * Description: This is the driver _init(9E) entry point.
2410  *
2411  * Return Code: Returns the value from mod_install(9F) or
2412  *		ddi_soft_state_init(9F) as appropriate.
2413  *
2414  *     Context: Called when driver module loaded.
2415  */
2416 
2417 int
2418 _init(void)
2419 {
2420 	int	err;
2421 
2422 	/* establish driver name from module name */
2423 	sd_label = (char *)mod_modname(&modlinkage);
2424 
2425 #ifndef XPV_HVM_DRIVER
2426 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2427 	    SD_MAXUNIT);
2428 	if (err != 0) {
2429 		return (err);
2430 	}
2431 
2432 #else /* XPV_HVM_DRIVER */
2433 	/* Remove the leading "hvm_" from the module name */
2434 	ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0);
2435 	sd_label += strlen("hvm_");
2436 
2437 #endif /* XPV_HVM_DRIVER */
2438 
2439 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2440 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2441 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2442 
2443 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2444 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2445 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2446 
2447 	/*
2448 	 * it's ok to init here even for fibre device
2449 	 */
2450 	sd_scsi_probe_cache_init();
2451 
2452 	sd_scsi_target_lun_init();
2453 
2454 	/*
2455 	 * Creating taskq before mod_install ensures that all callers (threads)
2456 	 * that enter the module after a successful mod_install encounter
2457 	 * a valid taskq.
2458 	 */
2459 	sd_taskq_create();
2460 
2461 	err = mod_install(&modlinkage);
2462 	if (err != 0) {
2463 		/* delete taskq if install fails */
2464 		sd_taskq_delete();
2465 
2466 		mutex_destroy(&sd_detach_mutex);
2467 		mutex_destroy(&sd_log_mutex);
2468 		mutex_destroy(&sd_label_mutex);
2469 
2470 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2471 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2472 		cv_destroy(&sd_tr.srq_inprocess_cv);
2473 
2474 		sd_scsi_probe_cache_fini();
2475 
2476 		sd_scsi_target_lun_fini();
2477 
2478 #ifndef XPV_HVM_DRIVER
2479 		ddi_soft_state_fini(&sd_state);
2480 #endif /* !XPV_HVM_DRIVER */
2481 		return (err);
2482 	}
2483 
2484 	return (err);
2485 }
2486 
2487 
2488 /*
2489  *    Function: _fini
2490  *
2491  * Description: This is the driver _fini(9E) entry point.
2492  *
2493  * Return Code: Returns the value from mod_remove(9F)
2494  *
2495  *     Context: Called when driver module is unloaded.
2496  */
2497 
2498 int
2499 _fini(void)
2500 {
2501 	int err;
2502 
2503 	if ((err = mod_remove(&modlinkage)) != 0) {
2504 		return (err);
2505 	}
2506 
2507 	sd_taskq_delete();
2508 
2509 	mutex_destroy(&sd_detach_mutex);
2510 	mutex_destroy(&sd_log_mutex);
2511 	mutex_destroy(&sd_label_mutex);
2512 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2513 
2514 	sd_scsi_probe_cache_fini();
2515 
2516 	sd_scsi_target_lun_fini();
2517 
2518 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2519 	cv_destroy(&sd_tr.srq_inprocess_cv);
2520 
2521 #ifndef XPV_HVM_DRIVER
2522 	ddi_soft_state_fini(&sd_state);
2523 #endif /* !XPV_HVM_DRIVER */
2524 
2525 	return (err);
2526 }
2527 
2528 
2529 /*
2530  *    Function: _info
2531  *
2532  * Description: This is the driver _info(9E) entry point.
2533  *
2534  *   Arguments: modinfop - pointer to the driver modinfo structure
2535  *
2536  * Return Code: Returns the value from mod_info(9F).
2537  *
2538  *     Context: Kernel thread context
2539  */
2540 
2541 int
2542 _info(struct modinfo *modinfop)
2543 {
2544 	return (mod_info(&modlinkage, modinfop));
2545 }
2546 
2547 
2548 /*
2549  * The following routines implement the driver message logging facility.
2550  * They provide component- and level- based debug output filtering.
2551  * Output may also be restricted to messages for a single instance by
2552  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2553  * to NULL, then messages for all instances are printed.
2554  *
2555  * These routines have been cloned from each other due to the language
2556  * constraints of macros and variable argument list processing.
2557  */
2558 
2559 
2560 /*
2561  *    Function: sd_log_err
2562  *
2563  * Description: This routine is called by the SD_ERROR macro for debug
2564  *		logging of error conditions.
2565  *
2566  *   Arguments: comp - driver component being logged
2567  *		dev  - pointer to driver info structure
2568  *		fmt  - error string and format to be logged
2569  */
2570 
2571 static void
2572 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2573 {
2574 	va_list		ap;
2575 	dev_info_t	*dev;
2576 
2577 	ASSERT(un != NULL);
2578 	dev = SD_DEVINFO(un);
2579 	ASSERT(dev != NULL);
2580 
2581 	/*
2582 	 * Filter messages based on the global component and level masks.
2583 	 * Also print if un matches the value of sd_debug_un, or if
2584 	 * sd_debug_un is set to NULL.
2585 	 */
2586 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2587 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2588 		mutex_enter(&sd_log_mutex);
2589 		va_start(ap, fmt);
2590 		(void) vsprintf(sd_log_buf, fmt, ap);
2591 		va_end(ap);
2592 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2593 		mutex_exit(&sd_log_mutex);
2594 	}
2595 #ifdef SD_FAULT_INJECTION
2596 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2597 	if (un->sd_injection_mask & comp) {
2598 		mutex_enter(&sd_log_mutex);
2599 		va_start(ap, fmt);
2600 		(void) vsprintf(sd_log_buf, fmt, ap);
2601 		va_end(ap);
2602 		sd_injection_log(sd_log_buf, un);
2603 		mutex_exit(&sd_log_mutex);
2604 	}
2605 #endif
2606 }
2607 
2608 
2609 /*
2610  *    Function: sd_log_info
2611  *
2612  * Description: This routine is called by the SD_INFO macro for debug
2613  *		logging of general purpose informational conditions.
2614  *
2615  *   Arguments: comp - driver component being logged
2616  *		dev  - pointer to driver info structure
2617  *		fmt  - info string and format to be logged
2618  */
2619 
2620 static void
2621 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2622 {
2623 	va_list		ap;
2624 	dev_info_t	*dev;
2625 
2626 	ASSERT(un != NULL);
2627 	dev = SD_DEVINFO(un);
2628 	ASSERT(dev != NULL);
2629 
2630 	/*
2631 	 * Filter messages based on the global component and level masks.
2632 	 * Also print if un matches the value of sd_debug_un, or if
2633 	 * sd_debug_un is set to NULL.
2634 	 */
2635 	if ((sd_component_mask & component) &&
2636 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2637 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2638 		mutex_enter(&sd_log_mutex);
2639 		va_start(ap, fmt);
2640 		(void) vsprintf(sd_log_buf, fmt, ap);
2641 		va_end(ap);
2642 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2643 		mutex_exit(&sd_log_mutex);
2644 	}
2645 #ifdef SD_FAULT_INJECTION
2646 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2647 	if (un->sd_injection_mask & component) {
2648 		mutex_enter(&sd_log_mutex);
2649 		va_start(ap, fmt);
2650 		(void) vsprintf(sd_log_buf, fmt, ap);
2651 		va_end(ap);
2652 		sd_injection_log(sd_log_buf, un);
2653 		mutex_exit(&sd_log_mutex);
2654 	}
2655 #endif
2656 }
2657 
2658 
2659 /*
2660  *    Function: sd_log_trace
2661  *
2662  * Description: This routine is called by the SD_TRACE macro for debug
2663  *		logging of trace conditions (i.e. function entry/exit).
2664  *
2665  *   Arguments: comp - driver component being logged
2666  *		dev  - pointer to driver info structure
2667  *		fmt  - trace string and format to be logged
2668  */
2669 
2670 static void
2671 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2672 {
2673 	va_list		ap;
2674 	dev_info_t	*dev;
2675 
2676 	ASSERT(un != NULL);
2677 	dev = SD_DEVINFO(un);
2678 	ASSERT(dev != NULL);
2679 
2680 	/*
2681 	 * Filter messages based on the global component and level masks.
2682 	 * Also print if un matches the value of sd_debug_un, or if
2683 	 * sd_debug_un is set to NULL.
2684 	 */
2685 	if ((sd_component_mask & component) &&
2686 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2687 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2688 		mutex_enter(&sd_log_mutex);
2689 		va_start(ap, fmt);
2690 		(void) vsprintf(sd_log_buf, fmt, ap);
2691 		va_end(ap);
2692 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2693 		mutex_exit(&sd_log_mutex);
2694 	}
2695 #ifdef SD_FAULT_INJECTION
2696 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2697 	if (un->sd_injection_mask & component) {
2698 		mutex_enter(&sd_log_mutex);
2699 		va_start(ap, fmt);
2700 		(void) vsprintf(sd_log_buf, fmt, ap);
2701 		va_end(ap);
2702 		sd_injection_log(sd_log_buf, un);
2703 		mutex_exit(&sd_log_mutex);
2704 	}
2705 #endif
2706 }
2707 
2708 
2709 /*
2710  *    Function: sdprobe
2711  *
2712  * Description: This is the driver probe(9e) entry point function.
2713  *
2714  *   Arguments: devi - opaque device info handle
2715  *
2716  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2717  *              DDI_PROBE_FAILURE: If the probe failed.
2718  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2719  *				   but may be present in the future.
2720  */
2721 
2722 static int
2723 sdprobe(dev_info_t *devi)
2724 {
2725 	struct scsi_device	*devp;
2726 	int			rval;
2727 #ifndef XPV_HVM_DRIVER
2728 	int			instance = ddi_get_instance(devi);
2729 #endif /* !XPV_HVM_DRIVER */
2730 
2731 	/*
2732 	 * if it wasn't for pln, sdprobe could actually be nulldev
2733 	 * in the "__fibre" case.
2734 	 */
2735 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2736 		return (DDI_PROBE_DONTCARE);
2737 	}
2738 
2739 	devp = ddi_get_driver_private(devi);
2740 
2741 	if (devp == NULL) {
2742 		/* Ooops... nexus driver is mis-configured... */
2743 		return (DDI_PROBE_FAILURE);
2744 	}
2745 
2746 #ifndef XPV_HVM_DRIVER
2747 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2748 		return (DDI_PROBE_PARTIAL);
2749 	}
2750 #endif /* !XPV_HVM_DRIVER */
2751 
2752 	/*
2753 	 * Call the SCSA utility probe routine to see if we actually
2754 	 * have a target at this SCSI nexus.
2755 	 */
2756 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2757 	case SCSIPROBE_EXISTS:
2758 		switch (devp->sd_inq->inq_dtype) {
2759 		case DTYPE_DIRECT:
2760 			rval = DDI_PROBE_SUCCESS;
2761 			break;
2762 		case DTYPE_RODIRECT:
2763 			/* CDs etc. Can be removable media */
2764 			rval = DDI_PROBE_SUCCESS;
2765 			break;
2766 		case DTYPE_OPTICAL:
2767 			/*
2768 			 * Rewritable optical driver HP115AA
2769 			 * Can also be removable media
2770 			 */
2771 
2772 			/*
2773 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2774 			 * pre solaris 9 sparc sd behavior is required
2775 			 *
2776 			 * If first time through and sd_dtype_optical_bind
2777 			 * has not been set in /etc/system check properties
2778 			 */
2779 
2780 			if (sd_dtype_optical_bind  < 0) {
2781 				sd_dtype_optical_bind = ddi_prop_get_int
2782 				    (DDI_DEV_T_ANY, devi, 0,
2783 				    "optical-device-bind", 1);
2784 			}
2785 
2786 			if (sd_dtype_optical_bind == 0) {
2787 				rval = DDI_PROBE_FAILURE;
2788 			} else {
2789 				rval = DDI_PROBE_SUCCESS;
2790 			}
2791 			break;
2792 
2793 		case DTYPE_NOTPRESENT:
2794 		default:
2795 			rval = DDI_PROBE_FAILURE;
2796 			break;
2797 		}
2798 		break;
2799 	default:
2800 		rval = DDI_PROBE_PARTIAL;
2801 		break;
2802 	}
2803 
2804 	/*
2805 	 * This routine checks for resource allocation prior to freeing,
2806 	 * so it will take care of the "smart probing" case where a
2807 	 * scsi_probe() may or may not have been issued and will *not*
2808 	 * free previously-freed resources.
2809 	 */
2810 	scsi_unprobe(devp);
2811 	return (rval);
2812 }
2813 
2814 
2815 /*
2816  *    Function: sdinfo
2817  *
2818  * Description: This is the driver getinfo(9e) entry point function.
2819  * 		Given the device number, return the devinfo pointer from
2820  *		the scsi_device structure or the instance number
2821  *		associated with the dev_t.
2822  *
2823  *   Arguments: dip     - pointer to device info structure
2824  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2825  *			  DDI_INFO_DEVT2INSTANCE)
2826  *		arg     - driver dev_t
2827  *		resultp - user buffer for request response
2828  *
2829  * Return Code: DDI_SUCCESS
2830  *              DDI_FAILURE
2831  */
2832 /* ARGSUSED */
2833 static int
2834 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2835 {
2836 	struct sd_lun	*un;
2837 	dev_t		dev;
2838 	int		instance;
2839 	int		error;
2840 
2841 	switch (infocmd) {
2842 	case DDI_INFO_DEVT2DEVINFO:
2843 		dev = (dev_t)arg;
2844 		instance = SDUNIT(dev);
2845 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2846 			return (DDI_FAILURE);
2847 		}
2848 		*result = (void *) SD_DEVINFO(un);
2849 		error = DDI_SUCCESS;
2850 		break;
2851 	case DDI_INFO_DEVT2INSTANCE:
2852 		dev = (dev_t)arg;
2853 		instance = SDUNIT(dev);
2854 		*result = (void *)(uintptr_t)instance;
2855 		error = DDI_SUCCESS;
2856 		break;
2857 	default:
2858 		error = DDI_FAILURE;
2859 	}
2860 	return (error);
2861 }
2862 
2863 /*
2864  *    Function: sd_prop_op
2865  *
2866  * Description: This is the driver prop_op(9e) entry point function.
2867  *		Return the number of blocks for the partition in question
2868  *		or forward the request to the property facilities.
2869  *
2870  *   Arguments: dev       - device number
2871  *		dip       - pointer to device info structure
2872  *		prop_op   - property operator
2873  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2874  *		name      - pointer to property name
2875  *		valuep    - pointer or address of the user buffer
2876  *		lengthp   - property length
2877  *
2878  * Return Code: DDI_PROP_SUCCESS
2879  *              DDI_PROP_NOT_FOUND
2880  *              DDI_PROP_UNDEFINED
2881  *              DDI_PROP_NO_MEMORY
2882  *              DDI_PROP_BUF_TOO_SMALL
2883  */
2884 
2885 static int
2886 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2887 	char *name, caddr_t valuep, int *lengthp)
2888 {
2889 	struct sd_lun	*un;
2890 
2891 	if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2892 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2893 		    name, valuep, lengthp));
2894 
2895 	return (cmlb_prop_op(un->un_cmlbhandle,
2896 	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2897 	    SDPART(dev), (void *)SD_PATH_DIRECT));
2898 }
2899 
2900 /*
2901  * The following functions are for smart probing:
2902  * sd_scsi_probe_cache_init()
2903  * sd_scsi_probe_cache_fini()
2904  * sd_scsi_clear_probe_cache()
2905  * sd_scsi_probe_with_cache()
2906  */
2907 
2908 /*
2909  *    Function: sd_scsi_probe_cache_init
2910  *
2911  * Description: Initializes the probe response cache mutex and head pointer.
2912  *
2913  *     Context: Kernel thread context
2914  */
2915 
2916 static void
2917 sd_scsi_probe_cache_init(void)
2918 {
2919 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2920 	sd_scsi_probe_cache_head = NULL;
2921 }
2922 
2923 
2924 /*
2925  *    Function: sd_scsi_probe_cache_fini
2926  *
2927  * Description: Frees all resources associated with the probe response cache.
2928  *
2929  *     Context: Kernel thread context
2930  */
2931 
2932 static void
2933 sd_scsi_probe_cache_fini(void)
2934 {
2935 	struct sd_scsi_probe_cache *cp;
2936 	struct sd_scsi_probe_cache *ncp;
2937 
2938 	/* Clean up our smart probing linked list */
2939 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2940 		ncp = cp->next;
2941 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2942 	}
2943 	sd_scsi_probe_cache_head = NULL;
2944 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2945 }
2946 
2947 
2948 /*
2949  *    Function: sd_scsi_clear_probe_cache
2950  *
2951  * Description: This routine clears the probe response cache. This is
2952  *		done when open() returns ENXIO so that when deferred
2953  *		attach is attempted (possibly after a device has been
2954  *		turned on) we will retry the probe. Since we don't know
2955  *		which target we failed to open, we just clear the
2956  *		entire cache.
2957  *
2958  *     Context: Kernel thread context
2959  */
2960 
2961 static void
2962 sd_scsi_clear_probe_cache(void)
2963 {
2964 	struct sd_scsi_probe_cache	*cp;
2965 	int				i;
2966 
2967 	mutex_enter(&sd_scsi_probe_cache_mutex);
2968 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2969 		/*
2970 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2971 		 * force probing to be performed the next time
2972 		 * sd_scsi_probe_with_cache is called.
2973 		 */
2974 		for (i = 0; i < NTARGETS_WIDE; i++) {
2975 			cp->cache[i] = SCSIPROBE_EXISTS;
2976 		}
2977 	}
2978 	mutex_exit(&sd_scsi_probe_cache_mutex);
2979 }
2980 
2981 
2982 /*
2983  *    Function: sd_scsi_probe_with_cache
2984  *
2985  * Description: This routine implements support for a scsi device probe
2986  *		with cache. The driver maintains a cache of the target
2987  *		responses to scsi probes. If we get no response from a
2988  *		target during a probe inquiry, we remember that, and we
2989  *		avoid additional calls to scsi_probe on non-zero LUNs
2990  *		on the same target until the cache is cleared. By doing
2991  *		so we avoid the 1/4 sec selection timeout for nonzero
2992  *		LUNs. lun0 of a target is always probed.
2993  *
2994  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2995  *              waitfunc - indicates what the allocator routines should
2996  *			   do when resources are not available. This value
2997  *			   is passed on to scsi_probe() when that routine
2998  *			   is called.
2999  *
3000  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
3001  *		otherwise the value returned by scsi_probe(9F).
3002  *
3003  *     Context: Kernel thread context
3004  */
3005 
3006 static int
3007 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
3008 {
3009 	struct sd_scsi_probe_cache	*cp;
3010 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
3011 	int		lun, tgt;
3012 
3013 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3014 	    SCSI_ADDR_PROP_LUN, 0);
3015 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3016 	    SCSI_ADDR_PROP_TARGET, -1);
3017 
3018 	/* Make sure caching enabled and target in range */
3019 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
3020 		/* do it the old way (no cache) */
3021 		return (scsi_probe(devp, waitfn));
3022 	}
3023 
3024 	mutex_enter(&sd_scsi_probe_cache_mutex);
3025 
3026 	/* Find the cache for this scsi bus instance */
3027 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
3028 		if (cp->pdip == pdip) {
3029 			break;
3030 		}
3031 	}
3032 
3033 	/* If we can't find a cache for this pdip, create one */
3034 	if (cp == NULL) {
3035 		int i;
3036 
3037 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3038 		    KM_SLEEP);
3039 		cp->pdip = pdip;
3040 		cp->next = sd_scsi_probe_cache_head;
3041 		sd_scsi_probe_cache_head = cp;
3042 		for (i = 0; i < NTARGETS_WIDE; i++) {
3043 			cp->cache[i] = SCSIPROBE_EXISTS;
3044 		}
3045 	}
3046 
3047 	mutex_exit(&sd_scsi_probe_cache_mutex);
3048 
3049 	/* Recompute the cache for this target if LUN zero */
3050 	if (lun == 0) {
3051 		cp->cache[tgt] = SCSIPROBE_EXISTS;
3052 	}
3053 
3054 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
3055 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3056 		return (SCSIPROBE_NORESP);
3057 	}
3058 
3059 	/* Do the actual probe; save & return the result */
3060 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3061 }
3062 
3063 
3064 /*
3065  *    Function: sd_scsi_target_lun_init
3066  *
3067  * Description: Initializes the attached lun chain mutex and head pointer.
3068  *
3069  *     Context: Kernel thread context
3070  */
3071 
3072 static void
3073 sd_scsi_target_lun_init(void)
3074 {
3075 	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3076 	sd_scsi_target_lun_head = NULL;
3077 }
3078 
3079 
3080 /*
3081  *    Function: sd_scsi_target_lun_fini
3082  *
3083  * Description: Frees all resources associated with the attached lun
3084  *              chain
3085  *
3086  *     Context: Kernel thread context
3087  */
3088 
3089 static void
3090 sd_scsi_target_lun_fini(void)
3091 {
3092 	struct sd_scsi_hba_tgt_lun	*cp;
3093 	struct sd_scsi_hba_tgt_lun	*ncp;
3094 
3095 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3096 		ncp = cp->next;
3097 		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3098 	}
3099 	sd_scsi_target_lun_head = NULL;
3100 	mutex_destroy(&sd_scsi_target_lun_mutex);
3101 }
3102 
3103 
3104 /*
3105  *    Function: sd_scsi_get_target_lun_count
3106  *
3107  * Description: This routine will check in the attached lun chain to see
3108  * 		how many luns are attached on the required SCSI controller
3109  * 		and target. Currently, some capabilities like tagged queue
3110  *		are supported per target based by HBA. So all luns in a
3111  *		target have the same capabilities. Based on this assumption,
3112  * 		sd should only set these capabilities once per target. This
3113  *		function is called when sd needs to decide how many luns
3114  *		already attached on a target.
3115  *
3116  *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
3117  *			  controller device.
3118  *              target	- The target ID on the controller's SCSI bus.
3119  *
3120  * Return Code: The number of luns attached on the required target and
3121  *		controller.
3122  *		-1 if target ID is not in parallel SCSI scope or the given
3123  * 		dip is not in the chain.
3124  *
3125  *     Context: Kernel thread context
3126  */
3127 
3128 static int
3129 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3130 {
3131 	struct sd_scsi_hba_tgt_lun	*cp;
3132 
3133 	if ((target < 0) || (target >= NTARGETS_WIDE)) {
3134 		return (-1);
3135 	}
3136 
3137 	mutex_enter(&sd_scsi_target_lun_mutex);
3138 
3139 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3140 		if (cp->pdip == dip) {
3141 			break;
3142 		}
3143 	}
3144 
3145 	mutex_exit(&sd_scsi_target_lun_mutex);
3146 
3147 	if (cp == NULL) {
3148 		return (-1);
3149 	}
3150 
3151 	return (cp->nlun[target]);
3152 }
3153 
3154 
3155 /*
3156  *    Function: sd_scsi_update_lun_on_target
3157  *
3158  * Description: This routine is used to update the attached lun chain when a
3159  *		lun is attached or detached on a target.
3160  *
3161  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
3162  *                        controller device.
3163  *              target  - The target ID on the controller's SCSI bus.
3164  *		flag	- Indicate the lun is attached or detached.
3165  *
3166  *     Context: Kernel thread context
3167  */
3168 
3169 static void
3170 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3171 {
3172 	struct sd_scsi_hba_tgt_lun	*cp;
3173 
3174 	mutex_enter(&sd_scsi_target_lun_mutex);
3175 
3176 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3177 		if (cp->pdip == dip) {
3178 			break;
3179 		}
3180 	}
3181 
3182 	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3183 		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3184 		    KM_SLEEP);
3185 		cp->pdip = dip;
3186 		cp->next = sd_scsi_target_lun_head;
3187 		sd_scsi_target_lun_head = cp;
3188 	}
3189 
3190 	mutex_exit(&sd_scsi_target_lun_mutex);
3191 
3192 	if (cp != NULL) {
3193 		if (flag == SD_SCSI_LUN_ATTACH) {
3194 			cp->nlun[target] ++;
3195 		} else {
3196 			cp->nlun[target] --;
3197 		}
3198 	}
3199 }
3200 
3201 
3202 /*
3203  *    Function: sd_spin_up_unit
3204  *
3205  * Description: Issues the following commands to spin-up the device:
3206  *		START STOP UNIT, and INQUIRY.
3207  *
3208  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3209  *                      structure for this target.
3210  *
3211  * Return Code: 0 - success
3212  *		EIO - failure
3213  *		EACCES - reservation conflict
3214  *
3215  *     Context: Kernel thread context
3216  */
3217 
3218 static int
3219 sd_spin_up_unit(sd_ssc_t *ssc)
3220 {
3221 	size_t	resid		= 0;
3222 	int	has_conflict	= FALSE;
3223 	uchar_t *bufaddr;
3224 	int 	status;
3225 	struct sd_lun	*un;
3226 
3227 	ASSERT(ssc != NULL);
3228 	un = ssc->ssc_un;
3229 	ASSERT(un != NULL);
3230 
3231 	/*
3232 	 * Send a throwaway START UNIT command.
3233 	 *
3234 	 * If we fail on this, we don't care presently what precisely
3235 	 * is wrong.  EMC's arrays will also fail this with a check
3236 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
3237 	 * we don't want to fail the attach because it may become
3238 	 * "active" later.
3239 	 * We don't know if power condition is supported or not at
3240 	 * this stage, use START STOP bit.
3241 	 */
3242 	status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3243 	    SD_TARGET_START, SD_PATH_DIRECT);
3244 
3245 	if (status != 0) {
3246 		if (status == EACCES)
3247 			has_conflict = TRUE;
3248 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3249 	}
3250 
3251 	/*
3252 	 * Send another INQUIRY command to the target. This is necessary for
3253 	 * non-removable media direct access devices because their INQUIRY data
3254 	 * may not be fully qualified until they are spun up (perhaps via the
3255 	 * START command above).  Note: This seems to be needed for some
3256 	 * legacy devices only.) The INQUIRY command should succeed even if a
3257 	 * Reservation Conflict is present.
3258 	 */
3259 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3260 
3261 	if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3262 	    != 0) {
3263 		kmem_free(bufaddr, SUN_INQSIZE);
3264 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3265 		return (EIO);
3266 	}
3267 
3268 	/*
3269 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3270 	 * Note that this routine does not return a failure here even if the
3271 	 * INQUIRY command did not return any data.  This is a legacy behavior.
3272 	 */
3273 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3274 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3275 	}
3276 
3277 	kmem_free(bufaddr, SUN_INQSIZE);
3278 
3279 	/* If we hit a reservation conflict above, tell the caller. */
3280 	if (has_conflict == TRUE) {
3281 		return (EACCES);
3282 	}
3283 
3284 	return (0);
3285 }
3286 
3287 #ifdef _LP64
3288 /*
3289  *    Function: sd_enable_descr_sense
3290  *
3291  * Description: This routine attempts to select descriptor sense format
3292  *		using the Control mode page.  Devices that support 64 bit
3293  *		LBAs (for >2TB luns) should also implement descriptor
3294  *		sense data so we will call this function whenever we see
3295  *		a lun larger than 2TB.  If for some reason the device
3296  *		supports 64 bit LBAs but doesn't support descriptor sense
3297  *		presumably the mode select will fail.  Everything will
3298  *		continue to work normally except that we will not get
3299  *		complete sense data for commands that fail with an LBA
3300  *		larger than 32 bits.
3301  *
3302  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3303  *                      structure for this target.
3304  *
3305  *     Context: Kernel thread context only
3306  */
3307 
3308 static void
3309 sd_enable_descr_sense(sd_ssc_t *ssc)
3310 {
3311 	uchar_t			*header;
3312 	struct mode_control_scsi3 *ctrl_bufp;
3313 	size_t			buflen;
3314 	size_t			bd_len;
3315 	int			status;
3316 	struct sd_lun		*un;
3317 
3318 	ASSERT(ssc != NULL);
3319 	un = ssc->ssc_un;
3320 	ASSERT(un != NULL);
3321 
3322 	/*
3323 	 * Read MODE SENSE page 0xA, Control Mode Page
3324 	 */
3325 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3326 	    sizeof (struct mode_control_scsi3);
3327 	header = kmem_zalloc(buflen, KM_SLEEP);
3328 
3329 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3330 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3331 
3332 	if (status != 0) {
3333 		SD_ERROR(SD_LOG_COMMON, un,
3334 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
3335 		goto eds_exit;
3336 	}
3337 
3338 	/*
3339 	 * Determine size of Block Descriptors in order to locate
3340 	 * the mode page data. ATAPI devices return 0, SCSI devices
3341 	 * should return MODE_BLK_DESC_LENGTH.
3342 	 */
3343 	bd_len  = ((struct mode_header *)header)->bdesc_length;
3344 
3345 	/* Clear the mode data length field for MODE SELECT */
3346 	((struct mode_header *)header)->length = 0;
3347 
3348 	ctrl_bufp = (struct mode_control_scsi3 *)
3349 	    (header + MODE_HEADER_LENGTH + bd_len);
3350 
3351 	/*
3352 	 * If the page length is smaller than the expected value,
3353 	 * the target device doesn't support D_SENSE. Bail out here.
3354 	 */
3355 	if (ctrl_bufp->mode_page.length <
3356 	    sizeof (struct mode_control_scsi3) - 2) {
3357 		SD_ERROR(SD_LOG_COMMON, un,
3358 		    "sd_enable_descr_sense: enable D_SENSE failed\n");
3359 		goto eds_exit;
3360 	}
3361 
3362 	/*
3363 	 * Clear PS bit for MODE SELECT
3364 	 */
3365 	ctrl_bufp->mode_page.ps = 0;
3366 
3367 	/*
3368 	 * Set D_SENSE to enable descriptor sense format.
3369 	 */
3370 	ctrl_bufp->d_sense = 1;
3371 
3372 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3373 
3374 	/*
3375 	 * Use MODE SELECT to commit the change to the D_SENSE bit
3376 	 */
3377 	status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3378 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3379 
3380 	if (status != 0) {
3381 		SD_INFO(SD_LOG_COMMON, un,
3382 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
3383 	} else {
3384 		kmem_free(header, buflen);
3385 		return;
3386 	}
3387 
3388 eds_exit:
3389 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3390 	kmem_free(header, buflen);
3391 }
3392 
3393 /*
3394  *    Function: sd_reenable_dsense_task
3395  *
3396  * Description: Re-enable descriptor sense after device or bus reset
3397  *
3398  *     Context: Executes in a taskq() thread context
3399  */
3400 static void
3401 sd_reenable_dsense_task(void *arg)
3402 {
3403 	struct	sd_lun	*un = arg;
3404 	sd_ssc_t	*ssc;
3405 
3406 	ASSERT(un != NULL);
3407 
3408 	ssc = sd_ssc_init(un);
3409 	sd_enable_descr_sense(ssc);
3410 	sd_ssc_fini(ssc);
3411 }
3412 #endif /* _LP64 */
3413 
3414 /*
3415  *    Function: sd_set_mmc_caps
3416  *
3417  * Description: This routine determines if the device is MMC compliant and if
3418  *		the device supports CDDA via a mode sense of the CDVD
3419  *		capabilities mode page. Also checks if the device is a
3420  *		dvdram writable device.
3421  *
3422  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3423  *                      structure for this target.
3424  *
3425  *     Context: Kernel thread context only
3426  */
3427 
3428 static void
3429 sd_set_mmc_caps(sd_ssc_t *ssc)
3430 {
3431 	struct mode_header_grp2		*sense_mhp;
3432 	uchar_t				*sense_page;
3433 	caddr_t				buf;
3434 	int				bd_len;
3435 	int				status;
3436 	struct uscsi_cmd		com;
3437 	int				rtn;
3438 	uchar_t				*out_data_rw, *out_data_hd;
3439 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3440 	uchar_t				*out_data_gesn;
3441 	int				gesn_len;
3442 	struct sd_lun			*un;
3443 
3444 	ASSERT(ssc != NULL);
3445 	un = ssc->ssc_un;
3446 	ASSERT(un != NULL);
3447 
3448 	/*
3449 	 * The flags which will be set in this function are - mmc compliant,
3450 	 * dvdram writable device, cdda support. Initialize them to FALSE
3451 	 * and if a capability is detected - it will be set to TRUE.
3452 	 */
3453 	un->un_f_mmc_cap = FALSE;
3454 	un->un_f_dvdram_writable_device = FALSE;
3455 	un->un_f_cfg_cdda = FALSE;
3456 
3457 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3458 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3459 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3460 
3461 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3462 
3463 	if (status != 0) {
3464 		/* command failed; just return */
3465 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3466 		return;
3467 	}
3468 	/*
3469 	 * If the mode sense request for the CDROM CAPABILITIES
3470 	 * page (0x2A) succeeds the device is assumed to be MMC.
3471 	 */
3472 	un->un_f_mmc_cap = TRUE;
3473 
3474 	/* See if GET STATUS EVENT NOTIFICATION is supported */
3475 	if (un->un_f_mmc_gesn_polling) {
3476 		gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3477 		out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3478 
3479 		rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3480 		    out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3481 
3482 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3483 
3484 		if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3485 			un->un_f_mmc_gesn_polling = FALSE;
3486 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3487 			    "sd_set_mmc_caps: gesn not supported "
3488 			    "%d %x %x %x %x\n", rtn,
3489 			    out_data_gesn[0], out_data_gesn[1],
3490 			    out_data_gesn[2], out_data_gesn[3]);
3491 		}
3492 
3493 		kmem_free(out_data_gesn, gesn_len);
3494 	}
3495 
3496 	/* Get to the page data */
3497 	sense_mhp = (struct mode_header_grp2 *)buf;
3498 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3499 	    sense_mhp->bdesc_length_lo;
3500 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3501 		/*
3502 		 * We did not get back the expected block descriptor
3503 		 * length so we cannot determine if the device supports
3504 		 * CDDA. However, we still indicate the device is MMC
3505 		 * according to the successful response to the page
3506 		 * 0x2A mode sense request.
3507 		 */
3508 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3509 		    "sd_set_mmc_caps: Mode Sense returned "
3510 		    "invalid block descriptor length\n");
3511 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3512 		return;
3513 	}
3514 
3515 	/* See if read CDDA is supported */
3516 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3517 	    bd_len);
3518 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3519 
3520 	/* See if writing DVD RAM is supported. */
3521 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3522 	if (un->un_f_dvdram_writable_device == TRUE) {
3523 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3524 		return;
3525 	}
3526 
3527 	/*
3528 	 * If the device presents DVD or CD capabilities in the mode
3529 	 * page, we can return here since a RRD will not have
3530 	 * these capabilities.
3531 	 */
3532 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3533 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3534 		return;
3535 	}
3536 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3537 
3538 	/*
3539 	 * If un->un_f_dvdram_writable_device is still FALSE,
3540 	 * check for a Removable Rigid Disk (RRD).  A RRD
3541 	 * device is identified by the features RANDOM_WRITABLE and
3542 	 * HARDWARE_DEFECT_MANAGEMENT.
3543 	 */
3544 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3545 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3546 
3547 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3548 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3549 	    RANDOM_WRITABLE, SD_PATH_STANDARD);
3550 
3551 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3552 
3553 	if (rtn != 0) {
3554 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3555 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3556 		return;
3557 	}
3558 
3559 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3560 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3561 
3562 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3563 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3564 	    HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3565 
3566 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3567 
3568 	if (rtn == 0) {
3569 		/*
3570 		 * We have good information, check for random writable
3571 		 * and hardware defect features.
3572 		 */
3573 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3574 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3575 			un->un_f_dvdram_writable_device = TRUE;
3576 		}
3577 	}
3578 
3579 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3580 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3581 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3582 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3583 }
3584 
3585 /*
3586  *    Function: sd_check_for_writable_cd
3587  *
3588  * Description: This routine determines if the media in the device is
3589  *		writable or not. It uses the get configuration command (0x46)
3590  *		to determine if the media is writable
3591  *
3592  *   Arguments: un - driver soft state (unit) structure
3593  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3594  *                           chain and the normal command waitq, or
3595  *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
3596  *                           "direct" chain and bypass the normal command
3597  *                           waitq.
3598  *
3599  *     Context: Never called at interrupt context.
3600  */
3601 
3602 static void
3603 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3604 {
3605 	struct uscsi_cmd		com;
3606 	uchar_t				*out_data;
3607 	uchar_t				*rqbuf;
3608 	int				rtn;
3609 	uchar_t				*out_data_rw, *out_data_hd;
3610 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3611 	struct mode_header_grp2		*sense_mhp;
3612 	uchar_t				*sense_page;
3613 	caddr_t				buf;
3614 	int				bd_len;
3615 	int				status;
3616 	struct sd_lun			*un;
3617 
3618 	ASSERT(ssc != NULL);
3619 	un = ssc->ssc_un;
3620 	ASSERT(un != NULL);
3621 	ASSERT(mutex_owned(SD_MUTEX(un)));
3622 
3623 	/*
3624 	 * Initialize the writable media to false, if configuration info.
3625 	 * tells us otherwise then only we will set it.
3626 	 */
3627 	un->un_f_mmc_writable_media = FALSE;
3628 	mutex_exit(SD_MUTEX(un));
3629 
3630 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3631 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3632 
3633 	rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3634 	    out_data, SD_PROFILE_HEADER_LEN, path_flag);
3635 
3636 	if (rtn != 0)
3637 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3638 
3639 	mutex_enter(SD_MUTEX(un));
3640 	if (rtn == 0) {
3641 		/*
3642 		 * We have good information, check for writable DVD.
3643 		 */
3644 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3645 			un->un_f_mmc_writable_media = TRUE;
3646 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3647 			kmem_free(rqbuf, SENSE_LENGTH);
3648 			return;
3649 		}
3650 	}
3651 
3652 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3653 	kmem_free(rqbuf, SENSE_LENGTH);
3654 
3655 	/*
3656 	 * Determine if this is a RRD type device.
3657 	 */
3658 	mutex_exit(SD_MUTEX(un));
3659 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3660 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3661 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3662 
3663 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3664 
3665 	mutex_enter(SD_MUTEX(un));
3666 	if (status != 0) {
3667 		/* command failed; just return */
3668 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3669 		return;
3670 	}
3671 
3672 	/* Get to the page data */
3673 	sense_mhp = (struct mode_header_grp2 *)buf;
3674 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3675 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3676 		/*
3677 		 * We did not get back the expected block descriptor length so
3678 		 * we cannot check the mode page.
3679 		 */
3680 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3681 		    "sd_check_for_writable_cd: Mode Sense returned "
3682 		    "invalid block descriptor length\n");
3683 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3684 		return;
3685 	}
3686 
3687 	/*
3688 	 * If the device presents DVD or CD capabilities in the mode
3689 	 * page, we can return here since a RRD device will not have
3690 	 * these capabilities.
3691 	 */
3692 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3693 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3694 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3695 		return;
3696 	}
3697 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3698 
3699 	/*
3700 	 * If un->un_f_mmc_writable_media is still FALSE,
3701 	 * check for RRD type media.  A RRD device is identified
3702 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3703 	 */
3704 	mutex_exit(SD_MUTEX(un));
3705 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3706 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3707 
3708 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3709 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3710 	    RANDOM_WRITABLE, path_flag);
3711 
3712 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3713 	if (rtn != 0) {
3714 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3715 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3716 		mutex_enter(SD_MUTEX(un));
3717 		return;
3718 	}
3719 
3720 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3721 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3722 
3723 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3724 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3725 	    HARDWARE_DEFECT_MANAGEMENT, path_flag);
3726 
3727 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3728 	mutex_enter(SD_MUTEX(un));
3729 	if (rtn == 0) {
3730 		/*
3731 		 * We have good information, check for random writable
3732 		 * and hardware defect features as current.
3733 		 */
3734 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3735 		    (out_data_rw[10] & 0x1) &&
3736 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3737 		    (out_data_hd[10] & 0x1)) {
3738 			un->un_f_mmc_writable_media = TRUE;
3739 		}
3740 	}
3741 
3742 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3743 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3744 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3745 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3746 }
3747 
3748 /*
3749  *    Function: sd_read_unit_properties
3750  *
3751  * Description: The following implements a property lookup mechanism.
3752  *		Properties for particular disks (keyed on vendor, model
3753  *		and rev numbers) are sought in the sd.conf file via
3754  *		sd_process_sdconf_file(), and if not found there, are
3755  *		looked for in a list hardcoded in this driver via
3756  *		sd_process_sdconf_table() Once located the properties
3757  *		are used to update the driver unit structure.
3758  *
3759  *   Arguments: un - driver soft state (unit) structure
3760  */
3761 
3762 static void
3763 sd_read_unit_properties(struct sd_lun *un)
3764 {
3765 	/*
3766 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3767 	 * the "sd-config-list" property (from the sd.conf file) or if
3768 	 * there was not a match for the inquiry vid/pid. If this event
3769 	 * occurs the static driver configuration table is searched for
3770 	 * a match.
3771 	 */
3772 	ASSERT(un != NULL);
3773 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3774 		sd_process_sdconf_table(un);
3775 	}
3776 
3777 	/* check for LSI device */
3778 	sd_is_lsi(un);
3779 
3780 
3781 }
3782 
3783 
3784 /*
3785  *    Function: sd_process_sdconf_file
3786  *
3787  * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3788  *		driver's config file (ie, sd.conf) and update the driver
3789  *		soft state structure accordingly.
3790  *
3791  *   Arguments: un - driver soft state (unit) structure
3792  *
3793  * Return Code: SD_SUCCESS - The properties were successfully set according
3794  *			     to the driver configuration file.
3795  *		SD_FAILURE - The driver config list was not obtained or
3796  *			     there was no vid/pid match. This indicates that
3797  *			     the static config table should be used.
3798  *
3799  * The config file has a property, "sd-config-list". Currently we support
3800  * two kinds of formats. For both formats, the value of this property
3801  * is a list of duplets:
3802  *
3803  *  sd-config-list=
3804  *	<duplet>,
3805  *	[,<duplet>]*;
3806  *
3807  * For the improved format, where
3808  *
3809  *     <duplet>:= "<vid+pid>","<tunable-list>"
3810  *
3811  * and
3812  *
3813  *     <tunable-list>:=   <tunable> [, <tunable> ]*;
3814  *     <tunable> =        <name> : <value>
3815  *
3816  * The <vid+pid> is the string that is returned by the target device on a
3817  * SCSI inquiry command, the <tunable-list> contains one or more tunables
3818  * to apply to all target devices with the specified <vid+pid>.
3819  *
3820  * Each <tunable> is a "<name> : <value>" pair.
3821  *
3822  * For the old format, the structure of each duplet is as follows:
3823  *
3824  *  <duplet>:= "<vid+pid>","<data-property-name_list>"
3825  *
3826  * The first entry of the duplet is the device ID string (the concatenated
3827  * vid & pid; not to be confused with a device_id).  This is defined in
3828  * the same way as in the sd_disk_table.
3829  *
3830  * The second part of the duplet is a string that identifies a
3831  * data-property-name-list. The data-property-name-list is defined as
3832  * follows:
3833  *
3834  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3835  *
3836  * The syntax of <data-property-name> depends on the <version> field.
3837  *
3838  * If version = SD_CONF_VERSION_1 we have the following syntax:
3839  *
3840  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3841  *
3842  * where the prop0 value will be used to set prop0 if bit0 set in the
3843  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3844  *
3845  */
3846 
3847 static int
3848 sd_process_sdconf_file(struct sd_lun *un)
3849 {
3850 	char	**config_list = NULL;
3851 	uint_t	nelements;
3852 	char	*vidptr;
3853 	int	vidlen;
3854 	char	*dnlist_ptr;
3855 	char	*dataname_ptr;
3856 	char	*dataname_lasts;
3857 	int	*data_list = NULL;
3858 	uint_t	data_list_len;
3859 	int	rval = SD_FAILURE;
3860 	int	i;
3861 
3862 	ASSERT(un != NULL);
3863 
3864 	/* Obtain the configuration list associated with the .conf file */
3865 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3866 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3867 	    &config_list, &nelements) != DDI_PROP_SUCCESS) {
3868 		return (SD_FAILURE);
3869 	}
3870 
3871 	/*
3872 	 * Compare vids in each duplet to the inquiry vid - if a match is
3873 	 * made, get the data value and update the soft state structure
3874 	 * accordingly.
3875 	 *
3876 	 * Each duplet should show as a pair of strings, return SD_FAILURE
3877 	 * otherwise.
3878 	 */
3879 	if (nelements & 1) {
3880 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3881 		    "sd-config-list should show as pairs of strings.\n");
3882 		if (config_list)
3883 			ddi_prop_free(config_list);
3884 		return (SD_FAILURE);
3885 	}
3886 
3887 	for (i = 0; i < nelements; i += 2) {
3888 		/*
3889 		 * Note: The assumption here is that each vid entry is on
3890 		 * a unique line from its associated duplet.
3891 		 */
3892 		vidptr = config_list[i];
3893 		vidlen = (int)strlen(vidptr);
3894 		if (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS) {
3895 			continue;
3896 		}
3897 
3898 		/*
3899 		 * dnlist contains 1 or more blank separated
3900 		 * data-property-name entries
3901 		 */
3902 		dnlist_ptr = config_list[i + 1];
3903 
3904 		if (strchr(dnlist_ptr, ':') != NULL) {
3905 			/*
3906 			 * Decode the improved format sd-config-list.
3907 			 */
3908 			sd_nvpair_str_decode(un, dnlist_ptr);
3909 		} else {
3910 			/*
3911 			 * The old format sd-config-list, loop through all
3912 			 * data-property-name entries in the
3913 			 * data-property-name-list
3914 			 * setting the properties for each.
3915 			 */
3916 			for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3917 			    &dataname_lasts); dataname_ptr != NULL;
3918 			    dataname_ptr = sd_strtok_r(NULL, " \t",
3919 			    &dataname_lasts)) {
3920 				int version;
3921 
3922 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3923 				    "sd_process_sdconf_file: disk:%s, "
3924 				    "data:%s\n", vidptr, dataname_ptr);
3925 
3926 				/* Get the data list */
3927 				if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3928 				    SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3929 				    &data_list_len) != DDI_PROP_SUCCESS) {
3930 					SD_INFO(SD_LOG_ATTACH_DETACH, un,
3931 					    "sd_process_sdconf_file: data "
3932 					    "property (%s) has no value\n",
3933 					    dataname_ptr);
3934 					continue;
3935 				}
3936 
3937 				version = data_list[0];
3938 
3939 				if (version == SD_CONF_VERSION_1) {
3940 					sd_tunables values;
3941 
3942 					/* Set the properties */
3943 					if (sd_chk_vers1_data(un, data_list[1],
3944 					    &data_list[2], data_list_len,
3945 					    dataname_ptr) == SD_SUCCESS) {
3946 						sd_get_tunables_from_conf(un,
3947 						    data_list[1], &data_list[2],
3948 						    &values);
3949 						sd_set_vers1_properties(un,
3950 						    data_list[1], &values);
3951 						rval = SD_SUCCESS;
3952 					} else {
3953 						rval = SD_FAILURE;
3954 					}
3955 				} else {
3956 					scsi_log(SD_DEVINFO(un), sd_label,
3957 					    CE_WARN, "data property %s version "
3958 					    "0x%x is invalid.",
3959 					    dataname_ptr, version);
3960 					rval = SD_FAILURE;
3961 				}
3962 				if (data_list)
3963 					ddi_prop_free(data_list);
3964 			}
3965 		}
3966 	}
3967 
3968 	/* free up the memory allocated by ddi_prop_lookup_string_array(). */
3969 	if (config_list) {
3970 		ddi_prop_free(config_list);
3971 	}
3972 
3973 	return (rval);
3974 }
3975 
3976 /*
3977  *    Function: sd_nvpair_str_decode()
3978  *
3979  * Description: Parse the improved format sd-config-list to get
3980  *    each entry of tunable, which includes a name-value pair.
3981  *    Then call sd_set_properties() to set the property.
3982  *
3983  *   Arguments: un - driver soft state (unit) structure
3984  *    nvpair_str - the tunable list
3985  */
3986 static void
3987 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3988 {
3989 	char	*nv, *name, *value, *token;
3990 	char	*nv_lasts, *v_lasts, *x_lasts;
3991 
3992 	for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3993 	    nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3994 		token = sd_strtok_r(nv, ":", &v_lasts);
3995 		name  = sd_strtok_r(token, " \t", &x_lasts);
3996 		token = sd_strtok_r(NULL, ":", &v_lasts);
3997 		value = sd_strtok_r(token, " \t", &x_lasts);
3998 		if (name == NULL || value == NULL) {
3999 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4000 			    "sd_nvpair_str_decode: "
4001 			    "name or value is not valid!\n");
4002 		} else {
4003 			sd_set_properties(un, name, value);
4004 		}
4005 	}
4006 }
4007 
4008 /*
4009  *    Function: sd_strtok_r()
4010  *
4011  * Description: This function uses strpbrk and strspn to break
4012  *    string into tokens on sequentially subsequent calls. Return
4013  *    NULL when no non-separator characters remain. The first
4014  *    argument is NULL for subsequent calls.
4015  */
4016 static char *
4017 sd_strtok_r(char *string, const char *sepset, char **lasts)
4018 {
4019 	char	*q, *r;
4020 
4021 	/* First or subsequent call */
4022 	if (string == NULL)
4023 		string = *lasts;
4024 
4025 	if (string == NULL)
4026 		return (NULL);
4027 
4028 	/* Skip leading separators */
4029 	q = string + strspn(string, sepset);
4030 
4031 	if (*q == '\0')
4032 		return (NULL);
4033 
4034 	if ((r = strpbrk(q, sepset)) == NULL)
4035 		*lasts = NULL;
4036 	else {
4037 		*r = '\0';
4038 		*lasts = r + 1;
4039 	}
4040 	return (q);
4041 }
4042 
4043 /*
4044  *    Function: sd_set_properties()
4045  *
4046  * Description: Set device properties based on the improved
4047  *    format sd-config-list.
4048  *
4049  *   Arguments: un - driver soft state (unit) structure
4050  *    name  - supported tunable name
4051  *    value - tunable value
4052  */
4053 static void
4054 sd_set_properties(struct sd_lun *un, char *name, char *value)
4055 {
4056 	char	*endptr = NULL;
4057 	long	val = 0;
4058 
4059 	if (strcasecmp(name, "cache-nonvolatile") == 0) {
4060 		if (strcasecmp(value, "true") == 0) {
4061 			un->un_f_suppress_cache_flush = TRUE;
4062 		} else if (strcasecmp(value, "false") == 0) {
4063 			un->un_f_suppress_cache_flush = FALSE;
4064 		} else {
4065 			goto value_invalid;
4066 		}
4067 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4068 		    "suppress_cache_flush flag set to %d\n",
4069 		    un->un_f_suppress_cache_flush);
4070 		return;
4071 	}
4072 
4073 	if (strcasecmp(name, "controller-type") == 0) {
4074 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4075 			un->un_ctype = val;
4076 		} else {
4077 			goto value_invalid;
4078 		}
4079 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4080 		    "ctype set to %d\n", un->un_ctype);
4081 		return;
4082 	}
4083 
4084 	if (strcasecmp(name, "delay-busy") == 0) {
4085 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4086 			un->un_busy_timeout = drv_usectohz(val / 1000);
4087 		} else {
4088 			goto value_invalid;
4089 		}
4090 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4091 		    "busy_timeout set to %d\n", un->un_busy_timeout);
4092 		return;
4093 	}
4094 
4095 	if (strcasecmp(name, "disksort") == 0) {
4096 		if (strcasecmp(value, "true") == 0) {
4097 			un->un_f_disksort_disabled = FALSE;
4098 		} else if (strcasecmp(value, "false") == 0) {
4099 			un->un_f_disksort_disabled = TRUE;
4100 		} else {
4101 			goto value_invalid;
4102 		}
4103 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4104 		    "disksort disabled flag set to %d\n",
4105 		    un->un_f_disksort_disabled);
4106 		return;
4107 	}
4108 
4109 	if (strcasecmp(name, "power-condition") == 0) {
4110 		if (strcasecmp(value, "true") == 0) {
4111 			un->un_f_power_condition_disabled = FALSE;
4112 		} else if (strcasecmp(value, "false") == 0) {
4113 			un->un_f_power_condition_disabled = TRUE;
4114 		} else {
4115 			goto value_invalid;
4116 		}
4117 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4118 		    "power condition disabled flag set to %d\n",
4119 		    un->un_f_power_condition_disabled);
4120 		return;
4121 	}
4122 
4123 	if (strcasecmp(name, "timeout-releasereservation") == 0) {
4124 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4125 			un->un_reserve_release_time = val;
4126 		} else {
4127 			goto value_invalid;
4128 		}
4129 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4130 		    "reservation release timeout set to %d\n",
4131 		    un->un_reserve_release_time);
4132 		return;
4133 	}
4134 
4135 	if (strcasecmp(name, "reset-lun") == 0) {
4136 		if (strcasecmp(value, "true") == 0) {
4137 			un->un_f_lun_reset_enabled = TRUE;
4138 		} else if (strcasecmp(value, "false") == 0) {
4139 			un->un_f_lun_reset_enabled = FALSE;
4140 		} else {
4141 			goto value_invalid;
4142 		}
4143 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4144 		    "lun reset enabled flag set to %d\n",
4145 		    un->un_f_lun_reset_enabled);
4146 		return;
4147 	}
4148 
4149 	if (strcasecmp(name, "retries-busy") == 0) {
4150 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4151 			un->un_busy_retry_count = val;
4152 		} else {
4153 			goto value_invalid;
4154 		}
4155 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4156 		    "busy retry count set to %d\n", un->un_busy_retry_count);
4157 		return;
4158 	}
4159 
4160 	if (strcasecmp(name, "retries-timeout") == 0) {
4161 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4162 			un->un_retry_count = val;
4163 		} else {
4164 			goto value_invalid;
4165 		}
4166 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4167 		    "timeout retry count set to %d\n", un->un_retry_count);
4168 		return;
4169 	}
4170 
4171 	if (strcasecmp(name, "retries-notready") == 0) {
4172 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4173 			un->un_notready_retry_count = val;
4174 		} else {
4175 			goto value_invalid;
4176 		}
4177 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4178 		    "notready retry count set to %d\n",
4179 		    un->un_notready_retry_count);
4180 		return;
4181 	}
4182 
4183 	if (strcasecmp(name, "retries-reset") == 0) {
4184 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4185 			un->un_reset_retry_count = val;
4186 		} else {
4187 			goto value_invalid;
4188 		}
4189 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4190 		    "reset retry count set to %d\n",
4191 		    un->un_reset_retry_count);
4192 		return;
4193 	}
4194 
4195 	if (strcasecmp(name, "throttle-max") == 0) {
4196 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4197 			un->un_saved_throttle = un->un_throttle = val;
4198 		} else {
4199 			goto value_invalid;
4200 		}
4201 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4202 		    "throttle set to %d\n", un->un_throttle);
4203 	}
4204 
4205 	if (strcasecmp(name, "throttle-min") == 0) {
4206 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4207 			un->un_min_throttle = val;
4208 		} else {
4209 			goto value_invalid;
4210 		}
4211 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4212 		    "min throttle set to %d\n", un->un_min_throttle);
4213 	}
4214 
4215 	if (strcasecmp(name, "rmw-type") == 0) {
4216 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4217 			un->un_f_rmw_type = val;
4218 		} else {
4219 			goto value_invalid;
4220 		}
4221 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4222 		    "RMW type set to %d\n", un->un_f_rmw_type);
4223 	}
4224 
4225 	if (strcasecmp(name, "physical-block-size") == 0) {
4226 		if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
4227 		    ISP2(val) && val >= un->un_tgt_blocksize &&
4228 		    val >= un->un_sys_blocksize) {
4229 			un->un_phy_blocksize = val;
4230 		} else {
4231 			goto value_invalid;
4232 		}
4233 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4234 		    "physical block size set to %d\n", un->un_phy_blocksize);
4235 	}
4236 
4237 	if (strcasecmp(name, "retries-victim") == 0) {
4238 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4239 			un->un_victim_retry_count = val;
4240 		} else {
4241 			goto value_invalid;
4242 		}
4243 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4244 		    "victim retry count set to %d\n",
4245 		    un->un_victim_retry_count);
4246 		return;
4247 	}
4248 
4249 	/*
4250 	 * Validate the throttle values.
4251 	 * If any of the numbers are invalid, set everything to defaults.
4252 	 */
4253 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4254 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4255 	    (un->un_min_throttle > un->un_throttle)) {
4256 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4257 		un->un_min_throttle = sd_min_throttle;
4258 	}
4259 
4260 	if (strcasecmp(name, "mmc-gesn-polling") == 0) {
4261 		if (strcasecmp(value, "true") == 0) {
4262 			un->un_f_mmc_gesn_polling = TRUE;
4263 		} else if (strcasecmp(value, "false") == 0) {
4264 			un->un_f_mmc_gesn_polling = FALSE;
4265 		} else {
4266 			goto value_invalid;
4267 		}
4268 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4269 		    "mmc-gesn-polling set to %d\n",
4270 		    un->un_f_mmc_gesn_polling);
4271 	}
4272 
4273 	return;
4274 
4275 value_invalid:
4276 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4277 	    "value of prop %s is invalid\n", name);
4278 }
4279 
4280 /*
4281  *    Function: sd_get_tunables_from_conf()
4282  *
4283  *
4284  *    This function reads the data list from the sd.conf file and pulls
4285  *    the values that can have numeric values as arguments and places
4286  *    the values in the appropriate sd_tunables member.
4287  *    Since the order of the data list members varies across platforms
4288  *    This function reads them from the data list in a platform specific
4289  *    order and places them into the correct sd_tunable member that is
4290  *    consistent across all platforms.
4291  */
4292 static void
4293 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
4294     sd_tunables *values)
4295 {
4296 	int i;
4297 	int mask;
4298 
4299 	bzero(values, sizeof (sd_tunables));
4300 
4301 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4302 
4303 		mask = 1 << i;
4304 		if (mask > flags) {
4305 			break;
4306 		}
4307 
4308 		switch (mask & flags) {
4309 		case 0:	/* This mask bit not set in flags */
4310 			continue;
4311 		case SD_CONF_BSET_THROTTLE:
4312 			values->sdt_throttle = data_list[i];
4313 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4314 			    "sd_get_tunables_from_conf: throttle = %d\n",
4315 			    values->sdt_throttle);
4316 			break;
4317 		case SD_CONF_BSET_CTYPE:
4318 			values->sdt_ctype = data_list[i];
4319 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4320 			    "sd_get_tunables_from_conf: ctype = %d\n",
4321 			    values->sdt_ctype);
4322 			break;
4323 		case SD_CONF_BSET_NRR_COUNT:
4324 			values->sdt_not_rdy_retries = data_list[i];
4325 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4326 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
4327 			    values->sdt_not_rdy_retries);
4328 			break;
4329 		case SD_CONF_BSET_BSY_RETRY_COUNT:
4330 			values->sdt_busy_retries = data_list[i];
4331 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4332 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
4333 			    values->sdt_busy_retries);
4334 			break;
4335 		case SD_CONF_BSET_RST_RETRIES:
4336 			values->sdt_reset_retries = data_list[i];
4337 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4338 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
4339 			    values->sdt_reset_retries);
4340 			break;
4341 		case SD_CONF_BSET_RSV_REL_TIME:
4342 			values->sdt_reserv_rel_time = data_list[i];
4343 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4344 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
4345 			    values->sdt_reserv_rel_time);
4346 			break;
4347 		case SD_CONF_BSET_MIN_THROTTLE:
4348 			values->sdt_min_throttle = data_list[i];
4349 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4350 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
4351 			    values->sdt_min_throttle);
4352 			break;
4353 		case SD_CONF_BSET_DISKSORT_DISABLED:
4354 			values->sdt_disk_sort_dis = data_list[i];
4355 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4356 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
4357 			    values->sdt_disk_sort_dis);
4358 			break;
4359 		case SD_CONF_BSET_LUN_RESET_ENABLED:
4360 			values->sdt_lun_reset_enable = data_list[i];
4361 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4362 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
4363 			    "\n", values->sdt_lun_reset_enable);
4364 			break;
4365 		case SD_CONF_BSET_CACHE_IS_NV:
4366 			values->sdt_suppress_cache_flush = data_list[i];
4367 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4368 			    "sd_get_tunables_from_conf: \
4369 			    suppress_cache_flush = %d"
4370 			    "\n", values->sdt_suppress_cache_flush);
4371 			break;
4372 		case SD_CONF_BSET_PC_DISABLED:
4373 			values->sdt_disk_sort_dis = data_list[i];
4374 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4375 			    "sd_get_tunables_from_conf: power_condition_dis = "
4376 			    "%d\n", values->sdt_power_condition_dis);
4377 			break;
4378 		}
4379 	}
4380 }
4381 
4382 /*
4383  *    Function: sd_process_sdconf_table
4384  *
4385  * Description: Search the static configuration table for a match on the
4386  *		inquiry vid/pid and update the driver soft state structure
4387  *		according to the table property values for the device.
4388  *
4389  *		The form of a configuration table entry is:
4390  *		  <vid+pid>,<flags>,<property-data>
4391  *		  "SEAGATE ST42400N",1,0x40000,
4392  *		  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
4393  *
4394  *   Arguments: un - driver soft state (unit) structure
4395  */
4396 
4397 static void
4398 sd_process_sdconf_table(struct sd_lun *un)
4399 {
4400 	char	*id = NULL;
4401 	int	table_index;
4402 	int	idlen;
4403 
4404 	ASSERT(un != NULL);
4405 	for (table_index = 0; table_index < sd_disk_table_size;
4406 	    table_index++) {
4407 		id = sd_disk_table[table_index].device_id;
4408 		idlen = strlen(id);
4409 
4410 		/*
4411 		 * The static configuration table currently does not
4412 		 * implement version 10 properties. Additionally,
4413 		 * multiple data-property-name entries are not
4414 		 * implemented in the static configuration table.
4415 		 */
4416 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4417 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4418 			    "sd_process_sdconf_table: disk %s\n", id);
4419 			sd_set_vers1_properties(un,
4420 			    sd_disk_table[table_index].flags,
4421 			    sd_disk_table[table_index].properties);
4422 			break;
4423 		}
4424 	}
4425 }
4426 
4427 
4428 /*
4429  *    Function: sd_sdconf_id_match
4430  *
4431  * Description: This local function implements a case sensitive vid/pid
4432  *		comparison as well as the boundary cases of wild card and
4433  *		multiple blanks.
4434  *
4435  *		Note: An implicit assumption made here is that the scsi
4436  *		inquiry structure will always keep the vid, pid and
4437  *		revision strings in consecutive sequence, so they can be
4438  *		read as a single string. If this assumption is not the
4439  *		case, a separate string, to be used for the check, needs
4440  *		to be built with these strings concatenated.
4441  *
4442  *   Arguments: un - driver soft state (unit) structure
4443  *		id - table or config file vid/pid
4444  *		idlen  - length of the vid/pid (bytes)
4445  *
4446  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4447  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4448  */
4449 
4450 static int
4451 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4452 {
4453 	struct scsi_inquiry	*sd_inq;
4454 	int 			rval = SD_SUCCESS;
4455 
4456 	ASSERT(un != NULL);
4457 	sd_inq = un->un_sd->sd_inq;
4458 	ASSERT(id != NULL);
4459 
4460 	/*
4461 	 * We use the inq_vid as a pointer to a buffer containing the
4462 	 * vid and pid and use the entire vid/pid length of the table
4463 	 * entry for the comparison. This works because the inq_pid
4464 	 * data member follows inq_vid in the scsi_inquiry structure.
4465 	 */
4466 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4467 		/*
4468 		 * The user id string is compared to the inquiry vid/pid
4469 		 * using a case insensitive comparison and ignoring
4470 		 * multiple spaces.
4471 		 */
4472 		rval = sd_blank_cmp(un, id, idlen);
4473 		if (rval != SD_SUCCESS) {
4474 			/*
4475 			 * User id strings that start and end with a "*"
4476 			 * are a special case. These do not have a
4477 			 * specific vendor, and the product string can
4478 			 * appear anywhere in the 16 byte PID portion of
4479 			 * the inquiry data. This is a simple strstr()
4480 			 * type search for the user id in the inquiry data.
4481 			 */
4482 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4483 				char	*pidptr = &id[1];
4484 				int	i;
4485 				int	j;
4486 				int	pidstrlen = idlen - 2;
4487 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
4488 				    pidstrlen;
4489 
4490 				if (j < 0) {
4491 					return (SD_FAILURE);
4492 				}
4493 				for (i = 0; i < j; i++) {
4494 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4495 					    pidptr, pidstrlen) == 0) {
4496 						rval = SD_SUCCESS;
4497 						break;
4498 					}
4499 				}
4500 			}
4501 		}
4502 	}
4503 	return (rval);
4504 }
4505 
4506 
4507 /*
4508  *    Function: sd_blank_cmp
4509  *
4510  * Description: If the id string starts and ends with a space, treat
4511  *		multiple consecutive spaces as equivalent to a single
4512  *		space. For example, this causes a sd_disk_table entry
4513  *		of " NEC CDROM " to match a device's id string of
4514  *		"NEC       CDROM".
4515  *
4516  *		Note: The success exit condition for this routine is if
4517  *		the pointer to the table entry is '\0' and the cnt of
4518  *		the inquiry length is zero. This will happen if the inquiry
4519  *		string returned by the device is padded with spaces to be
4520  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4521  *		SCSI spec states that the inquiry string is to be padded with
4522  *		spaces.
4523  *
4524  *   Arguments: un - driver soft state (unit) structure
4525  *		id - table or config file vid/pid
4526  *		idlen  - length of the vid/pid (bytes)
4527  *
4528  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4529  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4530  */
4531 
4532 static int
4533 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4534 {
4535 	char		*p1;
4536 	char		*p2;
4537 	int		cnt;
4538 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4539 	    sizeof (SD_INQUIRY(un)->inq_pid);
4540 
4541 	ASSERT(un != NULL);
4542 	p2 = un->un_sd->sd_inq->inq_vid;
4543 	ASSERT(id != NULL);
4544 	p1 = id;
4545 
4546 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4547 		/*
4548 		 * Note: string p1 is terminated by a NUL but string p2
4549 		 * isn't.  The end of p2 is determined by cnt.
4550 		 */
4551 		for (;;) {
4552 			/* skip over any extra blanks in both strings */
4553 			while ((*p1 != '\0') && (*p1 == ' ')) {
4554 				p1++;
4555 			}
4556 			while ((cnt != 0) && (*p2 == ' ')) {
4557 				p2++;
4558 				cnt--;
4559 			}
4560 
4561 			/* compare the two strings */
4562 			if ((cnt == 0) ||
4563 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4564 				break;
4565 			}
4566 			while ((cnt > 0) &&
4567 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4568 				p1++;
4569 				p2++;
4570 				cnt--;
4571 			}
4572 		}
4573 	}
4574 
4575 	/* return SD_SUCCESS if both strings match */
4576 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4577 }
4578 
4579 
4580 /*
4581  *    Function: sd_chk_vers1_data
4582  *
4583  * Description: Verify the version 1 device properties provided by the
4584  *		user via the configuration file
4585  *
4586  *   Arguments: un	     - driver soft state (unit) structure
4587  *		flags	     - integer mask indicating properties to be set
4588  *		prop_list    - integer list of property values
4589  *		list_len     - number of the elements
4590  *
4591  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4592  *		SD_FAILURE - Indicates the user provided data is invalid
4593  */
4594 
4595 static int
4596 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4597     int list_len, char *dataname_ptr)
4598 {
4599 	int i;
4600 	int mask = 1;
4601 	int index = 0;
4602 
4603 	ASSERT(un != NULL);
4604 
4605 	/* Check for a NULL property name and list */
4606 	if (dataname_ptr == NULL) {
4607 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4608 		    "sd_chk_vers1_data: NULL data property name.");
4609 		return (SD_FAILURE);
4610 	}
4611 	if (prop_list == NULL) {
4612 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4613 		    "sd_chk_vers1_data: %s NULL data property list.",
4614 		    dataname_ptr);
4615 		return (SD_FAILURE);
4616 	}
4617 
4618 	/* Display a warning if undefined bits are set in the flags */
4619 	if (flags & ~SD_CONF_BIT_MASK) {
4620 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4621 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4622 		    "Properties not set.",
4623 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4624 		return (SD_FAILURE);
4625 	}
4626 
4627 	/*
4628 	 * Verify the length of the list by identifying the highest bit set
4629 	 * in the flags and validating that the property list has a length
4630 	 * up to the index of this bit.
4631 	 */
4632 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4633 		if (flags & mask) {
4634 			index++;
4635 		}
4636 		mask = 1 << i;
4637 	}
4638 	if (list_len < (index + 2)) {
4639 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4640 		    "sd_chk_vers1_data: "
4641 		    "Data property list %s size is incorrect. "
4642 		    "Properties not set.", dataname_ptr);
4643 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4644 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4645 		return (SD_FAILURE);
4646 	}
4647 	return (SD_SUCCESS);
4648 }
4649 
4650 
4651 /*
4652  *    Function: sd_set_vers1_properties
4653  *
4654  * Description: Set version 1 device properties based on a property list
4655  *		retrieved from the driver configuration file or static
4656  *		configuration table. Version 1 properties have the format:
4657  *
4658  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4659  *
4660  *		where the prop0 value will be used to set prop0 if bit0
4661  *		is set in the flags
4662  *
4663  *   Arguments: un	     - driver soft state (unit) structure
4664  *		flags	     - integer mask indicating properties to be set
4665  *		prop_list    - integer list of property values
4666  */
4667 
4668 static void
4669 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4670 {
4671 	ASSERT(un != NULL);
4672 
4673 	/*
4674 	 * Set the flag to indicate cache is to be disabled. An attempt
4675 	 * to disable the cache via sd_cache_control() will be made
4676 	 * later during attach once the basic initialization is complete.
4677 	 */
4678 	if (flags & SD_CONF_BSET_NOCACHE) {
4679 		un->un_f_opt_disable_cache = TRUE;
4680 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4681 		    "sd_set_vers1_properties: caching disabled flag set\n");
4682 	}
4683 
4684 	/* CD-specific configuration parameters */
4685 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4686 		un->un_f_cfg_playmsf_bcd = TRUE;
4687 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4688 		    "sd_set_vers1_properties: playmsf_bcd set\n");
4689 	}
4690 	if (flags & SD_CONF_BSET_READSUB_BCD) {
4691 		un->un_f_cfg_readsub_bcd = TRUE;
4692 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4693 		    "sd_set_vers1_properties: readsub_bcd set\n");
4694 	}
4695 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4696 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
4697 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4698 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4699 	}
4700 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4701 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
4702 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4703 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4704 	}
4705 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4706 		un->un_f_cfg_no_read_header = TRUE;
4707 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4708 		    "sd_set_vers1_properties: no_read_header set\n");
4709 	}
4710 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
4711 		un->un_f_cfg_read_cd_xd4 = TRUE;
4712 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4713 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
4714 	}
4715 
4716 	/* Support for devices which do not have valid/unique serial numbers */
4717 	if (flags & SD_CONF_BSET_FAB_DEVID) {
4718 		un->un_f_opt_fab_devid = TRUE;
4719 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4720 		    "sd_set_vers1_properties: fab_devid bit set\n");
4721 	}
4722 
4723 	/* Support for user throttle configuration */
4724 	if (flags & SD_CONF_BSET_THROTTLE) {
4725 		ASSERT(prop_list != NULL);
4726 		un->un_saved_throttle = un->un_throttle =
4727 		    prop_list->sdt_throttle;
4728 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4729 		    "sd_set_vers1_properties: throttle set to %d\n",
4730 		    prop_list->sdt_throttle);
4731 	}
4732 
4733 	/* Set the per disk retry count according to the conf file or table. */
4734 	if (flags & SD_CONF_BSET_NRR_COUNT) {
4735 		ASSERT(prop_list != NULL);
4736 		if (prop_list->sdt_not_rdy_retries) {
4737 			un->un_notready_retry_count =
4738 			    prop_list->sdt_not_rdy_retries;
4739 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4740 			    "sd_set_vers1_properties: not ready retry count"
4741 			    " set to %d\n", un->un_notready_retry_count);
4742 		}
4743 	}
4744 
4745 	/* The controller type is reported for generic disk driver ioctls */
4746 	if (flags & SD_CONF_BSET_CTYPE) {
4747 		ASSERT(prop_list != NULL);
4748 		switch (prop_list->sdt_ctype) {
4749 		case CTYPE_CDROM:
4750 			un->un_ctype = prop_list->sdt_ctype;
4751 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4752 			    "sd_set_vers1_properties: ctype set to "
4753 			    "CTYPE_CDROM\n");
4754 			break;
4755 		case CTYPE_CCS:
4756 			un->un_ctype = prop_list->sdt_ctype;
4757 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4758 			    "sd_set_vers1_properties: ctype set to "
4759 			    "CTYPE_CCS\n");
4760 			break;
4761 		case CTYPE_ROD:		/* RW optical */
4762 			un->un_ctype = prop_list->sdt_ctype;
4763 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4764 			    "sd_set_vers1_properties: ctype set to "
4765 			    "CTYPE_ROD\n");
4766 			break;
4767 		default:
4768 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4769 			    "sd_set_vers1_properties: Could not set "
4770 			    "invalid ctype value (%d)",
4771 			    prop_list->sdt_ctype);
4772 		}
4773 	}
4774 
4775 	/* Purple failover timeout */
4776 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4777 		ASSERT(prop_list != NULL);
4778 		un->un_busy_retry_count =
4779 		    prop_list->sdt_busy_retries;
4780 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4781 		    "sd_set_vers1_properties: "
4782 		    "busy retry count set to %d\n",
4783 		    un->un_busy_retry_count);
4784 	}
4785 
4786 	/* Purple reset retry count */
4787 	if (flags & SD_CONF_BSET_RST_RETRIES) {
4788 		ASSERT(prop_list != NULL);
4789 		un->un_reset_retry_count =
4790 		    prop_list->sdt_reset_retries;
4791 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4792 		    "sd_set_vers1_properties: "
4793 		    "reset retry count set to %d\n",
4794 		    un->un_reset_retry_count);
4795 	}
4796 
4797 	/* Purple reservation release timeout */
4798 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4799 		ASSERT(prop_list != NULL);
4800 		un->un_reserve_release_time =
4801 		    prop_list->sdt_reserv_rel_time;
4802 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4803 		    "sd_set_vers1_properties: "
4804 		    "reservation release timeout set to %d\n",
4805 		    un->un_reserve_release_time);
4806 	}
4807 
4808 	/*
4809 	 * Driver flag telling the driver to verify that no commands are pending
4810 	 * for a device before issuing a Test Unit Ready. This is a workaround
4811 	 * for a firmware bug in some Seagate eliteI drives.
4812 	 */
4813 	if (flags & SD_CONF_BSET_TUR_CHECK) {
4814 		un->un_f_cfg_tur_check = TRUE;
4815 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4816 		    "sd_set_vers1_properties: tur queue check set\n");
4817 	}
4818 
4819 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4820 		un->un_min_throttle = prop_list->sdt_min_throttle;
4821 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4822 		    "sd_set_vers1_properties: min throttle set to %d\n",
4823 		    un->un_min_throttle);
4824 	}
4825 
4826 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4827 		un->un_f_disksort_disabled =
4828 		    (prop_list->sdt_disk_sort_dis != 0) ?
4829 		    TRUE : FALSE;
4830 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4831 		    "sd_set_vers1_properties: disksort disabled "
4832 		    "flag set to %d\n",
4833 		    prop_list->sdt_disk_sort_dis);
4834 	}
4835 
4836 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4837 		un->un_f_lun_reset_enabled =
4838 		    (prop_list->sdt_lun_reset_enable != 0) ?
4839 		    TRUE : FALSE;
4840 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4841 		    "sd_set_vers1_properties: lun reset enabled "
4842 		    "flag set to %d\n",
4843 		    prop_list->sdt_lun_reset_enable);
4844 	}
4845 
4846 	if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4847 		un->un_f_suppress_cache_flush =
4848 		    (prop_list->sdt_suppress_cache_flush != 0) ?
4849 		    TRUE : FALSE;
4850 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4851 		    "sd_set_vers1_properties: suppress_cache_flush "
4852 		    "flag set to %d\n",
4853 		    prop_list->sdt_suppress_cache_flush);
4854 	}
4855 
4856 	if (flags & SD_CONF_BSET_PC_DISABLED) {
4857 		un->un_f_power_condition_disabled =
4858 		    (prop_list->sdt_power_condition_dis != 0) ?
4859 		    TRUE : FALSE;
4860 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4861 		    "sd_set_vers1_properties: power_condition_disabled "
4862 		    "flag set to %d\n",
4863 		    prop_list->sdt_power_condition_dis);
4864 	}
4865 
4866 	/*
4867 	 * Validate the throttle values.
4868 	 * If any of the numbers are invalid, set everything to defaults.
4869 	 */
4870 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4871 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4872 	    (un->un_min_throttle > un->un_throttle)) {
4873 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4874 		un->un_min_throttle = sd_min_throttle;
4875 	}
4876 }
4877 
4878 /*
4879  *   Function: sd_is_lsi()
4880  *
4881  *   Description: Check for lsi devices, step through the static device
4882  *	table to match vid/pid.
4883  *
4884  *   Args: un - ptr to sd_lun
4885  *
4886  *   Notes:  When creating new LSI property, need to add the new LSI property
4887  *		to this function.
4888  */
4889 static void
4890 sd_is_lsi(struct sd_lun *un)
4891 {
4892 	char	*id = NULL;
4893 	int	table_index;
4894 	int	idlen;
4895 	void	*prop;
4896 
4897 	ASSERT(un != NULL);
4898 	for (table_index = 0; table_index < sd_disk_table_size;
4899 	    table_index++) {
4900 		id = sd_disk_table[table_index].device_id;
4901 		idlen = strlen(id);
4902 		if (idlen == 0) {
4903 			continue;
4904 		}
4905 
4906 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4907 			prop = sd_disk_table[table_index].properties;
4908 			if (prop == &lsi_properties ||
4909 			    prop == &lsi_oem_properties ||
4910 			    prop == &lsi_properties_scsi ||
4911 			    prop == &symbios_properties) {
4912 				un->un_f_cfg_is_lsi = TRUE;
4913 			}
4914 			break;
4915 		}
4916 	}
4917 }
4918 
4919 /*
4920  *    Function: sd_get_physical_geometry
4921  *
4922  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4923  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4924  *		target, and use this information to initialize the physical
4925  *		geometry cache specified by pgeom_p.
4926  *
4927  *		MODE SENSE is an optional command, so failure in this case
4928  *		does not necessarily denote an error. We want to use the
4929  *		MODE SENSE commands to derive the physical geometry of the
4930  *		device, but if either command fails, the logical geometry is
4931  *		used as the fallback for disk label geometry in cmlb.
4932  *
4933  *		This requires that un->un_blockcount and un->un_tgt_blocksize
4934  *		have already been initialized for the current target and
4935  *		that the current values be passed as args so that we don't
4936  *		end up ever trying to use -1 as a valid value. This could
4937  *		happen if either value is reset while we're not holding
4938  *		the mutex.
4939  *
4940  *   Arguments: un - driver soft state (unit) structure
4941  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4942  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4943  *			to use the USCSI "direct" chain and bypass the normal
4944  *			command waitq.
4945  *
4946  *     Context: Kernel thread only (can sleep).
4947  */
4948 
4949 static int
4950 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4951 	diskaddr_t capacity, int lbasize, int path_flag)
4952 {
4953 	struct	mode_format	*page3p;
4954 	struct	mode_geometry	*page4p;
4955 	struct	mode_header	*headerp;
4956 	int	sector_size;
4957 	int	nsect;
4958 	int	nhead;
4959 	int	ncyl;
4960 	int	intrlv;
4961 	int	spc;
4962 	diskaddr_t	modesense_capacity;
4963 	int	rpm;
4964 	int	bd_len;
4965 	int	mode_header_length;
4966 	uchar_t	*p3bufp;
4967 	uchar_t	*p4bufp;
4968 	int	cdbsize;
4969 	int 	ret = EIO;
4970 	sd_ssc_t *ssc;
4971 	int	status;
4972 
4973 	ASSERT(un != NULL);
4974 
4975 	if (lbasize == 0) {
4976 		if (ISCD(un)) {
4977 			lbasize = 2048;
4978 		} else {
4979 			lbasize = un->un_sys_blocksize;
4980 		}
4981 	}
4982 	pgeom_p->g_secsize = (unsigned short)lbasize;
4983 
4984 	/*
4985 	 * If the unit is a cd/dvd drive MODE SENSE page three
4986 	 * and MODE SENSE page four are reserved (see SBC spec
4987 	 * and MMC spec). To prevent soft errors just return
4988 	 * using the default LBA size.
4989 	 *
4990 	 * Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
4991 	 * implement support for mode pages 3 and 4 return here to prevent
4992 	 * illegal requests on SATA drives.
4993 	 *
4994 	 * These pages are also reserved in SBC-2 and later.  We assume SBC-2
4995 	 * or later for a direct-attached block device if the SCSI version is
4996 	 * at least SPC-3.
4997 	 */
4998 
4999 	if (ISCD(un) ||
5000 	    un->un_interconnect_type == SD_INTERCONNECT_SATA ||
5001 	    (un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
5002 		return (ret);
5003 
5004 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
5005 
5006 	/*
5007 	 * Retrieve MODE SENSE page 3 - Format Device Page
5008 	 */
5009 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
5010 	ssc = sd_ssc_init(un);
5011 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
5012 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
5013 	if (status != 0) {
5014 		SD_ERROR(SD_LOG_COMMON, un,
5015 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
5016 		goto page3_exit;
5017 	}
5018 
5019 	/*
5020 	 * Determine size of Block Descriptors in order to locate the mode
5021 	 * page data.  ATAPI devices return 0, SCSI devices should return
5022 	 * MODE_BLK_DESC_LENGTH.
5023 	 */
5024 	headerp = (struct mode_header *)p3bufp;
5025 	if (un->un_f_cfg_is_atapi == TRUE) {
5026 		struct mode_header_grp2 *mhp =
5027 		    (struct mode_header_grp2 *)headerp;
5028 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
5029 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5030 	} else {
5031 		mode_header_length = MODE_HEADER_LENGTH;
5032 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5033 	}
5034 
5035 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5036 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5037 		    "sd_get_physical_geometry: received unexpected bd_len "
5038 		    "of %d, page3\n", bd_len);
5039 		status = EIO;
5040 		goto page3_exit;
5041 	}
5042 
5043 	page3p = (struct mode_format *)
5044 	    ((caddr_t)headerp + mode_header_length + bd_len);
5045 
5046 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5047 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5048 		    "sd_get_physical_geometry: mode sense pg3 code mismatch "
5049 		    "%d\n", page3p->mode_page.code);
5050 		status = EIO;
5051 		goto page3_exit;
5052 	}
5053 
5054 	/*
5055 	 * Use this physical geometry data only if BOTH MODE SENSE commands
5056 	 * complete successfully; otherwise, revert to the logical geometry.
5057 	 * So, we need to save everything in temporary variables.
5058 	 */
5059 	sector_size = BE_16(page3p->data_bytes_sect);
5060 
5061 	/*
5062 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5063 	 */
5064 	if (sector_size == 0) {
5065 		sector_size = un->un_sys_blocksize;
5066 	} else {
5067 		sector_size &= ~(un->un_sys_blocksize - 1);
5068 	}
5069 
5070 	nsect  = BE_16(page3p->sect_track);
5071 	intrlv = BE_16(page3p->interleave);
5072 
5073 	SD_INFO(SD_LOG_COMMON, un,
5074 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
5075 	SD_INFO(SD_LOG_COMMON, un,
5076 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
5077 	    page3p->mode_page.code, nsect, sector_size);
5078 	SD_INFO(SD_LOG_COMMON, un,
5079 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5080 	    BE_16(page3p->track_skew),
5081 	    BE_16(page3p->cylinder_skew));
5082 
5083 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5084 
5085 	/*
5086 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5087 	 */
5088 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5089 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
5090 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
5091 	if (status != 0) {
5092 		SD_ERROR(SD_LOG_COMMON, un,
5093 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
5094 		goto page4_exit;
5095 	}
5096 
5097 	/*
5098 	 * Determine size of Block Descriptors in order to locate the mode
5099 	 * page data.  ATAPI devices return 0, SCSI devices should return
5100 	 * MODE_BLK_DESC_LENGTH.
5101 	 */
5102 	headerp = (struct mode_header *)p4bufp;
5103 	if (un->un_f_cfg_is_atapi == TRUE) {
5104 		struct mode_header_grp2 *mhp =
5105 		    (struct mode_header_grp2 *)headerp;
5106 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5107 	} else {
5108 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5109 	}
5110 
5111 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5112 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5113 		    "sd_get_physical_geometry: received unexpected bd_len of "
5114 		    "%d, page4\n", bd_len);
5115 		status = EIO;
5116 		goto page4_exit;
5117 	}
5118 
5119 	page4p = (struct mode_geometry *)
5120 	    ((caddr_t)headerp + mode_header_length + bd_len);
5121 
5122 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5123 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5124 		    "sd_get_physical_geometry: mode sense pg4 code mismatch "
5125 		    "%d\n", page4p->mode_page.code);
5126 		status = EIO;
5127 		goto page4_exit;
5128 	}
5129 
5130 	/*
5131 	 * Stash the data now, after we know that both commands completed.
5132 	 */
5133 
5134 
5135 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
5136 	spc   = nhead * nsect;
5137 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5138 	rpm   = BE_16(page4p->rpm);
5139 
5140 	modesense_capacity = spc * ncyl;
5141 
5142 	SD_INFO(SD_LOG_COMMON, un,
5143 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5144 	SD_INFO(SD_LOG_COMMON, un,
5145 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5146 	SD_INFO(SD_LOG_COMMON, un,
5147 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
5148 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
5149 	    (void *)pgeom_p, capacity);
5150 
5151 	/*
5152 	 * Compensate if the drive's geometry is not rectangular, i.e.,
5153 	 * the product of C * H * S returned by MODE SENSE >= that returned
5154 	 * by read capacity. This is an idiosyncrasy of the original x86
5155 	 * disk subsystem.
5156 	 */
5157 	if (modesense_capacity >= capacity) {
5158 		SD_INFO(SD_LOG_COMMON, un,
5159 		    "sd_get_physical_geometry: adjusting acyl; "
5160 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
5161 		    (modesense_capacity - capacity + spc - 1) / spc);
5162 		if (sector_size != 0) {
5163 			/* 1243403: NEC D38x7 drives don't support sec size */
5164 			pgeom_p->g_secsize = (unsigned short)sector_size;
5165 		}
5166 		pgeom_p->g_nsect    = (unsigned short)nsect;
5167 		pgeom_p->g_nhead    = (unsigned short)nhead;
5168 		pgeom_p->g_capacity = capacity;
5169 		pgeom_p->g_acyl	    =
5170 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5171 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5172 	}
5173 
5174 	pgeom_p->g_rpm    = (unsigned short)rpm;
5175 	pgeom_p->g_intrlv = (unsigned short)intrlv;
5176 	ret = 0;
5177 
5178 	SD_INFO(SD_LOG_COMMON, un,
5179 	    "sd_get_physical_geometry: mode sense geometry:\n");
5180 	SD_INFO(SD_LOG_COMMON, un,
5181 	    "   nsect: %d; sector size: %d; interlv: %d\n",
5182 	    nsect, sector_size, intrlv);
5183 	SD_INFO(SD_LOG_COMMON, un,
5184 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5185 	    nhead, ncyl, rpm, modesense_capacity);
5186 	SD_INFO(SD_LOG_COMMON, un,
5187 	    "sd_get_physical_geometry: (cached)\n");
5188 	SD_INFO(SD_LOG_COMMON, un,
5189 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5190 	    pgeom_p->g_ncyl,  pgeom_p->g_acyl,
5191 	    pgeom_p->g_nhead, pgeom_p->g_nsect);
5192 	SD_INFO(SD_LOG_COMMON, un,
5193 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5194 	    pgeom_p->g_secsize, pgeom_p->g_capacity,
5195 	    pgeom_p->g_intrlv, pgeom_p->g_rpm);
5196 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5197 
5198 page4_exit:
5199 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5200 
5201 page3_exit:
5202 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5203 
5204 	if (status != 0) {
5205 		if (status == EIO) {
5206 			/*
5207 			 * Some disks do not support mode sense(6), we
5208 			 * should ignore this kind of error(sense key is
5209 			 * 0x5 - illegal request).
5210 			 */
5211 			uint8_t *sensep;
5212 			int senlen;
5213 
5214 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
5215 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
5216 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
5217 
5218 			if (senlen > 0 &&
5219 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
5220 				sd_ssc_assessment(ssc,
5221 				    SD_FMT_IGNORE_COMPROMISE);
5222 			} else {
5223 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
5224 			}
5225 		} else {
5226 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5227 		}
5228 	}
5229 	sd_ssc_fini(ssc);
5230 	return (ret);
5231 }
5232 
5233 /*
5234  *    Function: sd_get_virtual_geometry
5235  *
5236  * Description: Ask the controller to tell us about the target device.
5237  *
5238  *   Arguments: un - pointer to softstate
5239  *		capacity - disk capacity in #blocks
5240  *		lbasize - disk block size in bytes
5241  *
5242  *     Context: Kernel thread only
5243  */
5244 
5245 static int
5246 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
5247     diskaddr_t capacity, int lbasize)
5248 {
5249 	uint_t	geombuf;
5250 	int	spc;
5251 
5252 	ASSERT(un != NULL);
5253 
5254 	/* Set sector size, and total number of sectors */
5255 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5256 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5257 
5258 	/* Let the HBA tell us its geometry */
5259 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5260 
5261 	/* A value of -1 indicates an undefined "geometry" property */
5262 	if (geombuf == (-1)) {
5263 		return (EINVAL);
5264 	}
5265 
5266 	/* Initialize the logical geometry cache. */
5267 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5268 	lgeom_p->g_nsect   = geombuf & 0xffff;
5269 	lgeom_p->g_secsize = un->un_sys_blocksize;
5270 
5271 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5272 
5273 	/*
5274 	 * Note: The driver originally converted the capacity value from
5275 	 * target blocks to system blocks. However, the capacity value passed
5276 	 * to this routine is already in terms of system blocks (this scaling
5277 	 * is done when the READ CAPACITY command is issued and processed).
5278 	 * This 'error' may have gone undetected because the usage of g_ncyl
5279 	 * (which is based upon g_capacity) is very limited within the driver
5280 	 */
5281 	lgeom_p->g_capacity = capacity;
5282 
5283 	/*
5284 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5285 	 * hba may return zero values if the device has been removed.
5286 	 */
5287 	if (spc == 0) {
5288 		lgeom_p->g_ncyl = 0;
5289 	} else {
5290 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5291 	}
5292 	lgeom_p->g_acyl = 0;
5293 
5294 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5295 	return (0);
5296 
5297 }
5298 /*
5299  *    Function: sd_update_block_info
5300  *
5301  * Description: Calculate a byte count to sector count bitshift value
5302  *		from sector size.
5303  *
5304  *   Arguments: un: unit struct.
5305  *		lbasize: new target sector size
5306  *		capacity: new target capacity, ie. block count
5307  *
5308  *     Context: Kernel thread context
5309  */
5310 
5311 static void
5312 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5313 {
5314 	if (lbasize != 0) {
5315 		un->un_tgt_blocksize = lbasize;
5316 		un->un_f_tgt_blocksize_is_valid = TRUE;
5317 		if (!un->un_f_has_removable_media) {
5318 			un->un_sys_blocksize = lbasize;
5319 		}
5320 	}
5321 
5322 	if (capacity != 0) {
5323 		un->un_blockcount		= capacity;
5324 		un->un_f_blockcount_is_valid	= TRUE;
5325 
5326 		/*
5327 		 * The capacity has changed so update the errstats.
5328 		 */
5329 		if (un->un_errstats != NULL) {
5330 			struct sd_errstats *stp;
5331 
5332 			capacity *= un->un_sys_blocksize;
5333 			stp = (struct sd_errstats *)un->un_errstats->ks_data;
5334 			if (stp->sd_capacity.value.ui64 < capacity)
5335 				stp->sd_capacity.value.ui64 = capacity;
5336 		}
5337 	}
5338 }
5339 
5340 
5341 /*
5342  *    Function: sd_register_devid
5343  *
5344  * Description: This routine will obtain the device id information from the
5345  *		target, obtain the serial number, and register the device
5346  *		id with the ddi framework.
5347  *
5348  *   Arguments: devi - the system's dev_info_t for the device.
5349  *		un - driver soft state (unit) structure
5350  *		reservation_flag - indicates if a reservation conflict
5351  *		occurred during attach
5352  *
5353  *     Context: Kernel Thread
5354  */
5355 static void
5356 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5357 {
5358 	int		rval		= 0;
5359 	uchar_t		*inq80		= NULL;
5360 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
5361 	size_t		inq80_resid	= 0;
5362 	uchar_t		*inq83		= NULL;
5363 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
5364 	size_t		inq83_resid	= 0;
5365 	int		dlen, len;
5366 	char		*sn;
5367 	struct sd_lun	*un;
5368 
5369 	ASSERT(ssc != NULL);
5370 	un = ssc->ssc_un;
5371 	ASSERT(un != NULL);
5372 	ASSERT(mutex_owned(SD_MUTEX(un)));
5373 	ASSERT((SD_DEVINFO(un)) == devi);
5374 
5375 
5376 	/*
5377 	 * We check the availability of the World Wide Name (0x83) and Unit
5378 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5379 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5380 	 * 0x83 is available, that is the best choice.  Our next choice is
5381 	 * 0x80.  If neither are available, we munge the devid from the device
5382 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5383 	 * to fabricate a devid for non-Sun qualified disks.
5384 	 */
5385 	if (sd_check_vpd_page_support(ssc) == 0) {
5386 		/* collect page 80 data if available */
5387 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5388 
5389 			mutex_exit(SD_MUTEX(un));
5390 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5391 
5392 			rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5393 			    0x01, 0x80, &inq80_resid);
5394 
5395 			if (rval != 0) {
5396 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5397 				kmem_free(inq80, inq80_len);
5398 				inq80 = NULL;
5399 				inq80_len = 0;
5400 			} else if (ddi_prop_exists(
5401 			    DDI_DEV_T_NONE, SD_DEVINFO(un),
5402 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5403 			    INQUIRY_SERIAL_NO) == 0) {
5404 				/*
5405 				 * If we don't already have a serial number
5406 				 * property, do quick verify of data returned
5407 				 * and define property.
5408 				 */
5409 				dlen = inq80_len - inq80_resid;
5410 				len = (size_t)inq80[3];
5411 				if ((dlen >= 4) && ((len + 4) <= dlen)) {
5412 					/*
5413 					 * Ensure sn termination, skip leading
5414 					 * blanks, and create property
5415 					 * 'inquiry-serial-no'.
5416 					 */
5417 					sn = (char *)&inq80[4];
5418 					sn[len] = 0;
5419 					while (*sn && (*sn == ' '))
5420 						sn++;
5421 					if (*sn) {
5422 						(void) ddi_prop_update_string(
5423 						    DDI_DEV_T_NONE,
5424 						    SD_DEVINFO(un),
5425 						    INQUIRY_SERIAL_NO, sn);
5426 					}
5427 				}
5428 			}
5429 			mutex_enter(SD_MUTEX(un));
5430 		}
5431 
5432 		/* collect page 83 data if available */
5433 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5434 			mutex_exit(SD_MUTEX(un));
5435 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5436 
5437 			rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5438 			    0x01, 0x83, &inq83_resid);
5439 
5440 			if (rval != 0) {
5441 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5442 				kmem_free(inq83, inq83_len);
5443 				inq83 = NULL;
5444 				inq83_len = 0;
5445 			}
5446 			mutex_enter(SD_MUTEX(un));
5447 		}
5448 	}
5449 
5450 	/*
5451 	 * If transport has already registered a devid for this target
5452 	 * then that takes precedence over the driver's determination
5453 	 * of the devid.
5454 	 *
5455 	 * NOTE: The reason this check is done here instead of at the beginning
5456 	 * of the function is to allow the code above to create the
5457 	 * 'inquiry-serial-no' property.
5458 	 */
5459 	if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5460 		ASSERT(un->un_devid);
5461 		un->un_f_devid_transport_defined = TRUE;
5462 		goto cleanup; /* use devid registered by the transport */
5463 	}
5464 
5465 	/*
5466 	 * This is the case of antiquated Sun disk drives that have the
5467 	 * FAB_DEVID property set in the disk_table.  These drives
5468 	 * manage the devid's by storing them in last 2 available sectors
5469 	 * on the drive and have them fabricated by the ddi layer by calling
5470 	 * ddi_devid_init and passing the DEVID_FAB flag.
5471 	 */
5472 	if (un->un_f_opt_fab_devid == TRUE) {
5473 		/*
5474 		 * Depending on EINVAL isn't reliable, since a reserved disk
5475 		 * may result in invalid geometry, so check to make sure a
5476 		 * reservation conflict did not occur during attach.
5477 		 */
5478 		if ((sd_get_devid(ssc) == EINVAL) &&
5479 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
5480 			/*
5481 			 * The devid is invalid AND there is no reservation
5482 			 * conflict.  Fabricate a new devid.
5483 			 */
5484 			(void) sd_create_devid(ssc);
5485 		}
5486 
5487 		/* Register the devid if it exists */
5488 		if (un->un_devid != NULL) {
5489 			(void) ddi_devid_register(SD_DEVINFO(un),
5490 			    un->un_devid);
5491 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5492 			    "sd_register_devid: Devid Fabricated\n");
5493 		}
5494 		goto cleanup;
5495 	}
5496 
5497 	/* encode best devid possible based on data available */
5498 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5499 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
5500 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5501 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
5502 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5503 
5504 		/* devid successfully encoded, register devid */
5505 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5506 
5507 	} else {
5508 		/*
5509 		 * Unable to encode a devid based on data available.
5510 		 * This is not a Sun qualified disk.  Older Sun disk
5511 		 * drives that have the SD_FAB_DEVID property
5512 		 * set in the disk_table and non Sun qualified
5513 		 * disks are treated in the same manner.  These
5514 		 * drives manage the devid's by storing them in
5515 		 * last 2 available sectors on the drive and
5516 		 * have them fabricated by the ddi layer by
5517 		 * calling ddi_devid_init and passing the
5518 		 * DEVID_FAB flag.
5519 		 * Create a fabricate devid only if there's no
5520 		 * fabricate devid existed.
5521 		 */
5522 		if (sd_get_devid(ssc) == EINVAL) {
5523 			(void) sd_create_devid(ssc);
5524 		}
5525 		un->un_f_opt_fab_devid = TRUE;
5526 
5527 		/* Register the devid if it exists */
5528 		if (un->un_devid != NULL) {
5529 			(void) ddi_devid_register(SD_DEVINFO(un),
5530 			    un->un_devid);
5531 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5532 			    "sd_register_devid: devid fabricated using "
5533 			    "ddi framework\n");
5534 		}
5535 	}
5536 
5537 cleanup:
5538 	/* clean up resources */
5539 	if (inq80 != NULL) {
5540 		kmem_free(inq80, inq80_len);
5541 	}
5542 	if (inq83 != NULL) {
5543 		kmem_free(inq83, inq83_len);
5544 	}
5545 }
5546 
5547 
5548 
5549 /*
5550  *    Function: sd_get_devid
5551  *
5552  * Description: This routine will return 0 if a valid device id has been
5553  *		obtained from the target and stored in the soft state. If a
5554  *		valid device id has not been previously read and stored, a
5555  *		read attempt will be made.
5556  *
5557  *   Arguments: un - driver soft state (unit) structure
5558  *
5559  * Return Code: 0 if we successfully get the device id
5560  *
5561  *     Context: Kernel Thread
5562  */
5563 
5564 static int
5565 sd_get_devid(sd_ssc_t *ssc)
5566 {
5567 	struct dk_devid		*dkdevid;
5568 	ddi_devid_t		tmpid;
5569 	uint_t			*ip;
5570 	size_t			sz;
5571 	diskaddr_t		blk;
5572 	int			status;
5573 	int			chksum;
5574 	int			i;
5575 	size_t			buffer_size;
5576 	struct sd_lun		*un;
5577 
5578 	ASSERT(ssc != NULL);
5579 	un = ssc->ssc_un;
5580 	ASSERT(un != NULL);
5581 	ASSERT(mutex_owned(SD_MUTEX(un)));
5582 
5583 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5584 	    un);
5585 
5586 	if (un->un_devid != NULL) {
5587 		return (0);
5588 	}
5589 
5590 	mutex_exit(SD_MUTEX(un));
5591 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5592 	    (void *)SD_PATH_DIRECT) != 0) {
5593 		mutex_enter(SD_MUTEX(un));
5594 		return (EINVAL);
5595 	}
5596 
5597 	/*
5598 	 * Read and verify device id, stored in the reserved cylinders at the
5599 	 * end of the disk. Backup label is on the odd sectors of the last
5600 	 * track of the last cylinder. Device id will be on track of the next
5601 	 * to last cylinder.
5602 	 */
5603 	mutex_enter(SD_MUTEX(un));
5604 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5605 	mutex_exit(SD_MUTEX(un));
5606 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5607 	status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5608 	    SD_PATH_DIRECT);
5609 
5610 	if (status != 0) {
5611 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5612 		goto error;
5613 	}
5614 
5615 	/* Validate the revision */
5616 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5617 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5618 		status = EINVAL;
5619 		goto error;
5620 	}
5621 
5622 	/* Calculate the checksum */
5623 	chksum = 0;
5624 	ip = (uint_t *)dkdevid;
5625 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5626 	    i++) {
5627 		chksum ^= ip[i];
5628 	}
5629 
5630 	/* Compare the checksums */
5631 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
5632 		status = EINVAL;
5633 		goto error;
5634 	}
5635 
5636 	/* Validate the device id */
5637 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5638 		status = EINVAL;
5639 		goto error;
5640 	}
5641 
5642 	/*
5643 	 * Store the device id in the driver soft state
5644 	 */
5645 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5646 	tmpid = kmem_alloc(sz, KM_SLEEP);
5647 
5648 	mutex_enter(SD_MUTEX(un));
5649 
5650 	un->un_devid = tmpid;
5651 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5652 
5653 	kmem_free(dkdevid, buffer_size);
5654 
5655 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5656 
5657 	return (status);
5658 error:
5659 	mutex_enter(SD_MUTEX(un));
5660 	kmem_free(dkdevid, buffer_size);
5661 	return (status);
5662 }
5663 
5664 
5665 /*
5666  *    Function: sd_create_devid
5667  *
5668  * Description: This routine will fabricate the device id and write it
5669  *		to the disk.
5670  *
5671  *   Arguments: un - driver soft state (unit) structure
5672  *
5673  * Return Code: value of the fabricated device id
5674  *
5675  *     Context: Kernel Thread
5676  */
5677 
5678 static ddi_devid_t
5679 sd_create_devid(sd_ssc_t *ssc)
5680 {
5681 	struct sd_lun	*un;
5682 
5683 	ASSERT(ssc != NULL);
5684 	un = ssc->ssc_un;
5685 	ASSERT(un != NULL);
5686 
5687 	/* Fabricate the devid */
5688 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5689 	    == DDI_FAILURE) {
5690 		return (NULL);
5691 	}
5692 
5693 	/* Write the devid to disk */
5694 	if (sd_write_deviceid(ssc) != 0) {
5695 		ddi_devid_free(un->un_devid);
5696 		un->un_devid = NULL;
5697 	}
5698 
5699 	return (un->un_devid);
5700 }
5701 
5702 
5703 /*
5704  *    Function: sd_write_deviceid
5705  *
5706  * Description: This routine will write the device id to the disk
5707  *		reserved sector.
5708  *
5709  *   Arguments: un - driver soft state (unit) structure
5710  *
5711  * Return Code: EINVAL
5712  *		value returned by sd_send_scsi_cmd
5713  *
5714  *     Context: Kernel Thread
5715  */
5716 
5717 static int
5718 sd_write_deviceid(sd_ssc_t *ssc)
5719 {
5720 	struct dk_devid		*dkdevid;
5721 	uchar_t			*buf;
5722 	diskaddr_t		blk;
5723 	uint_t			*ip, chksum;
5724 	int			status;
5725 	int			i;
5726 	struct sd_lun		*un;
5727 
5728 	ASSERT(ssc != NULL);
5729 	un = ssc->ssc_un;
5730 	ASSERT(un != NULL);
5731 	ASSERT(mutex_owned(SD_MUTEX(un)));
5732 
5733 	mutex_exit(SD_MUTEX(un));
5734 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5735 	    (void *)SD_PATH_DIRECT) != 0) {
5736 		mutex_enter(SD_MUTEX(un));
5737 		return (-1);
5738 	}
5739 
5740 
5741 	/* Allocate the buffer */
5742 	buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5743 	dkdevid = (struct dk_devid *)buf;
5744 
5745 	/* Fill in the revision */
5746 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5747 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5748 
5749 	/* Copy in the device id */
5750 	mutex_enter(SD_MUTEX(un));
5751 	bcopy(un->un_devid, &dkdevid->dkd_devid,
5752 	    ddi_devid_sizeof(un->un_devid));
5753 	mutex_exit(SD_MUTEX(un));
5754 
5755 	/* Calculate the checksum */
5756 	chksum = 0;
5757 	ip = (uint_t *)dkdevid;
5758 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5759 	    i++) {
5760 		chksum ^= ip[i];
5761 	}
5762 
5763 	/* Fill-in checksum */
5764 	DKD_FORMCHKSUM(chksum, dkdevid);
5765 
5766 	/* Write the reserved sector */
5767 	status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5768 	    SD_PATH_DIRECT);
5769 	if (status != 0)
5770 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5771 
5772 	kmem_free(buf, un->un_sys_blocksize);
5773 
5774 	mutex_enter(SD_MUTEX(un));
5775 	return (status);
5776 }
5777 
5778 
5779 /*
5780  *    Function: sd_check_vpd_page_support
5781  *
5782  * Description: This routine sends an inquiry command with the EVPD bit set and
5783  *		a page code of 0x00 to the device. It is used to determine which
5784  *		vital product pages are available to find the devid. We are
5785  *		looking for pages 0x83 0x80 or 0xB1.  If we return a negative 1,
5786  *		the device does not support that command.
5787  *
5788  *   Arguments: un  - driver soft state (unit) structure
5789  *
5790  * Return Code: 0 - success
5791  *		1 - check condition
5792  *
5793  *     Context: This routine can sleep.
5794  */
5795 
5796 static int
5797 sd_check_vpd_page_support(sd_ssc_t *ssc)
5798 {
5799 	uchar_t	*page_list	= NULL;
5800 	uchar_t	page_length	= 0xff;	/* Use max possible length */
5801 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
5802 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
5803 	int    	rval		= 0;
5804 	int	counter;
5805 	struct sd_lun		*un;
5806 
5807 	ASSERT(ssc != NULL);
5808 	un = ssc->ssc_un;
5809 	ASSERT(un != NULL);
5810 	ASSERT(mutex_owned(SD_MUTEX(un)));
5811 
5812 	mutex_exit(SD_MUTEX(un));
5813 
5814 	/*
5815 	 * We'll set the page length to the maximum to save figuring it out
5816 	 * with an additional call.
5817 	 */
5818 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
5819 
5820 	rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5821 	    page_code, NULL);
5822 
5823 	if (rval != 0)
5824 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5825 
5826 	mutex_enter(SD_MUTEX(un));
5827 
5828 	/*
5829 	 * Now we must validate that the device accepted the command, as some
5830 	 * drives do not support it.  If the drive does support it, we will
5831 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
5832 	 * not, we return -1.
5833 	 */
5834 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5835 		/* Loop to find one of the 2 pages we need */
5836 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
5837 
5838 		/*
5839 		 * Pages are returned in ascending order, and 0x83 is what we
5840 		 * are hoping for.
5841 		 */
5842 		while ((page_list[counter] <= 0xB1) &&
5843 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
5844 		    VPD_HEAD_OFFSET))) {
5845 			/*
5846 			 * Add 3 because page_list[3] is the number of
5847 			 * pages minus 3
5848 			 */
5849 
5850 			switch (page_list[counter]) {
5851 			case 0x00:
5852 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5853 				break;
5854 			case 0x80:
5855 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5856 				break;
5857 			case 0x81:
5858 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5859 				break;
5860 			case 0x82:
5861 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5862 				break;
5863 			case 0x83:
5864 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5865 				break;
5866 			case 0x86:
5867 				un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5868 				break;
5869 			case 0xB1:
5870 				un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5871 				break;
5872 			}
5873 			counter++;
5874 		}
5875 
5876 	} else {
5877 		rval = -1;
5878 
5879 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
5880 		    "sd_check_vpd_page_support: This drive does not implement "
5881 		    "VPD pages.\n");
5882 	}
5883 
5884 	kmem_free(page_list, page_length);
5885 
5886 	return (rval);
5887 }
5888 
5889 
5890 /*
5891  *    Function: sd_setup_pm
5892  *
5893  * Description: Initialize Power Management on the device
5894  *
5895  *     Context: Kernel Thread
5896  */
5897 
5898 static void
5899 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5900 {
5901 	uint_t		log_page_size;
5902 	uchar_t		*log_page_data;
5903 	int		rval = 0;
5904 	struct sd_lun	*un;
5905 
5906 	ASSERT(ssc != NULL);
5907 	un = ssc->ssc_un;
5908 	ASSERT(un != NULL);
5909 
5910 	/*
5911 	 * Since we are called from attach, holding a mutex for
5912 	 * un is unnecessary. Because some of the routines called
5913 	 * from here require SD_MUTEX to not be held, assert this
5914 	 * right up front.
5915 	 */
5916 	ASSERT(!mutex_owned(SD_MUTEX(un)));
5917 	/*
5918 	 * Since the sd device does not have the 'reg' property,
5919 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5920 	 * The following code is to tell cpr that this device
5921 	 * DOES need to be suspended and resumed.
5922 	 */
5923 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
5924 	    "pm-hardware-state", "needs-suspend-resume");
5925 
5926 	/*
5927 	 * This complies with the new power management framework
5928 	 * for certain desktop machines. Create the pm_components
5929 	 * property as a string array property.
5930 	 * If un_f_pm_supported is TRUE, that means the disk
5931 	 * attached HBA has set the "pm-capable" property and
5932 	 * the value of this property is bigger than 0.
5933 	 */
5934 	if (un->un_f_pm_supported) {
5935 		/*
5936 		 * not all devices have a motor, try it first.
5937 		 * some devices may return ILLEGAL REQUEST, some
5938 		 * will hang
5939 		 * The following START_STOP_UNIT is used to check if target
5940 		 * device has a motor.
5941 		 */
5942 		un->un_f_start_stop_supported = TRUE;
5943 
5944 		if (un->un_f_power_condition_supported) {
5945 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
5946 			    SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5947 			    SD_PATH_DIRECT);
5948 			if (rval != 0) {
5949 				un->un_f_power_condition_supported = FALSE;
5950 			}
5951 		}
5952 		if (!un->un_f_power_condition_supported) {
5953 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
5954 			    SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5955 		}
5956 		if (rval != 0) {
5957 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5958 			un->un_f_start_stop_supported = FALSE;
5959 		}
5960 
5961 		/*
5962 		 * create pm properties anyways otherwise the parent can't
5963 		 * go to sleep
5964 		 */
5965 		un->un_f_pm_is_enabled = TRUE;
5966 		(void) sd_create_pm_components(devi, un);
5967 
5968 		/*
5969 		 * If it claims that log sense is supported, check it out.
5970 		 */
5971 		if (un->un_f_log_sense_supported) {
5972 			rval = sd_log_page_supported(ssc,
5973 			    START_STOP_CYCLE_PAGE);
5974 			if (rval == 1) {
5975 				/* Page found, use it. */
5976 				un->un_start_stop_cycle_page =
5977 				    START_STOP_CYCLE_PAGE;
5978 			} else {
5979 				/*
5980 				 * Page not found or log sense is not
5981 				 * supported.
5982 				 * Notice we do not check the old style
5983 				 * START_STOP_CYCLE_VU_PAGE because this
5984 				 * code path does not apply to old disks.
5985 				 */
5986 				un->un_f_log_sense_supported = FALSE;
5987 				un->un_f_pm_log_sense_smart = FALSE;
5988 			}
5989 		}
5990 
5991 		return;
5992 	}
5993 
5994 	/*
5995 	 * For the disk whose attached HBA has not set the "pm-capable"
5996 	 * property, check if it supports the power management.
5997 	 */
5998 	if (!un->un_f_log_sense_supported) {
5999 		un->un_power_level = SD_SPINDLE_ON;
6000 		un->un_f_pm_is_enabled = FALSE;
6001 		return;
6002 	}
6003 
6004 	rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
6005 
6006 #ifdef	SDDEBUG
6007 	if (sd_force_pm_supported) {
6008 		/* Force a successful result */
6009 		rval = 1;
6010 	}
6011 #endif
6012 
6013 	/*
6014 	 * If the start-stop cycle counter log page is not supported
6015 	 * or if the pm-capable property is set to be false (0),
6016 	 * then we should not create the pm_components property.
6017 	 */
6018 	if (rval == -1) {
6019 		/*
6020 		 * Error.
6021 		 * Reading log sense failed, most likely this is
6022 		 * an older drive that does not support log sense.
6023 		 * If this fails auto-pm is not supported.
6024 		 */
6025 		un->un_power_level = SD_SPINDLE_ON;
6026 		un->un_f_pm_is_enabled = FALSE;
6027 
6028 	} else if (rval == 0) {
6029 		/*
6030 		 * Page not found.
6031 		 * The start stop cycle counter is implemented as page
6032 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6033 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6034 		 */
6035 		if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
6036 			/*
6037 			 * Page found, use this one.
6038 			 */
6039 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6040 			un->un_f_pm_is_enabled = TRUE;
6041 		} else {
6042 			/*
6043 			 * Error or page not found.
6044 			 * auto-pm is not supported for this device.
6045 			 */
6046 			un->un_power_level = SD_SPINDLE_ON;
6047 			un->un_f_pm_is_enabled = FALSE;
6048 		}
6049 	} else {
6050 		/*
6051 		 * Page found, use it.
6052 		 */
6053 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6054 		un->un_f_pm_is_enabled = TRUE;
6055 	}
6056 
6057 
6058 	if (un->un_f_pm_is_enabled == TRUE) {
6059 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6060 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6061 
6062 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6063 		    log_page_size, un->un_start_stop_cycle_page,
6064 		    0x01, 0, SD_PATH_DIRECT);
6065 
6066 		if (rval != 0) {
6067 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6068 		}
6069 
6070 #ifdef	SDDEBUG
6071 		if (sd_force_pm_supported) {
6072 			/* Force a successful result */
6073 			rval = 0;
6074 		}
6075 #endif
6076 
6077 		/*
6078 		 * If the Log sense for Page( Start/stop cycle counter page)
6079 		 * succeeds, then power management is supported and we can
6080 		 * enable auto-pm.
6081 		 */
6082 		if (rval == 0)  {
6083 			(void) sd_create_pm_components(devi, un);
6084 		} else {
6085 			un->un_power_level = SD_SPINDLE_ON;
6086 			un->un_f_pm_is_enabled = FALSE;
6087 		}
6088 
6089 		kmem_free(log_page_data, log_page_size);
6090 	}
6091 }
6092 
6093 
6094 /*
6095  *    Function: sd_create_pm_components
6096  *
6097  * Description: Initialize PM property.
6098  *
6099  *     Context: Kernel thread context
6100  */
6101 
6102 static void
6103 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6104 {
6105 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6106 
6107 	if (un->un_f_power_condition_supported) {
6108 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6109 		    "pm-components", sd_pwr_pc.pm_comp, 5)
6110 		    != DDI_PROP_SUCCESS) {
6111 			un->un_power_level = SD_SPINDLE_ACTIVE;
6112 			un->un_f_pm_is_enabled = FALSE;
6113 			return;
6114 		}
6115 	} else {
6116 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6117 		    "pm-components", sd_pwr_ss.pm_comp, 3)
6118 		    != DDI_PROP_SUCCESS) {
6119 			un->un_power_level = SD_SPINDLE_ON;
6120 			un->un_f_pm_is_enabled = FALSE;
6121 			return;
6122 		}
6123 	}
6124 	/*
6125 	 * When components are initially created they are idle,
6126 	 * power up any non-removables.
6127 	 * Note: the return value of pm_raise_power can't be used
6128 	 * for determining if PM should be enabled for this device.
6129 	 * Even if you check the return values and remove this
6130 	 * property created above, the PM framework will not honor the
6131 	 * change after the first call to pm_raise_power. Hence,
6132 	 * removal of that property does not help if pm_raise_power
6133 	 * fails. In the case of removable media, the start/stop
6134 	 * will fail if the media is not present.
6135 	 */
6136 	if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6137 	    SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6138 		mutex_enter(SD_MUTEX(un));
6139 		un->un_power_level = SD_PM_STATE_ACTIVE(un);
6140 		mutex_enter(&un->un_pm_mutex);
6141 		/* Set to on and not busy. */
6142 		un->un_pm_count = 0;
6143 	} else {
6144 		mutex_enter(SD_MUTEX(un));
6145 		un->un_power_level = SD_PM_STATE_STOPPED(un);
6146 		mutex_enter(&un->un_pm_mutex);
6147 		/* Set to off. */
6148 		un->un_pm_count = -1;
6149 	}
6150 	mutex_exit(&un->un_pm_mutex);
6151 	mutex_exit(SD_MUTEX(un));
6152 }
6153 
6154 
6155 /*
6156  *    Function: sd_ddi_suspend
6157  *
6158  * Description: Performs system power-down operations. This includes
6159  *		setting the drive state to indicate its suspended so
6160  *		that no new commands will be accepted. Also, wait for
6161  *		all commands that are in transport or queued to a timer
6162  *		for retry to complete. All timeout threads are cancelled.
6163  *
6164  * Return Code: DDI_FAILURE or DDI_SUCCESS
6165  *
6166  *     Context: Kernel thread context
6167  */
6168 
6169 static int
6170 sd_ddi_suspend(dev_info_t *devi)
6171 {
6172 	struct	sd_lun	*un;
6173 	clock_t		wait_cmds_complete;
6174 
6175 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6176 	if (un == NULL) {
6177 		return (DDI_FAILURE);
6178 	}
6179 
6180 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6181 
6182 	mutex_enter(SD_MUTEX(un));
6183 
6184 	/* Return success if the device is already suspended. */
6185 	if (un->un_state == SD_STATE_SUSPENDED) {
6186 		mutex_exit(SD_MUTEX(un));
6187 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6188 		    "device already suspended, exiting\n");
6189 		return (DDI_SUCCESS);
6190 	}
6191 
6192 	/* Return failure if the device is being used by HA */
6193 	if (un->un_resvd_status &
6194 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6195 		mutex_exit(SD_MUTEX(un));
6196 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6197 		    "device in use by HA, exiting\n");
6198 		return (DDI_FAILURE);
6199 	}
6200 
6201 	/*
6202 	 * Return failure if the device is in a resource wait
6203 	 * or power changing state.
6204 	 */
6205 	if ((un->un_state == SD_STATE_RWAIT) ||
6206 	    (un->un_state == SD_STATE_PM_CHANGING)) {
6207 		mutex_exit(SD_MUTEX(un));
6208 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6209 		    "device in resource wait state, exiting\n");
6210 		return (DDI_FAILURE);
6211 	}
6212 
6213 
6214 	un->un_save_state = un->un_last_state;
6215 	New_state(un, SD_STATE_SUSPENDED);
6216 
6217 	/*
6218 	 * Wait for all commands that are in transport or queued to a timer
6219 	 * for retry to complete.
6220 	 *
6221 	 * While waiting, no new commands will be accepted or sent because of
6222 	 * the new state we set above.
6223 	 *
6224 	 * Wait till current operation has completed. If we are in the resource
6225 	 * wait state (with an intr outstanding) then we need to wait till the
6226 	 * intr completes and starts the next cmd. We want to wait for
6227 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6228 	 */
6229 	wait_cmds_complete = ddi_get_lbolt() +
6230 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
6231 
6232 	while (un->un_ncmds_in_transport != 0) {
6233 		/*
6234 		 * Fail if commands do not finish in the specified time.
6235 		 */
6236 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6237 		    wait_cmds_complete) == -1) {
6238 			/*
6239 			 * Undo the state changes made above. Everything
6240 			 * must go back to it's original value.
6241 			 */
6242 			Restore_state(un);
6243 			un->un_last_state = un->un_save_state;
6244 			/* Wake up any threads that might be waiting. */
6245 			cv_broadcast(&un->un_suspend_cv);
6246 			mutex_exit(SD_MUTEX(un));
6247 			SD_ERROR(SD_LOG_IO_PM, un,
6248 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
6249 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6250 			return (DDI_FAILURE);
6251 		}
6252 	}
6253 
6254 	/*
6255 	 * Cancel SCSI watch thread and timeouts, if any are active
6256 	 */
6257 
6258 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6259 		opaque_t temp_token = un->un_swr_token;
6260 		mutex_exit(SD_MUTEX(un));
6261 		scsi_watch_suspend(temp_token);
6262 		mutex_enter(SD_MUTEX(un));
6263 	}
6264 
6265 	if (un->un_reset_throttle_timeid != NULL) {
6266 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
6267 		un->un_reset_throttle_timeid = NULL;
6268 		mutex_exit(SD_MUTEX(un));
6269 		(void) untimeout(temp_id);
6270 		mutex_enter(SD_MUTEX(un));
6271 	}
6272 
6273 	if (un->un_dcvb_timeid != NULL) {
6274 		timeout_id_t temp_id = un->un_dcvb_timeid;
6275 		un->un_dcvb_timeid = NULL;
6276 		mutex_exit(SD_MUTEX(un));
6277 		(void) untimeout(temp_id);
6278 		mutex_enter(SD_MUTEX(un));
6279 	}
6280 
6281 	mutex_enter(&un->un_pm_mutex);
6282 	if (un->un_pm_timeid != NULL) {
6283 		timeout_id_t temp_id = un->un_pm_timeid;
6284 		un->un_pm_timeid = NULL;
6285 		mutex_exit(&un->un_pm_mutex);
6286 		mutex_exit(SD_MUTEX(un));
6287 		(void) untimeout(temp_id);
6288 		mutex_enter(SD_MUTEX(un));
6289 	} else {
6290 		mutex_exit(&un->un_pm_mutex);
6291 	}
6292 
6293 	if (un->un_rmw_msg_timeid != NULL) {
6294 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
6295 		un->un_rmw_msg_timeid = NULL;
6296 		mutex_exit(SD_MUTEX(un));
6297 		(void) untimeout(temp_id);
6298 		mutex_enter(SD_MUTEX(un));
6299 	}
6300 
6301 	if (un->un_retry_timeid != NULL) {
6302 		timeout_id_t temp_id = un->un_retry_timeid;
6303 		un->un_retry_timeid = NULL;
6304 		mutex_exit(SD_MUTEX(un));
6305 		(void) untimeout(temp_id);
6306 		mutex_enter(SD_MUTEX(un));
6307 
6308 		if (un->un_retry_bp != NULL) {
6309 			un->un_retry_bp->av_forw = un->un_waitq_headp;
6310 			un->un_waitq_headp = un->un_retry_bp;
6311 			if (un->un_waitq_tailp == NULL) {
6312 				un->un_waitq_tailp = un->un_retry_bp;
6313 			}
6314 			un->un_retry_bp = NULL;
6315 			un->un_retry_statp = NULL;
6316 		}
6317 	}
6318 
6319 	if (un->un_direct_priority_timeid != NULL) {
6320 		timeout_id_t temp_id = un->un_direct_priority_timeid;
6321 		un->un_direct_priority_timeid = NULL;
6322 		mutex_exit(SD_MUTEX(un));
6323 		(void) untimeout(temp_id);
6324 		mutex_enter(SD_MUTEX(un));
6325 	}
6326 
6327 	if (un->un_f_is_fibre == TRUE) {
6328 		/*
6329 		 * Remove callbacks for insert and remove events
6330 		 */
6331 		if (un->un_insert_event != NULL) {
6332 			mutex_exit(SD_MUTEX(un));
6333 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
6334 			mutex_enter(SD_MUTEX(un));
6335 			un->un_insert_event = NULL;
6336 		}
6337 
6338 		if (un->un_remove_event != NULL) {
6339 			mutex_exit(SD_MUTEX(un));
6340 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
6341 			mutex_enter(SD_MUTEX(un));
6342 			un->un_remove_event = NULL;
6343 		}
6344 	}
6345 
6346 	mutex_exit(SD_MUTEX(un));
6347 
6348 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6349 
6350 	return (DDI_SUCCESS);
6351 }
6352 
6353 
6354 /*
6355  *    Function: sd_ddi_resume
6356  *
6357  * Description: Performs system power-up operations..
6358  *
6359  * Return Code: DDI_SUCCESS
6360  *		DDI_FAILURE
6361  *
6362  *     Context: Kernel thread context
6363  */
6364 
6365 static int
6366 sd_ddi_resume(dev_info_t *devi)
6367 {
6368 	struct	sd_lun	*un;
6369 
6370 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6371 	if (un == NULL) {
6372 		return (DDI_FAILURE);
6373 	}
6374 
6375 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6376 
6377 	mutex_enter(SD_MUTEX(un));
6378 	Restore_state(un);
6379 
6380 	/*
6381 	 * Restore the state which was saved to give the
6382 	 * the right state in un_last_state
6383 	 */
6384 	un->un_last_state = un->un_save_state;
6385 	/*
6386 	 * Note: throttle comes back at full.
6387 	 * Also note: this MUST be done before calling pm_raise_power
6388 	 * otherwise the system can get hung in biowait. The scenario where
6389 	 * this'll happen is under cpr suspend. Writing of the system
6390 	 * state goes through sddump, which writes 0 to un_throttle. If
6391 	 * writing the system state then fails, example if the partition is
6392 	 * too small, then cpr attempts a resume. If throttle isn't restored
6393 	 * from the saved value until after calling pm_raise_power then
6394 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6395 	 * in biowait.
6396 	 */
6397 	un->un_throttle = un->un_saved_throttle;
6398 
6399 	/*
6400 	 * The chance of failure is very rare as the only command done in power
6401 	 * entry point is START command when you transition from 0->1 or
6402 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6403 	 * which suspend was done. Ignore the return value as the resume should
6404 	 * not be failed. In the case of removable media the media need not be
6405 	 * inserted and hence there is a chance that raise power will fail with
6406 	 * media not present.
6407 	 */
6408 	if (un->un_f_attach_spinup) {
6409 		mutex_exit(SD_MUTEX(un));
6410 		(void) pm_raise_power(SD_DEVINFO(un), 0,
6411 		    SD_PM_STATE_ACTIVE(un));
6412 		mutex_enter(SD_MUTEX(un));
6413 	}
6414 
6415 	/*
6416 	 * Don't broadcast to the suspend cv and therefore possibly
6417 	 * start I/O until after power has been restored.
6418 	 */
6419 	cv_broadcast(&un->un_suspend_cv);
6420 	cv_broadcast(&un->un_state_cv);
6421 
6422 	/* restart thread */
6423 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6424 		scsi_watch_resume(un->un_swr_token);
6425 	}
6426 
6427 #if (defined(__fibre))
6428 	if (un->un_f_is_fibre == TRUE) {
6429 		/*
6430 		 * Add callbacks for insert and remove events
6431 		 */
6432 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6433 			sd_init_event_callbacks(un);
6434 		}
6435 	}
6436 #endif
6437 
6438 	/*
6439 	 * Transport any pending commands to the target.
6440 	 *
6441 	 * If this is a low-activity device commands in queue will have to wait
6442 	 * until new commands come in, which may take awhile. Also, we
6443 	 * specifically don't check un_ncmds_in_transport because we know that
6444 	 * there really are no commands in progress after the unit was
6445 	 * suspended and we could have reached the throttle level, been
6446 	 * suspended, and have no new commands coming in for awhile. Highly
6447 	 * unlikely, but so is the low-activity disk scenario.
6448 	 */
6449 	ddi_xbuf_dispatch(un->un_xbuf_attr);
6450 
6451 	sd_start_cmds(un, NULL);
6452 	mutex_exit(SD_MUTEX(un));
6453 
6454 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6455 
6456 	return (DDI_SUCCESS);
6457 }
6458 
6459 
6460 /*
6461  *    Function: sd_pm_state_change
6462  *
6463  * Description: Change the driver power state.
6464  * 		Someone else is required to actually change the driver
6465  * 		power level.
6466  *
6467  *   Arguments: un - driver soft state (unit) structure
6468  *              level - the power level that is changed to
6469  *              flag - to decide how to change the power state
6470  *
6471  * Return Code: DDI_SUCCESS
6472  *
6473  *     Context: Kernel thread context
6474  */
6475 static int
6476 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6477 {
6478 	ASSERT(un != NULL);
6479 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6480 
6481 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6482 	mutex_enter(SD_MUTEX(un));
6483 
6484 	if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6485 		un->un_power_level = level;
6486 		ASSERT(!mutex_owned(&un->un_pm_mutex));
6487 		mutex_enter(&un->un_pm_mutex);
6488 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6489 			un->un_pm_count++;
6490 			ASSERT(un->un_pm_count == 0);
6491 		}
6492 		mutex_exit(&un->un_pm_mutex);
6493 	} else {
6494 		/*
6495 		 * Exit if power management is not enabled for this device,
6496 		 * or if the device is being used by HA.
6497 		 */
6498 		if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6499 		    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6500 			mutex_exit(SD_MUTEX(un));
6501 			SD_TRACE(SD_LOG_POWER, un,
6502 			    "sd_pm_state_change: exiting\n");
6503 			return (DDI_FAILURE);
6504 		}
6505 
6506 		SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6507 		    "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6508 
6509 		/*
6510 		 * See if the device is not busy, ie.:
6511 		 *    - we have no commands in the driver for this device
6512 		 *    - not waiting for resources
6513 		 */
6514 		if ((un->un_ncmds_in_driver == 0) &&
6515 		    (un->un_state != SD_STATE_RWAIT)) {
6516 			/*
6517 			 * The device is not busy, so it is OK to go to low
6518 			 * power state. Indicate low power, but rely on someone
6519 			 * else to actually change it.
6520 			 */
6521 			mutex_enter(&un->un_pm_mutex);
6522 			un->un_pm_count = -1;
6523 			mutex_exit(&un->un_pm_mutex);
6524 			un->un_power_level = level;
6525 		}
6526 	}
6527 
6528 	mutex_exit(SD_MUTEX(un));
6529 
6530 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6531 
6532 	return (DDI_SUCCESS);
6533 }
6534 
6535 
6536 /*
6537  *    Function: sd_pm_idletimeout_handler
6538  *
6539  * Description: A timer routine that's active only while a device is busy.
6540  *		The purpose is to extend slightly the pm framework's busy
6541  *		view of the device to prevent busy/idle thrashing for
6542  *		back-to-back commands. Do this by comparing the current time
6543  *		to the time at which the last command completed and when the
6544  *		difference is greater than sd_pm_idletime, call
6545  *		pm_idle_component. In addition to indicating idle to the pm
6546  *		framework, update the chain type to again use the internal pm
6547  *		layers of the driver.
6548  *
6549  *   Arguments: arg - driver soft state (unit) structure
6550  *
6551  *     Context: Executes in a timeout(9F) thread context
6552  */
6553 
6554 static void
6555 sd_pm_idletimeout_handler(void *arg)
6556 {
6557 	const hrtime_t idletime = sd_pm_idletime * NANOSEC;
6558 	struct sd_lun *un = arg;
6559 
6560 	mutex_enter(&sd_detach_mutex);
6561 	if (un->un_detach_count != 0) {
6562 		/* Abort if the instance is detaching */
6563 		mutex_exit(&sd_detach_mutex);
6564 		return;
6565 	}
6566 	mutex_exit(&sd_detach_mutex);
6567 
6568 	/*
6569 	 * Grab both mutexes, in the proper order, since we're accessing
6570 	 * both PM and softstate variables.
6571 	 */
6572 	mutex_enter(SD_MUTEX(un));
6573 	mutex_enter(&un->un_pm_mutex);
6574 	if (((gethrtime() - un->un_pm_idle_time) > idletime) &&
6575 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6576 		/*
6577 		 * Update the chain types.
6578 		 * This takes affect on the next new command received.
6579 		 */
6580 		if (un->un_f_non_devbsize_supported) {
6581 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6582 		} else {
6583 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6584 		}
6585 		un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6586 
6587 		SD_TRACE(SD_LOG_IO_PM, un,
6588 		    "sd_pm_idletimeout_handler: idling device\n");
6589 		(void) pm_idle_component(SD_DEVINFO(un), 0);
6590 		un->un_pm_idle_timeid = NULL;
6591 	} else {
6592 		un->un_pm_idle_timeid =
6593 		    timeout(sd_pm_idletimeout_handler, un,
6594 		    (drv_usectohz((clock_t)300000))); /* 300 ms. */
6595 	}
6596 	mutex_exit(&un->un_pm_mutex);
6597 	mutex_exit(SD_MUTEX(un));
6598 }
6599 
6600 
6601 /*
6602  *    Function: sd_pm_timeout_handler
6603  *
6604  * Description: Callback to tell framework we are idle.
6605  *
6606  *     Context: timeout(9f) thread context.
6607  */
6608 
6609 static void
6610 sd_pm_timeout_handler(void *arg)
6611 {
6612 	struct sd_lun *un = arg;
6613 
6614 	(void) pm_idle_component(SD_DEVINFO(un), 0);
6615 	mutex_enter(&un->un_pm_mutex);
6616 	un->un_pm_timeid = NULL;
6617 	mutex_exit(&un->un_pm_mutex);
6618 }
6619 
6620 
6621 /*
6622  *    Function: sdpower
6623  *
6624  * Description: PM entry point.
6625  *
6626  * Return Code: DDI_SUCCESS
6627  *		DDI_FAILURE
6628  *
6629  *     Context: Kernel thread context
6630  */
6631 
6632 static int
6633 sdpower(dev_info_t *devi, int component, int level)
6634 {
6635 	struct sd_lun	*un;
6636 	int		instance;
6637 	int		rval = DDI_SUCCESS;
6638 	uint_t		i, log_page_size, maxcycles, ncycles;
6639 	uchar_t		*log_page_data;
6640 	int		log_sense_page;
6641 	int		medium_present;
6642 	time_t		intvlp;
6643 	struct pm_trans_data	sd_pm_tran_data;
6644 	uchar_t		save_state;
6645 	int		sval;
6646 	uchar_t		state_before_pm;
6647 	int		got_semaphore_here;
6648 	sd_ssc_t	*ssc;
6649 	int	last_power_level;
6650 
6651 	instance = ddi_get_instance(devi);
6652 
6653 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6654 	    !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6655 		return (DDI_FAILURE);
6656 	}
6657 
6658 	ssc = sd_ssc_init(un);
6659 
6660 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6661 
6662 	/*
6663 	 * Must synchronize power down with close.
6664 	 * Attempt to decrement/acquire the open/close semaphore,
6665 	 * but do NOT wait on it. If it's not greater than zero,
6666 	 * ie. it can't be decremented without waiting, then
6667 	 * someone else, either open or close, already has it
6668 	 * and the try returns 0. Use that knowledge here to determine
6669 	 * if it's OK to change the device power level.
6670 	 * Also, only increment it on exit if it was decremented, ie. gotten,
6671 	 * here.
6672 	 */
6673 	got_semaphore_here = sema_tryp(&un->un_semoclose);
6674 
6675 	mutex_enter(SD_MUTEX(un));
6676 
6677 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6678 	    un->un_ncmds_in_driver);
6679 
6680 	/*
6681 	 * If un_ncmds_in_driver is non-zero it indicates commands are
6682 	 * already being processed in the driver, or if the semaphore was
6683 	 * not gotten here it indicates an open or close is being processed.
6684 	 * At the same time somebody is requesting to go to a lower power
6685 	 * that can't perform I/O, which can't happen, therefore we need to
6686 	 * return failure.
6687 	 */
6688 	if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6689 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6690 		mutex_exit(SD_MUTEX(un));
6691 
6692 		if (got_semaphore_here != 0) {
6693 			sema_v(&un->un_semoclose);
6694 		}
6695 		SD_TRACE(SD_LOG_IO_PM, un,
6696 		    "sdpower: exit, device has queued cmds.\n");
6697 
6698 		goto sdpower_failed;
6699 	}
6700 
6701 	/*
6702 	 * if it is OFFLINE that means the disk is completely dead
6703 	 * in our case we have to put the disk in on or off by sending commands
6704 	 * Of course that will fail anyway so return back here.
6705 	 *
6706 	 * Power changes to a device that's OFFLINE or SUSPENDED
6707 	 * are not allowed.
6708 	 */
6709 	if ((un->un_state == SD_STATE_OFFLINE) ||
6710 	    (un->un_state == SD_STATE_SUSPENDED)) {
6711 		mutex_exit(SD_MUTEX(un));
6712 
6713 		if (got_semaphore_here != 0) {
6714 			sema_v(&un->un_semoclose);
6715 		}
6716 		SD_TRACE(SD_LOG_IO_PM, un,
6717 		    "sdpower: exit, device is off-line.\n");
6718 
6719 		goto sdpower_failed;
6720 	}
6721 
6722 	/*
6723 	 * Change the device's state to indicate it's power level
6724 	 * is being changed. Do this to prevent a power off in the
6725 	 * middle of commands, which is especially bad on devices
6726 	 * that are really powered off instead of just spun down.
6727 	 */
6728 	state_before_pm = un->un_state;
6729 	un->un_state = SD_STATE_PM_CHANGING;
6730 
6731 	mutex_exit(SD_MUTEX(un));
6732 
6733 	/*
6734 	 * If log sense command is not supported, bypass the
6735 	 * following checking, otherwise, check the log sense
6736 	 * information for this device.
6737 	 */
6738 	if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6739 	    un->un_f_log_sense_supported) {
6740 		/*
6741 		 * Get the log sense information to understand whether the
6742 		 * the powercycle counts have gone beyond the threshhold.
6743 		 */
6744 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6745 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6746 
6747 		mutex_enter(SD_MUTEX(un));
6748 		log_sense_page = un->un_start_stop_cycle_page;
6749 		mutex_exit(SD_MUTEX(un));
6750 
6751 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6752 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6753 
6754 		if (rval != 0) {
6755 			if (rval == EIO)
6756 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6757 			else
6758 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6759 		}
6760 
6761 #ifdef	SDDEBUG
6762 		if (sd_force_pm_supported) {
6763 			/* Force a successful result */
6764 			rval = 0;
6765 		}
6766 #endif
6767 		if (rval != 0) {
6768 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6769 			    "Log Sense Failed\n");
6770 
6771 			kmem_free(log_page_data, log_page_size);
6772 			/* Cannot support power management on those drives */
6773 
6774 			if (got_semaphore_here != 0) {
6775 				sema_v(&un->un_semoclose);
6776 			}
6777 			/*
6778 			 * On exit put the state back to it's original value
6779 			 * and broadcast to anyone waiting for the power
6780 			 * change completion.
6781 			 */
6782 			mutex_enter(SD_MUTEX(un));
6783 			un->un_state = state_before_pm;
6784 			cv_broadcast(&un->un_suspend_cv);
6785 			mutex_exit(SD_MUTEX(un));
6786 			SD_TRACE(SD_LOG_IO_PM, un,
6787 			    "sdpower: exit, Log Sense Failed.\n");
6788 
6789 			goto sdpower_failed;
6790 		}
6791 
6792 		/*
6793 		 * From the page data - Convert the essential information to
6794 		 * pm_trans_data
6795 		 */
6796 		maxcycles =
6797 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6798 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
6799 
6800 		ncycles =
6801 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6802 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
6803 
6804 		if (un->un_f_pm_log_sense_smart) {
6805 			sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6806 			sd_pm_tran_data.un.smart_count.consumed = ncycles;
6807 			sd_pm_tran_data.un.smart_count.flag = 0;
6808 			sd_pm_tran_data.format = DC_SMART_FORMAT;
6809 		} else {
6810 			sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6811 			sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6812 			for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6813 				sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6814 				    log_page_data[8+i];
6815 			}
6816 			sd_pm_tran_data.un.scsi_cycles.flag = 0;
6817 			sd_pm_tran_data.format = DC_SCSI_FORMAT;
6818 		}
6819 
6820 		kmem_free(log_page_data, log_page_size);
6821 
6822 		/*
6823 		 * Call pm_trans_check routine to get the Ok from
6824 		 * the global policy
6825 		 */
6826 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6827 #ifdef	SDDEBUG
6828 		if (sd_force_pm_supported) {
6829 			/* Force a successful result */
6830 			rval = 1;
6831 		}
6832 #endif
6833 		switch (rval) {
6834 		case 0:
6835 			/*
6836 			 * Not Ok to Power cycle or error in parameters passed
6837 			 * Would have given the advised time to consider power
6838 			 * cycle. Based on the new intvlp parameter we are
6839 			 * supposed to pretend we are busy so that pm framework
6840 			 * will never call our power entry point. Because of
6841 			 * that install a timeout handler and wait for the
6842 			 * recommended time to elapse so that power management
6843 			 * can be effective again.
6844 			 *
6845 			 * To effect this behavior, call pm_busy_component to
6846 			 * indicate to the framework this device is busy.
6847 			 * By not adjusting un_pm_count the rest of PM in
6848 			 * the driver will function normally, and independent
6849 			 * of this but because the framework is told the device
6850 			 * is busy it won't attempt powering down until it gets
6851 			 * a matching idle. The timeout handler sends this.
6852 			 * Note: sd_pm_entry can't be called here to do this
6853 			 * because sdpower may have been called as a result
6854 			 * of a call to pm_raise_power from within sd_pm_entry.
6855 			 *
6856 			 * If a timeout handler is already active then
6857 			 * don't install another.
6858 			 */
6859 			mutex_enter(&un->un_pm_mutex);
6860 			if (un->un_pm_timeid == NULL) {
6861 				un->un_pm_timeid =
6862 				    timeout(sd_pm_timeout_handler,
6863 				    un, intvlp * drv_usectohz(1000000));
6864 				mutex_exit(&un->un_pm_mutex);
6865 				(void) pm_busy_component(SD_DEVINFO(un), 0);
6866 			} else {
6867 				mutex_exit(&un->un_pm_mutex);
6868 			}
6869 			if (got_semaphore_here != 0) {
6870 				sema_v(&un->un_semoclose);
6871 			}
6872 			/*
6873 			 * On exit put the state back to it's original value
6874 			 * and broadcast to anyone waiting for the power
6875 			 * change completion.
6876 			 */
6877 			mutex_enter(SD_MUTEX(un));
6878 			un->un_state = state_before_pm;
6879 			cv_broadcast(&un->un_suspend_cv);
6880 			mutex_exit(SD_MUTEX(un));
6881 
6882 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6883 			    "trans check Failed, not ok to power cycle.\n");
6884 
6885 			goto sdpower_failed;
6886 		case -1:
6887 			if (got_semaphore_here != 0) {
6888 				sema_v(&un->un_semoclose);
6889 			}
6890 			/*
6891 			 * On exit put the state back to it's original value
6892 			 * and broadcast to anyone waiting for the power
6893 			 * change completion.
6894 			 */
6895 			mutex_enter(SD_MUTEX(un));
6896 			un->un_state = state_before_pm;
6897 			cv_broadcast(&un->un_suspend_cv);
6898 			mutex_exit(SD_MUTEX(un));
6899 			SD_TRACE(SD_LOG_IO_PM, un,
6900 			    "sdpower: exit, trans check command Failed.\n");
6901 
6902 			goto sdpower_failed;
6903 		}
6904 	}
6905 
6906 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6907 		/*
6908 		 * Save the last state... if the STOP FAILS we need it
6909 		 * for restoring
6910 		 */
6911 		mutex_enter(SD_MUTEX(un));
6912 		save_state = un->un_last_state;
6913 		last_power_level = un->un_power_level;
6914 		/*
6915 		 * There must not be any cmds. getting processed
6916 		 * in the driver when we get here. Power to the
6917 		 * device is potentially going off.
6918 		 */
6919 		ASSERT(un->un_ncmds_in_driver == 0);
6920 		mutex_exit(SD_MUTEX(un));
6921 
6922 		/*
6923 		 * For now PM suspend the device completely before spindle is
6924 		 * turned off
6925 		 */
6926 		if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
6927 		    == DDI_FAILURE) {
6928 			if (got_semaphore_here != 0) {
6929 				sema_v(&un->un_semoclose);
6930 			}
6931 			/*
6932 			 * On exit put the state back to it's original value
6933 			 * and broadcast to anyone waiting for the power
6934 			 * change completion.
6935 			 */
6936 			mutex_enter(SD_MUTEX(un));
6937 			un->un_state = state_before_pm;
6938 			un->un_power_level = last_power_level;
6939 			cv_broadcast(&un->un_suspend_cv);
6940 			mutex_exit(SD_MUTEX(un));
6941 			SD_TRACE(SD_LOG_IO_PM, un,
6942 			    "sdpower: exit, PM suspend Failed.\n");
6943 
6944 			goto sdpower_failed;
6945 		}
6946 	}
6947 
6948 	/*
6949 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
6950 	 * close, or strategy. Dump no long uses this routine, it uses it's
6951 	 * own code so it can be done in polled mode.
6952 	 */
6953 
6954 	medium_present = TRUE;
6955 
6956 	/*
6957 	 * When powering up, issue a TUR in case the device is at unit
6958 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
6959 	 * a deadlock on un_pm_busy_cv will occur.
6960 	 */
6961 	if (SD_PM_IS_IO_CAPABLE(un, level)) {
6962 		sval = sd_send_scsi_TEST_UNIT_READY(ssc,
6963 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
6964 		if (sval != 0)
6965 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6966 	}
6967 
6968 	if (un->un_f_power_condition_supported) {
6969 		char *pm_condition_name[] = {"STOPPED", "STANDBY",
6970 		    "IDLE", "ACTIVE"};
6971 		SD_TRACE(SD_LOG_IO_PM, un,
6972 		    "sdpower: sending \'%s\' power condition",
6973 		    pm_condition_name[level]);
6974 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
6975 		    sd_pl2pc[level], SD_PATH_DIRECT);
6976 	} else {
6977 		SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
6978 		    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
6979 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
6980 		    ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
6981 		    SD_TARGET_STOP), SD_PATH_DIRECT);
6982 	}
6983 	if (sval != 0) {
6984 		if (sval == EIO)
6985 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6986 		else
6987 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6988 	}
6989 
6990 	/* Command failed, check for media present. */
6991 	if ((sval == ENXIO) && un->un_f_has_removable_media) {
6992 		medium_present = FALSE;
6993 	}
6994 
6995 	/*
6996 	 * The conditions of interest here are:
6997 	 *   if a spindle off with media present fails,
6998 	 *	then restore the state and return an error.
6999 	 *   else if a spindle on fails,
7000 	 *	then return an error (there's no state to restore).
7001 	 * In all other cases we setup for the new state
7002 	 * and return success.
7003 	 */
7004 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
7005 		if ((medium_present == TRUE) && (sval != 0)) {
7006 			/* The stop command from above failed */
7007 			rval = DDI_FAILURE;
7008 			/*
7009 			 * The stop command failed, and we have media
7010 			 * present. Put the level back by calling the
7011 			 * sd_pm_resume() and set the state back to
7012 			 * it's previous value.
7013 			 */
7014 			(void) sd_pm_state_change(un, last_power_level,
7015 			    SD_PM_STATE_ROLLBACK);
7016 			mutex_enter(SD_MUTEX(un));
7017 			un->un_last_state = save_state;
7018 			mutex_exit(SD_MUTEX(un));
7019 		} else if (un->un_f_monitor_media_state) {
7020 			/*
7021 			 * The stop command from above succeeded.
7022 			 * Terminate watch thread in case of removable media
7023 			 * devices going into low power state. This is as per
7024 			 * the requirements of pm framework, otherwise commands
7025 			 * will be generated for the device (through watch
7026 			 * thread), even when the device is in low power state.
7027 			 */
7028 			mutex_enter(SD_MUTEX(un));
7029 			un->un_f_watcht_stopped = FALSE;
7030 			if (un->un_swr_token != NULL) {
7031 				opaque_t temp_token = un->un_swr_token;
7032 				un->un_f_watcht_stopped = TRUE;
7033 				un->un_swr_token = NULL;
7034 				mutex_exit(SD_MUTEX(un));
7035 				(void) scsi_watch_request_terminate(temp_token,
7036 				    SCSI_WATCH_TERMINATE_ALL_WAIT);
7037 			} else {
7038 				mutex_exit(SD_MUTEX(un));
7039 			}
7040 		}
7041 	} else {
7042 		/*
7043 		 * The level requested is I/O capable.
7044 		 * Legacy behavior: return success on a failed spinup
7045 		 * if there is no media in the drive.
7046 		 * Do this by looking at medium_present here.
7047 		 */
7048 		if ((sval != 0) && medium_present) {
7049 			/* The start command from above failed */
7050 			rval = DDI_FAILURE;
7051 		} else {
7052 			/*
7053 			 * The start command from above succeeded
7054 			 * PM resume the devices now that we have
7055 			 * started the disks
7056 			 */
7057 			(void) sd_pm_state_change(un, level,
7058 			    SD_PM_STATE_CHANGE);
7059 
7060 			/*
7061 			 * Resume the watch thread since it was suspended
7062 			 * when the device went into low power mode.
7063 			 */
7064 			if (un->un_f_monitor_media_state) {
7065 				mutex_enter(SD_MUTEX(un));
7066 				if (un->un_f_watcht_stopped == TRUE) {
7067 					opaque_t temp_token;
7068 
7069 					un->un_f_watcht_stopped = FALSE;
7070 					mutex_exit(SD_MUTEX(un));
7071 					temp_token =
7072 					    sd_watch_request_submit(un);
7073 					mutex_enter(SD_MUTEX(un));
7074 					un->un_swr_token = temp_token;
7075 				}
7076 				mutex_exit(SD_MUTEX(un));
7077 			}
7078 		}
7079 	}
7080 
7081 	if (got_semaphore_here != 0) {
7082 		sema_v(&un->un_semoclose);
7083 	}
7084 	/*
7085 	 * On exit put the state back to it's original value
7086 	 * and broadcast to anyone waiting for the power
7087 	 * change completion.
7088 	 */
7089 	mutex_enter(SD_MUTEX(un));
7090 	un->un_state = state_before_pm;
7091 	cv_broadcast(&un->un_suspend_cv);
7092 	mutex_exit(SD_MUTEX(un));
7093 
7094 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7095 
7096 	sd_ssc_fini(ssc);
7097 	return (rval);
7098 
7099 sdpower_failed:
7100 
7101 	sd_ssc_fini(ssc);
7102 	return (DDI_FAILURE);
7103 }
7104 
7105 
7106 
7107 /*
7108  *    Function: sdattach
7109  *
7110  * Description: Driver's attach(9e) entry point function.
7111  *
7112  *   Arguments: devi - opaque device info handle
7113  *		cmd  - attach  type
7114  *
7115  * Return Code: DDI_SUCCESS
7116  *		DDI_FAILURE
7117  *
7118  *     Context: Kernel thread context
7119  */
7120 
7121 static int
7122 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7123 {
7124 	switch (cmd) {
7125 	case DDI_ATTACH:
7126 		return (sd_unit_attach(devi));
7127 	case DDI_RESUME:
7128 		return (sd_ddi_resume(devi));
7129 	default:
7130 		break;
7131 	}
7132 	return (DDI_FAILURE);
7133 }
7134 
7135 
7136 /*
7137  *    Function: sddetach
7138  *
7139  * Description: Driver's detach(9E) entry point function.
7140  *
7141  *   Arguments: devi - opaque device info handle
7142  *		cmd  - detach  type
7143  *
7144  * Return Code: DDI_SUCCESS
7145  *		DDI_FAILURE
7146  *
7147  *     Context: Kernel thread context
7148  */
7149 
7150 static int
7151 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7152 {
7153 	switch (cmd) {
7154 	case DDI_DETACH:
7155 		return (sd_unit_detach(devi));
7156 	case DDI_SUSPEND:
7157 		return (sd_ddi_suspend(devi));
7158 	default:
7159 		break;
7160 	}
7161 	return (DDI_FAILURE);
7162 }
7163 
7164 
7165 /*
7166  *     Function: sd_sync_with_callback
7167  *
7168  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7169  *		 state while the callback routine is active.
7170  *
7171  *    Arguments: un: softstate structure for the instance
7172  *
7173  *	Context: Kernel thread context
7174  */
7175 
7176 static void
7177 sd_sync_with_callback(struct sd_lun *un)
7178 {
7179 	ASSERT(un != NULL);
7180 
7181 	mutex_enter(SD_MUTEX(un));
7182 
7183 	ASSERT(un->un_in_callback >= 0);
7184 
7185 	while (un->un_in_callback > 0) {
7186 		mutex_exit(SD_MUTEX(un));
7187 		delay(2);
7188 		mutex_enter(SD_MUTEX(un));
7189 	}
7190 
7191 	mutex_exit(SD_MUTEX(un));
7192 }
7193 
7194 /*
7195  *    Function: sd_unit_attach
7196  *
7197  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7198  *		the soft state structure for the device and performs
7199  *		all necessary structure and device initializations.
7200  *
7201  *   Arguments: devi: the system's dev_info_t for the device.
7202  *
7203  * Return Code: DDI_SUCCESS if attach is successful.
7204  *		DDI_FAILURE if any part of the attach fails.
7205  *
7206  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7207  *		Kernel thread context only.  Can sleep.
7208  */
7209 
7210 static int
7211 sd_unit_attach(dev_info_t *devi)
7212 {
7213 	struct	scsi_device	*devp;
7214 	struct	sd_lun		*un;
7215 	char			*variantp;
7216 	char			name_str[48];
7217 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
7218 	int	instance;
7219 	int	rval;
7220 	int	wc_enabled;
7221 	int	wc_changeable;
7222 	int	tgt;
7223 	uint64_t	capacity;
7224 	uint_t		lbasize = 0;
7225 	dev_info_t	*pdip = ddi_get_parent(devi);
7226 	int		offbyone = 0;
7227 	int		geom_label_valid = 0;
7228 	sd_ssc_t	*ssc;
7229 	int		status;
7230 	struct sd_fm_internal	*sfip = NULL;
7231 	int		max_xfer_size;
7232 
7233 	/*
7234 	 * Retrieve the target driver's private data area. This was set
7235 	 * up by the HBA.
7236 	 */
7237 	devp = ddi_get_driver_private(devi);
7238 
7239 	/*
7240 	 * Retrieve the target ID of the device.
7241 	 */
7242 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7243 	    SCSI_ADDR_PROP_TARGET, -1);
7244 
7245 	/*
7246 	 * Since we have no idea what state things were left in by the last
7247 	 * user of the device, set up some 'default' settings, ie. turn 'em
7248 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7249 	 * Do this before the scsi_probe, which sends an inquiry.
7250 	 * This is a fix for bug (4430280).
7251 	 * Of special importance is wide-xfer. The drive could have been left
7252 	 * in wide transfer mode by the last driver to communicate with it,
7253 	 * this includes us. If that's the case, and if the following is not
7254 	 * setup properly or we don't re-negotiate with the drive prior to
7255 	 * transferring data to/from the drive, it causes bus parity errors,
7256 	 * data overruns, and unexpected interrupts. This first occurred when
7257 	 * the fix for bug (4378686) was made.
7258 	 */
7259 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7260 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7261 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7262 
7263 	/*
7264 	 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
7265 	 * on a target. Setting it per lun instance actually sets the
7266 	 * capability of this target, which affects those luns already
7267 	 * attached on the same target. So during attach, we can only disable
7268 	 * this capability only when no other lun has been attached on this
7269 	 * target. By doing this, we assume a target has the same tagged-qing
7270 	 * capability for every lun. The condition can be removed when HBA
7271 	 * is changed to support per lun based tagged-qing capability.
7272 	 */
7273 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
7274 		(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7275 	}
7276 
7277 	/*
7278 	 * Use scsi_probe() to issue an INQUIRY command to the device.
7279 	 * This call will allocate and fill in the scsi_inquiry structure
7280 	 * and point the sd_inq member of the scsi_device structure to it.
7281 	 * If the attach succeeds, then this memory will not be de-allocated
7282 	 * (via scsi_unprobe()) until the instance is detached.
7283 	 */
7284 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7285 		goto probe_failed;
7286 	}
7287 
7288 	/*
7289 	 * Check the device type as specified in the inquiry data and
7290 	 * claim it if it is of a type that we support.
7291 	 */
7292 	switch (devp->sd_inq->inq_dtype) {
7293 	case DTYPE_DIRECT:
7294 		break;
7295 	case DTYPE_RODIRECT:
7296 		break;
7297 	case DTYPE_OPTICAL:
7298 		break;
7299 	case DTYPE_NOTPRESENT:
7300 	default:
7301 		/* Unsupported device type; fail the attach. */
7302 		goto probe_failed;
7303 	}
7304 
7305 	/*
7306 	 * Allocate the soft state structure for this unit.
7307 	 *
7308 	 * We rely upon this memory being set to all zeroes by
7309 	 * ddi_soft_state_zalloc().  We assume that any member of the
7310 	 * soft state structure that is not explicitly initialized by
7311 	 * this routine will have a value of zero.
7312 	 */
7313 	instance = ddi_get_instance(devp->sd_dev);
7314 #ifndef XPV_HVM_DRIVER
7315 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7316 		goto probe_failed;
7317 	}
7318 #endif /* !XPV_HVM_DRIVER */
7319 
7320 	/*
7321 	 * Retrieve a pointer to the newly-allocated soft state.
7322 	 *
7323 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7324 	 * was successful, unless something has gone horribly wrong and the
7325 	 * ddi's soft state internals are corrupt (in which case it is
7326 	 * probably better to halt here than just fail the attach....)
7327 	 */
7328 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7329 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
7330 		    instance);
7331 		/*NOTREACHED*/
7332 	}
7333 
7334 	/*
7335 	 * Link the back ptr of the driver soft state to the scsi_device
7336 	 * struct for this lun.
7337 	 * Save a pointer to the softstate in the driver-private area of
7338 	 * the scsi_device struct.
7339 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7340 	 * we first set un->un_sd below.
7341 	 */
7342 	un->un_sd = devp;
7343 	devp->sd_private = (opaque_t)un;
7344 
7345 	/*
7346 	 * The following must be after devp is stored in the soft state struct.
7347 	 */
7348 #ifdef SDDEBUG
7349 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7350 	    "%s_unit_attach: un:0x%p instance:%d\n",
7351 	    ddi_driver_name(devi), un, instance);
7352 #endif
7353 
7354 	/*
7355 	 * Set up the device type and node type (for the minor nodes).
7356 	 * By default we assume that the device can at least support the
7357 	 * Common Command Set. Call it a CD-ROM if it reports itself
7358 	 * as a RODIRECT device.
7359 	 */
7360 	switch (devp->sd_inq->inq_dtype) {
7361 	case DTYPE_RODIRECT:
7362 		un->un_node_type = DDI_NT_CD_CHAN;
7363 		un->un_ctype	 = CTYPE_CDROM;
7364 		break;
7365 	case DTYPE_OPTICAL:
7366 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7367 		un->un_ctype	 = CTYPE_ROD;
7368 		break;
7369 	default:
7370 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7371 		un->un_ctype	 = CTYPE_CCS;
7372 		break;
7373 	}
7374 
7375 	/*
7376 	 * Try to read the interconnect type from the HBA.
7377 	 *
7378 	 * Note: This driver is currently compiled as two binaries, a parallel
7379 	 * scsi version (sd) and a fibre channel version (ssd). All functional
7380 	 * differences are determined at compile time. In the future a single
7381 	 * binary will be provided and the interconnect type will be used to
7382 	 * differentiate between fibre and parallel scsi behaviors. At that time
7383 	 * it will be necessary for all fibre channel HBAs to support this
7384 	 * property.
7385 	 *
7386 	 * set un_f_is_fiber to TRUE ( default fiber )
7387 	 */
7388 	un->un_f_is_fibre = TRUE;
7389 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7390 	case INTERCONNECT_SSA:
7391 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
7392 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7393 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7394 		break;
7395 	case INTERCONNECT_PARALLEL:
7396 		un->un_f_is_fibre = FALSE;
7397 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7398 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7399 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7400 		break;
7401 	case INTERCONNECT_SAS:
7402 		un->un_f_is_fibre = FALSE;
7403 		un->un_interconnect_type = SD_INTERCONNECT_SAS;
7404 		un->un_node_type = DDI_NT_BLOCK_SAS;
7405 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7406 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7407 		break;
7408 	case INTERCONNECT_SATA:
7409 		un->un_f_is_fibre = FALSE;
7410 		un->un_interconnect_type = SD_INTERCONNECT_SATA;
7411 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7412 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7413 		break;
7414 	case INTERCONNECT_FIBRE:
7415 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7416 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7417 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7418 		break;
7419 	case INTERCONNECT_FABRIC:
7420 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7421 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
7422 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7423 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7424 		break;
7425 	default:
7426 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7427 		/*
7428 		 * The HBA does not support the "interconnect-type" property
7429 		 * (or did not provide a recognized type).
7430 		 *
7431 		 * Note: This will be obsoleted when a single fibre channel
7432 		 * and parallel scsi driver is delivered. In the meantime the
7433 		 * interconnect type will be set to the platform default.If that
7434 		 * type is not parallel SCSI, it means that we should be
7435 		 * assuming "ssd" semantics. However, here this also means that
7436 		 * the FC HBA is not supporting the "interconnect-type" property
7437 		 * like we expect it to, so log this occurrence.
7438 		 */
7439 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7440 		if (!SD_IS_PARALLEL_SCSI(un)) {
7441 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7442 			    "sd_unit_attach: un:0x%p Assuming "
7443 			    "INTERCONNECT_FIBRE\n", un);
7444 		} else {
7445 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7446 			    "sd_unit_attach: un:0x%p Assuming "
7447 			    "INTERCONNECT_PARALLEL\n", un);
7448 			un->un_f_is_fibre = FALSE;
7449 		}
7450 #else
7451 		/*
7452 		 * Note: This source will be implemented when a single fibre
7453 		 * channel and parallel scsi driver is delivered. The default
7454 		 * will be to assume that if a device does not support the
7455 		 * "interconnect-type" property it is a parallel SCSI HBA and
7456 		 * we will set the interconnect type for parallel scsi.
7457 		 */
7458 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7459 		un->un_f_is_fibre = FALSE;
7460 #endif
7461 		break;
7462 	}
7463 
7464 	if (un->un_f_is_fibre == TRUE) {
7465 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7466 		    SCSI_VERSION_3) {
7467 			switch (un->un_interconnect_type) {
7468 			case SD_INTERCONNECT_FIBRE:
7469 			case SD_INTERCONNECT_SSA:
7470 				un->un_node_type = DDI_NT_BLOCK_WWN;
7471 				break;
7472 			default:
7473 				break;
7474 			}
7475 		}
7476 	}
7477 
7478 	/*
7479 	 * Initialize the Request Sense command for the target
7480 	 */
7481 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7482 		goto alloc_rqs_failed;
7483 	}
7484 
7485 	/*
7486 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7487 	 * with separate binary for sd and ssd.
7488 	 *
7489 	 * x86 has 1 binary, un_retry_count is set base on connection type.
7490 	 * The hardcoded values will go away when Sparc uses 1 binary
7491 	 * for sd and ssd.  This hardcoded values need to match
7492 	 * SD_RETRY_COUNT in sddef.h
7493 	 * The value used is base on interconnect type.
7494 	 * fibre = 3, parallel = 5
7495 	 */
7496 #if defined(__i386) || defined(__amd64)
7497 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7498 #else
7499 	un->un_retry_count = SD_RETRY_COUNT;
7500 #endif
7501 
7502 	/*
7503 	 * Set the per disk retry count to the default number of retries
7504 	 * for disks and CDROMs. This value can be overridden by the
7505 	 * disk property list or an entry in sd.conf.
7506 	 */
7507 	un->un_notready_retry_count =
7508 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7509 	    : DISK_NOT_READY_RETRY_COUNT(un);
7510 
7511 	/*
7512 	 * Set the busy retry count to the default value of un_retry_count.
7513 	 * This can be overridden by entries in sd.conf or the device
7514 	 * config table.
7515 	 */
7516 	un->un_busy_retry_count = un->un_retry_count;
7517 
7518 	/*
7519 	 * Init the reset threshold for retries.  This number determines
7520 	 * how many retries must be performed before a reset can be issued
7521 	 * (for certain error conditions). This can be overridden by entries
7522 	 * in sd.conf or the device config table.
7523 	 */
7524 	un->un_reset_retry_count = (un->un_retry_count / 2);
7525 
7526 	/*
7527 	 * Set the victim_retry_count to the default un_retry_count
7528 	 */
7529 	un->un_victim_retry_count = (2 * un->un_retry_count);
7530 
7531 	/*
7532 	 * Set the reservation release timeout to the default value of
7533 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
7534 	 * device config table.
7535 	 */
7536 	un->un_reserve_release_time = 5;
7537 
7538 	/*
7539 	 * Set up the default maximum transfer size. Note that this may
7540 	 * get updated later in the attach, when setting up default wide
7541 	 * operations for disks.
7542 	 */
7543 #if defined(__i386) || defined(__amd64)
7544 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7545 	un->un_partial_dma_supported = 1;
7546 #else
7547 	un->un_max_xfer_size = (uint_t)maxphys;
7548 #endif
7549 
7550 	/*
7551 	 * Get "allow bus device reset" property (defaults to "enabled" if
7552 	 * the property was not defined). This is to disable bus resets for
7553 	 * certain kinds of error recovery. Note: In the future when a run-time
7554 	 * fibre check is available the soft state flag should default to
7555 	 * enabled.
7556 	 */
7557 	if (un->un_f_is_fibre == TRUE) {
7558 		un->un_f_allow_bus_device_reset = TRUE;
7559 	} else {
7560 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7561 		    "allow-bus-device-reset", 1) != 0) {
7562 			un->un_f_allow_bus_device_reset = TRUE;
7563 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7564 			    "sd_unit_attach: un:0x%p Bus device reset "
7565 			    "enabled\n", un);
7566 		} else {
7567 			un->un_f_allow_bus_device_reset = FALSE;
7568 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7569 			    "sd_unit_attach: un:0x%p Bus device reset "
7570 			    "disabled\n", un);
7571 		}
7572 	}
7573 
7574 	/*
7575 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
7576 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
7577 	 *
7578 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
7579 	 * property. The new "variant" property with a value of "atapi" has been
7580 	 * introduced so that future 'variants' of standard SCSI behavior (like
7581 	 * atapi) could be specified by the underlying HBA drivers by supplying
7582 	 * a new value for the "variant" property, instead of having to define a
7583 	 * new property.
7584 	 */
7585 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7586 		un->un_f_cfg_is_atapi = TRUE;
7587 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7588 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
7589 	}
7590 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7591 	    &variantp) == DDI_PROP_SUCCESS) {
7592 		if (strcmp(variantp, "atapi") == 0) {
7593 			un->un_f_cfg_is_atapi = TRUE;
7594 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7595 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
7596 		}
7597 		ddi_prop_free(variantp);
7598 	}
7599 
7600 	un->un_cmd_timeout	= SD_IO_TIME;
7601 
7602 	un->un_busy_timeout  = SD_BSY_TIMEOUT;
7603 
7604 	/* Info on current states, statuses, etc. (Updated frequently) */
7605 	un->un_state		= SD_STATE_NORMAL;
7606 	un->un_last_state	= SD_STATE_NORMAL;
7607 
7608 	/* Control & status info for command throttling */
7609 	un->un_throttle		= sd_max_throttle;
7610 	un->un_saved_throttle	= sd_max_throttle;
7611 	un->un_min_throttle	= sd_min_throttle;
7612 
7613 	if (un->un_f_is_fibre == TRUE) {
7614 		un->un_f_use_adaptive_throttle = TRUE;
7615 	} else {
7616 		un->un_f_use_adaptive_throttle = FALSE;
7617 	}
7618 
7619 	/* Removable media support. */
7620 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7621 	un->un_mediastate		= DKIO_NONE;
7622 	un->un_specified_mediastate	= DKIO_NONE;
7623 
7624 	/* CVs for suspend/resume (PM or DR) */
7625 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
7626 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7627 
7628 	/* Power management support. */
7629 	un->un_power_level = SD_SPINDLE_UNINIT;
7630 
7631 	cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
7632 	un->un_f_wcc_inprog = 0;
7633 
7634 	/*
7635 	 * The open/close semaphore is used to serialize threads executing
7636 	 * in the driver's open & close entry point routines for a given
7637 	 * instance.
7638 	 */
7639 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7640 
7641 	/*
7642 	 * The conf file entry and softstate variable is a forceful override,
7643 	 * meaning a non-zero value must be entered to change the default.
7644 	 */
7645 	un->un_f_disksort_disabled = FALSE;
7646 	un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7647 	un->un_f_enable_rmw = FALSE;
7648 
7649 	/*
7650 	 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7651 	 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7652 	 */
7653 	un->un_f_mmc_gesn_polling = TRUE;
7654 
7655 	/*
7656 	 * physical sector size defaults to DEV_BSIZE currently. We can
7657 	 * override this value via the driver configuration file so we must
7658 	 * set it before calling sd_read_unit_properties().
7659 	 */
7660 	un->un_phy_blocksize = DEV_BSIZE;
7661 
7662 	/*
7663 	 * Retrieve the properties from the static driver table or the driver
7664 	 * configuration file (.conf) for this unit and update the soft state
7665 	 * for the device as needed for the indicated properties.
7666 	 * Note: the property configuration needs to occur here as some of the
7667 	 * following routines may have dependencies on soft state flags set
7668 	 * as part of the driver property configuration.
7669 	 */
7670 	sd_read_unit_properties(un);
7671 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7672 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7673 
7674 	/*
7675 	 * Only if a device has "hotpluggable" property, it is
7676 	 * treated as hotpluggable device. Otherwise, it is
7677 	 * regarded as non-hotpluggable one.
7678 	 */
7679 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7680 	    -1) != -1) {
7681 		un->un_f_is_hotpluggable = TRUE;
7682 	}
7683 
7684 	/*
7685 	 * set unit's attributes(flags) according to "hotpluggable" and
7686 	 * RMB bit in INQUIRY data.
7687 	 */
7688 	sd_set_unit_attributes(un, devi);
7689 
7690 	/*
7691 	 * By default, we mark the capacity, lbasize, and geometry
7692 	 * as invalid. Only if we successfully read a valid capacity
7693 	 * will we update the un_blockcount and un_tgt_blocksize with the
7694 	 * valid values (the geometry will be validated later).
7695 	 */
7696 	un->un_f_blockcount_is_valid	= FALSE;
7697 	un->un_f_tgt_blocksize_is_valid	= FALSE;
7698 
7699 	/*
7700 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7701 	 * otherwise.
7702 	 */
7703 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
7704 	un->un_blockcount = 0;
7705 
7706 	/*
7707 	 * Set up the per-instance info needed to determine the correct
7708 	 * CDBs and other info for issuing commands to the target.
7709 	 */
7710 	sd_init_cdb_limits(un);
7711 
7712 	/*
7713 	 * Set up the IO chains to use, based upon the target type.
7714 	 */
7715 	if (un->un_f_non_devbsize_supported) {
7716 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7717 	} else {
7718 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7719 	}
7720 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7721 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7722 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7723 
7724 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7725 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
7726 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7727 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7728 
7729 
7730 	if (ISCD(un)) {
7731 		un->un_additional_codes = sd_additional_codes;
7732 	} else {
7733 		un->un_additional_codes = NULL;
7734 	}
7735 
7736 	/*
7737 	 * Create the kstats here so they can be available for attach-time
7738 	 * routines that send commands to the unit (either polled or via
7739 	 * sd_send_scsi_cmd).
7740 	 *
7741 	 * Note: This is a critical sequence that needs to be maintained:
7742 	 *	1) Instantiate the kstats here, before any routines using the
7743 	 *	   iopath (i.e. sd_send_scsi_cmd).
7744 	 *	2) Instantiate and initialize the partition stats
7745 	 *	   (sd_set_pstats).
7746 	 *	3) Initialize the error stats (sd_set_errstats), following
7747 	 *	   sd_validate_geometry(),sd_register_devid(),
7748 	 *	   and sd_cache_control().
7749 	 */
7750 
7751 	un->un_stats = kstat_create(sd_label, instance,
7752 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7753 	if (un->un_stats != NULL) {
7754 		un->un_stats->ks_lock = SD_MUTEX(un);
7755 		kstat_install(un->un_stats);
7756 	}
7757 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7758 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
7759 
7760 	sd_create_errstats(un, instance);
7761 	if (un->un_errstats == NULL) {
7762 		goto create_errstats_failed;
7763 	}
7764 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7765 	    "sd_unit_attach: un:0x%p errstats created\n", un);
7766 
7767 	/*
7768 	 * The following if/else code was relocated here from below as part
7769 	 * of the fix for bug (4430280). However with the default setup added
7770 	 * on entry to this routine, it's no longer absolutely necessary for
7771 	 * this to be before the call to sd_spin_up_unit.
7772 	 */
7773 	if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7774 		int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7775 		    (devp->sd_inq->inq_ansi == 5)) &&
7776 		    devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7777 
7778 		/*
7779 		 * If tagged queueing is supported by the target
7780 		 * and by the host adapter then we will enable it
7781 		 */
7782 		un->un_tagflags = 0;
7783 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7784 		    (un->un_f_arq_enabled == TRUE)) {
7785 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7786 			    1, 1) == 1) {
7787 				un->un_tagflags = FLAG_STAG;
7788 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7789 				    "sd_unit_attach: un:0x%p tag queueing "
7790 				    "enabled\n", un);
7791 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
7792 			    "untagged-qing", 0) == 1) {
7793 				un->un_f_opt_queueing = TRUE;
7794 				un->un_saved_throttle = un->un_throttle =
7795 				    min(un->un_throttle, 3);
7796 			} else {
7797 				un->un_f_opt_queueing = FALSE;
7798 				un->un_saved_throttle = un->un_throttle = 1;
7799 			}
7800 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7801 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
7802 			/* The Host Adapter supports internal queueing. */
7803 			un->un_f_opt_queueing = TRUE;
7804 			un->un_saved_throttle = un->un_throttle =
7805 			    min(un->un_throttle, 3);
7806 		} else {
7807 			un->un_f_opt_queueing = FALSE;
7808 			un->un_saved_throttle = un->un_throttle = 1;
7809 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7810 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
7811 		}
7812 
7813 		/*
7814 		 * Enable large transfers for SATA/SAS drives
7815 		 */
7816 		if (SD_IS_SERIAL(un)) {
7817 			un->un_max_xfer_size =
7818 			    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7819 			    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7820 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7821 			    "sd_unit_attach: un:0x%p max transfer "
7822 			    "size=0x%x\n", un, un->un_max_xfer_size);
7823 
7824 		}
7825 
7826 		/* Setup or tear down default wide operations for disks */
7827 
7828 		/*
7829 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7830 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
7831 		 * system and be set to different values. In the future this
7832 		 * code may need to be updated when the ssd module is
7833 		 * obsoleted and removed from the system. (4299588)
7834 		 */
7835 		if (SD_IS_PARALLEL_SCSI(un) &&
7836 		    (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7837 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7838 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7839 			    1, 1) == 1) {
7840 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7841 				    "sd_unit_attach: un:0x%p Wide Transfer "
7842 				    "enabled\n", un);
7843 			}
7844 
7845 			/*
7846 			 * If tagged queuing has also been enabled, then
7847 			 * enable large xfers
7848 			 */
7849 			if (un->un_saved_throttle == sd_max_throttle) {
7850 				un->un_max_xfer_size =
7851 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7852 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7853 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7854 				    "sd_unit_attach: un:0x%p max transfer "
7855 				    "size=0x%x\n", un, un->un_max_xfer_size);
7856 			}
7857 		} else {
7858 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7859 			    0, 1) == 1) {
7860 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7861 				    "sd_unit_attach: un:0x%p "
7862 				    "Wide Transfer disabled\n", un);
7863 			}
7864 		}
7865 	} else {
7866 		un->un_tagflags = FLAG_STAG;
7867 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7868 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7869 	}
7870 
7871 	/*
7872 	 * If this target supports LUN reset, try to enable it.
7873 	 */
7874 	if (un->un_f_lun_reset_enabled) {
7875 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7876 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7877 			    "un:0x%p lun_reset capability set\n", un);
7878 		} else {
7879 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7880 			    "un:0x%p lun-reset capability not set\n", un);
7881 		}
7882 	}
7883 
7884 	/*
7885 	 * Adjust the maximum transfer size. This is to fix
7886 	 * the problem of partial DMA support on SPARC. Some
7887 	 * HBA driver, like aac, has very small dma_attr_maxxfer
7888 	 * size, which requires partial DMA support on SPARC.
7889 	 * In the future the SPARC pci nexus driver may solve
7890 	 * the problem instead of this fix.
7891 	 */
7892 	max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7893 	if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7894 		/* We need DMA partial even on sparc to ensure sddump() works */
7895 		un->un_max_xfer_size = max_xfer_size;
7896 		if (un->un_partial_dma_supported == 0)
7897 			un->un_partial_dma_supported = 1;
7898 	}
7899 	if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7900 	    DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7901 		if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7902 		    un->un_max_xfer_size) == 1) {
7903 			un->un_buf_breakup_supported = 1;
7904 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7905 			    "un:0x%p Buf breakup enabled\n", un);
7906 		}
7907 	}
7908 
7909 	/*
7910 	 * Set PKT_DMA_PARTIAL flag.
7911 	 */
7912 	if (un->un_partial_dma_supported == 1) {
7913 		un->un_pkt_flags = PKT_DMA_PARTIAL;
7914 	} else {
7915 		un->un_pkt_flags = 0;
7916 	}
7917 
7918 	/* Initialize sd_ssc_t for internal uscsi commands */
7919 	ssc = sd_ssc_init(un);
7920 	scsi_fm_init(devp);
7921 
7922 	/*
7923 	 * Allocate memory for SCSI FMA stuffs.
7924 	 */
7925 	un->un_fm_private =
7926 	    kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7927 	sfip = (struct sd_fm_internal *)un->un_fm_private;
7928 	sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7929 	sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7930 	sfip->fm_ssc.ssc_un = un;
7931 
7932 	if (ISCD(un) ||
7933 	    un->un_f_has_removable_media ||
7934 	    devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7935 		/*
7936 		 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7937 		 * Their log are unchanged.
7938 		 */
7939 		sfip->fm_log_level = SD_FM_LOG_NSUP;
7940 	} else {
7941 		/*
7942 		 * If enter here, it should be non-CDROM and FM-capable
7943 		 * device, and it will not keep the old scsi_log as before
7944 		 * in /var/adm/messages. However, the property
7945 		 * "fm-scsi-log" will control whether the FM telemetry will
7946 		 * be logged in /var/adm/messages.
7947 		 */
7948 		int fm_scsi_log;
7949 		fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7950 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7951 
7952 		if (fm_scsi_log)
7953 			sfip->fm_log_level = SD_FM_LOG_EREPORT;
7954 		else
7955 			sfip->fm_log_level = SD_FM_LOG_SILENT;
7956 	}
7957 
7958 	/*
7959 	 * At this point in the attach, we have enough info in the
7960 	 * soft state to be able to issue commands to the target.
7961 	 *
7962 	 * All command paths used below MUST issue their commands as
7963 	 * SD_PATH_DIRECT. This is important as intermediate layers
7964 	 * are not all initialized yet (such as PM).
7965 	 */
7966 
7967 	/*
7968 	 * Send a TEST UNIT READY command to the device. This should clear
7969 	 * any outstanding UNIT ATTENTION that may be present.
7970 	 *
7971 	 * Note: Don't check for success, just track if there is a reservation,
7972 	 * this is a throw away command to clear any unit attentions.
7973 	 *
7974 	 * Note: This MUST be the first command issued to the target during
7975 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
7976 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7977 	 * with attempts at spinning up a device with no media.
7978 	 */
7979 	status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7980 	if (status != 0) {
7981 		if (status == EACCES)
7982 			reservation_flag = SD_TARGET_IS_RESERVED;
7983 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7984 	}
7985 
7986 	/*
7987 	 * If the device is NOT a removable media device, attempt to spin
7988 	 * it up (using the START_STOP_UNIT command) and read its capacity
7989 	 * (using the READ CAPACITY command).  Note, however, that either
7990 	 * of these could fail and in some cases we would continue with
7991 	 * the attach despite the failure (see below).
7992 	 */
7993 	if (un->un_f_descr_format_supported) {
7994 
7995 		switch (sd_spin_up_unit(ssc)) {
7996 		case 0:
7997 			/*
7998 			 * Spin-up was successful; now try to read the
7999 			 * capacity.  If successful then save the results
8000 			 * and mark the capacity & lbasize as valid.
8001 			 */
8002 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8003 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
8004 
8005 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
8006 			    &lbasize, SD_PATH_DIRECT);
8007 
8008 			switch (status) {
8009 			case 0: {
8010 				if (capacity > DK_MAX_BLOCKS) {
8011 #ifdef _LP64
8012 					if ((capacity + 1) >
8013 					    SD_GROUP1_MAX_ADDRESS) {
8014 						/*
8015 						 * Enable descriptor format
8016 						 * sense data so that we can
8017 						 * get 64 bit sense data
8018 						 * fields.
8019 						 */
8020 						sd_enable_descr_sense(ssc);
8021 					}
8022 #else
8023 					/* 32-bit kernels can't handle this */
8024 					scsi_log(SD_DEVINFO(un),
8025 					    sd_label, CE_WARN,
8026 					    "disk has %llu blocks, which "
8027 					    "is too large for a 32-bit "
8028 					    "kernel", capacity);
8029 
8030 #if defined(__i386) || defined(__amd64)
8031 					/*
8032 					 * 1TB disk was treated as (1T - 512)B
8033 					 * in the past, so that it might have
8034 					 * valid VTOC and solaris partitions,
8035 					 * we have to allow it to continue to
8036 					 * work.
8037 					 */
8038 					if (capacity -1 > DK_MAX_BLOCKS)
8039 #endif
8040 					goto spinup_failed;
8041 #endif
8042 				}
8043 
8044 				/*
8045 				 * Here it's not necessary to check the case:
8046 				 * the capacity of the device is bigger than
8047 				 * what the max hba cdb can support. Because
8048 				 * sd_send_scsi_READ_CAPACITY will retrieve
8049 				 * the capacity by sending USCSI command, which
8050 				 * is constrained by the max hba cdb. Actually,
8051 				 * sd_send_scsi_READ_CAPACITY will return
8052 				 * EINVAL when using bigger cdb than required
8053 				 * cdb length. Will handle this case in
8054 				 * "case EINVAL".
8055 				 */
8056 
8057 				/*
8058 				 * The following relies on
8059 				 * sd_send_scsi_READ_CAPACITY never
8060 				 * returning 0 for capacity and/or lbasize.
8061 				 */
8062 				sd_update_block_info(un, lbasize, capacity);
8063 
8064 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8065 				    "sd_unit_attach: un:0x%p capacity = %ld "
8066 				    "blocks; lbasize= %ld.\n", un,
8067 				    un->un_blockcount, un->un_tgt_blocksize);
8068 
8069 				break;
8070 			}
8071 			case EINVAL:
8072 				/*
8073 				 * In the case where the max-cdb-length property
8074 				 * is smaller than the required CDB length for
8075 				 * a SCSI device, a target driver can fail to
8076 				 * attach to that device.
8077 				 */
8078 				scsi_log(SD_DEVINFO(un),
8079 				    sd_label, CE_WARN,
8080 				    "disk capacity is too large "
8081 				    "for current cdb length");
8082 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8083 
8084 				goto spinup_failed;
8085 			case EACCES:
8086 				/*
8087 				 * Should never get here if the spin-up
8088 				 * succeeded, but code it in anyway.
8089 				 * From here, just continue with the attach...
8090 				 */
8091 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8092 				    "sd_unit_attach: un:0x%p "
8093 				    "sd_send_scsi_READ_CAPACITY "
8094 				    "returned reservation conflict\n", un);
8095 				reservation_flag = SD_TARGET_IS_RESERVED;
8096 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8097 				break;
8098 			default:
8099 				/*
8100 				 * Likewise, should never get here if the
8101 				 * spin-up succeeded. Just continue with
8102 				 * the attach...
8103 				 */
8104 				if (status == EIO)
8105 					sd_ssc_assessment(ssc,
8106 					    SD_FMT_STATUS_CHECK);
8107 				else
8108 					sd_ssc_assessment(ssc,
8109 					    SD_FMT_IGNORE);
8110 				break;
8111 			}
8112 			break;
8113 		case EACCES:
8114 			/*
8115 			 * Device is reserved by another host.  In this case
8116 			 * we could not spin it up or read the capacity, but
8117 			 * we continue with the attach anyway.
8118 			 */
8119 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8120 			    "sd_unit_attach: un:0x%p spin-up reservation "
8121 			    "conflict.\n", un);
8122 			reservation_flag = SD_TARGET_IS_RESERVED;
8123 			break;
8124 		default:
8125 			/* Fail the attach if the spin-up failed. */
8126 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8127 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8128 			goto spinup_failed;
8129 		}
8130 
8131 	}
8132 
8133 	/*
8134 	 * Check to see if this is a MMC drive
8135 	 */
8136 	if (ISCD(un)) {
8137 		sd_set_mmc_caps(ssc);
8138 	}
8139 
8140 	/*
8141 	 * Add a zero-length attribute to tell the world we support
8142 	 * kernel ioctls (for layered drivers)
8143 	 */
8144 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8145 	    DDI_KERNEL_IOCTL, NULL, 0);
8146 
8147 	/*
8148 	 * Add a boolean property to tell the world we support
8149 	 * the B_FAILFAST flag (for layered drivers)
8150 	 */
8151 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8152 	    "ddi-failfast-supported", NULL, 0);
8153 
8154 	/*
8155 	 * Initialize power management
8156 	 */
8157 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8158 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8159 	sd_setup_pm(ssc, devi);
8160 	if (un->un_f_pm_is_enabled == FALSE) {
8161 		/*
8162 		 * For performance, point to a jump table that does
8163 		 * not include pm.
8164 		 * The direct and priority chains don't change with PM.
8165 		 *
8166 		 * Note: this is currently done based on individual device
8167 		 * capabilities. When an interface for determining system
8168 		 * power enabled state becomes available, or when additional
8169 		 * layers are added to the command chain, these values will
8170 		 * have to be re-evaluated for correctness.
8171 		 */
8172 		if (un->un_f_non_devbsize_supported) {
8173 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8174 		} else {
8175 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8176 		}
8177 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8178 	}
8179 
8180 	/*
8181 	 * This property is set to 0 by HA software to avoid retries
8182 	 * on a reserved disk. (The preferred property name is
8183 	 * "retry-on-reservation-conflict") (1189689)
8184 	 *
8185 	 * Note: The use of a global here can have unintended consequences. A
8186 	 * per instance variable is preferable to match the capabilities of
8187 	 * different underlying hba's (4402600)
8188 	 */
8189 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8190 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8191 	    sd_retry_on_reservation_conflict);
8192 	if (sd_retry_on_reservation_conflict != 0) {
8193 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8194 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8195 		    sd_retry_on_reservation_conflict);
8196 	}
8197 
8198 	/* Set up options for QFULL handling. */
8199 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8200 	    "qfull-retries", -1)) != -1) {
8201 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8202 		    rval, 1);
8203 	}
8204 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8205 	    "qfull-retry-interval", -1)) != -1) {
8206 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8207 		    rval, 1);
8208 	}
8209 
8210 	/*
8211 	 * This just prints a message that announces the existence of the
8212 	 * device. The message is always printed in the system logfile, but
8213 	 * only appears on the console if the system is booted with the
8214 	 * -v (verbose) argument.
8215 	 */
8216 	ddi_report_dev(devi);
8217 
8218 	un->un_mediastate = DKIO_NONE;
8219 
8220 	/*
8221 	 * Check if this is a SSD(Solid State Drive).
8222 	 */
8223 	sd_check_solid_state(ssc);
8224 
8225 	/*
8226 	 * Check whether the drive is in emulation mode.
8227 	 */
8228 	sd_check_emulation_mode(ssc);
8229 
8230 	cmlb_alloc_handle(&un->un_cmlbhandle);
8231 
8232 #if defined(__i386) || defined(__amd64)
8233 	/*
8234 	 * On x86, compensate for off-by-1 legacy error
8235 	 */
8236 	if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8237 	    (lbasize == un->un_sys_blocksize))
8238 		offbyone = CMLB_OFF_BY_ONE;
8239 #endif
8240 
8241 	if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
8242 	    VOID2BOOLEAN(un->un_f_has_removable_media != 0),
8243 	    VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
8244 	    un->un_node_type, offbyone, un->un_cmlbhandle,
8245 	    (void *)SD_PATH_DIRECT) != 0) {
8246 		goto cmlb_attach_failed;
8247 	}
8248 
8249 
8250 	/*
8251 	 * Read and validate the device's geometry (ie, disk label)
8252 	 * A new unformatted drive will not have a valid geometry, but
8253 	 * the driver needs to successfully attach to this device so
8254 	 * the drive can be formatted via ioctls.
8255 	 */
8256 	geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8257 	    (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8258 
8259 	mutex_enter(SD_MUTEX(un));
8260 
8261 	/*
8262 	 * Read and initialize the devid for the unit.
8263 	 */
8264 	if (un->un_f_devid_supported) {
8265 		sd_register_devid(ssc, devi, reservation_flag);
8266 	}
8267 	mutex_exit(SD_MUTEX(un));
8268 
8269 #if (defined(__fibre))
8270 	/*
8271 	 * Register callbacks for fibre only.  You can't do this solely
8272 	 * on the basis of the devid_type because this is hba specific.
8273 	 * We need to query our hba capabilities to find out whether to
8274 	 * register or not.
8275 	 */
8276 	if (un->un_f_is_fibre) {
8277 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8278 			sd_init_event_callbacks(un);
8279 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8280 			    "sd_unit_attach: un:0x%p event callbacks inserted",
8281 			    un);
8282 		}
8283 	}
8284 #endif
8285 
8286 	if (un->un_f_opt_disable_cache == TRUE) {
8287 		/*
8288 		 * Disable both read cache and write cache.  This is
8289 		 * the historic behavior of the keywords in the config file.
8290 		 */
8291 		if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8292 		    0) {
8293 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8294 			    "sd_unit_attach: un:0x%p Could not disable "
8295 			    "caching", un);
8296 			goto devid_failed;
8297 		}
8298 	}
8299 
8300 	/*
8301 	 * Check the value of the WCE bit and if it's allowed to be changed,
8302 	 * set un_f_write_cache_enabled and un_f_cache_mode_changeable
8303 	 * accordingly.
8304 	 */
8305 	(void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8306 	sd_get_write_cache_changeable(ssc, &wc_changeable);
8307 	mutex_enter(SD_MUTEX(un));
8308 	un->un_f_write_cache_enabled = (wc_enabled != 0);
8309 	un->un_f_cache_mode_changeable = (wc_changeable != 0);
8310 	mutex_exit(SD_MUTEX(un));
8311 
8312 	if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
8313 	    un->un_tgt_blocksize != DEV_BSIZE) ||
8314 	    un->un_f_enable_rmw) {
8315 		if (!(un->un_wm_cache)) {
8316 			(void) snprintf(name_str, sizeof (name_str),
8317 			    "%s%d_cache",
8318 			    ddi_driver_name(SD_DEVINFO(un)),
8319 			    ddi_get_instance(SD_DEVINFO(un)));
8320 			un->un_wm_cache = kmem_cache_create(
8321 			    name_str, sizeof (struct sd_w_map),
8322 			    8, sd_wm_cache_constructor,
8323 			    sd_wm_cache_destructor, NULL,
8324 			    (void *)un, NULL, 0);
8325 			if (!(un->un_wm_cache)) {
8326 				goto wm_cache_failed;
8327 			}
8328 		}
8329 	}
8330 
8331 	/*
8332 	 * Check the value of the NV_SUP bit and set
8333 	 * un_f_suppress_cache_flush accordingly.
8334 	 */
8335 	sd_get_nv_sup(ssc);
8336 
8337 	/*
8338 	 * Find out what type of reservation this disk supports.
8339 	 */
8340 	status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
8341 
8342 	switch (status) {
8343 	case 0:
8344 		/*
8345 		 * SCSI-3 reservations are supported.
8346 		 */
8347 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8348 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8349 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8350 		break;
8351 	case ENOTSUP:
8352 		/*
8353 		 * The PERSISTENT RESERVE IN command would not be recognized by
8354 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8355 		 */
8356 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8357 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8358 		un->un_reservation_type = SD_SCSI2_RESERVATION;
8359 
8360 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8361 		break;
8362 	default:
8363 		/*
8364 		 * default to SCSI-3 reservations
8365 		 */
8366 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8367 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8368 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8369 
8370 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8371 		break;
8372 	}
8373 
8374 	/*
8375 	 * Set the pstat and error stat values here, so data obtained during the
8376 	 * previous attach-time routines is available.
8377 	 *
8378 	 * Note: This is a critical sequence that needs to be maintained:
8379 	 *	1) Instantiate the kstats before any routines using the iopath
8380 	 *	   (i.e. sd_send_scsi_cmd).
8381 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8382 	 *	   stats (sd_set_pstats)here, following
8383 	 *	   cmlb_validate_geometry(), sd_register_devid(), and
8384 	 *	   sd_cache_control().
8385 	 */
8386 
8387 	if (un->un_f_pkstats_enabled && geom_label_valid) {
8388 		sd_set_pstats(un);
8389 		SD_TRACE(SD_LOG_IO_PARTITION, un,
8390 		    "sd_unit_attach: un:0x%p pstats created and set\n", un);
8391 	}
8392 
8393 	sd_set_errstats(un);
8394 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8395 	    "sd_unit_attach: un:0x%p errstats set\n", un);
8396 
8397 
8398 	/*
8399 	 * After successfully attaching an instance, we record the information
8400 	 * of how many luns have been attached on the relative target and
8401 	 * controller for parallel SCSI. This information is used when sd tries
8402 	 * to set the tagged queuing capability in HBA.
8403 	 */
8404 	if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8405 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8406 	}
8407 
8408 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8409 	    "sd_unit_attach: un:0x%p exit success\n", un);
8410 
8411 	/* Uninitialize sd_ssc_t pointer */
8412 	sd_ssc_fini(ssc);
8413 
8414 	return (DDI_SUCCESS);
8415 
8416 	/*
8417 	 * An error occurred during the attach; clean up & return failure.
8418 	 */
8419 wm_cache_failed:
8420 devid_failed:
8421 
8422 setup_pm_failed:
8423 	ddi_remove_minor_node(devi, NULL);
8424 
8425 cmlb_attach_failed:
8426 	/*
8427 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8428 	 */
8429 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8430 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8431 
8432 	/*
8433 	 * Refer to the comments of setting tagged-qing in the beginning of
8434 	 * sd_unit_attach. We can only disable tagged queuing when there is
8435 	 * no lun attached on the target.
8436 	 */
8437 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8438 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8439 	}
8440 
8441 	if (un->un_f_is_fibre == FALSE) {
8442 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8443 	}
8444 
8445 spinup_failed:
8446 
8447 	/* Uninitialize sd_ssc_t pointer */
8448 	sd_ssc_fini(ssc);
8449 
8450 	mutex_enter(SD_MUTEX(un));
8451 
8452 	/* Deallocate SCSI FMA memory spaces */
8453 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8454 
8455 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8456 	if (un->un_direct_priority_timeid != NULL) {
8457 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8458 		un->un_direct_priority_timeid = NULL;
8459 		mutex_exit(SD_MUTEX(un));
8460 		(void) untimeout(temp_id);
8461 		mutex_enter(SD_MUTEX(un));
8462 	}
8463 
8464 	/* Cancel any pending start/stop timeouts */
8465 	if (un->un_startstop_timeid != NULL) {
8466 		timeout_id_t temp_id = un->un_startstop_timeid;
8467 		un->un_startstop_timeid = NULL;
8468 		mutex_exit(SD_MUTEX(un));
8469 		(void) untimeout(temp_id);
8470 		mutex_enter(SD_MUTEX(un));
8471 	}
8472 
8473 	/* Cancel any pending reset-throttle timeouts */
8474 	if (un->un_reset_throttle_timeid != NULL) {
8475 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8476 		un->un_reset_throttle_timeid = NULL;
8477 		mutex_exit(SD_MUTEX(un));
8478 		(void) untimeout(temp_id);
8479 		mutex_enter(SD_MUTEX(un));
8480 	}
8481 
8482 	/* Cancel rmw warning message timeouts */
8483 	if (un->un_rmw_msg_timeid != NULL) {
8484 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8485 		un->un_rmw_msg_timeid = NULL;
8486 		mutex_exit(SD_MUTEX(un));
8487 		(void) untimeout(temp_id);
8488 		mutex_enter(SD_MUTEX(un));
8489 	}
8490 
8491 	/* Cancel any pending retry timeouts */
8492 	if (un->un_retry_timeid != NULL) {
8493 		timeout_id_t temp_id = un->un_retry_timeid;
8494 		un->un_retry_timeid = NULL;
8495 		mutex_exit(SD_MUTEX(un));
8496 		(void) untimeout(temp_id);
8497 		mutex_enter(SD_MUTEX(un));
8498 	}
8499 
8500 	/* Cancel any pending delayed cv broadcast timeouts */
8501 	if (un->un_dcvb_timeid != NULL) {
8502 		timeout_id_t temp_id = un->un_dcvb_timeid;
8503 		un->un_dcvb_timeid = NULL;
8504 		mutex_exit(SD_MUTEX(un));
8505 		(void) untimeout(temp_id);
8506 		mutex_enter(SD_MUTEX(un));
8507 	}
8508 
8509 	mutex_exit(SD_MUTEX(un));
8510 
8511 	/* There should not be any in-progress I/O so ASSERT this check */
8512 	ASSERT(un->un_ncmds_in_transport == 0);
8513 	ASSERT(un->un_ncmds_in_driver == 0);
8514 
8515 	/* Do not free the softstate if the callback routine is active */
8516 	sd_sync_with_callback(un);
8517 
8518 	/*
8519 	 * Partition stats apparently are not used with removables. These would
8520 	 * not have been created during attach, so no need to clean them up...
8521 	 */
8522 	if (un->un_errstats != NULL) {
8523 		kstat_delete(un->un_errstats);
8524 		un->un_errstats = NULL;
8525 	}
8526 
8527 create_errstats_failed:
8528 
8529 	if (un->un_stats != NULL) {
8530 		kstat_delete(un->un_stats);
8531 		un->un_stats = NULL;
8532 	}
8533 
8534 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8535 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8536 
8537 	ddi_prop_remove_all(devi);
8538 	sema_destroy(&un->un_semoclose);
8539 	cv_destroy(&un->un_state_cv);
8540 
8541 getrbuf_failed:
8542 
8543 	sd_free_rqs(un);
8544 
8545 alloc_rqs_failed:
8546 
8547 	devp->sd_private = NULL;
8548 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
8549 
8550 get_softstate_failed:
8551 	/*
8552 	 * Note: the man pages are unclear as to whether or not doing a
8553 	 * ddi_soft_state_free(sd_state, instance) is the right way to
8554 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
8555 	 * ddi_get_soft_state() fails.  The implication seems to be
8556 	 * that the get_soft_state cannot fail if the zalloc succeeds.
8557 	 */
8558 #ifndef XPV_HVM_DRIVER
8559 	ddi_soft_state_free(sd_state, instance);
8560 #endif /* !XPV_HVM_DRIVER */
8561 
8562 probe_failed:
8563 	scsi_unprobe(devp);
8564 
8565 	return (DDI_FAILURE);
8566 }
8567 
8568 
8569 /*
8570  *    Function: sd_unit_detach
8571  *
8572  * Description: Performs DDI_DETACH processing for sddetach().
8573  *
8574  * Return Code: DDI_SUCCESS
8575  *		DDI_FAILURE
8576  *
8577  *     Context: Kernel thread context
8578  */
8579 
8580 static int
8581 sd_unit_detach(dev_info_t *devi)
8582 {
8583 	struct scsi_device	*devp;
8584 	struct sd_lun		*un;
8585 	int			i;
8586 	int			tgt;
8587 	dev_t			dev;
8588 	dev_info_t		*pdip = ddi_get_parent(devi);
8589 #ifndef XPV_HVM_DRIVER
8590 	int			instance = ddi_get_instance(devi);
8591 #endif /* !XPV_HVM_DRIVER */
8592 
8593 	mutex_enter(&sd_detach_mutex);
8594 
8595 	/*
8596 	 * Fail the detach for any of the following:
8597 	 *  - Unable to get the sd_lun struct for the instance
8598 	 *  - A layered driver has an outstanding open on the instance
8599 	 *  - Another thread is already detaching this instance
8600 	 *  - Another thread is currently performing an open
8601 	 */
8602 	devp = ddi_get_driver_private(devi);
8603 	if ((devp == NULL) ||
8604 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8605 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8606 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8607 		mutex_exit(&sd_detach_mutex);
8608 		return (DDI_FAILURE);
8609 	}
8610 
8611 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8612 
8613 	/*
8614 	 * Mark this instance as currently in a detach, to inhibit any
8615 	 * opens from a layered driver.
8616 	 */
8617 	un->un_detach_count++;
8618 	mutex_exit(&sd_detach_mutex);
8619 
8620 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8621 	    SCSI_ADDR_PROP_TARGET, -1);
8622 
8623 	dev = sd_make_device(SD_DEVINFO(un));
8624 
8625 #ifndef lint
8626 	_NOTE(COMPETING_THREADS_NOW);
8627 #endif
8628 
8629 	mutex_enter(SD_MUTEX(un));
8630 
8631 	/*
8632 	 * Fail the detach if there are any outstanding layered
8633 	 * opens on this device.
8634 	 */
8635 	for (i = 0; i < NDKMAP; i++) {
8636 		if (un->un_ocmap.lyropen[i] != 0) {
8637 			goto err_notclosed;
8638 		}
8639 	}
8640 
8641 	/*
8642 	 * Verify there are NO outstanding commands issued to this device.
8643 	 * ie, un_ncmds_in_transport == 0.
8644 	 * It's possible to have outstanding commands through the physio
8645 	 * code path, even though everything's closed.
8646 	 */
8647 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8648 	    (un->un_direct_priority_timeid != NULL) ||
8649 	    (un->un_state == SD_STATE_RWAIT)) {
8650 		mutex_exit(SD_MUTEX(un));
8651 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8652 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
8653 		goto err_stillbusy;
8654 	}
8655 
8656 	/*
8657 	 * If we have the device reserved, release the reservation.
8658 	 */
8659 	if ((un->un_resvd_status & SD_RESERVE) &&
8660 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
8661 		mutex_exit(SD_MUTEX(un));
8662 		/*
8663 		 * Note: sd_reserve_release sends a command to the device
8664 		 * via the sd_ioctlcmd() path, and can sleep.
8665 		 */
8666 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8667 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8668 			    "sd_dr_detach: Cannot release reservation \n");
8669 		}
8670 	} else {
8671 		mutex_exit(SD_MUTEX(un));
8672 	}
8673 
8674 	/*
8675 	 * Untimeout any reserve recover, throttle reset, restart unit
8676 	 * and delayed broadcast timeout threads. Protect the timeout pointer
8677 	 * from getting nulled by their callback functions.
8678 	 */
8679 	mutex_enter(SD_MUTEX(un));
8680 	if (un->un_resvd_timeid != NULL) {
8681 		timeout_id_t temp_id = un->un_resvd_timeid;
8682 		un->un_resvd_timeid = NULL;
8683 		mutex_exit(SD_MUTEX(un));
8684 		(void) untimeout(temp_id);
8685 		mutex_enter(SD_MUTEX(un));
8686 	}
8687 
8688 	if (un->un_reset_throttle_timeid != NULL) {
8689 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8690 		un->un_reset_throttle_timeid = NULL;
8691 		mutex_exit(SD_MUTEX(un));
8692 		(void) untimeout(temp_id);
8693 		mutex_enter(SD_MUTEX(un));
8694 	}
8695 
8696 	if (un->un_startstop_timeid != NULL) {
8697 		timeout_id_t temp_id = un->un_startstop_timeid;
8698 		un->un_startstop_timeid = NULL;
8699 		mutex_exit(SD_MUTEX(un));
8700 		(void) untimeout(temp_id);
8701 		mutex_enter(SD_MUTEX(un));
8702 	}
8703 
8704 	if (un->un_rmw_msg_timeid != NULL) {
8705 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8706 		un->un_rmw_msg_timeid = NULL;
8707 		mutex_exit(SD_MUTEX(un));
8708 		(void) untimeout(temp_id);
8709 		mutex_enter(SD_MUTEX(un));
8710 	}
8711 
8712 	if (un->un_dcvb_timeid != NULL) {
8713 		timeout_id_t temp_id = un->un_dcvb_timeid;
8714 		un->un_dcvb_timeid = NULL;
8715 		mutex_exit(SD_MUTEX(un));
8716 		(void) untimeout(temp_id);
8717 	} else {
8718 		mutex_exit(SD_MUTEX(un));
8719 	}
8720 
8721 	/* Remove any pending reservation reclaim requests for this device */
8722 	sd_rmv_resv_reclaim_req(dev);
8723 
8724 	mutex_enter(SD_MUTEX(un));
8725 
8726 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8727 	if (un->un_direct_priority_timeid != NULL) {
8728 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8729 		un->un_direct_priority_timeid = NULL;
8730 		mutex_exit(SD_MUTEX(un));
8731 		(void) untimeout(temp_id);
8732 		mutex_enter(SD_MUTEX(un));
8733 	}
8734 
8735 	/* Cancel any active multi-host disk watch thread requests */
8736 	if (un->un_mhd_token != NULL) {
8737 		mutex_exit(SD_MUTEX(un));
8738 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8739 		if (scsi_watch_request_terminate(un->un_mhd_token,
8740 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8741 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8742 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
8743 			/*
8744 			 * Note: We are returning here after having removed
8745 			 * some driver timeouts above. This is consistent with
8746 			 * the legacy implementation but perhaps the watch
8747 			 * terminate call should be made with the wait flag set.
8748 			 */
8749 			goto err_stillbusy;
8750 		}
8751 		mutex_enter(SD_MUTEX(un));
8752 		un->un_mhd_token = NULL;
8753 	}
8754 
8755 	if (un->un_swr_token != NULL) {
8756 		mutex_exit(SD_MUTEX(un));
8757 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8758 		if (scsi_watch_request_terminate(un->un_swr_token,
8759 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8760 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8761 			    "sd_dr_detach: Cannot cancel swr watch request\n");
8762 			/*
8763 			 * Note: We are returning here after having removed
8764 			 * some driver timeouts above. This is consistent with
8765 			 * the legacy implementation but perhaps the watch
8766 			 * terminate call should be made with the wait flag set.
8767 			 */
8768 			goto err_stillbusy;
8769 		}
8770 		mutex_enter(SD_MUTEX(un));
8771 		un->un_swr_token = NULL;
8772 	}
8773 
8774 	mutex_exit(SD_MUTEX(un));
8775 
8776 	/*
8777 	 * Clear any scsi_reset_notifies. We clear the reset notifies
8778 	 * if we have not registered one.
8779 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8780 	 */
8781 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8782 	    sd_mhd_reset_notify_cb, (caddr_t)un);
8783 
8784 	/*
8785 	 * protect the timeout pointers from getting nulled by
8786 	 * their callback functions during the cancellation process.
8787 	 * In such a scenario untimeout can be invoked with a null value.
8788 	 */
8789 	_NOTE(NO_COMPETING_THREADS_NOW);
8790 
8791 	mutex_enter(&un->un_pm_mutex);
8792 	if (un->un_pm_idle_timeid != NULL) {
8793 		timeout_id_t temp_id = un->un_pm_idle_timeid;
8794 		un->un_pm_idle_timeid = NULL;
8795 		mutex_exit(&un->un_pm_mutex);
8796 
8797 		/*
8798 		 * Timeout is active; cancel it.
8799 		 * Note that it'll never be active on a device
8800 		 * that does not support PM therefore we don't
8801 		 * have to check before calling pm_idle_component.
8802 		 */
8803 		(void) untimeout(temp_id);
8804 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8805 		mutex_enter(&un->un_pm_mutex);
8806 	}
8807 
8808 	/*
8809 	 * Check whether there is already a timeout scheduled for power
8810 	 * management. If yes then don't lower the power here, that's.
8811 	 * the timeout handler's job.
8812 	 */
8813 	if (un->un_pm_timeid != NULL) {
8814 		timeout_id_t temp_id = un->un_pm_timeid;
8815 		un->un_pm_timeid = NULL;
8816 		mutex_exit(&un->un_pm_mutex);
8817 		/*
8818 		 * Timeout is active; cancel it.
8819 		 * Note that it'll never be active on a device
8820 		 * that does not support PM therefore we don't
8821 		 * have to check before calling pm_idle_component.
8822 		 */
8823 		(void) untimeout(temp_id);
8824 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8825 
8826 	} else {
8827 		mutex_exit(&un->un_pm_mutex);
8828 		if ((un->un_f_pm_is_enabled == TRUE) &&
8829 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8830 		    != DDI_SUCCESS)) {
8831 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8832 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
8833 			/*
8834 			 * Fix for bug: 4297749, item # 13
8835 			 * The above test now includes a check to see if PM is
8836 			 * supported by this device before call
8837 			 * pm_lower_power().
8838 			 * Note, the following is not dead code. The call to
8839 			 * pm_lower_power above will generate a call back into
8840 			 * our sdpower routine which might result in a timeout
8841 			 * handler getting activated. Therefore the following
8842 			 * code is valid and necessary.
8843 			 */
8844 			mutex_enter(&un->un_pm_mutex);
8845 			if (un->un_pm_timeid != NULL) {
8846 				timeout_id_t temp_id = un->un_pm_timeid;
8847 				un->un_pm_timeid = NULL;
8848 				mutex_exit(&un->un_pm_mutex);
8849 				(void) untimeout(temp_id);
8850 				(void) pm_idle_component(SD_DEVINFO(un), 0);
8851 			} else {
8852 				mutex_exit(&un->un_pm_mutex);
8853 			}
8854 		}
8855 	}
8856 
8857 	/*
8858 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8859 	 * Relocated here from above to be after the call to
8860 	 * pm_lower_power, which was getting errors.
8861 	 */
8862 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8863 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8864 
8865 	/*
8866 	 * Currently, tagged queuing is supported per target based by HBA.
8867 	 * Setting this per lun instance actually sets the capability of this
8868 	 * target in HBA, which affects those luns already attached on the
8869 	 * same target. So during detach, we can only disable this capability
8870 	 * only when this is the only lun left on this target. By doing
8871 	 * this, we assume a target has the same tagged queuing capability
8872 	 * for every lun. The condition can be removed when HBA is changed to
8873 	 * support per lun based tagged queuing capability.
8874 	 */
8875 	if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8876 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8877 	}
8878 
8879 	if (un->un_f_is_fibre == FALSE) {
8880 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8881 	}
8882 
8883 	/*
8884 	 * Remove any event callbacks, fibre only
8885 	 */
8886 	if (un->un_f_is_fibre == TRUE) {
8887 		if ((un->un_insert_event != NULL) &&
8888 		    (ddi_remove_event_handler(un->un_insert_cb_id) !=
8889 		    DDI_SUCCESS)) {
8890 			/*
8891 			 * Note: We are returning here after having done
8892 			 * substantial cleanup above. This is consistent
8893 			 * with the legacy implementation but this may not
8894 			 * be the right thing to do.
8895 			 */
8896 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8897 			    "sd_dr_detach: Cannot cancel insert event\n");
8898 			goto err_remove_event;
8899 		}
8900 		un->un_insert_event = NULL;
8901 
8902 		if ((un->un_remove_event != NULL) &&
8903 		    (ddi_remove_event_handler(un->un_remove_cb_id) !=
8904 		    DDI_SUCCESS)) {
8905 			/*
8906 			 * Note: We are returning here after having done
8907 			 * substantial cleanup above. This is consistent
8908 			 * with the legacy implementation but this may not
8909 			 * be the right thing to do.
8910 			 */
8911 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8912 			    "sd_dr_detach: Cannot cancel remove event\n");
8913 			goto err_remove_event;
8914 		}
8915 		un->un_remove_event = NULL;
8916 	}
8917 
8918 	/* Do not free the softstate if the callback routine is active */
8919 	sd_sync_with_callback(un);
8920 
8921 	cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8922 	cmlb_free_handle(&un->un_cmlbhandle);
8923 
8924 	/*
8925 	 * Hold the detach mutex here, to make sure that no other threads ever
8926 	 * can access a (partially) freed soft state structure.
8927 	 */
8928 	mutex_enter(&sd_detach_mutex);
8929 
8930 	/*
8931 	 * Clean up the soft state struct.
8932 	 * Cleanup is done in reverse order of allocs/inits.
8933 	 * At this point there should be no competing threads anymore.
8934 	 */
8935 
8936 	scsi_fm_fini(devp);
8937 
8938 	/*
8939 	 * Deallocate memory for SCSI FMA.
8940 	 */
8941 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8942 
8943 	/*
8944 	 * Unregister and free device id if it was not registered
8945 	 * by the transport.
8946 	 */
8947 	if (un->un_f_devid_transport_defined == FALSE)
8948 		ddi_devid_unregister(devi);
8949 
8950 	/*
8951 	 * free the devid structure if allocated before (by ddi_devid_init()
8952 	 * or ddi_devid_get()).
8953 	 */
8954 	if (un->un_devid) {
8955 		ddi_devid_free(un->un_devid);
8956 		un->un_devid = NULL;
8957 	}
8958 
8959 	/*
8960 	 * Destroy wmap cache if it exists.
8961 	 */
8962 	if (un->un_wm_cache != NULL) {
8963 		kmem_cache_destroy(un->un_wm_cache);
8964 		un->un_wm_cache = NULL;
8965 	}
8966 
8967 	/*
8968 	 * kstat cleanup is done in detach for all device types (4363169).
8969 	 * We do not want to fail detach if the device kstats are not deleted
8970 	 * since there is a confusion about the devo_refcnt for the device.
8971 	 * We just delete the kstats and let detach complete successfully.
8972 	 */
8973 	if (un->un_stats != NULL) {
8974 		kstat_delete(un->un_stats);
8975 		un->un_stats = NULL;
8976 	}
8977 	if (un->un_errstats != NULL) {
8978 		kstat_delete(un->un_errstats);
8979 		un->un_errstats = NULL;
8980 	}
8981 
8982 	/* Remove partition stats */
8983 	if (un->un_f_pkstats_enabled) {
8984 		for (i = 0; i < NSDMAP; i++) {
8985 			if (un->un_pstats[i] != NULL) {
8986 				kstat_delete(un->un_pstats[i]);
8987 				un->un_pstats[i] = NULL;
8988 			}
8989 		}
8990 	}
8991 
8992 	/* Remove xbuf registration */
8993 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8994 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8995 
8996 	/* Remove driver properties */
8997 	ddi_prop_remove_all(devi);
8998 
8999 	mutex_destroy(&un->un_pm_mutex);
9000 	cv_destroy(&un->un_pm_busy_cv);
9001 
9002 	cv_destroy(&un->un_wcc_cv);
9003 
9004 	/* Open/close semaphore */
9005 	sema_destroy(&un->un_semoclose);
9006 
9007 	/* Removable media condvar. */
9008 	cv_destroy(&un->un_state_cv);
9009 
9010 	/* Suspend/resume condvar. */
9011 	cv_destroy(&un->un_suspend_cv);
9012 	cv_destroy(&un->un_disk_busy_cv);
9013 
9014 	sd_free_rqs(un);
9015 
9016 	/* Free up soft state */
9017 	devp->sd_private = NULL;
9018 
9019 	bzero(un, sizeof (struct sd_lun));
9020 #ifndef XPV_HVM_DRIVER
9021 	ddi_soft_state_free(sd_state, instance);
9022 #endif /* !XPV_HVM_DRIVER */
9023 
9024 	mutex_exit(&sd_detach_mutex);
9025 
9026 	/* This frees up the INQUIRY data associated with the device. */
9027 	scsi_unprobe(devp);
9028 
9029 	/*
9030 	 * After successfully detaching an instance, we update the information
9031 	 * of how many luns have been attached in the relative target and
9032 	 * controller for parallel SCSI. This information is used when sd tries
9033 	 * to set the tagged queuing capability in HBA.
9034 	 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
9035 	 * check if the device is parallel SCSI. However, we don't need to
9036 	 * check here because we've already checked during attach. No device
9037 	 * that is not parallel SCSI is in the chain.
9038 	 */
9039 	if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9040 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
9041 	}
9042 
9043 	return (DDI_SUCCESS);
9044 
9045 err_notclosed:
9046 	mutex_exit(SD_MUTEX(un));
9047 
9048 err_stillbusy:
9049 	_NOTE(NO_COMPETING_THREADS_NOW);
9050 
9051 err_remove_event:
9052 	mutex_enter(&sd_detach_mutex);
9053 	un->un_detach_count--;
9054 	mutex_exit(&sd_detach_mutex);
9055 
9056 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9057 	return (DDI_FAILURE);
9058 }
9059 
9060 
9061 /*
9062  *    Function: sd_create_errstats
9063  *
9064  * Description: This routine instantiates the device error stats.
9065  *
9066  *		Note: During attach the stats are instantiated first so they are
9067  *		available for attach-time routines that utilize the driver
9068  *		iopath to send commands to the device. The stats are initialized
9069  *		separately so data obtained during some attach-time routines is
9070  *		available. (4362483)
9071  *
9072  *   Arguments: un - driver soft state (unit) structure
9073  *		instance - driver instance
9074  *
9075  *     Context: Kernel thread context
9076  */
9077 
9078 static void
9079 sd_create_errstats(struct sd_lun *un, int instance)
9080 {
9081 	struct	sd_errstats	*stp;
9082 	char	kstatmodule_err[KSTAT_STRLEN];
9083 	char	kstatname[KSTAT_STRLEN];
9084 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9085 
9086 	ASSERT(un != NULL);
9087 
9088 	if (un->un_errstats != NULL) {
9089 		return;
9090 	}
9091 
9092 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9093 	    "%serr", sd_label);
9094 	(void) snprintf(kstatname, sizeof (kstatname),
9095 	    "%s%d,err", sd_label, instance);
9096 
9097 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9098 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9099 
9100 	if (un->un_errstats == NULL) {
9101 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9102 		    "sd_create_errstats: Failed kstat_create\n");
9103 		return;
9104 	}
9105 
9106 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9107 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9108 	    KSTAT_DATA_UINT32);
9109 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9110 	    KSTAT_DATA_UINT32);
9111 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9112 	    KSTAT_DATA_UINT32);
9113 	kstat_named_init(&stp->sd_vid,		"Vendor",
9114 	    KSTAT_DATA_CHAR);
9115 	kstat_named_init(&stp->sd_pid,		"Product",
9116 	    KSTAT_DATA_CHAR);
9117 	kstat_named_init(&stp->sd_revision,	"Revision",
9118 	    KSTAT_DATA_CHAR);
9119 	kstat_named_init(&stp->sd_serial,	"Serial No",
9120 	    KSTAT_DATA_CHAR);
9121 	kstat_named_init(&stp->sd_capacity,	"Size",
9122 	    KSTAT_DATA_ULONGLONG);
9123 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9124 	    KSTAT_DATA_UINT32);
9125 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9126 	    KSTAT_DATA_UINT32);
9127 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9128 	    KSTAT_DATA_UINT32);
9129 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9130 	    KSTAT_DATA_UINT32);
9131 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9132 	    KSTAT_DATA_UINT32);
9133 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9134 	    KSTAT_DATA_UINT32);
9135 
9136 	un->un_errstats->ks_private = un;
9137 	un->un_errstats->ks_update  = nulldev;
9138 
9139 	kstat_install(un->un_errstats);
9140 }
9141 
9142 
9143 /*
9144  *    Function: sd_set_errstats
9145  *
9146  * Description: This routine sets the value of the vendor id, product id,
9147  *		revision, serial number, and capacity device error stats.
9148  *
9149  *		Note: During attach the stats are instantiated first so they are
9150  *		available for attach-time routines that utilize the driver
9151  *		iopath to send commands to the device. The stats are initialized
9152  *		separately so data obtained during some attach-time routines is
9153  *		available. (4362483)
9154  *
9155  *   Arguments: un - driver soft state (unit) structure
9156  *
9157  *     Context: Kernel thread context
9158  */
9159 
9160 static void
9161 sd_set_errstats(struct sd_lun *un)
9162 {
9163 	struct	sd_errstats	*stp;
9164 	char 			*sn;
9165 
9166 	ASSERT(un != NULL);
9167 	ASSERT(un->un_errstats != NULL);
9168 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9169 	ASSERT(stp != NULL);
9170 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9171 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9172 	(void) strncpy(stp->sd_revision.value.c,
9173 	    un->un_sd->sd_inq->inq_revision, 4);
9174 
9175 	/*
9176 	 * All the errstats are persistent across detach/attach,
9177 	 * so reset all the errstats here in case of the hot
9178 	 * replacement of disk drives, except for not changed
9179 	 * Sun qualified drives.
9180 	 */
9181 	if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9182 	    (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9183 	    sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9184 		stp->sd_softerrs.value.ui32 = 0;
9185 		stp->sd_harderrs.value.ui32 = 0;
9186 		stp->sd_transerrs.value.ui32 = 0;
9187 		stp->sd_rq_media_err.value.ui32 = 0;
9188 		stp->sd_rq_ntrdy_err.value.ui32 = 0;
9189 		stp->sd_rq_nodev_err.value.ui32 = 0;
9190 		stp->sd_rq_recov_err.value.ui32 = 0;
9191 		stp->sd_rq_illrq_err.value.ui32 = 0;
9192 		stp->sd_rq_pfa_err.value.ui32 = 0;
9193 	}
9194 
9195 	/*
9196 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9197 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9198 	 * (4376302))
9199 	 */
9200 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9201 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9202 		    sizeof (SD_INQUIRY(un)->inq_serial));
9203 	} else {
9204 		/*
9205 		 * Set the "Serial No" kstat for non-Sun qualified drives
9206 		 */
9207 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
9208 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
9209 		    INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
9210 			(void) strlcpy(stp->sd_serial.value.c, sn,
9211 			    sizeof (stp->sd_serial.value.c));
9212 			ddi_prop_free(sn);
9213 		}
9214 	}
9215 
9216 	if (un->un_f_blockcount_is_valid != TRUE) {
9217 		/*
9218 		 * Set capacity error stat to 0 for no media. This ensures
9219 		 * a valid capacity is displayed in response to 'iostat -E'
9220 		 * when no media is present in the device.
9221 		 */
9222 		stp->sd_capacity.value.ui64 = 0;
9223 	} else {
9224 		/*
9225 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9226 		 * capacity.
9227 		 *
9228 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9229 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9230 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9231 		 */
9232 		stp->sd_capacity.value.ui64 = (uint64_t)
9233 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9234 	}
9235 }
9236 
9237 
9238 /*
9239  *    Function: sd_set_pstats
9240  *
9241  * Description: This routine instantiates and initializes the partition
9242  *              stats for each partition with more than zero blocks.
9243  *		(4363169)
9244  *
9245  *   Arguments: un - driver soft state (unit) structure
9246  *
9247  *     Context: Kernel thread context
9248  */
9249 
9250 static void
9251 sd_set_pstats(struct sd_lun *un)
9252 {
9253 	char	kstatname[KSTAT_STRLEN];
9254 	int	instance;
9255 	int	i;
9256 	diskaddr_t	nblks = 0;
9257 	char	*partname = NULL;
9258 
9259 	ASSERT(un != NULL);
9260 
9261 	instance = ddi_get_instance(SD_DEVINFO(un));
9262 
9263 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
9264 	for (i = 0; i < NSDMAP; i++) {
9265 
9266 		if (cmlb_partinfo(un->un_cmlbhandle, i,
9267 		    &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9268 			continue;
9269 		mutex_enter(SD_MUTEX(un));
9270 
9271 		if ((un->un_pstats[i] == NULL) &&
9272 		    (nblks != 0)) {
9273 
9274 			(void) snprintf(kstatname, sizeof (kstatname),
9275 			    "%s%d,%s", sd_label, instance,
9276 			    partname);
9277 
9278 			un->un_pstats[i] = kstat_create(sd_label,
9279 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
9280 			    1, KSTAT_FLAG_PERSISTENT);
9281 			if (un->un_pstats[i] != NULL) {
9282 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9283 				kstat_install(un->un_pstats[i]);
9284 			}
9285 		}
9286 		mutex_exit(SD_MUTEX(un));
9287 	}
9288 }
9289 
9290 
9291 #if (defined(__fibre))
9292 /*
9293  *    Function: sd_init_event_callbacks
9294  *
9295  * Description: This routine initializes the insertion and removal event
9296  *		callbacks. (fibre only)
9297  *
9298  *   Arguments: un - driver soft state (unit) structure
9299  *
9300  *     Context: Kernel thread context
9301  */
9302 
9303 static void
9304 sd_init_event_callbacks(struct sd_lun *un)
9305 {
9306 	ASSERT(un != NULL);
9307 
9308 	if ((un->un_insert_event == NULL) &&
9309 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9310 	    &un->un_insert_event) == DDI_SUCCESS)) {
9311 		/*
9312 		 * Add the callback for an insertion event
9313 		 */
9314 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9315 		    un->un_insert_event, sd_event_callback, (void *)un,
9316 		    &(un->un_insert_cb_id));
9317 	}
9318 
9319 	if ((un->un_remove_event == NULL) &&
9320 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9321 	    &un->un_remove_event) == DDI_SUCCESS)) {
9322 		/*
9323 		 * Add the callback for a removal event
9324 		 */
9325 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9326 		    un->un_remove_event, sd_event_callback, (void *)un,
9327 		    &(un->un_remove_cb_id));
9328 	}
9329 }
9330 
9331 
9332 /*
9333  *    Function: sd_event_callback
9334  *
9335  * Description: This routine handles insert/remove events (photon). The
9336  *		state is changed to OFFLINE which can be used to supress
9337  *		error msgs. (fibre only)
9338  *
9339  *   Arguments: un - driver soft state (unit) structure
9340  *
9341  *     Context: Callout thread context
9342  */
9343 /* ARGSUSED */
9344 static void
9345 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9346     void *bus_impldata)
9347 {
9348 	struct sd_lun *un = (struct sd_lun *)arg;
9349 
9350 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9351 	if (event == un->un_insert_event) {
9352 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9353 		mutex_enter(SD_MUTEX(un));
9354 		if (un->un_state == SD_STATE_OFFLINE) {
9355 			if (un->un_last_state != SD_STATE_SUSPENDED) {
9356 				un->un_state = un->un_last_state;
9357 			} else {
9358 				/*
9359 				 * We have gone through SUSPEND/RESUME while
9360 				 * we were offline. Restore the last state
9361 				 */
9362 				un->un_state = un->un_save_state;
9363 			}
9364 		}
9365 		mutex_exit(SD_MUTEX(un));
9366 
9367 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9368 	} else if (event == un->un_remove_event) {
9369 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9370 		mutex_enter(SD_MUTEX(un));
9371 		/*
9372 		 * We need to handle an event callback that occurs during
9373 		 * the suspend operation, since we don't prevent it.
9374 		 */
9375 		if (un->un_state != SD_STATE_OFFLINE) {
9376 			if (un->un_state != SD_STATE_SUSPENDED) {
9377 				New_state(un, SD_STATE_OFFLINE);
9378 			} else {
9379 				un->un_last_state = SD_STATE_OFFLINE;
9380 			}
9381 		}
9382 		mutex_exit(SD_MUTEX(un));
9383 	} else {
9384 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9385 		    "!Unknown event\n");
9386 	}
9387 
9388 }
9389 #endif
9390 
9391 /*
9392  * Values related to caching mode page depending on whether the unit is ATAPI.
9393  */
9394 #define	SDC_CDB_GROUP(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9395 	CDB_GROUP1 : CDB_GROUP0)
9396 #define	SDC_HDRLEN(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9397 	MODE_HEADER_LENGTH_GRP2 : MODE_HEADER_LENGTH)
9398 /*
9399  * Use mode_cache_scsi3 to ensure we get all of the mode sense data, otherwise
9400  * the mode select will fail (mode_cache_scsi3 is a superset of mode_caching).
9401  */
9402 #define	SDC_BUFLEN(un) (SDC_HDRLEN(un) + MODE_BLK_DESC_LENGTH + \
9403 	sizeof (struct mode_cache_scsi3))
9404 
9405 static int
9406 sd_get_caching_mode_page(sd_ssc_t *ssc, uchar_t page_control, uchar_t **header,
9407     int *bdlen)
9408 {
9409 	struct sd_lun	*un = ssc->ssc_un;
9410 	struct mode_caching *mode_caching_page;
9411 	size_t		buflen = SDC_BUFLEN(un);
9412 	int		hdrlen = SDC_HDRLEN(un);
9413 	int		rval;
9414 
9415 	/*
9416 	 * Do a test unit ready, otherwise a mode sense may not work if this
9417 	 * is the first command sent to the device after boot.
9418 	 */
9419 	if (sd_send_scsi_TEST_UNIT_READY(ssc, 0) != 0)
9420 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9421 
9422 	/*
9423 	 * Allocate memory for the retrieved mode page and its headers.  Set
9424 	 * a pointer to the page itself.
9425 	 */
9426 	*header = kmem_zalloc(buflen, KM_SLEEP);
9427 
9428 	/* Get the information from the device */
9429 	rval = sd_send_scsi_MODE_SENSE(ssc, SDC_CDB_GROUP(un), *header, buflen,
9430 	    page_control | MODEPAGE_CACHING, SD_PATH_DIRECT);
9431 	if (rval != 0) {
9432 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, "%s: Mode Sense Failed\n",
9433 		    __func__);
9434 		goto mode_sense_failed;
9435 	}
9436 
9437 	/*
9438 	 * Determine size of Block Descriptors in order to locate
9439 	 * the mode page data. ATAPI devices return 0, SCSI devices
9440 	 * should return MODE_BLK_DESC_LENGTH.
9441 	 */
9442 	if (un->un_f_cfg_is_atapi == TRUE) {
9443 		struct mode_header_grp2 *mhp =
9444 		    (struct mode_header_grp2 *)(*header);
9445 		*bdlen = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9446 	} else {
9447 		*bdlen = ((struct mode_header *)(*header))->bdesc_length;
9448 	}
9449 
9450 	if (*bdlen > MODE_BLK_DESC_LENGTH) {
9451 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9452 		    "%s: Mode Sense returned invalid block descriptor length\n",
9453 		    __func__);
9454 		rval = EIO;
9455 		goto mode_sense_failed;
9456 	}
9457 
9458 	mode_caching_page = (struct mode_caching *)(*header + hdrlen + *bdlen);
9459 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9460 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9461 		    "%s: Mode Sense caching page code mismatch %d\n",
9462 		    __func__, mode_caching_page->mode_page.code);
9463 		rval = EIO;
9464 	}
9465 
9466 mode_sense_failed:
9467 	if (rval != 0) {
9468 		kmem_free(*header, buflen);
9469 		*header = NULL;
9470 		*bdlen = 0;
9471 	}
9472 	return (rval);
9473 }
9474 
9475 /*
9476  *    Function: sd_cache_control()
9477  *
9478  * Description: This routine is the driver entry point for setting
9479  *		read and write caching by modifying the WCE (write cache
9480  *		enable) and RCD (read cache disable) bits of mode
9481  *		page 8 (MODEPAGE_CACHING).
9482  *
9483  *   Arguments: ssc		- ssc contains pointer to driver soft state
9484  *				  (unit) structure for this target.
9485  *		rcd_flag	- flag for controlling the read cache
9486  *		wce_flag	- flag for controlling the write cache
9487  *
9488  * Return Code: EIO
9489  *		code returned by sd_send_scsi_MODE_SENSE and
9490  *		sd_send_scsi_MODE_SELECT
9491  *
9492  *     Context: Kernel Thread
9493  */
9494 
9495 static int
9496 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9497 {
9498 	struct sd_lun	*un = ssc->ssc_un;
9499 	struct mode_caching *mode_caching_page;
9500 	uchar_t		*header;
9501 	size_t		buflen = SDC_BUFLEN(un);
9502 	int		hdrlen = SDC_HDRLEN(un);
9503 	int		bdlen;
9504 	int		rval;
9505 
9506 	rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9507 	switch (rval) {
9508 	case 0:
9509 		/* Check the relevant bits on successful mode sense */
9510 		mode_caching_page = (struct mode_caching *)(header + hdrlen +
9511 		    bdlen);
9512 		if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9513 		    (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9514 		    (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9515 		    (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9516 			size_t sbuflen;
9517 			uchar_t save_pg;
9518 
9519 			/*
9520 			 * Construct select buffer length based on the
9521 			 * length of the sense data returned.
9522 			 */
9523 			sbuflen = hdrlen + bdlen + sizeof (struct mode_page) +
9524 			    (int)mode_caching_page->mode_page.length;
9525 
9526 			/* Set the caching bits as requested */
9527 			if (rcd_flag == SD_CACHE_ENABLE)
9528 				mode_caching_page->rcd = 0;
9529 			else if (rcd_flag == SD_CACHE_DISABLE)
9530 				mode_caching_page->rcd = 1;
9531 
9532 			if (wce_flag == SD_CACHE_ENABLE)
9533 				mode_caching_page->wce = 1;
9534 			else if (wce_flag == SD_CACHE_DISABLE)
9535 				mode_caching_page->wce = 0;
9536 
9537 			/*
9538 			 * Save the page if the mode sense says the
9539 			 * drive supports it.
9540 			 */
9541 			save_pg = mode_caching_page->mode_page.ps ?
9542 			    SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9543 
9544 			/* Clear reserved bits before mode select */
9545 			mode_caching_page->mode_page.ps = 0;
9546 
9547 			/*
9548 			 * Clear out mode header for mode select.
9549 			 * The rest of the retrieved page will be reused.
9550 			 */
9551 			bzero(header, hdrlen);
9552 
9553 			if (un->un_f_cfg_is_atapi == TRUE) {
9554 				struct mode_header_grp2 *mhp =
9555 				    (struct mode_header_grp2 *)header;
9556 				mhp->bdesc_length_hi = bdlen >> 8;
9557 				mhp->bdesc_length_lo = (uchar_t)bdlen & 0xff;
9558 			} else {
9559 				((struct mode_header *)header)->bdesc_length =
9560 				    bdlen;
9561 			}
9562 
9563 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9564 
9565 			/* Issue mode select to change the cache settings */
9566 			rval = sd_send_scsi_MODE_SELECT(ssc, SDC_CDB_GROUP(un),
9567 			    header, sbuflen, save_pg, SD_PATH_DIRECT);
9568 		}
9569 		kmem_free(header, buflen);
9570 		break;
9571 	case EIO:
9572 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9573 		break;
9574 	default:
9575 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9576 		break;
9577 	}
9578 
9579 	return (rval);
9580 }
9581 
9582 
9583 /*
9584  *    Function: sd_get_write_cache_enabled()
9585  *
9586  * Description: This routine is the driver entry point for determining if write
9587  *		caching is enabled.  It examines the WCE (write cache enable)
9588  *		bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9589  *		bits set to MODEPAGE_CURRENT.
9590  *
9591  *   Arguments: ssc		- ssc contains pointer to driver soft state
9592  *				  (unit) structure for this target.
9593  *		is_enabled	- pointer to int where write cache enabled state
9594  *				  is returned (non-zero -> write cache enabled)
9595  *
9596  * Return Code: EIO
9597  *		code returned by sd_send_scsi_MODE_SENSE
9598  *
9599  *     Context: Kernel Thread
9600  *
9601  * NOTE: If ioctl is added to disable write cache, this sequence should
9602  * be followed so that no locking is required for accesses to
9603  * un->un_f_write_cache_enabled:
9604  * 	do mode select to clear wce
9605  * 	do synchronize cache to flush cache
9606  * 	set un->un_f_write_cache_enabled = FALSE
9607  *
9608  * Conversely, an ioctl to enable the write cache should be done
9609  * in this order:
9610  * 	set un->un_f_write_cache_enabled = TRUE
9611  * 	do mode select to set wce
9612  */
9613 
9614 static int
9615 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9616 {
9617 	struct sd_lun	*un = ssc->ssc_un;
9618 	struct mode_caching *mode_caching_page;
9619 	uchar_t		*header;
9620 	size_t		buflen = SDC_BUFLEN(un);
9621 	int		hdrlen = SDC_HDRLEN(un);
9622 	int		bdlen;
9623 	int		rval;
9624 
9625 	/* In case of error, flag as enabled */
9626 	*is_enabled = TRUE;
9627 
9628 	rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9629 	switch (rval) {
9630 	case 0:
9631 		mode_caching_page = (struct mode_caching *)(header + hdrlen +
9632 		    bdlen);
9633 		*is_enabled = mode_caching_page->wce;
9634 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9635 		kmem_free(header, buflen);
9636 		break;
9637 	case EIO: {
9638 		/*
9639 		 * Some disks do not support Mode Sense(6), we
9640 		 * should ignore this kind of error (sense key is
9641 		 * 0x5 - illegal request).
9642 		 */
9643 		uint8_t *sensep;
9644 		int senlen;
9645 
9646 		sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9647 		senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9648 		    ssc->ssc_uscsi_cmd->uscsi_rqresid);
9649 
9650 		if (senlen > 0 &&
9651 		    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9652 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9653 		} else {
9654 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9655 		}
9656 		break;
9657 	}
9658 	default:
9659 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9660 		break;
9661 	}
9662 
9663 	return (rval);
9664 }
9665 
9666 /*
9667  *    Function: sd_get_write_cache_changeable()
9668  *
9669  * Description: This routine is the driver entry point for determining if write
9670  *		caching is changeable.  It examines the WCE (write cache enable)
9671  *		bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9672  *		bits set to MODEPAGE_CHANGEABLE.
9673  *
9674  *   Arguments: ssc		- ssc contains pointer to driver soft state
9675  *				  (unit) structure for this target.
9676  *		is_changeable	- pointer to int where write cache changeable
9677  *				  state is returned (non-zero -> write cache
9678  *				  changeable)
9679  *
9680  *     Context: Kernel Thread
9681  */
9682 
9683 static void
9684 sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable)
9685 {
9686 	struct sd_lun	*un = ssc->ssc_un;
9687 	struct mode_caching *mode_caching_page;
9688 	uchar_t		*header;
9689 	size_t		buflen = SDC_BUFLEN(un);
9690 	int		hdrlen = SDC_HDRLEN(un);
9691 	int		bdlen;
9692 	int		rval;
9693 
9694 	/* In case of error, flag as enabled */
9695 	*is_changeable = TRUE;
9696 
9697 	rval = sd_get_caching_mode_page(ssc, MODEPAGE_CHANGEABLE, &header,
9698 	    &bdlen);
9699 	switch (rval) {
9700 	case 0:
9701 		mode_caching_page = (struct mode_caching *)(header + hdrlen +
9702 		    bdlen);
9703 		*is_changeable = mode_caching_page->wce;
9704 		kmem_free(header, buflen);
9705 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9706 		break;
9707 	case EIO:
9708 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9709 		break;
9710 	default:
9711 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9712 		break;
9713 	}
9714 }
9715 
9716 /*
9717  *    Function: sd_get_nv_sup()
9718  *
9719  * Description: This routine is the driver entry point for
9720  * determining whether non-volatile cache is supported. This
9721  * determination process works as follows:
9722  *
9723  * 1. sd first queries sd.conf on whether
9724  * suppress_cache_flush bit is set for this device.
9725  *
9726  * 2. if not there, then queries the internal disk table.
9727  *
9728  * 3. if either sd.conf or internal disk table specifies
9729  * cache flush be suppressed, we don't bother checking
9730  * NV_SUP bit.
9731  *
9732  * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9733  * the optional INQUIRY VPD page 0x86. If the device
9734  * supports VPD page 0x86, sd examines the NV_SUP
9735  * (non-volatile cache support) bit in the INQUIRY VPD page
9736  * 0x86:
9737  *   o If NV_SUP bit is set, sd assumes the device has a
9738  *   non-volatile cache and set the
9739  *   un_f_sync_nv_supported to TRUE.
9740  *   o Otherwise cache is not non-volatile,
9741  *   un_f_sync_nv_supported is set to FALSE.
9742  *
9743  * Arguments: un - driver soft state (unit) structure
9744  *
9745  * Return Code:
9746  *
9747  *     Context: Kernel Thread
9748  */
9749 
9750 static void
9751 sd_get_nv_sup(sd_ssc_t *ssc)
9752 {
9753 	int		rval		= 0;
9754 	uchar_t		*inq86		= NULL;
9755 	size_t		inq86_len	= MAX_INQUIRY_SIZE;
9756 	size_t		inq86_resid	= 0;
9757 	struct		dk_callback *dkc;
9758 	struct sd_lun	*un;
9759 
9760 	ASSERT(ssc != NULL);
9761 	un = ssc->ssc_un;
9762 	ASSERT(un != NULL);
9763 
9764 	mutex_enter(SD_MUTEX(un));
9765 
9766 	/*
9767 	 * Be conservative on the device's support of
9768 	 * SYNC_NV bit: un_f_sync_nv_supported is
9769 	 * initialized to be false.
9770 	 */
9771 	un->un_f_sync_nv_supported = FALSE;
9772 
9773 	/*
9774 	 * If either sd.conf or internal disk table
9775 	 * specifies cache flush be suppressed, then
9776 	 * we don't bother checking NV_SUP bit.
9777 	 */
9778 	if (un->un_f_suppress_cache_flush == TRUE) {
9779 		mutex_exit(SD_MUTEX(un));
9780 		return;
9781 	}
9782 
9783 	if (sd_check_vpd_page_support(ssc) == 0 &&
9784 	    un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9785 		mutex_exit(SD_MUTEX(un));
9786 		/* collect page 86 data if available */
9787 		inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9788 
9789 		rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9790 		    0x01, 0x86, &inq86_resid);
9791 
9792 		if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9793 			SD_TRACE(SD_LOG_COMMON, un,
9794 			    "sd_get_nv_sup: \
9795 			    successfully get VPD page: %x \
9796 			    PAGE LENGTH: %x BYTE 6: %x\n",
9797 			    inq86[1], inq86[3], inq86[6]);
9798 
9799 			mutex_enter(SD_MUTEX(un));
9800 			/*
9801 			 * check the value of NV_SUP bit: only if the device
9802 			 * reports NV_SUP bit to be 1, the
9803 			 * un_f_sync_nv_supported bit will be set to true.
9804 			 */
9805 			if (inq86[6] & SD_VPD_NV_SUP) {
9806 				un->un_f_sync_nv_supported = TRUE;
9807 			}
9808 			mutex_exit(SD_MUTEX(un));
9809 		} else if (rval != 0) {
9810 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9811 		}
9812 
9813 		kmem_free(inq86, inq86_len);
9814 	} else {
9815 		mutex_exit(SD_MUTEX(un));
9816 	}
9817 
9818 	/*
9819 	 * Send a SYNC CACHE command to check whether
9820 	 * SYNC_NV bit is supported. This command should have
9821 	 * un_f_sync_nv_supported set to correct value.
9822 	 */
9823 	mutex_enter(SD_MUTEX(un));
9824 	if (un->un_f_sync_nv_supported) {
9825 		mutex_exit(SD_MUTEX(un));
9826 		dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9827 		dkc->dkc_flag = FLUSH_VOLATILE;
9828 		(void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9829 
9830 		/*
9831 		 * Send a TEST UNIT READY command to the device. This should
9832 		 * clear any outstanding UNIT ATTENTION that may be present.
9833 		 */
9834 		rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9835 		if (rval != 0)
9836 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9837 
9838 		kmem_free(dkc, sizeof (struct dk_callback));
9839 	} else {
9840 		mutex_exit(SD_MUTEX(un));
9841 	}
9842 
9843 	SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9844 	    un_f_suppress_cache_flush is set to %d\n",
9845 	    un->un_f_suppress_cache_flush);
9846 }
9847 
9848 /*
9849  *    Function: sd_make_device
9850  *
9851  * Description: Utility routine to return the Solaris device number from
9852  *		the data in the device's dev_info structure.
9853  *
9854  * Return Code: The Solaris device number
9855  *
9856  *     Context: Any
9857  */
9858 
9859 static dev_t
9860 sd_make_device(dev_info_t *devi)
9861 {
9862 	return (makedevice(ddi_driver_major(devi),
9863 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
9864 }
9865 
9866 
9867 /*
9868  *    Function: sd_pm_entry
9869  *
9870  * Description: Called at the start of a new command to manage power
9871  *		and busy status of a device. This includes determining whether
9872  *		the current power state of the device is sufficient for
9873  *		performing the command or whether it must be changed.
9874  *		The PM framework is notified appropriately.
9875  *		Only with a return status of DDI_SUCCESS will the
9876  *		component be busy to the framework.
9877  *
9878  *		All callers of sd_pm_entry must check the return status
9879  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
9880  *		of DDI_FAILURE indicates the device failed to power up.
9881  *		In this case un_pm_count has been adjusted so the result
9882  *		on exit is still powered down, ie. count is less than 0.
9883  *		Calling sd_pm_exit with this count value hits an ASSERT.
9884  *
9885  * Return Code: DDI_SUCCESS or DDI_FAILURE
9886  *
9887  *     Context: Kernel thread context.
9888  */
9889 
9890 static int
9891 sd_pm_entry(struct sd_lun *un)
9892 {
9893 	int return_status = DDI_SUCCESS;
9894 
9895 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9896 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9897 
9898 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9899 
9900 	if (un->un_f_pm_is_enabled == FALSE) {
9901 		SD_TRACE(SD_LOG_IO_PM, un,
9902 		    "sd_pm_entry: exiting, PM not enabled\n");
9903 		return (return_status);
9904 	}
9905 
9906 	/*
9907 	 * Just increment a counter if PM is enabled. On the transition from
9908 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9909 	 * the count with each IO and mark the device as idle when the count
9910 	 * hits 0.
9911 	 *
9912 	 * If the count is less than 0 the device is powered down. If a powered
9913 	 * down device is successfully powered up then the count must be
9914 	 * incremented to reflect the power up. Note that it'll get incremented
9915 	 * a second time to become busy.
9916 	 *
9917 	 * Because the following has the potential to change the device state
9918 	 * and must release the un_pm_mutex to do so, only one thread can be
9919 	 * allowed through at a time.
9920 	 */
9921 
9922 	mutex_enter(&un->un_pm_mutex);
9923 	while (un->un_pm_busy == TRUE) {
9924 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9925 	}
9926 	un->un_pm_busy = TRUE;
9927 
9928 	if (un->un_pm_count < 1) {
9929 
9930 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9931 
9932 		/*
9933 		 * Indicate we are now busy so the framework won't attempt to
9934 		 * power down the device. This call will only fail if either
9935 		 * we passed a bad component number or the device has no
9936 		 * components. Neither of these should ever happen.
9937 		 */
9938 		mutex_exit(&un->un_pm_mutex);
9939 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
9940 		ASSERT(return_status == DDI_SUCCESS);
9941 
9942 		mutex_enter(&un->un_pm_mutex);
9943 
9944 		if (un->un_pm_count < 0) {
9945 			mutex_exit(&un->un_pm_mutex);
9946 
9947 			SD_TRACE(SD_LOG_IO_PM, un,
9948 			    "sd_pm_entry: power up component\n");
9949 
9950 			/*
9951 			 * pm_raise_power will cause sdpower to be called
9952 			 * which brings the device power level to the
9953 			 * desired state, If successful, un_pm_count and
9954 			 * un_power_level will be updated appropriately.
9955 			 */
9956 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
9957 			    SD_PM_STATE_ACTIVE(un));
9958 
9959 			mutex_enter(&un->un_pm_mutex);
9960 
9961 			if (return_status != DDI_SUCCESS) {
9962 				/*
9963 				 * Power up failed.
9964 				 * Idle the device and adjust the count
9965 				 * so the result on exit is that we're
9966 				 * still powered down, ie. count is less than 0.
9967 				 */
9968 				SD_TRACE(SD_LOG_IO_PM, un,
9969 				    "sd_pm_entry: power up failed,"
9970 				    " idle the component\n");
9971 
9972 				(void) pm_idle_component(SD_DEVINFO(un), 0);
9973 				un->un_pm_count--;
9974 			} else {
9975 				/*
9976 				 * Device is powered up, verify the
9977 				 * count is non-negative.
9978 				 * This is debug only.
9979 				 */
9980 				ASSERT(un->un_pm_count == 0);
9981 			}
9982 		}
9983 
9984 		if (return_status == DDI_SUCCESS) {
9985 			/*
9986 			 * For performance, now that the device has been tagged
9987 			 * as busy, and it's known to be powered up, update the
9988 			 * chain types to use jump tables that do not include
9989 			 * pm. This significantly lowers the overhead and
9990 			 * therefore improves performance.
9991 			 */
9992 
9993 			mutex_exit(&un->un_pm_mutex);
9994 			mutex_enter(SD_MUTEX(un));
9995 			SD_TRACE(SD_LOG_IO_PM, un,
9996 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
9997 			    un->un_uscsi_chain_type);
9998 
9999 			if (un->un_f_non_devbsize_supported) {
10000 				un->un_buf_chain_type =
10001 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
10002 			} else {
10003 				un->un_buf_chain_type =
10004 				    SD_CHAIN_INFO_DISK_NO_PM;
10005 			}
10006 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
10007 
10008 			SD_TRACE(SD_LOG_IO_PM, un,
10009 			    "             changed  uscsi_chain_type to   %d\n",
10010 			    un->un_uscsi_chain_type);
10011 			mutex_exit(SD_MUTEX(un));
10012 			mutex_enter(&un->un_pm_mutex);
10013 
10014 			if (un->un_pm_idle_timeid == NULL) {
10015 				/* 300 ms. */
10016 				un->un_pm_idle_timeid =
10017 				    timeout(sd_pm_idletimeout_handler, un,
10018 				    (drv_usectohz((clock_t)300000)));
10019 				/*
10020 				 * Include an extra call to busy which keeps the
10021 				 * device busy with-respect-to the PM layer
10022 				 * until the timer fires, at which time it'll
10023 				 * get the extra idle call.
10024 				 */
10025 				(void) pm_busy_component(SD_DEVINFO(un), 0);
10026 			}
10027 		}
10028 	}
10029 	un->un_pm_busy = FALSE;
10030 	/* Next... */
10031 	cv_signal(&un->un_pm_busy_cv);
10032 
10033 	un->un_pm_count++;
10034 
10035 	SD_TRACE(SD_LOG_IO_PM, un,
10036 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
10037 
10038 	mutex_exit(&un->un_pm_mutex);
10039 
10040 	return (return_status);
10041 }
10042 
10043 
10044 /*
10045  *    Function: sd_pm_exit
10046  *
10047  * Description: Called at the completion of a command to manage busy
10048  *		status for the device. If the device becomes idle the
10049  *		PM framework is notified.
10050  *
10051  *     Context: Kernel thread context
10052  */
10053 
10054 static void
10055 sd_pm_exit(struct sd_lun *un)
10056 {
10057 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10058 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10059 
10060 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10061 
10062 	/*
10063 	 * After attach the following flag is only read, so don't
10064 	 * take the penalty of acquiring a mutex for it.
10065 	 */
10066 	if (un->un_f_pm_is_enabled == TRUE) {
10067 
10068 		mutex_enter(&un->un_pm_mutex);
10069 		un->un_pm_count--;
10070 
10071 		SD_TRACE(SD_LOG_IO_PM, un,
10072 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10073 
10074 		ASSERT(un->un_pm_count >= 0);
10075 		if (un->un_pm_count == 0) {
10076 			mutex_exit(&un->un_pm_mutex);
10077 
10078 			SD_TRACE(SD_LOG_IO_PM, un,
10079 			    "sd_pm_exit: idle component\n");
10080 
10081 			(void) pm_idle_component(SD_DEVINFO(un), 0);
10082 
10083 		} else {
10084 			mutex_exit(&un->un_pm_mutex);
10085 		}
10086 	}
10087 
10088 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10089 }
10090 
10091 
10092 /*
10093  *    Function: sdopen
10094  *
10095  * Description: Driver's open(9e) entry point function.
10096  *
10097  *   Arguments: dev_i   - pointer to device number
10098  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10099  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10100  *		cred_p  - user credential pointer
10101  *
10102  * Return Code: EINVAL
10103  *		ENXIO
10104  *		EIO
10105  *		EROFS
10106  *		EBUSY
10107  *
10108  *     Context: Kernel thread context
10109  */
10110 /* ARGSUSED */
10111 static int
10112 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10113 {
10114 	struct sd_lun	*un;
10115 	int		nodelay;
10116 	int		part;
10117 	uint64_t	partmask;
10118 	int		instance;
10119 	dev_t		dev;
10120 	int		rval = EIO;
10121 	diskaddr_t	nblks = 0;
10122 	diskaddr_t	label_cap;
10123 
10124 	/* Validate the open type */
10125 	if (otyp >= OTYPCNT) {
10126 		return (EINVAL);
10127 	}
10128 
10129 	dev = *dev_p;
10130 	instance = SDUNIT(dev);
10131 	mutex_enter(&sd_detach_mutex);
10132 
10133 	/*
10134 	 * Fail the open if there is no softstate for the instance, or
10135 	 * if another thread somewhere is trying to detach the instance.
10136 	 */
10137 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10138 	    (un->un_detach_count != 0)) {
10139 		mutex_exit(&sd_detach_mutex);
10140 		/*
10141 		 * The probe cache only needs to be cleared when open (9e) fails
10142 		 * with ENXIO (4238046).
10143 		 */
10144 		/*
10145 		 * un-conditionally clearing probe cache is ok with
10146 		 * separate sd/ssd binaries
10147 		 * x86 platform can be an issue with both parallel
10148 		 * and fibre in 1 binary
10149 		 */
10150 		sd_scsi_clear_probe_cache();
10151 		return (ENXIO);
10152 	}
10153 
10154 	/*
10155 	 * The un_layer_count is to prevent another thread in specfs from
10156 	 * trying to detach the instance, which can happen when we are
10157 	 * called from a higher-layer driver instead of thru specfs.
10158 	 * This will not be needed when DDI provides a layered driver
10159 	 * interface that allows specfs to know that an instance is in
10160 	 * use by a layered driver & should not be detached.
10161 	 *
10162 	 * Note: the semantics for layered driver opens are exactly one
10163 	 * close for every open.
10164 	 */
10165 	if (otyp == OTYP_LYR) {
10166 		un->un_layer_count++;
10167 	}
10168 
10169 	/*
10170 	 * Keep a count of the current # of opens in progress. This is because
10171 	 * some layered drivers try to call us as a regular open. This can
10172 	 * cause problems that we cannot prevent, however by keeping this count
10173 	 * we can at least keep our open and detach routines from racing against
10174 	 * each other under such conditions.
10175 	 */
10176 	un->un_opens_in_progress++;
10177 	mutex_exit(&sd_detach_mutex);
10178 
10179 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
10180 	part	 = SDPART(dev);
10181 	partmask = 1 << part;
10182 
10183 	/*
10184 	 * We use a semaphore here in order to serialize
10185 	 * open and close requests on the device.
10186 	 */
10187 	sema_p(&un->un_semoclose);
10188 
10189 	mutex_enter(SD_MUTEX(un));
10190 
10191 	/*
10192 	 * All device accesses go thru sdstrategy() where we check
10193 	 * on suspend status but there could be a scsi_poll command,
10194 	 * which bypasses sdstrategy(), so we need to check pm
10195 	 * status.
10196 	 */
10197 
10198 	if (!nodelay) {
10199 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10200 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10201 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10202 		}
10203 
10204 		mutex_exit(SD_MUTEX(un));
10205 		if (sd_pm_entry(un) != DDI_SUCCESS) {
10206 			rval = EIO;
10207 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10208 			    "sdopen: sd_pm_entry failed\n");
10209 			goto open_failed_with_pm;
10210 		}
10211 		mutex_enter(SD_MUTEX(un));
10212 	}
10213 
10214 	/* check for previous exclusive open */
10215 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10216 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10217 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10218 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10219 
10220 	if (un->un_exclopen & (partmask)) {
10221 		goto excl_open_fail;
10222 	}
10223 
10224 	if (flag & FEXCL) {
10225 		int i;
10226 		if (un->un_ocmap.lyropen[part]) {
10227 			goto excl_open_fail;
10228 		}
10229 		for (i = 0; i < (OTYPCNT - 1); i++) {
10230 			if (un->un_ocmap.regopen[i] & (partmask)) {
10231 				goto excl_open_fail;
10232 			}
10233 		}
10234 	}
10235 
10236 	/*
10237 	 * Check the write permission if this is a removable media device,
10238 	 * NDELAY has not been set, and writable permission is requested.
10239 	 *
10240 	 * Note: If NDELAY was set and this is write-protected media the WRITE
10241 	 * attempt will fail with EIO as part of the I/O processing. This is a
10242 	 * more permissive implementation that allows the open to succeed and
10243 	 * WRITE attempts to fail when appropriate.
10244 	 */
10245 	if (un->un_f_chk_wp_open) {
10246 		if ((flag & FWRITE) && (!nodelay)) {
10247 			mutex_exit(SD_MUTEX(un));
10248 			/*
10249 			 * Defer the check for write permission on writable
10250 			 * DVD drive till sdstrategy and will not fail open even
10251 			 * if FWRITE is set as the device can be writable
10252 			 * depending upon the media and the media can change
10253 			 * after the call to open().
10254 			 */
10255 			if (un->un_f_dvdram_writable_device == FALSE) {
10256 				if (ISCD(un) || sr_check_wp(dev)) {
10257 				rval = EROFS;
10258 				mutex_enter(SD_MUTEX(un));
10259 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10260 				    "write to cd or write protected media\n");
10261 				goto open_fail;
10262 				}
10263 			}
10264 			mutex_enter(SD_MUTEX(un));
10265 		}
10266 	}
10267 
10268 	/*
10269 	 * If opening in NDELAY/NONBLOCK mode, just return.
10270 	 * Check if disk is ready and has a valid geometry later.
10271 	 */
10272 	if (!nodelay) {
10273 		sd_ssc_t	*ssc;
10274 
10275 		mutex_exit(SD_MUTEX(un));
10276 		ssc = sd_ssc_init(un);
10277 		rval = sd_ready_and_valid(ssc, part);
10278 		sd_ssc_fini(ssc);
10279 		mutex_enter(SD_MUTEX(un));
10280 		/*
10281 		 * Fail if device is not ready or if the number of disk
10282 		 * blocks is zero or negative for non CD devices.
10283 		 */
10284 
10285 		nblks = 0;
10286 
10287 		if (rval == SD_READY_VALID && (!ISCD(un))) {
10288 			/* if cmlb_partinfo fails, nblks remains 0 */
10289 			mutex_exit(SD_MUTEX(un));
10290 			(void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10291 			    NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10292 			mutex_enter(SD_MUTEX(un));
10293 		}
10294 
10295 		if ((rval != SD_READY_VALID) ||
10296 		    (!ISCD(un) && nblks <= 0)) {
10297 			rval = un->un_f_has_removable_media ? ENXIO : EIO;
10298 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10299 			    "device not ready or invalid disk block value\n");
10300 			goto open_fail;
10301 		}
10302 #if defined(__i386) || defined(__amd64)
10303 	} else {
10304 		uchar_t *cp;
10305 		/*
10306 		 * x86 requires special nodelay handling, so that p0 is
10307 		 * always defined and accessible.
10308 		 * Invalidate geometry only if device is not already open.
10309 		 */
10310 		cp = &un->un_ocmap.chkd[0];
10311 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10312 			if (*cp != (uchar_t)0) {
10313 				break;
10314 			}
10315 			cp++;
10316 		}
10317 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10318 			mutex_exit(SD_MUTEX(un));
10319 			cmlb_invalidate(un->un_cmlbhandle,
10320 			    (void *)SD_PATH_DIRECT);
10321 			mutex_enter(SD_MUTEX(un));
10322 		}
10323 
10324 #endif
10325 	}
10326 
10327 	if (otyp == OTYP_LYR) {
10328 		un->un_ocmap.lyropen[part]++;
10329 	} else {
10330 		un->un_ocmap.regopen[otyp] |= partmask;
10331 	}
10332 
10333 	/* Set up open and exclusive open flags */
10334 	if (flag & FEXCL) {
10335 		un->un_exclopen |= (partmask);
10336 	}
10337 
10338 	/*
10339 	 * If the lun is EFI labeled and lun capacity is greater than the
10340 	 * capacity contained in the label, log a sys-event to notify the
10341 	 * interested module.
10342 	 * To avoid an infinite loop of logging sys-event, we only log the
10343 	 * event when the lun is not opened in NDELAY mode. The event handler
10344 	 * should open the lun in NDELAY mode.
10345 	 */
10346 	if (!nodelay) {
10347 		mutex_exit(SD_MUTEX(un));
10348 		if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10349 		    (void*)SD_PATH_DIRECT) == 0) {
10350 			mutex_enter(SD_MUTEX(un));
10351 			if (un->un_f_blockcount_is_valid &&
10352 			    un->un_blockcount > label_cap &&
10353 			    un->un_f_expnevent == B_FALSE) {
10354 				un->un_f_expnevent = B_TRUE;
10355 				mutex_exit(SD_MUTEX(un));
10356 				sd_log_lun_expansion_event(un,
10357 				    (nodelay ? KM_NOSLEEP : KM_SLEEP));
10358 				mutex_enter(SD_MUTEX(un));
10359 			}
10360 		} else {
10361 			mutex_enter(SD_MUTEX(un));
10362 		}
10363 	}
10364 
10365 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10366 	    "open of part %d type %d\n", part, otyp);
10367 
10368 	mutex_exit(SD_MUTEX(un));
10369 	if (!nodelay) {
10370 		sd_pm_exit(un);
10371 	}
10372 
10373 	sema_v(&un->un_semoclose);
10374 
10375 	mutex_enter(&sd_detach_mutex);
10376 	un->un_opens_in_progress--;
10377 	mutex_exit(&sd_detach_mutex);
10378 
10379 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10380 	return (DDI_SUCCESS);
10381 
10382 excl_open_fail:
10383 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10384 	rval = EBUSY;
10385 
10386 open_fail:
10387 	mutex_exit(SD_MUTEX(un));
10388 
10389 	/*
10390 	 * On a failed open we must exit the pm management.
10391 	 */
10392 	if (!nodelay) {
10393 		sd_pm_exit(un);
10394 	}
10395 open_failed_with_pm:
10396 	sema_v(&un->un_semoclose);
10397 
10398 	mutex_enter(&sd_detach_mutex);
10399 	un->un_opens_in_progress--;
10400 	if (otyp == OTYP_LYR) {
10401 		un->un_layer_count--;
10402 	}
10403 	mutex_exit(&sd_detach_mutex);
10404 
10405 	return (rval);
10406 }
10407 
10408 
10409 /*
10410  *    Function: sdclose
10411  *
10412  * Description: Driver's close(9e) entry point function.
10413  *
10414  *   Arguments: dev    - device number
10415  *		flag   - file status flag, informational only
10416  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10417  *		cred_p - user credential pointer
10418  *
10419  * Return Code: ENXIO
10420  *
10421  *     Context: Kernel thread context
10422  */
10423 /* ARGSUSED */
10424 static int
10425 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10426 {
10427 	struct sd_lun	*un;
10428 	uchar_t		*cp;
10429 	int		part;
10430 	int		nodelay;
10431 	int		rval = 0;
10432 
10433 	/* Validate the open type */
10434 	if (otyp >= OTYPCNT) {
10435 		return (ENXIO);
10436 	}
10437 
10438 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10439 		return (ENXIO);
10440 	}
10441 
10442 	part = SDPART(dev);
10443 	nodelay = flag & (FNDELAY | FNONBLOCK);
10444 
10445 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10446 	    "sdclose: close of part %d type %d\n", part, otyp);
10447 
10448 	/*
10449 	 * We use a semaphore here in order to serialize
10450 	 * open and close requests on the device.
10451 	 */
10452 	sema_p(&un->un_semoclose);
10453 
10454 	mutex_enter(SD_MUTEX(un));
10455 
10456 	/* Don't proceed if power is being changed. */
10457 	while (un->un_state == SD_STATE_PM_CHANGING) {
10458 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10459 	}
10460 
10461 	if (un->un_exclopen & (1 << part)) {
10462 		un->un_exclopen &= ~(1 << part);
10463 	}
10464 
10465 	/* Update the open partition map */
10466 	if (otyp == OTYP_LYR) {
10467 		un->un_ocmap.lyropen[part] -= 1;
10468 	} else {
10469 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10470 	}
10471 
10472 	cp = &un->un_ocmap.chkd[0];
10473 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10474 		if (*cp != NULL) {
10475 			break;
10476 		}
10477 		cp++;
10478 	}
10479 
10480 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10481 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10482 
10483 		/*
10484 		 * We avoid persistance upon the last close, and set
10485 		 * the throttle back to the maximum.
10486 		 */
10487 		un->un_throttle = un->un_saved_throttle;
10488 
10489 		if (un->un_state == SD_STATE_OFFLINE) {
10490 			if (un->un_f_is_fibre == FALSE) {
10491 				scsi_log(SD_DEVINFO(un), sd_label,
10492 				    CE_WARN, "offline\n");
10493 			}
10494 			mutex_exit(SD_MUTEX(un));
10495 			cmlb_invalidate(un->un_cmlbhandle,
10496 			    (void *)SD_PATH_DIRECT);
10497 			mutex_enter(SD_MUTEX(un));
10498 
10499 		} else {
10500 			/*
10501 			 * Flush any outstanding writes in NVRAM cache.
10502 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10503 			 * cmd, it may not work for non-Pluto devices.
10504 			 * SYNCHRONIZE CACHE is not required for removables,
10505 			 * except DVD-RAM drives.
10506 			 *
10507 			 * Also note: because SYNCHRONIZE CACHE is currently
10508 			 * the only command issued here that requires the
10509 			 * drive be powered up, only do the power up before
10510 			 * sending the Sync Cache command. If additional
10511 			 * commands are added which require a powered up
10512 			 * drive, the following sequence may have to change.
10513 			 *
10514 			 * And finally, note that parallel SCSI on SPARC
10515 			 * only issues a Sync Cache to DVD-RAM, a newly
10516 			 * supported device.
10517 			 */
10518 #if defined(__i386) || defined(__amd64)
10519 			if ((un->un_f_sync_cache_supported &&
10520 			    un->un_f_sync_cache_required) ||
10521 			    un->un_f_dvdram_writable_device == TRUE) {
10522 #else
10523 			if (un->un_f_dvdram_writable_device == TRUE) {
10524 #endif
10525 				mutex_exit(SD_MUTEX(un));
10526 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10527 					rval =
10528 					    sd_send_scsi_SYNCHRONIZE_CACHE(un,
10529 					    NULL);
10530 					/* ignore error if not supported */
10531 					if (rval == ENOTSUP) {
10532 						rval = 0;
10533 					} else if (rval != 0) {
10534 						rval = EIO;
10535 					}
10536 					sd_pm_exit(un);
10537 				} else {
10538 					rval = EIO;
10539 				}
10540 				mutex_enter(SD_MUTEX(un));
10541 			}
10542 
10543 			/*
10544 			 * For devices which supports DOOR_LOCK, send an ALLOW
10545 			 * MEDIA REMOVAL command, but don't get upset if it
10546 			 * fails. We need to raise the power of the drive before
10547 			 * we can call sd_send_scsi_DOORLOCK()
10548 			 */
10549 			if (un->un_f_doorlock_supported) {
10550 				mutex_exit(SD_MUTEX(un));
10551 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10552 					sd_ssc_t	*ssc;
10553 
10554 					ssc = sd_ssc_init(un);
10555 					rval = sd_send_scsi_DOORLOCK(ssc,
10556 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10557 					if (rval != 0)
10558 						sd_ssc_assessment(ssc,
10559 						    SD_FMT_IGNORE);
10560 					sd_ssc_fini(ssc);
10561 
10562 					sd_pm_exit(un);
10563 					if (ISCD(un) && (rval != 0) &&
10564 					    (nodelay != 0)) {
10565 						rval = ENXIO;
10566 					}
10567 				} else {
10568 					rval = EIO;
10569 				}
10570 				mutex_enter(SD_MUTEX(un));
10571 			}
10572 
10573 			/*
10574 			 * If a device has removable media, invalidate all
10575 			 * parameters related to media, such as geometry,
10576 			 * blocksize, and blockcount.
10577 			 */
10578 			if (un->un_f_has_removable_media) {
10579 				sr_ejected(un);
10580 			}
10581 
10582 			/*
10583 			 * Destroy the cache (if it exists) which was
10584 			 * allocated for the write maps since this is
10585 			 * the last close for this media.
10586 			 */
10587 			if (un->un_wm_cache) {
10588 				/*
10589 				 * Check if there are pending commands.
10590 				 * and if there are give a warning and
10591 				 * do not destroy the cache.
10592 				 */
10593 				if (un->un_ncmds_in_driver > 0) {
10594 					scsi_log(SD_DEVINFO(un),
10595 					    sd_label, CE_WARN,
10596 					    "Unable to clean up memory "
10597 					    "because of pending I/O\n");
10598 				} else {
10599 					kmem_cache_destroy(
10600 					    un->un_wm_cache);
10601 					un->un_wm_cache = NULL;
10602 				}
10603 			}
10604 		}
10605 	}
10606 
10607 	mutex_exit(SD_MUTEX(un));
10608 	sema_v(&un->un_semoclose);
10609 
10610 	if (otyp == OTYP_LYR) {
10611 		mutex_enter(&sd_detach_mutex);
10612 		/*
10613 		 * The detach routine may run when the layer count
10614 		 * drops to zero.
10615 		 */
10616 		un->un_layer_count--;
10617 		mutex_exit(&sd_detach_mutex);
10618 	}
10619 
10620 	return (rval);
10621 }
10622 
10623 
10624 /*
10625  *    Function: sd_ready_and_valid
10626  *
10627  * Description: Test if device is ready and has a valid geometry.
10628  *
10629  *   Arguments: ssc - sd_ssc_t will contain un
10630  *		un  - driver soft state (unit) structure
10631  *
10632  * Return Code: SD_READY_VALID		ready and valid label
10633  *		SD_NOT_READY_VALID	not ready, no label
10634  *		SD_RESERVED_BY_OTHERS	reservation conflict
10635  *
10636  *     Context: Never called at interrupt context.
10637  */
10638 
10639 static int
10640 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10641 {
10642 	struct sd_errstats	*stp;
10643 	uint64_t		capacity;
10644 	uint_t			lbasize;
10645 	int			rval = SD_READY_VALID;
10646 	char			name_str[48];
10647 	boolean_t		is_valid;
10648 	struct sd_lun		*un;
10649 	int			status;
10650 
10651 	ASSERT(ssc != NULL);
10652 	un = ssc->ssc_un;
10653 	ASSERT(un != NULL);
10654 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10655 
10656 	mutex_enter(SD_MUTEX(un));
10657 	/*
10658 	 * If a device has removable media, we must check if media is
10659 	 * ready when checking if this device is ready and valid.
10660 	 */
10661 	if (un->un_f_has_removable_media) {
10662 		mutex_exit(SD_MUTEX(un));
10663 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10664 
10665 		if (status != 0) {
10666 			rval = SD_NOT_READY_VALID;
10667 			mutex_enter(SD_MUTEX(un));
10668 
10669 			/* Ignore all failed status for removalbe media */
10670 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10671 
10672 			goto done;
10673 		}
10674 
10675 		is_valid = SD_IS_VALID_LABEL(un);
10676 		mutex_enter(SD_MUTEX(un));
10677 		if (!is_valid ||
10678 		    (un->un_f_blockcount_is_valid == FALSE) ||
10679 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10680 
10681 			/* capacity has to be read every open. */
10682 			mutex_exit(SD_MUTEX(un));
10683 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10684 			    &lbasize, SD_PATH_DIRECT);
10685 
10686 			if (status != 0) {
10687 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10688 
10689 				cmlb_invalidate(un->un_cmlbhandle,
10690 				    (void *)SD_PATH_DIRECT);
10691 				mutex_enter(SD_MUTEX(un));
10692 				rval = SD_NOT_READY_VALID;
10693 
10694 				goto done;
10695 			} else {
10696 				mutex_enter(SD_MUTEX(un));
10697 				sd_update_block_info(un, lbasize, capacity);
10698 			}
10699 		}
10700 
10701 		/*
10702 		 * Check if the media in the device is writable or not.
10703 		 */
10704 		if (!is_valid && ISCD(un)) {
10705 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10706 		}
10707 
10708 	} else {
10709 		/*
10710 		 * Do a test unit ready to clear any unit attention from non-cd
10711 		 * devices.
10712 		 */
10713 		mutex_exit(SD_MUTEX(un));
10714 
10715 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10716 		if (status != 0) {
10717 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10718 		}
10719 
10720 		mutex_enter(SD_MUTEX(un));
10721 	}
10722 
10723 
10724 	/*
10725 	 * If this is a non 512 block device, allocate space for
10726 	 * the wmap cache. This is being done here since every time
10727 	 * a media is changed this routine will be called and the
10728 	 * block size is a function of media rather than device.
10729 	 */
10730 	if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10731 	    un->un_f_non_devbsize_supported) &&
10732 	    un->un_tgt_blocksize != DEV_BSIZE) ||
10733 	    un->un_f_enable_rmw) {
10734 		if (!(un->un_wm_cache)) {
10735 			(void) snprintf(name_str, sizeof (name_str),
10736 			    "%s%d_cache",
10737 			    ddi_driver_name(SD_DEVINFO(un)),
10738 			    ddi_get_instance(SD_DEVINFO(un)));
10739 			un->un_wm_cache = kmem_cache_create(
10740 			    name_str, sizeof (struct sd_w_map),
10741 			    8, sd_wm_cache_constructor,
10742 			    sd_wm_cache_destructor, NULL,
10743 			    (void *)un, NULL, 0);
10744 			if (!(un->un_wm_cache)) {
10745 				rval = ENOMEM;
10746 				goto done;
10747 			}
10748 		}
10749 	}
10750 
10751 	if (un->un_state == SD_STATE_NORMAL) {
10752 		/*
10753 		 * If the target is not yet ready here (defined by a TUR
10754 		 * failure), invalidate the geometry and print an 'offline'
10755 		 * message. This is a legacy message, as the state of the
10756 		 * target is not actually changed to SD_STATE_OFFLINE.
10757 		 *
10758 		 * If the TUR fails for EACCES (Reservation Conflict),
10759 		 * SD_RESERVED_BY_OTHERS will be returned to indicate
10760 		 * reservation conflict. If the TUR fails for other
10761 		 * reasons, SD_NOT_READY_VALID will be returned.
10762 		 */
10763 		int err;
10764 
10765 		mutex_exit(SD_MUTEX(un));
10766 		err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10767 		mutex_enter(SD_MUTEX(un));
10768 
10769 		if (err != 0) {
10770 			mutex_exit(SD_MUTEX(un));
10771 			cmlb_invalidate(un->un_cmlbhandle,
10772 			    (void *)SD_PATH_DIRECT);
10773 			mutex_enter(SD_MUTEX(un));
10774 			if (err == EACCES) {
10775 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10776 				    "reservation conflict\n");
10777 				rval = SD_RESERVED_BY_OTHERS;
10778 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10779 			} else {
10780 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10781 				    "drive offline\n");
10782 				rval = SD_NOT_READY_VALID;
10783 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10784 			}
10785 			goto done;
10786 		}
10787 	}
10788 
10789 	if (un->un_f_format_in_progress == FALSE) {
10790 		mutex_exit(SD_MUTEX(un));
10791 
10792 		(void) cmlb_validate(un->un_cmlbhandle, 0,
10793 		    (void *)SD_PATH_DIRECT);
10794 		if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10795 		    NULL, (void *) SD_PATH_DIRECT) != 0) {
10796 			rval = SD_NOT_READY_VALID;
10797 			mutex_enter(SD_MUTEX(un));
10798 
10799 			goto done;
10800 		}
10801 		if (un->un_f_pkstats_enabled) {
10802 			sd_set_pstats(un);
10803 			SD_TRACE(SD_LOG_IO_PARTITION, un,
10804 			    "sd_ready_and_valid: un:0x%p pstats created and "
10805 			    "set\n", un);
10806 		}
10807 		mutex_enter(SD_MUTEX(un));
10808 	}
10809 
10810 	/*
10811 	 * If this device supports DOOR_LOCK command, try and send
10812 	 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10813 	 * if it fails. For a CD, however, it is an error
10814 	 */
10815 	if (un->un_f_doorlock_supported) {
10816 		mutex_exit(SD_MUTEX(un));
10817 		status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10818 		    SD_PATH_DIRECT);
10819 
10820 		if ((status != 0) && ISCD(un)) {
10821 			rval = SD_NOT_READY_VALID;
10822 			mutex_enter(SD_MUTEX(un));
10823 
10824 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10825 
10826 			goto done;
10827 		} else if (status != 0)
10828 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10829 		mutex_enter(SD_MUTEX(un));
10830 	}
10831 
10832 	/* The state has changed, inform the media watch routines */
10833 	un->un_mediastate = DKIO_INSERTED;
10834 	cv_broadcast(&un->un_state_cv);
10835 	rval = SD_READY_VALID;
10836 
10837 done:
10838 
10839 	/*
10840 	 * Initialize the capacity kstat value, if no media previously
10841 	 * (capacity kstat is 0) and a media has been inserted
10842 	 * (un_blockcount > 0).
10843 	 */
10844 	if (un->un_errstats != NULL) {
10845 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
10846 		if ((stp->sd_capacity.value.ui64 == 0) &&
10847 		    (un->un_f_blockcount_is_valid == TRUE)) {
10848 			stp->sd_capacity.value.ui64 =
10849 			    (uint64_t)((uint64_t)un->un_blockcount *
10850 			    un->un_sys_blocksize);
10851 		}
10852 	}
10853 
10854 	mutex_exit(SD_MUTEX(un));
10855 	return (rval);
10856 }
10857 
10858 
10859 /*
10860  *    Function: sdmin
10861  *
10862  * Description: Routine to limit the size of a data transfer. Used in
10863  *		conjunction with physio(9F).
10864  *
10865  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10866  *
10867  *     Context: Kernel thread context.
10868  */
10869 
10870 static void
10871 sdmin(struct buf *bp)
10872 {
10873 	struct sd_lun	*un;
10874 	int		instance;
10875 
10876 	instance = SDUNIT(bp->b_edev);
10877 
10878 	un = ddi_get_soft_state(sd_state, instance);
10879 	ASSERT(un != NULL);
10880 
10881 	/*
10882 	 * We depend on buf breakup to restrict
10883 	 * IO size if it is enabled.
10884 	 */
10885 	if (un->un_buf_breakup_supported) {
10886 		return;
10887 	}
10888 
10889 	if (bp->b_bcount > un->un_max_xfer_size) {
10890 		bp->b_bcount = un->un_max_xfer_size;
10891 	}
10892 }
10893 
10894 
10895 /*
10896  *    Function: sdread
10897  *
10898  * Description: Driver's read(9e) entry point function.
10899  *
10900  *   Arguments: dev   - device number
10901  *		uio   - structure pointer describing where data is to be stored
10902  *			in user's space
10903  *		cred_p  - user credential pointer
10904  *
10905  * Return Code: ENXIO
10906  *		EIO
10907  *		EINVAL
10908  *		value returned by physio
10909  *
10910  *     Context: Kernel thread context.
10911  */
10912 /* ARGSUSED */
10913 static int
10914 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10915 {
10916 	struct sd_lun	*un = NULL;
10917 	int		secmask;
10918 	int		err = 0;
10919 	sd_ssc_t	*ssc;
10920 
10921 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10922 		return (ENXIO);
10923 	}
10924 
10925 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10926 
10927 
10928 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10929 		mutex_enter(SD_MUTEX(un));
10930 		/*
10931 		 * Because the call to sd_ready_and_valid will issue I/O we
10932 		 * must wait here if either the device is suspended or
10933 		 * if it's power level is changing.
10934 		 */
10935 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10936 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10937 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10938 		}
10939 		un->un_ncmds_in_driver++;
10940 		mutex_exit(SD_MUTEX(un));
10941 
10942 		/* Initialize sd_ssc_t for internal uscsi commands */
10943 		ssc = sd_ssc_init(un);
10944 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10945 			err = EIO;
10946 		} else {
10947 			err = 0;
10948 		}
10949 		sd_ssc_fini(ssc);
10950 
10951 		mutex_enter(SD_MUTEX(un));
10952 		un->un_ncmds_in_driver--;
10953 		ASSERT(un->un_ncmds_in_driver >= 0);
10954 		mutex_exit(SD_MUTEX(un));
10955 		if (err != 0)
10956 			return (err);
10957 	}
10958 
10959 	/*
10960 	 * Read requests are restricted to multiples of the system block size.
10961 	 */
10962 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10963 	    !un->un_f_enable_rmw)
10964 		secmask = un->un_tgt_blocksize - 1;
10965 	else
10966 		secmask = DEV_BSIZE - 1;
10967 
10968 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10969 		SD_ERROR(SD_LOG_READ_WRITE, un,
10970 		    "sdread: file offset not modulo %d\n",
10971 		    secmask + 1);
10972 		err = EINVAL;
10973 	} else if (uio->uio_iov->iov_len & (secmask)) {
10974 		SD_ERROR(SD_LOG_READ_WRITE, un,
10975 		    "sdread: transfer length not modulo %d\n",
10976 		    secmask + 1);
10977 		err = EINVAL;
10978 	} else {
10979 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10980 	}
10981 
10982 	return (err);
10983 }
10984 
10985 
10986 /*
10987  *    Function: sdwrite
10988  *
10989  * Description: Driver's write(9e) entry point function.
10990  *
10991  *   Arguments: dev   - device number
10992  *		uio   - structure pointer describing where data is stored in
10993  *			user's space
10994  *		cred_p  - user credential pointer
10995  *
10996  * Return Code: ENXIO
10997  *		EIO
10998  *		EINVAL
10999  *		value returned by physio
11000  *
11001  *     Context: Kernel thread context.
11002  */
11003 /* ARGSUSED */
11004 static int
11005 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
11006 {
11007 	struct sd_lun	*un = NULL;
11008 	int		secmask;
11009 	int		err = 0;
11010 	sd_ssc_t	*ssc;
11011 
11012 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11013 		return (ENXIO);
11014 	}
11015 
11016 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11017 
11018 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11019 		mutex_enter(SD_MUTEX(un));
11020 		/*
11021 		 * Because the call to sd_ready_and_valid will issue I/O we
11022 		 * must wait here if either the device is suspended or
11023 		 * if it's power level is changing.
11024 		 */
11025 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11026 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11027 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11028 		}
11029 		un->un_ncmds_in_driver++;
11030 		mutex_exit(SD_MUTEX(un));
11031 
11032 		/* Initialize sd_ssc_t for internal uscsi commands */
11033 		ssc = sd_ssc_init(un);
11034 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11035 			err = EIO;
11036 		} else {
11037 			err = 0;
11038 		}
11039 		sd_ssc_fini(ssc);
11040 
11041 		mutex_enter(SD_MUTEX(un));
11042 		un->un_ncmds_in_driver--;
11043 		ASSERT(un->un_ncmds_in_driver >= 0);
11044 		mutex_exit(SD_MUTEX(un));
11045 		if (err != 0)
11046 			return (err);
11047 	}
11048 
11049 	/*
11050 	 * Write requests are restricted to multiples of the system block size.
11051 	 */
11052 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11053 	    !un->un_f_enable_rmw)
11054 		secmask = un->un_tgt_blocksize - 1;
11055 	else
11056 		secmask = DEV_BSIZE - 1;
11057 
11058 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11059 		SD_ERROR(SD_LOG_READ_WRITE, un,
11060 		    "sdwrite: file offset not modulo %d\n",
11061 		    secmask + 1);
11062 		err = EINVAL;
11063 	} else if (uio->uio_iov->iov_len & (secmask)) {
11064 		SD_ERROR(SD_LOG_READ_WRITE, un,
11065 		    "sdwrite: transfer length not modulo %d\n",
11066 		    secmask + 1);
11067 		err = EINVAL;
11068 	} else {
11069 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11070 	}
11071 
11072 	return (err);
11073 }
11074 
11075 
11076 /*
11077  *    Function: sdaread
11078  *
11079  * Description: Driver's aread(9e) entry point function.
11080  *
11081  *   Arguments: dev   - device number
11082  *		aio   - structure pointer describing where data is to be stored
11083  *		cred_p  - user credential pointer
11084  *
11085  * Return Code: ENXIO
11086  *		EIO
11087  *		EINVAL
11088  *		value returned by aphysio
11089  *
11090  *     Context: Kernel thread context.
11091  */
11092 /* ARGSUSED */
11093 static int
11094 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11095 {
11096 	struct sd_lun	*un = NULL;
11097 	struct uio	*uio = aio->aio_uio;
11098 	int		secmask;
11099 	int		err = 0;
11100 	sd_ssc_t	*ssc;
11101 
11102 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11103 		return (ENXIO);
11104 	}
11105 
11106 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11107 
11108 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11109 		mutex_enter(SD_MUTEX(un));
11110 		/*
11111 		 * Because the call to sd_ready_and_valid will issue I/O we
11112 		 * must wait here if either the device is suspended or
11113 		 * if it's power level is changing.
11114 		 */
11115 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11116 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11117 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11118 		}
11119 		un->un_ncmds_in_driver++;
11120 		mutex_exit(SD_MUTEX(un));
11121 
11122 		/* Initialize sd_ssc_t for internal uscsi commands */
11123 		ssc = sd_ssc_init(un);
11124 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11125 			err = EIO;
11126 		} else {
11127 			err = 0;
11128 		}
11129 		sd_ssc_fini(ssc);
11130 
11131 		mutex_enter(SD_MUTEX(un));
11132 		un->un_ncmds_in_driver--;
11133 		ASSERT(un->un_ncmds_in_driver >= 0);
11134 		mutex_exit(SD_MUTEX(un));
11135 		if (err != 0)
11136 			return (err);
11137 	}
11138 
11139 	/*
11140 	 * Read requests are restricted to multiples of the system block size.
11141 	 */
11142 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11143 	    !un->un_f_enable_rmw)
11144 		secmask = un->un_tgt_blocksize - 1;
11145 	else
11146 		secmask = DEV_BSIZE - 1;
11147 
11148 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11149 		SD_ERROR(SD_LOG_READ_WRITE, un,
11150 		    "sdaread: file offset not modulo %d\n",
11151 		    secmask + 1);
11152 		err = EINVAL;
11153 	} else if (uio->uio_iov->iov_len & (secmask)) {
11154 		SD_ERROR(SD_LOG_READ_WRITE, un,
11155 		    "sdaread: transfer length not modulo %d\n",
11156 		    secmask + 1);
11157 		err = EINVAL;
11158 	} else {
11159 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11160 	}
11161 
11162 	return (err);
11163 }
11164 
11165 
11166 /*
11167  *    Function: sdawrite
11168  *
11169  * Description: Driver's awrite(9e) entry point function.
11170  *
11171  *   Arguments: dev   - device number
11172  *		aio   - structure pointer describing where data is stored
11173  *		cred_p  - user credential pointer
11174  *
11175  * Return Code: ENXIO
11176  *		EIO
11177  *		EINVAL
11178  *		value returned by aphysio
11179  *
11180  *     Context: Kernel thread context.
11181  */
11182 /* ARGSUSED */
11183 static int
11184 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11185 {
11186 	struct sd_lun	*un = NULL;
11187 	struct uio	*uio = aio->aio_uio;
11188 	int		secmask;
11189 	int		err = 0;
11190 	sd_ssc_t	*ssc;
11191 
11192 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11193 		return (ENXIO);
11194 	}
11195 
11196 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11197 
11198 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11199 		mutex_enter(SD_MUTEX(un));
11200 		/*
11201 		 * Because the call to sd_ready_and_valid will issue I/O we
11202 		 * must wait here if either the device is suspended or
11203 		 * if it's power level is changing.
11204 		 */
11205 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11206 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11207 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11208 		}
11209 		un->un_ncmds_in_driver++;
11210 		mutex_exit(SD_MUTEX(un));
11211 
11212 		/* Initialize sd_ssc_t for internal uscsi commands */
11213 		ssc = sd_ssc_init(un);
11214 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11215 			err = EIO;
11216 		} else {
11217 			err = 0;
11218 		}
11219 		sd_ssc_fini(ssc);
11220 
11221 		mutex_enter(SD_MUTEX(un));
11222 		un->un_ncmds_in_driver--;
11223 		ASSERT(un->un_ncmds_in_driver >= 0);
11224 		mutex_exit(SD_MUTEX(un));
11225 		if (err != 0)
11226 			return (err);
11227 	}
11228 
11229 	/*
11230 	 * Write requests are restricted to multiples of the system block size.
11231 	 */
11232 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11233 	    !un->un_f_enable_rmw)
11234 		secmask = un->un_tgt_blocksize - 1;
11235 	else
11236 		secmask = DEV_BSIZE - 1;
11237 
11238 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11239 		SD_ERROR(SD_LOG_READ_WRITE, un,
11240 		    "sdawrite: file offset not modulo %d\n",
11241 		    secmask + 1);
11242 		err = EINVAL;
11243 	} else if (uio->uio_iov->iov_len & (secmask)) {
11244 		SD_ERROR(SD_LOG_READ_WRITE, un,
11245 		    "sdawrite: transfer length not modulo %d\n",
11246 		    secmask + 1);
11247 		err = EINVAL;
11248 	} else {
11249 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11250 	}
11251 
11252 	return (err);
11253 }
11254 
11255 
11256 
11257 
11258 
11259 /*
11260  * Driver IO processing follows the following sequence:
11261  *
11262  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11263  *         |                |                     ^
11264  *         v                v                     |
11265  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11266  *         |                |                     |                   |
11267  *         v                |                     |                   |
11268  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11269  *         |                |                     ^                   ^
11270  *         v                v                     |                   |
11271  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11272  *         |                |                     |                   |
11273  *     +---+                |                     +------------+      +-------+
11274  *     |                    |                                  |              |
11275  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11276  *     |                    v                                  |              |
11277  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11278  *     |                    |                                  ^              |
11279  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11280  *     |                    v                                  |              |
11281  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11282  *     |                    |                                  ^              |
11283  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11284  *     |                    v                                  |              |
11285  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11286  *     |                    |                                  ^              |
11287  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11288  *     |                    v                                  |              |
11289  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11290  *     |                    |                                  ^              |
11291  *     |                    |                                  |              |
11292  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11293  *                          |                           ^
11294  *                          v                           |
11295  *                   sd_core_iostart()                  |
11296  *                          |                           |
11297  *                          |                           +------>(*destroypkt)()
11298  *                          +-> sd_start_cmds() <-+     |           |
11299  *                          |                     |     |           v
11300  *                          |                     |     |  scsi_destroy_pkt(9F)
11301  *                          |                     |     |
11302  *                          +->(*initpkt)()       +- sdintr()
11303  *                          |  |                        |  |
11304  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11305  *                          |  +-> scsi_setup_cdb(9F)   |
11306  *                          |                           |
11307  *                          +--> scsi_transport(9F)     |
11308  *                                     |                |
11309  *                                     +----> SCSA ---->+
11310  *
11311  *
11312  * This code is based upon the following presumptions:
11313  *
11314  *   - iostart and iodone functions operate on buf(9S) structures. These
11315  *     functions perform the necessary operations on the buf(9S) and pass
11316  *     them along to the next function in the chain by using the macros
11317  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11318  *     (for iodone side functions).
11319  *
11320  *   - The iostart side functions may sleep. The iodone side functions
11321  *     are called under interrupt context and may NOT sleep. Therefore
11322  *     iodone side functions also may not call iostart side functions.
11323  *     (NOTE: iostart side functions should NOT sleep for memory, as
11324  *     this could result in deadlock.)
11325  *
11326  *   - An iostart side function may call its corresponding iodone side
11327  *     function directly (if necessary).
11328  *
11329  *   - In the event of an error, an iostart side function can return a buf(9S)
11330  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11331  *     b_error in the usual way of course).
11332  *
11333  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11334  *     requests to the iostart side functions.  The iostart side functions in
11335  *     this case would be called under the context of a taskq thread, so it's
11336  *     OK for them to block/sleep/spin in this case.
11337  *
11338  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11339  *     pass them along to the next function in the chain.  The corresponding
11340  *     iodone side functions must coalesce the "shadow" bufs and return
11341  *     the "original" buf to the next higher layer.
11342  *
11343  *   - The b_private field of the buf(9S) struct holds a pointer to
11344  *     an sd_xbuf struct, which contains information needed to
11345  *     construct the scsi_pkt for the command.
11346  *
11347  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11348  *     layer must acquire & release the SD_MUTEX(un) as needed.
11349  */
11350 
11351 
11352 /*
11353  * Create taskq for all targets in the system. This is created at
11354  * _init(9E) and destroyed at _fini(9E).
11355  *
11356  * Note: here we set the minalloc to a reasonably high number to ensure that
11357  * we will have an adequate supply of task entries available at interrupt time.
11358  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11359  * sd_create_taskq().  Since we do not want to sleep for allocations at
11360  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11361  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11362  * requests any one instant in time.
11363  */
11364 #define	SD_TASKQ_NUMTHREADS	8
11365 #define	SD_TASKQ_MINALLOC	256
11366 #define	SD_TASKQ_MAXALLOC	256
11367 
11368 static taskq_t	*sd_tq = NULL;
11369 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11370 
11371 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11372 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11373 
11374 /*
11375  * The following task queue is being created for the write part of
11376  * read-modify-write of non-512 block size devices.
11377  * Limit the number of threads to 1 for now. This number has been chosen
11378  * considering the fact that it applies only to dvd ram drives/MO drives
11379  * currently. Performance for which is not main criteria at this stage.
11380  * Note: It needs to be explored if we can use a single taskq in future
11381  */
11382 #define	SD_WMR_TASKQ_NUMTHREADS	1
11383 static taskq_t	*sd_wmr_tq = NULL;
11384 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11385 
11386 /*
11387  *    Function: sd_taskq_create
11388  *
11389  * Description: Create taskq thread(s) and preallocate task entries
11390  *
11391  * Return Code: Returns a pointer to the allocated taskq_t.
11392  *
11393  *     Context: Can sleep. Requires blockable context.
11394  *
11395  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11396  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
11397  *		- taskq_create() will block for memory, also it will panic
11398  *		  if it cannot create the requested number of threads.
11399  *		- Currently taskq_create() creates threads that cannot be
11400  *		  swapped.
11401  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
11402  *		  supply of taskq entries at interrupt time (ie, so that we
11403  *		  do not have to sleep for memory)
11404  */
11405 
11406 static void
11407 sd_taskq_create(void)
11408 {
11409 	char	taskq_name[TASKQ_NAMELEN];
11410 
11411 	ASSERT(sd_tq == NULL);
11412 	ASSERT(sd_wmr_tq == NULL);
11413 
11414 	(void) snprintf(taskq_name, sizeof (taskq_name),
11415 	    "%s_drv_taskq", sd_label);
11416 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11417 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11418 	    TASKQ_PREPOPULATE));
11419 
11420 	(void) snprintf(taskq_name, sizeof (taskq_name),
11421 	    "%s_rmw_taskq", sd_label);
11422 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11423 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11424 	    TASKQ_PREPOPULATE));
11425 }
11426 
11427 
11428 /*
11429  *    Function: sd_taskq_delete
11430  *
11431  * Description: Complementary cleanup routine for sd_taskq_create().
11432  *
11433  *     Context: Kernel thread context.
11434  */
11435 
11436 static void
11437 sd_taskq_delete(void)
11438 {
11439 	ASSERT(sd_tq != NULL);
11440 	ASSERT(sd_wmr_tq != NULL);
11441 	taskq_destroy(sd_tq);
11442 	taskq_destroy(sd_wmr_tq);
11443 	sd_tq = NULL;
11444 	sd_wmr_tq = NULL;
11445 }
11446 
11447 
11448 /*
11449  *    Function: sdstrategy
11450  *
11451  * Description: Driver's strategy (9E) entry point function.
11452  *
11453  *   Arguments: bp - pointer to buf(9S)
11454  *
11455  * Return Code: Always returns zero
11456  *
11457  *     Context: Kernel thread context.
11458  */
11459 
11460 static int
11461 sdstrategy(struct buf *bp)
11462 {
11463 	struct sd_lun *un;
11464 
11465 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11466 	if (un == NULL) {
11467 		bioerror(bp, EIO);
11468 		bp->b_resid = bp->b_bcount;
11469 		biodone(bp);
11470 		return (0);
11471 	}
11472 
11473 	/* As was done in the past, fail new cmds. if state is dumping. */
11474 	if (un->un_state == SD_STATE_DUMPING) {
11475 		bioerror(bp, ENXIO);
11476 		bp->b_resid = bp->b_bcount;
11477 		biodone(bp);
11478 		return (0);
11479 	}
11480 
11481 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11482 
11483 	/*
11484 	 * Commands may sneak in while we released the mutex in
11485 	 * DDI_SUSPEND, we should block new commands. However, old
11486 	 * commands that are still in the driver at this point should
11487 	 * still be allowed to drain.
11488 	 */
11489 	mutex_enter(SD_MUTEX(un));
11490 	/*
11491 	 * Must wait here if either the device is suspended or
11492 	 * if it's power level is changing.
11493 	 */
11494 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11495 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11496 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11497 	}
11498 
11499 	un->un_ncmds_in_driver++;
11500 
11501 	/*
11502 	 * atapi: Since we are running the CD for now in PIO mode we need to
11503 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11504 	 * the HBA's init_pkt routine.
11505 	 */
11506 	if (un->un_f_cfg_is_atapi == TRUE) {
11507 		mutex_exit(SD_MUTEX(un));
11508 		bp_mapin(bp);
11509 		mutex_enter(SD_MUTEX(un));
11510 	}
11511 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11512 	    un->un_ncmds_in_driver);
11513 
11514 	if (bp->b_flags & B_WRITE)
11515 		un->un_f_sync_cache_required = TRUE;
11516 
11517 	mutex_exit(SD_MUTEX(un));
11518 
11519 	/*
11520 	 * This will (eventually) allocate the sd_xbuf area and
11521 	 * call sd_xbuf_strategy().  We just want to return the
11522 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11523 	 * imized tail call which saves us a stack frame.
11524 	 */
11525 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11526 }
11527 
11528 
11529 /*
11530  *    Function: sd_xbuf_strategy
11531  *
11532  * Description: Function for initiating IO operations via the
11533  *		ddi_xbuf_qstrategy() mechanism.
11534  *
11535  *     Context: Kernel thread context.
11536  */
11537 
11538 static void
11539 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11540 {
11541 	struct sd_lun *un = arg;
11542 
11543 	ASSERT(bp != NULL);
11544 	ASSERT(xp != NULL);
11545 	ASSERT(un != NULL);
11546 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11547 
11548 	/*
11549 	 * Initialize the fields in the xbuf and save a pointer to the
11550 	 * xbuf in bp->b_private.
11551 	 */
11552 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11553 
11554 	/* Send the buf down the iostart chain */
11555 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11556 }
11557 
11558 
11559 /*
11560  *    Function: sd_xbuf_init
11561  *
11562  * Description: Prepare the given sd_xbuf struct for use.
11563  *
11564  *   Arguments: un - ptr to softstate
11565  *		bp - ptr to associated buf(9S)
11566  *		xp - ptr to associated sd_xbuf
11567  *		chain_type - IO chain type to use:
11568  *			SD_CHAIN_NULL
11569  *			SD_CHAIN_BUFIO
11570  *			SD_CHAIN_USCSI
11571  *			SD_CHAIN_DIRECT
11572  *			SD_CHAIN_DIRECT_PRIORITY
11573  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11574  *			initialization; may be NULL if none.
11575  *
11576  *     Context: Kernel thread context
11577  */
11578 
11579 static void
11580 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11581 	uchar_t chain_type, void *pktinfop)
11582 {
11583 	int index;
11584 
11585 	ASSERT(un != NULL);
11586 	ASSERT(bp != NULL);
11587 	ASSERT(xp != NULL);
11588 
11589 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11590 	    bp, chain_type);
11591 
11592 	xp->xb_un	= un;
11593 	xp->xb_pktp	= NULL;
11594 	xp->xb_pktinfo	= pktinfop;
11595 	xp->xb_private	= bp->b_private;
11596 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
11597 
11598 	/*
11599 	 * Set up the iostart and iodone chain indexes in the xbuf, based
11600 	 * upon the specified chain type to use.
11601 	 */
11602 	switch (chain_type) {
11603 	case SD_CHAIN_NULL:
11604 		/*
11605 		 * Fall thru to just use the values for the buf type, even
11606 		 * tho for the NULL chain these values will never be used.
11607 		 */
11608 		/* FALLTHRU */
11609 	case SD_CHAIN_BUFIO:
11610 		index = un->un_buf_chain_type;
11611 		if ((!un->un_f_has_removable_media) &&
11612 		    (un->un_tgt_blocksize != 0) &&
11613 		    (un->un_tgt_blocksize != DEV_BSIZE ||
11614 		    un->un_f_enable_rmw)) {
11615 			int secmask = 0, blknomask = 0;
11616 			if (un->un_f_enable_rmw) {
11617 				blknomask =
11618 				    (un->un_phy_blocksize / DEV_BSIZE) - 1;
11619 				secmask = un->un_phy_blocksize - 1;
11620 			} else {
11621 				blknomask =
11622 				    (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11623 				secmask = un->un_tgt_blocksize - 1;
11624 			}
11625 
11626 			if ((bp->b_lblkno & (blknomask)) ||
11627 			    (bp->b_bcount & (secmask))) {
11628 				if ((un->un_f_rmw_type !=
11629 				    SD_RMW_TYPE_RETURN_ERROR) ||
11630 				    un->un_f_enable_rmw) {
11631 					if (un->un_f_pm_is_enabled == FALSE)
11632 						index =
11633 						    SD_CHAIN_INFO_MSS_DSK_NO_PM;
11634 					else
11635 						index =
11636 						    SD_CHAIN_INFO_MSS_DISK;
11637 				}
11638 			}
11639 		}
11640 		break;
11641 	case SD_CHAIN_USCSI:
11642 		index = un->un_uscsi_chain_type;
11643 		break;
11644 	case SD_CHAIN_DIRECT:
11645 		index = un->un_direct_chain_type;
11646 		break;
11647 	case SD_CHAIN_DIRECT_PRIORITY:
11648 		index = un->un_priority_chain_type;
11649 		break;
11650 	default:
11651 		/* We're really broken if we ever get here... */
11652 		panic("sd_xbuf_init: illegal chain type!");
11653 		/*NOTREACHED*/
11654 	}
11655 
11656 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11657 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11658 
11659 	/*
11660 	 * It might be a bit easier to simply bzero the entire xbuf above,
11661 	 * but it turns out that since we init a fair number of members anyway,
11662 	 * we save a fair number cycles by doing explicit assignment of zero.
11663 	 */
11664 	xp->xb_pkt_flags	= 0;
11665 	xp->xb_dma_resid	= 0;
11666 	xp->xb_retry_count	= 0;
11667 	xp->xb_victim_retry_count = 0;
11668 	xp->xb_ua_retry_count	= 0;
11669 	xp->xb_nr_retry_count	= 0;
11670 	xp->xb_sense_bp		= NULL;
11671 	xp->xb_sense_status	= 0;
11672 	xp->xb_sense_state	= 0;
11673 	xp->xb_sense_resid	= 0;
11674 	xp->xb_ena		= 0;
11675 
11676 	bp->b_private	= xp;
11677 	bp->b_flags	&= ~(B_DONE | B_ERROR);
11678 	bp->b_resid	= 0;
11679 	bp->av_forw	= NULL;
11680 	bp->av_back	= NULL;
11681 	bioerror(bp, 0);
11682 
11683 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11684 }
11685 
11686 
11687 /*
11688  *    Function: sd_uscsi_strategy
11689  *
11690  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11691  *
11692  *   Arguments: bp - buf struct ptr
11693  *
11694  * Return Code: Always returns 0
11695  *
11696  *     Context: Kernel thread context
11697  */
11698 
11699 static int
11700 sd_uscsi_strategy(struct buf *bp)
11701 {
11702 	struct sd_lun		*un;
11703 	struct sd_uscsi_info	*uip;
11704 	struct sd_xbuf		*xp;
11705 	uchar_t			chain_type;
11706 	uchar_t			cmd;
11707 
11708 	ASSERT(bp != NULL);
11709 
11710 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11711 	if (un == NULL) {
11712 		bioerror(bp, EIO);
11713 		bp->b_resid = bp->b_bcount;
11714 		biodone(bp);
11715 		return (0);
11716 	}
11717 
11718 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11719 
11720 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11721 
11722 	/*
11723 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11724 	 */
11725 	ASSERT(bp->b_private != NULL);
11726 	uip = (struct sd_uscsi_info *)bp->b_private;
11727 	cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11728 
11729 	mutex_enter(SD_MUTEX(un));
11730 	/*
11731 	 * atapi: Since we are running the CD for now in PIO mode we need to
11732 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11733 	 * the HBA's init_pkt routine.
11734 	 */
11735 	if (un->un_f_cfg_is_atapi == TRUE) {
11736 		mutex_exit(SD_MUTEX(un));
11737 		bp_mapin(bp);
11738 		mutex_enter(SD_MUTEX(un));
11739 	}
11740 	un->un_ncmds_in_driver++;
11741 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11742 	    un->un_ncmds_in_driver);
11743 
11744 	if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11745 	    (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11746 		un->un_f_sync_cache_required = TRUE;
11747 
11748 	mutex_exit(SD_MUTEX(un));
11749 
11750 	switch (uip->ui_flags) {
11751 	case SD_PATH_DIRECT:
11752 		chain_type = SD_CHAIN_DIRECT;
11753 		break;
11754 	case SD_PATH_DIRECT_PRIORITY:
11755 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
11756 		break;
11757 	default:
11758 		chain_type = SD_CHAIN_USCSI;
11759 		break;
11760 	}
11761 
11762 	/*
11763 	 * We may allocate extra buf for external USCSI commands. If the
11764 	 * application asks for bigger than 20-byte sense data via USCSI,
11765 	 * SCSA layer will allocate 252 bytes sense buf for that command.
11766 	 */
11767 	if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11768 	    SENSE_LENGTH) {
11769 		xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11770 		    MAX_SENSE_LENGTH, KM_SLEEP);
11771 	} else {
11772 		xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11773 	}
11774 
11775 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11776 
11777 	/* Use the index obtained within xbuf_init */
11778 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11779 
11780 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11781 
11782 	return (0);
11783 }
11784 
11785 /*
11786  *    Function: sd_send_scsi_cmd
11787  *
11788  * Description: Runs a USCSI command for user (when called thru sdioctl),
11789  *		or for the driver
11790  *
11791  *   Arguments: dev - the dev_t for the device
11792  *		incmd - ptr to a valid uscsi_cmd struct
11793  *		flag - bit flag, indicating open settings, 32/64 bit type
11794  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11795  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11796  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11797  *			to use the USCSI "direct" chain and bypass the normal
11798  *			command waitq.
11799  *
11800  * Return Code: 0 -  successful completion of the given command
11801  *		EIO - scsi_uscsi_handle_command() failed
11802  *		ENXIO  - soft state not found for specified dev
11803  *		EINVAL
11804  *		EFAULT - copyin/copyout error
11805  *		return code of scsi_uscsi_handle_command():
11806  *			EIO
11807  *			ENXIO
11808  *			EACCES
11809  *
11810  *     Context: Waits for command to complete. Can sleep.
11811  */
11812 
11813 static int
11814 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11815 	enum uio_seg dataspace, int path_flag)
11816 {
11817 	struct sd_lun	*un;
11818 	sd_ssc_t	*ssc;
11819 	int		rval;
11820 
11821 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11822 	if (un == NULL) {
11823 		return (ENXIO);
11824 	}
11825 
11826 	/*
11827 	 * Using sd_ssc_send to handle uscsi cmd
11828 	 */
11829 	ssc = sd_ssc_init(un);
11830 	rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11831 	sd_ssc_fini(ssc);
11832 
11833 	return (rval);
11834 }
11835 
11836 /*
11837  *    Function: sd_ssc_init
11838  *
11839  * Description: Uscsi end-user call this function to initialize necessary
11840  *              fields, such as uscsi_cmd and sd_uscsi_info struct.
11841  *
11842  *              The return value of sd_send_scsi_cmd will be treated as a
11843  *              fault in various conditions. Even it is not Zero, some
11844  *              callers may ignore the return value. That is to say, we can
11845  *              not make an accurate assessment in sdintr, since if a
11846  *              command is failed in sdintr it does not mean the caller of
11847  *              sd_send_scsi_cmd will treat it as a real failure.
11848  *
11849  *              To avoid printing too many error logs for a failed uscsi
11850  *              packet that the caller may not treat it as a failure, the
11851  *              sd will keep silent for handling all uscsi commands.
11852  *
11853  *              During detach->attach and attach-open, for some types of
11854  *              problems, the driver should be providing information about
11855  *              the problem encountered. Device use USCSI_SILENT, which
11856  *              suppresses all driver information. The result is that no
11857  *              information about the problem is available. Being
11858  *              completely silent during this time is inappropriate. The
11859  *              driver needs a more selective filter than USCSI_SILENT, so
11860  *              that information related to faults is provided.
11861  *
11862  *              To make the accurate accessment, the caller  of
11863  *              sd_send_scsi_USCSI_CMD should take the ownership and
11864  *              get necessary information to print error messages.
11865  *
11866  *              If we want to print necessary info of uscsi command, we need to
11867  *              keep the uscsi_cmd and sd_uscsi_info till we can make the
11868  *              assessment. We use sd_ssc_init to alloc necessary
11869  *              structs for sending an uscsi command and we are also
11870  *              responsible for free the memory by calling
11871  *              sd_ssc_fini.
11872  *
11873  *              The calling secquences will look like:
11874  *              sd_ssc_init->
11875  *
11876  *                  ...
11877  *
11878  *                  sd_send_scsi_USCSI_CMD->
11879  *                      sd_ssc_send-> - - - sdintr
11880  *                  ...
11881  *
11882  *                  if we think the return value should be treated as a
11883  *                  failure, we make the accessment here and print out
11884  *                  necessary by retrieving uscsi_cmd and sd_uscsi_info'
11885  *
11886  *                  ...
11887  *
11888  *              sd_ssc_fini
11889  *
11890  *
11891  *   Arguments: un - pointer to driver soft state (unit) structure for this
11892  *                   target.
11893  *
11894  * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11895  *                         uscsi_cmd and sd_uscsi_info.
11896  *                  NULL - if can not alloc memory for sd_ssc_t struct
11897  *
11898  *     Context: Kernel Thread.
11899  */
11900 static sd_ssc_t *
11901 sd_ssc_init(struct sd_lun *un)
11902 {
11903 	sd_ssc_t		*ssc;
11904 	struct uscsi_cmd	*ucmdp;
11905 	struct sd_uscsi_info	*uip;
11906 
11907 	ASSERT(un != NULL);
11908 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11909 
11910 	/*
11911 	 * Allocate sd_ssc_t structure
11912 	 */
11913 	ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11914 
11915 	/*
11916 	 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11917 	 */
11918 	ucmdp = scsi_uscsi_alloc();
11919 
11920 	/*
11921 	 * Allocate sd_uscsi_info structure
11922 	 */
11923 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11924 
11925 	ssc->ssc_uscsi_cmd = ucmdp;
11926 	ssc->ssc_uscsi_info = uip;
11927 	ssc->ssc_un = un;
11928 
11929 	return (ssc);
11930 }
11931 
11932 /*
11933  * Function: sd_ssc_fini
11934  *
11935  * Description: To free sd_ssc_t and it's hanging off
11936  *
11937  * Arguments: ssc - struct pointer of sd_ssc_t.
11938  */
11939 static void
11940 sd_ssc_fini(sd_ssc_t *ssc)
11941 {
11942 	scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11943 
11944 	if (ssc->ssc_uscsi_info != NULL) {
11945 		kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11946 		ssc->ssc_uscsi_info = NULL;
11947 	}
11948 
11949 	kmem_free(ssc, sizeof (sd_ssc_t));
11950 	ssc = NULL;
11951 }
11952 
11953 /*
11954  * Function: sd_ssc_send
11955  *
11956  * Description: Runs a USCSI command for user when called through sdioctl,
11957  *              or for the driver.
11958  *
11959  *   Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11960  *                    sd_uscsi_info in.
11961  *		incmd - ptr to a valid uscsi_cmd struct
11962  *		flag - bit flag, indicating open settings, 32/64 bit type
11963  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11964  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11965  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11966  *			to use the USCSI "direct" chain and bypass the normal
11967  *			command waitq.
11968  *
11969  * Return Code: 0 -  successful completion of the given command
11970  *		EIO - scsi_uscsi_handle_command() failed
11971  *		ENXIO  - soft state not found for specified dev
11972  *		ECANCELED - command cancelled due to low power
11973  *		EINVAL
11974  *		EFAULT - copyin/copyout error
11975  *		return code of scsi_uscsi_handle_command():
11976  *			EIO
11977  *			ENXIO
11978  *			EACCES
11979  *
11980  *     Context: Kernel Thread;
11981  *              Waits for command to complete. Can sleep.
11982  */
11983 static int
11984 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11985 	enum uio_seg dataspace, int path_flag)
11986 {
11987 	struct sd_uscsi_info	*uip;
11988 	struct uscsi_cmd	*uscmd;
11989 	struct sd_lun		*un;
11990 	dev_t			dev;
11991 
11992 	int	format = 0;
11993 	int	rval;
11994 
11995 	ASSERT(ssc != NULL);
11996 	un = ssc->ssc_un;
11997 	ASSERT(un != NULL);
11998 	uscmd = ssc->ssc_uscsi_cmd;
11999 	ASSERT(uscmd != NULL);
12000 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12001 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12002 		/*
12003 		 * If enter here, it indicates that the previous uscsi
12004 		 * command has not been processed by sd_ssc_assessment.
12005 		 * This is violating our rules of FMA telemetry processing.
12006 		 * We should print out this message and the last undisposed
12007 		 * uscsi command.
12008 		 */
12009 		if (uscmd->uscsi_cdb != NULL) {
12010 			SD_INFO(SD_LOG_SDTEST, un,
12011 			    "sd_ssc_send is missing the alternative "
12012 			    "sd_ssc_assessment when running command 0x%x.\n",
12013 			    uscmd->uscsi_cdb[0]);
12014 		}
12015 		/*
12016 		 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
12017 		 * the initial status.
12018 		 */
12019 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12020 	}
12021 
12022 	/*
12023 	 * We need to make sure sd_ssc_send will have sd_ssc_assessment
12024 	 * followed to avoid missing FMA telemetries.
12025 	 */
12026 	ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
12027 
12028 	/*
12029 	 * if USCSI_PMFAILFAST is set and un is in low power, fail the
12030 	 * command immediately.
12031 	 */
12032 	mutex_enter(SD_MUTEX(un));
12033 	mutex_enter(&un->un_pm_mutex);
12034 	if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
12035 	    SD_DEVICE_IS_IN_LOW_POWER(un)) {
12036 		SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
12037 		    "un:0x%p is in low power\n", un);
12038 		mutex_exit(&un->un_pm_mutex);
12039 		mutex_exit(SD_MUTEX(un));
12040 		return (ECANCELED);
12041 	}
12042 	mutex_exit(&un->un_pm_mutex);
12043 	mutex_exit(SD_MUTEX(un));
12044 
12045 #ifdef SDDEBUG
12046 	switch (dataspace) {
12047 	case UIO_USERSPACE:
12048 		SD_TRACE(SD_LOG_IO, un,
12049 		    "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
12050 		break;
12051 	case UIO_SYSSPACE:
12052 		SD_TRACE(SD_LOG_IO, un,
12053 		    "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
12054 		break;
12055 	default:
12056 		SD_TRACE(SD_LOG_IO, un,
12057 		    "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
12058 		break;
12059 	}
12060 #endif
12061 
12062 	rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
12063 	    SD_ADDRESS(un), &uscmd);
12064 	if (rval != 0) {
12065 		SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
12066 		    "scsi_uscsi_alloc_and_copyin failed\n", un);
12067 		return (rval);
12068 	}
12069 
12070 	if ((uscmd->uscsi_cdb != NULL) &&
12071 	    (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
12072 		mutex_enter(SD_MUTEX(un));
12073 		un->un_f_format_in_progress = TRUE;
12074 		mutex_exit(SD_MUTEX(un));
12075 		format = 1;
12076 	}
12077 
12078 	/*
12079 	 * Allocate an sd_uscsi_info struct and fill it with the info
12080 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
12081 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
12082 	 * since we allocate the buf here in this function, we do not
12083 	 * need to preserve the prior contents of b_private.
12084 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12085 	 */
12086 	uip = ssc->ssc_uscsi_info;
12087 	uip->ui_flags = path_flag;
12088 	uip->ui_cmdp = uscmd;
12089 
12090 	/*
12091 	 * Commands sent with priority are intended for error recovery
12092 	 * situations, and do not have retries performed.
12093 	 */
12094 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12095 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12096 	}
12097 	uscmd->uscsi_flags &= ~USCSI_NOINTR;
12098 
12099 	dev = SD_GET_DEV(un);
12100 	rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12101 	    sd_uscsi_strategy, NULL, uip);
12102 
12103 	/*
12104 	 * mark ssc_flags right after handle_cmd to make sure
12105 	 * the uscsi has been sent
12106 	 */
12107 	ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12108 
12109 #ifdef SDDEBUG
12110 	SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12111 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
12112 	    uscmd->uscsi_status, uscmd->uscsi_resid);
12113 	if (uscmd->uscsi_bufaddr != NULL) {
12114 		SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12115 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
12116 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12117 		if (dataspace == UIO_SYSSPACE) {
12118 			SD_DUMP_MEMORY(un, SD_LOG_IO,
12119 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
12120 			    uscmd->uscsi_buflen, SD_LOG_HEX);
12121 		}
12122 	}
12123 #endif
12124 
12125 	if (format == 1) {
12126 		mutex_enter(SD_MUTEX(un));
12127 		un->un_f_format_in_progress = FALSE;
12128 		mutex_exit(SD_MUTEX(un));
12129 	}
12130 
12131 	(void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
12132 
12133 	return (rval);
12134 }
12135 
12136 /*
12137  *     Function: sd_ssc_print
12138  *
12139  * Description: Print information available to the console.
12140  *
12141  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12142  *                    sd_uscsi_info in.
12143  *            sd_severity - log level.
12144  *     Context: Kernel thread or interrupt context.
12145  */
12146 static void
12147 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
12148 {
12149 	struct uscsi_cmd	*ucmdp;
12150 	struct scsi_device	*devp;
12151 	dev_info_t 		*devinfo;
12152 	uchar_t			*sensep;
12153 	int			senlen;
12154 	union scsi_cdb		*cdbp;
12155 	uchar_t			com;
12156 	extern struct scsi_key_strings scsi_cmds[];
12157 
12158 	ASSERT(ssc != NULL);
12159 	ASSERT(ssc->ssc_un != NULL);
12160 
12161 	if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
12162 		return;
12163 	ucmdp = ssc->ssc_uscsi_cmd;
12164 	devp = SD_SCSI_DEVP(ssc->ssc_un);
12165 	devinfo = SD_DEVINFO(ssc->ssc_un);
12166 	ASSERT(ucmdp != NULL);
12167 	ASSERT(devp != NULL);
12168 	ASSERT(devinfo != NULL);
12169 	sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
12170 	senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
12171 	cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
12172 
12173 	/* In certain case (like DOORLOCK), the cdb could be NULL. */
12174 	if (cdbp == NULL)
12175 		return;
12176 	/* We don't print log if no sense data available. */
12177 	if (senlen == 0)
12178 		sensep = NULL;
12179 	com = cdbp->scc_cmd;
12180 	scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
12181 	    scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
12182 }
12183 
12184 /*
12185  *     Function: sd_ssc_assessment
12186  *
12187  * Description: We use this function to make an assessment at the point
12188  *              where SD driver may encounter a potential error.
12189  *
12190  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12191  *                  sd_uscsi_info in.
12192  *            tp_assess - a hint of strategy for ereport posting.
12193  *            Possible values of tp_assess include:
12194  *                SD_FMT_IGNORE - we don't post any ereport because we're
12195  *                sure that it is ok to ignore the underlying problems.
12196  *                SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
12197  *                but it might be not correct to ignore the underlying hardware
12198  *                error.
12199  *                SD_FMT_STATUS_CHECK - we will post an ereport with the
12200  *                payload driver-assessment of value "fail" or
12201  *                "fatal"(depending on what information we have here). This
12202  *                assessment value is usually set when SD driver think there
12203  *                is a potential error occurred(Typically, when return value
12204  *                of the SCSI command is EIO).
12205  *                SD_FMT_STANDARD - we will post an ereport with the payload
12206  *                driver-assessment of value "info". This assessment value is
12207  *                set when the SCSI command returned successfully and with
12208  *                sense data sent back.
12209  *
12210  *     Context: Kernel thread.
12211  */
12212 static void
12213 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
12214 {
12215 	int senlen = 0;
12216 	struct uscsi_cmd *ucmdp = NULL;
12217 	struct sd_lun *un;
12218 
12219 	ASSERT(ssc != NULL);
12220 	un = ssc->ssc_un;
12221 	ASSERT(un != NULL);
12222 	ucmdp = ssc->ssc_uscsi_cmd;
12223 	ASSERT(ucmdp != NULL);
12224 
12225 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12226 		ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
12227 	} else {
12228 		/*
12229 		 * If enter here, it indicates that we have a wrong
12230 		 * calling sequence of sd_ssc_send and sd_ssc_assessment,
12231 		 * both of which should be called in a pair in case of
12232 		 * loss of FMA telemetries.
12233 		 */
12234 		if (ucmdp->uscsi_cdb != NULL) {
12235 			SD_INFO(SD_LOG_SDTEST, un,
12236 			    "sd_ssc_assessment is missing the "
12237 			    "alternative sd_ssc_send when running 0x%x, "
12238 			    "or there are superfluous sd_ssc_assessment for "
12239 			    "the same sd_ssc_send.\n",
12240 			    ucmdp->uscsi_cdb[0]);
12241 		}
12242 		/*
12243 		 * Set the ssc_flags to the initial value to avoid passing
12244 		 * down dirty flags to the following sd_ssc_send function.
12245 		 */
12246 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12247 		return;
12248 	}
12249 
12250 	/*
12251 	 * Only handle an issued command which is waiting for assessment.
12252 	 * A command which is not issued will not have
12253 	 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
12254 	 */
12255 	if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
12256 		sd_ssc_print(ssc, SCSI_ERR_INFO);
12257 		return;
12258 	} else {
12259 		/*
12260 		 * For an issued command, we should clear this flag in
12261 		 * order to make the sd_ssc_t structure be used off
12262 		 * multiple uscsi commands.
12263 		 */
12264 		ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
12265 	}
12266 
12267 	/*
12268 	 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
12269 	 * commands here. And we should clear the ssc_flags before return.
12270 	 */
12271 	if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
12272 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12273 		return;
12274 	}
12275 
12276 	switch (tp_assess) {
12277 	case SD_FMT_IGNORE:
12278 	case SD_FMT_IGNORE_COMPROMISE:
12279 		break;
12280 	case SD_FMT_STATUS_CHECK:
12281 		/*
12282 		 * For a failed command(including the succeeded command
12283 		 * with invalid data sent back).
12284 		 */
12285 		sd_ssc_post(ssc, SD_FM_DRV_FATAL);
12286 		break;
12287 	case SD_FMT_STANDARD:
12288 		/*
12289 		 * Always for the succeeded commands probably with sense
12290 		 * data sent back.
12291 		 * Limitation:
12292 		 *	We can only handle a succeeded command with sense
12293 		 *	data sent back when auto-request-sense is enabled.
12294 		 */
12295 		senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
12296 		    ssc->ssc_uscsi_cmd->uscsi_rqresid;
12297 		if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
12298 		    (un->un_f_arq_enabled == TRUE) &&
12299 		    senlen > 0 &&
12300 		    ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
12301 			sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
12302 		}
12303 		break;
12304 	default:
12305 		/*
12306 		 * Should not have other type of assessment.
12307 		 */
12308 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
12309 		    "sd_ssc_assessment got wrong "
12310 		    "sd_type_assessment %d.\n", tp_assess);
12311 		break;
12312 	}
12313 	/*
12314 	 * Clear up the ssc_flags before return.
12315 	 */
12316 	ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12317 }
12318 
12319 /*
12320  *    Function: sd_ssc_post
12321  *
12322  * Description: 1. read the driver property to get fm-scsi-log flag.
12323  *              2. print log if fm_log_capable is non-zero.
12324  *              3. call sd_ssc_ereport_post to post ereport if possible.
12325  *
12326  *    Context: May be called from kernel thread or interrupt context.
12327  */
12328 static void
12329 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
12330 {
12331 	struct sd_lun	*un;
12332 	int		sd_severity;
12333 
12334 	ASSERT(ssc != NULL);
12335 	un = ssc->ssc_un;
12336 	ASSERT(un != NULL);
12337 
12338 	/*
12339 	 * We may enter here from sd_ssc_assessment(for USCSI command) or
12340 	 * by directly called from sdintr context.
12341 	 * We don't handle a non-disk drive(CD-ROM, removable media).
12342 	 * Clear the ssc_flags before return in case we've set
12343 	 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12344 	 * driver.
12345 	 */
12346 	if (ISCD(un) || un->un_f_has_removable_media) {
12347 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12348 		return;
12349 	}
12350 
12351 	switch (sd_assess) {
12352 		case SD_FM_DRV_FATAL:
12353 			sd_severity = SCSI_ERR_FATAL;
12354 			break;
12355 		case SD_FM_DRV_RECOVERY:
12356 			sd_severity = SCSI_ERR_RECOVERED;
12357 			break;
12358 		case SD_FM_DRV_RETRY:
12359 			sd_severity = SCSI_ERR_RETRYABLE;
12360 			break;
12361 		case SD_FM_DRV_NOTICE:
12362 			sd_severity = SCSI_ERR_INFO;
12363 			break;
12364 		default:
12365 			sd_severity = SCSI_ERR_UNKNOWN;
12366 	}
12367 	/* print log */
12368 	sd_ssc_print(ssc, sd_severity);
12369 
12370 	/* always post ereport */
12371 	sd_ssc_ereport_post(ssc, sd_assess);
12372 }
12373 
12374 /*
12375  *    Function: sd_ssc_set_info
12376  *
12377  * Description: Mark ssc_flags and set ssc_info which would be the
12378  *              payload of uderr ereport. This function will cause
12379  *              sd_ssc_ereport_post to post uderr ereport only.
12380  *              Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12381  *              the function will also call SD_ERROR or scsi_log for a
12382  *              CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12383  *
12384  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12385  *                  sd_uscsi_info in.
12386  *            ssc_flags - indicate the sub-category of a uderr.
12387  *            comp - this argument is meaningful only when
12388  *                   ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12389  *                   values include:
12390  *                   > 0, SD_ERROR is used with comp as the driver logging
12391  *                   component;
12392  *                   = 0, scsi-log is used to log error telemetries;
12393  *                   < 0, no log available for this telemetry.
12394  *
12395  *    Context: Kernel thread or interrupt context
12396  */
12397 static void
12398 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12399 {
12400 	va_list	ap;
12401 
12402 	ASSERT(ssc != NULL);
12403 	ASSERT(ssc->ssc_un != NULL);
12404 
12405 	ssc->ssc_flags |= ssc_flags;
12406 	va_start(ap, fmt);
12407 	(void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12408 	va_end(ap);
12409 
12410 	/*
12411 	 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12412 	 * with invalid data sent back. For non-uscsi command, the
12413 	 * following code will be bypassed.
12414 	 */
12415 	if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12416 		if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12417 			/*
12418 			 * If the error belong to certain component and we
12419 			 * do not want it to show up on the console, we
12420 			 * will use SD_ERROR, otherwise scsi_log is
12421 			 * preferred.
12422 			 */
12423 			if (comp > 0) {
12424 				SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12425 			} else if (comp == 0) {
12426 				scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12427 				    CE_WARN, ssc->ssc_info);
12428 			}
12429 		}
12430 	}
12431 }
12432 
12433 /*
12434  *    Function: sd_buf_iodone
12435  *
12436  * Description: Frees the sd_xbuf & returns the buf to its originator.
12437  *
12438  *     Context: May be called from interrupt context.
12439  */
12440 /* ARGSUSED */
12441 static void
12442 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12443 {
12444 	struct sd_xbuf *xp;
12445 
12446 	ASSERT(un != NULL);
12447 	ASSERT(bp != NULL);
12448 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12449 
12450 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12451 
12452 	xp = SD_GET_XBUF(bp);
12453 	ASSERT(xp != NULL);
12454 
12455 	/* xbuf is gone after this */
12456 	if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12457 		mutex_enter(SD_MUTEX(un));
12458 
12459 		/*
12460 		 * Grab time when the cmd completed.
12461 		 * This is used for determining if the system has been
12462 		 * idle long enough to make it idle to the PM framework.
12463 		 * This is for lowering the overhead, and therefore improving
12464 		 * performance per I/O operation.
12465 		 */
12466 		un->un_pm_idle_time = gethrtime();
12467 
12468 		un->un_ncmds_in_driver--;
12469 		ASSERT(un->un_ncmds_in_driver >= 0);
12470 		SD_INFO(SD_LOG_IO, un,
12471 		    "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12472 		    un->un_ncmds_in_driver);
12473 
12474 		mutex_exit(SD_MUTEX(un));
12475 	}
12476 
12477 	biodone(bp);				/* bp is gone after this */
12478 
12479 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12480 }
12481 
12482 
12483 /*
12484  *    Function: sd_uscsi_iodone
12485  *
12486  * Description: Frees the sd_xbuf & returns the buf to its originator.
12487  *
12488  *     Context: May be called from interrupt context.
12489  */
12490 /* ARGSUSED */
12491 static void
12492 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12493 {
12494 	struct sd_xbuf *xp;
12495 
12496 	ASSERT(un != NULL);
12497 	ASSERT(bp != NULL);
12498 
12499 	xp = SD_GET_XBUF(bp);
12500 	ASSERT(xp != NULL);
12501 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12502 
12503 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12504 
12505 	bp->b_private = xp->xb_private;
12506 
12507 	mutex_enter(SD_MUTEX(un));
12508 
12509 	/*
12510 	 * Grab time when the cmd completed.
12511 	 * This is used for determining if the system has been
12512 	 * idle long enough to make it idle to the PM framework.
12513 	 * This is for lowering the overhead, and therefore improving
12514 	 * performance per I/O operation.
12515 	 */
12516 	un->un_pm_idle_time = gethrtime();
12517 
12518 	un->un_ncmds_in_driver--;
12519 	ASSERT(un->un_ncmds_in_driver >= 0);
12520 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12521 	    un->un_ncmds_in_driver);
12522 
12523 	mutex_exit(SD_MUTEX(un));
12524 
12525 	if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12526 	    SENSE_LENGTH) {
12527 		kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12528 		    MAX_SENSE_LENGTH);
12529 	} else {
12530 		kmem_free(xp, sizeof (struct sd_xbuf));
12531 	}
12532 
12533 	biodone(bp);
12534 
12535 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12536 }
12537 
12538 
12539 /*
12540  *    Function: sd_mapblockaddr_iostart
12541  *
12542  * Description: Verify request lies within the partition limits for
12543  *		the indicated minor device.  Issue "overrun" buf if
12544  *		request would exceed partition range.  Converts
12545  *		partition-relative block address to absolute.
12546  *
12547  *              Upon exit of this function:
12548  *              1.I/O is aligned
12549  *                 xp->xb_blkno represents the absolute sector address
12550  *              2.I/O is misaligned
12551  *                 xp->xb_blkno represents the absolute logical block address
12552  *                 based on DEV_BSIZE. The logical block address will be
12553  *                 converted to physical sector address in sd_mapblocksize_\
12554  *                 iostart.
12555  *              3.I/O is misaligned but is aligned in "overrun" buf
12556  *                 xp->xb_blkno represents the absolute logical block address
12557  *                 based on DEV_BSIZE. The logical block address will be
12558  *                 converted to physical sector address in sd_mapblocksize_\
12559  *                 iostart. But no RMW will be issued in this case.
12560  *
12561  *     Context: Can sleep
12562  *
12563  *      Issues: This follows what the old code did, in terms of accessing
12564  *		some of the partition info in the unit struct without holding
12565  *		the mutext.  This is a general issue, if the partition info
12566  *		can be altered while IO is in progress... as soon as we send
12567  *		a buf, its partitioning can be invalid before it gets to the
12568  *		device.  Probably the right fix is to move partitioning out
12569  *		of the driver entirely.
12570  */
12571 
12572 static void
12573 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12574 {
12575 	diskaddr_t	nblocks;	/* #blocks in the given partition */
12576 	daddr_t	blocknum;	/* Block number specified by the buf */
12577 	size_t	requested_nblocks;
12578 	size_t	available_nblocks;
12579 	int	partition;
12580 	diskaddr_t	partition_offset;
12581 	struct sd_xbuf *xp;
12582 	int secmask = 0, blknomask = 0;
12583 	ushort_t is_aligned = TRUE;
12584 
12585 	ASSERT(un != NULL);
12586 	ASSERT(bp != NULL);
12587 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12588 
12589 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12590 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12591 
12592 	xp = SD_GET_XBUF(bp);
12593 	ASSERT(xp != NULL);
12594 
12595 	/*
12596 	 * If the geometry is not indicated as valid, attempt to access
12597 	 * the unit & verify the geometry/label. This can be the case for
12598 	 * removable-media devices, of if the device was opened in
12599 	 * NDELAY/NONBLOCK mode.
12600 	 */
12601 	partition = SDPART(bp->b_edev);
12602 
12603 	if (!SD_IS_VALID_LABEL(un)) {
12604 		sd_ssc_t *ssc;
12605 		/*
12606 		 * Initialize sd_ssc_t for internal uscsi commands
12607 		 * In case of potential porformance issue, we need
12608 		 * to alloc memory only if there is invalid label
12609 		 */
12610 		ssc = sd_ssc_init(un);
12611 
12612 		if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12613 			/*
12614 			 * For removable devices it is possible to start an
12615 			 * I/O without a media by opening the device in nodelay
12616 			 * mode. Also for writable CDs there can be many
12617 			 * scenarios where there is no geometry yet but volume
12618 			 * manager is trying to issue a read() just because
12619 			 * it can see TOC on the CD. So do not print a message
12620 			 * for removables.
12621 			 */
12622 			if (!un->un_f_has_removable_media) {
12623 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12624 				    "i/o to invalid geometry\n");
12625 			}
12626 			bioerror(bp, EIO);
12627 			bp->b_resid = bp->b_bcount;
12628 			SD_BEGIN_IODONE(index, un, bp);
12629 
12630 			sd_ssc_fini(ssc);
12631 			return;
12632 		}
12633 		sd_ssc_fini(ssc);
12634 	}
12635 
12636 	nblocks = 0;
12637 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
12638 	    &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12639 
12640 	if (un->un_f_enable_rmw) {
12641 		blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12642 		secmask = un->un_phy_blocksize - 1;
12643 	} else {
12644 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12645 		secmask = un->un_tgt_blocksize - 1;
12646 	}
12647 
12648 	if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12649 		is_aligned = FALSE;
12650 	}
12651 
12652 	if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12653 		/*
12654 		 * If I/O is aligned, no need to involve RMW(Read Modify Write)
12655 		 * Convert the logical block number to target's physical sector
12656 		 * number.
12657 		 */
12658 		if (is_aligned) {
12659 			xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12660 		} else {
12661 			/*
12662 			 * There is no RMW if we're just reading, so don't
12663 			 * warn or error out because of it.
12664 			 */
12665 			if (bp->b_flags & B_READ) {
12666 				/*EMPTY*/
12667 			} else if (!un->un_f_enable_rmw &&
12668 			    un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) {
12669 				bp->b_flags |= B_ERROR;
12670 				goto error_exit;
12671 			} else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) {
12672 				mutex_enter(SD_MUTEX(un));
12673 				if (!un->un_f_enable_rmw &&
12674 				    un->un_rmw_msg_timeid == NULL) {
12675 					scsi_log(SD_DEVINFO(un), sd_label,
12676 					    CE_WARN, "I/O request is not "
12677 					    "aligned with %d disk sector size. "
12678 					    "It is handled through Read Modify "
12679 					    "Write but the performance is "
12680 					    "very low.\n",
12681 					    un->un_tgt_blocksize);
12682 					un->un_rmw_msg_timeid =
12683 					    timeout(sd_rmw_msg_print_handler,
12684 					    un, SD_RMW_MSG_PRINT_TIMEOUT);
12685 				} else {
12686 					un->un_rmw_incre_count ++;
12687 				}
12688 				mutex_exit(SD_MUTEX(un));
12689 			}
12690 
12691 			nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12692 			partition_offset = SD_TGT2SYSBLOCK(un,
12693 			    partition_offset);
12694 		}
12695 	}
12696 
12697 	/*
12698 	 * blocknum is the starting block number of the request. At this
12699 	 * point it is still relative to the start of the minor device.
12700 	 */
12701 	blocknum = xp->xb_blkno;
12702 
12703 	/*
12704 	 * Legacy: If the starting block number is one past the last block
12705 	 * in the partition, do not set B_ERROR in the buf.
12706 	 */
12707 	if (blocknum == nblocks)  {
12708 		goto error_exit;
12709 	}
12710 
12711 	/*
12712 	 * Confirm that the first block of the request lies within the
12713 	 * partition limits. Also the requested number of bytes must be
12714 	 * a multiple of the system block size.
12715 	 */
12716 	if ((blocknum < 0) || (blocknum >= nblocks) ||
12717 	    ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12718 		bp->b_flags |= B_ERROR;
12719 		goto error_exit;
12720 	}
12721 
12722 	/*
12723 	 * If the requsted # blocks exceeds the available # blocks, that
12724 	 * is an overrun of the partition.
12725 	 */
12726 	if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12727 		requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12728 	} else {
12729 		requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12730 	}
12731 
12732 	available_nblocks = (size_t)(nblocks - blocknum);
12733 	ASSERT(nblocks >= blocknum);
12734 
12735 	if (requested_nblocks > available_nblocks) {
12736 		size_t resid;
12737 
12738 		/*
12739 		 * Allocate an "overrun" buf to allow the request to proceed
12740 		 * for the amount of space available in the partition. The
12741 		 * amount not transferred will be added into the b_resid
12742 		 * when the operation is complete. The overrun buf
12743 		 * replaces the original buf here, and the original buf
12744 		 * is saved inside the overrun buf, for later use.
12745 		 */
12746 		if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12747 			resid = SD_TGTBLOCKS2BYTES(un,
12748 			    (offset_t)(requested_nblocks - available_nblocks));
12749 		} else {
12750 			resid = SD_SYSBLOCKS2BYTES(
12751 			    (offset_t)(requested_nblocks - available_nblocks));
12752 		}
12753 
12754 		size_t count = bp->b_bcount - resid;
12755 		/*
12756 		 * Note: count is an unsigned entity thus it'll NEVER
12757 		 * be less than 0 so ASSERT the original values are
12758 		 * correct.
12759 		 */
12760 		ASSERT(bp->b_bcount >= resid);
12761 
12762 		bp = sd_bioclone_alloc(bp, count, blocknum,
12763 		    (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12764 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12765 		ASSERT(xp != NULL);
12766 	}
12767 
12768 	/* At this point there should be no residual for this buf. */
12769 	ASSERT(bp->b_resid == 0);
12770 
12771 	/* Convert the block number to an absolute address. */
12772 	xp->xb_blkno += partition_offset;
12773 
12774 	SD_NEXT_IOSTART(index, un, bp);
12775 
12776 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12777 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12778 
12779 	return;
12780 
12781 error_exit:
12782 	bp->b_resid = bp->b_bcount;
12783 	SD_BEGIN_IODONE(index, un, bp);
12784 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12785 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12786 }
12787 
12788 
12789 /*
12790  *    Function: sd_mapblockaddr_iodone
12791  *
12792  * Description: Completion-side processing for partition management.
12793  *
12794  *     Context: May be called under interrupt context
12795  */
12796 
12797 static void
12798 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12799 {
12800 	/* int	partition; */	/* Not used, see below. */
12801 	ASSERT(un != NULL);
12802 	ASSERT(bp != NULL);
12803 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12804 
12805 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12806 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12807 
12808 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12809 		/*
12810 		 * We have an "overrun" buf to deal with...
12811 		 */
12812 		struct sd_xbuf	*xp;
12813 		struct buf	*obp;	/* ptr to the original buf */
12814 
12815 		xp = SD_GET_XBUF(bp);
12816 		ASSERT(xp != NULL);
12817 
12818 		/* Retrieve the pointer to the original buf */
12819 		obp = (struct buf *)xp->xb_private;
12820 		ASSERT(obp != NULL);
12821 
12822 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12823 		bioerror(obp, bp->b_error);
12824 
12825 		sd_bioclone_free(bp);
12826 
12827 		/*
12828 		 * Get back the original buf.
12829 		 * Note that since the restoration of xb_blkno below
12830 		 * was removed, the sd_xbuf is not needed.
12831 		 */
12832 		bp = obp;
12833 		/*
12834 		 * xp = SD_GET_XBUF(bp);
12835 		 * ASSERT(xp != NULL);
12836 		 */
12837 	}
12838 
12839 	/*
12840 	 * Convert sd->xb_blkno back to a minor-device relative value.
12841 	 * Note: this has been commented out, as it is not needed in the
12842 	 * current implementation of the driver (ie, since this function
12843 	 * is at the top of the layering chains, so the info will be
12844 	 * discarded) and it is in the "hot" IO path.
12845 	 *
12846 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
12847 	 * xp->xb_blkno -= un->un_offset[partition];
12848 	 */
12849 
12850 	SD_NEXT_IODONE(index, un, bp);
12851 
12852 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12853 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12854 }
12855 
12856 
12857 /*
12858  *    Function: sd_mapblocksize_iostart
12859  *
12860  * Description: Convert between system block size (un->un_sys_blocksize)
12861  *		and target block size (un->un_tgt_blocksize).
12862  *
12863  *     Context: Can sleep to allocate resources.
12864  *
12865  * Assumptions: A higher layer has already performed any partition validation,
12866  *		and converted the xp->xb_blkno to an absolute value relative
12867  *		to the start of the device.
12868  *
12869  *		It is also assumed that the higher layer has implemented
12870  *		an "overrun" mechanism for the case where the request would
12871  *		read/write beyond the end of a partition.  In this case we
12872  *		assume (and ASSERT) that bp->b_resid == 0.
12873  *
12874  *		Note: The implementation for this routine assumes the target
12875  *		block size remains constant between allocation and transport.
12876  */
12877 
12878 static void
12879 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12880 {
12881 	struct sd_mapblocksize_info	*bsp;
12882 	struct sd_xbuf			*xp;
12883 	offset_t first_byte;
12884 	daddr_t	start_block, end_block;
12885 	daddr_t	request_bytes;
12886 	ushort_t is_aligned = FALSE;
12887 
12888 	ASSERT(un != NULL);
12889 	ASSERT(bp != NULL);
12890 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12891 	ASSERT(bp->b_resid == 0);
12892 
12893 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12894 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12895 
12896 	/*
12897 	 * For a non-writable CD, a write request is an error
12898 	 */
12899 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12900 	    (un->un_f_mmc_writable_media == FALSE)) {
12901 		bioerror(bp, EIO);
12902 		bp->b_resid = bp->b_bcount;
12903 		SD_BEGIN_IODONE(index, un, bp);
12904 		return;
12905 	}
12906 
12907 	/*
12908 	 * We do not need a shadow buf if the device is using
12909 	 * un->un_sys_blocksize as its block size or if bcount == 0.
12910 	 * In this case there is no layer-private data block allocated.
12911 	 */
12912 	if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12913 	    (bp->b_bcount == 0)) {
12914 		goto done;
12915 	}
12916 
12917 #if defined(__i386) || defined(__amd64)
12918 	/* We do not support non-block-aligned transfers for ROD devices */
12919 	ASSERT(!ISROD(un));
12920 #endif
12921 
12922 	xp = SD_GET_XBUF(bp);
12923 	ASSERT(xp != NULL);
12924 
12925 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12926 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12927 	    un->un_tgt_blocksize, DEV_BSIZE);
12928 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12929 	    "request start block:0x%x\n", xp->xb_blkno);
12930 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12931 	    "request len:0x%x\n", bp->b_bcount);
12932 
12933 	/*
12934 	 * Allocate the layer-private data area for the mapblocksize layer.
12935 	 * Layers are allowed to use the xp_private member of the sd_xbuf
12936 	 * struct to store the pointer to their layer-private data block, but
12937 	 * each layer also has the responsibility of restoring the prior
12938 	 * contents of xb_private before returning the buf/xbuf to the
12939 	 * higher layer that sent it.
12940 	 *
12941 	 * Here we save the prior contents of xp->xb_private into the
12942 	 * bsp->mbs_oprivate field of our layer-private data area. This value
12943 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12944 	 * the layer-private area and returning the buf/xbuf to the layer
12945 	 * that sent it.
12946 	 *
12947 	 * Note that here we use kmem_zalloc for the allocation as there are
12948 	 * parts of the mapblocksize code that expect certain fields to be
12949 	 * zero unless explicitly set to a required value.
12950 	 */
12951 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12952 	bsp->mbs_oprivate = xp->xb_private;
12953 	xp->xb_private = bsp;
12954 
12955 	/*
12956 	 * This treats the data on the disk (target) as an array of bytes.
12957 	 * first_byte is the byte offset, from the beginning of the device,
12958 	 * to the location of the request. This is converted from a
12959 	 * un->un_sys_blocksize block address to a byte offset, and then back
12960 	 * to a block address based upon a un->un_tgt_blocksize block size.
12961 	 *
12962 	 * xp->xb_blkno should be absolute upon entry into this function,
12963 	 * but, but it is based upon partitions that use the "system"
12964 	 * block size. It must be adjusted to reflect the block size of
12965 	 * the target.
12966 	 *
12967 	 * Note that end_block is actually the block that follows the last
12968 	 * block of the request, but that's what is needed for the computation.
12969 	 */
12970 	first_byte  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12971 	if (un->un_f_enable_rmw) {
12972 		start_block = xp->xb_blkno =
12973 		    (first_byte / un->un_phy_blocksize) *
12974 		    (un->un_phy_blocksize / DEV_BSIZE);
12975 		end_block   = ((first_byte + bp->b_bcount +
12976 		    un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
12977 		    (un->un_phy_blocksize / DEV_BSIZE);
12978 	} else {
12979 		start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12980 		end_block   = (first_byte + bp->b_bcount +
12981 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
12982 	}
12983 
12984 	/* request_bytes is rounded up to a multiple of the target block size */
12985 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12986 
12987 	/*
12988 	 * See if the starting address of the request and the request
12989 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12990 	 * then we do not need to allocate a shadow buf to handle the request.
12991 	 */
12992 	if (un->un_f_enable_rmw) {
12993 		if (((first_byte % un->un_phy_blocksize) == 0) &&
12994 		    ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
12995 			is_aligned = TRUE;
12996 		}
12997 	} else {
12998 		if (((first_byte % un->un_tgt_blocksize) == 0) &&
12999 		    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
13000 			is_aligned = TRUE;
13001 		}
13002 	}
13003 
13004 	if ((bp->b_flags & B_READ) == 0) {
13005 		/*
13006 		 * Lock the range for a write operation. An aligned request is
13007 		 * considered a simple write; otherwise the request must be a
13008 		 * read-modify-write.
13009 		 */
13010 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
13011 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
13012 	}
13013 
13014 	/*
13015 	 * Alloc a shadow buf if the request is not aligned. Also, this is
13016 	 * where the READ command is generated for a read-modify-write. (The
13017 	 * write phase is deferred until after the read completes.)
13018 	 */
13019 	if (is_aligned == FALSE) {
13020 
13021 		struct sd_mapblocksize_info	*shadow_bsp;
13022 		struct sd_xbuf	*shadow_xp;
13023 		struct buf	*shadow_bp;
13024 
13025 		/*
13026 		 * Allocate the shadow buf and it associated xbuf. Note that
13027 		 * after this call the xb_blkno value in both the original
13028 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
13029 		 * same: absolute relative to the start of the device, and
13030 		 * adjusted for the target block size. The b_blkno in the
13031 		 * shadow buf will also be set to this value. We should never
13032 		 * change b_blkno in the original bp however.
13033 		 *
13034 		 * Note also that the shadow buf will always need to be a
13035 		 * READ command, regardless of whether the incoming command
13036 		 * is a READ or a WRITE.
13037 		 */
13038 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
13039 		    xp->xb_blkno,
13040 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
13041 
13042 		shadow_xp = SD_GET_XBUF(shadow_bp);
13043 
13044 		/*
13045 		 * Allocate the layer-private data for the shadow buf.
13046 		 * (No need to preserve xb_private in the shadow xbuf.)
13047 		 */
13048 		shadow_xp->xb_private = shadow_bsp =
13049 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
13050 
13051 		/*
13052 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
13053 		 * to figure out where the start of the user data is (based upon
13054 		 * the system block size) in the data returned by the READ
13055 		 * command (which will be based upon the target blocksize). Note
13056 		 * that this is only really used if the request is unaligned.
13057 		 */
13058 		if (un->un_f_enable_rmw) {
13059 			bsp->mbs_copy_offset = (ssize_t)(first_byte -
13060 			    ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
13061 			ASSERT((bsp->mbs_copy_offset >= 0) &&
13062 			    (bsp->mbs_copy_offset < un->un_phy_blocksize));
13063 		} else {
13064 			bsp->mbs_copy_offset = (ssize_t)(first_byte -
13065 			    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
13066 			ASSERT((bsp->mbs_copy_offset >= 0) &&
13067 			    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
13068 		}
13069 
13070 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
13071 
13072 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
13073 
13074 		/* Transfer the wmap (if any) to the shadow buf */
13075 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
13076 		bsp->mbs_wmp = NULL;
13077 
13078 		/*
13079 		 * The shadow buf goes on from here in place of the
13080 		 * original buf.
13081 		 */
13082 		shadow_bsp->mbs_orig_bp = bp;
13083 		bp = shadow_bp;
13084 	}
13085 
13086 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13087 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
13088 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13089 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
13090 	    request_bytes);
13091 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13092 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
13093 
13094 done:
13095 	SD_NEXT_IOSTART(index, un, bp);
13096 
13097 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13098 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
13099 }
13100 
13101 
13102 /*
13103  *    Function: sd_mapblocksize_iodone
13104  *
13105  * Description: Completion side processing for block-size mapping.
13106  *
13107  *     Context: May be called under interrupt context
13108  */
13109 
13110 static void
13111 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
13112 {
13113 	struct sd_mapblocksize_info	*bsp;
13114 	struct sd_xbuf	*xp;
13115 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
13116 	struct buf	*orig_bp;	/* ptr to the original buf */
13117 	offset_t	shadow_end;
13118 	offset_t	request_end;
13119 	offset_t	shadow_start;
13120 	ssize_t		copy_offset;
13121 	size_t		copy_length;
13122 	size_t		shortfall;
13123 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
13124 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
13125 
13126 	ASSERT(un != NULL);
13127 	ASSERT(bp != NULL);
13128 
13129 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13130 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13131 
13132 	/*
13133 	 * There is no shadow buf or layer-private data if the target is
13134 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
13135 	 */
13136 	if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
13137 	    (bp->b_bcount == 0)) {
13138 		goto exit;
13139 	}
13140 
13141 	xp = SD_GET_XBUF(bp);
13142 	ASSERT(xp != NULL);
13143 
13144 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
13145 	bsp = xp->xb_private;
13146 
13147 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13148 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13149 
13150 	if (is_write) {
13151 		/*
13152 		 * For a WRITE request we must free up the block range that
13153 		 * we have locked up.  This holds regardless of whether this is
13154 		 * an aligned write request or a read-modify-write request.
13155 		 */
13156 		sd_range_unlock(un, bsp->mbs_wmp);
13157 		bsp->mbs_wmp = NULL;
13158 	}
13159 
13160 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13161 		/*
13162 		 * An aligned read or write command will have no shadow buf;
13163 		 * there is not much else to do with it.
13164 		 */
13165 		goto done;
13166 	}
13167 
13168 	orig_bp = bsp->mbs_orig_bp;
13169 	ASSERT(orig_bp != NULL);
13170 	orig_xp = SD_GET_XBUF(orig_bp);
13171 	ASSERT(orig_xp != NULL);
13172 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13173 
13174 	if (!is_write && has_wmap) {
13175 		/*
13176 		 * A READ with a wmap means this is the READ phase of a
13177 		 * read-modify-write. If an error occurred on the READ then
13178 		 * we do not proceed with the WRITE phase or copy any data.
13179 		 * Just release the write maps and return with an error.
13180 		 */
13181 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13182 			orig_bp->b_resid = orig_bp->b_bcount;
13183 			bioerror(orig_bp, bp->b_error);
13184 			sd_range_unlock(un, bsp->mbs_wmp);
13185 			goto freebuf_done;
13186 		}
13187 	}
13188 
13189 	/*
13190 	 * Here is where we set up to copy the data from the shadow buf
13191 	 * into the space associated with the original buf.
13192 	 *
13193 	 * To deal with the conversion between block sizes, these
13194 	 * computations treat the data as an array of bytes, with the
13195 	 * first byte (byte 0) corresponding to the first byte in the
13196 	 * first block on the disk.
13197 	 */
13198 
13199 	/*
13200 	 * shadow_start and shadow_len indicate the location and size of
13201 	 * the data returned with the shadow IO request.
13202 	 */
13203 	if (un->un_f_enable_rmw) {
13204 		shadow_start  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
13205 	} else {
13206 		shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13207 	}
13208 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
13209 
13210 	/*
13211 	 * copy_offset gives the offset (in bytes) from the start of the first
13212 	 * block of the READ request to the beginning of the data.  We retrieve
13213 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13214 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
13215 	 * data to be copied (in bytes).
13216 	 */
13217 	copy_offset  = bsp->mbs_copy_offset;
13218 	if (un->un_f_enable_rmw) {
13219 		ASSERT((copy_offset >= 0) &&
13220 		    (copy_offset < un->un_phy_blocksize));
13221 	} else {
13222 		ASSERT((copy_offset >= 0) &&
13223 		    (copy_offset < un->un_tgt_blocksize));
13224 	}
13225 
13226 	copy_length  = orig_bp->b_bcount;
13227 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
13228 
13229 	/*
13230 	 * Set up the resid and error fields of orig_bp as appropriate.
13231 	 */
13232 	if (shadow_end >= request_end) {
13233 		/* We got all the requested data; set resid to zero */
13234 		orig_bp->b_resid = 0;
13235 	} else {
13236 		/*
13237 		 * We failed to get enough data to fully satisfy the original
13238 		 * request. Just copy back whatever data we got and set
13239 		 * up the residual and error code as required.
13240 		 *
13241 		 * 'shortfall' is the amount by which the data received with the
13242 		 * shadow buf has "fallen short" of the requested amount.
13243 		 */
13244 		shortfall = (size_t)(request_end - shadow_end);
13245 
13246 		if (shortfall > orig_bp->b_bcount) {
13247 			/*
13248 			 * We did not get enough data to even partially
13249 			 * fulfill the original request.  The residual is
13250 			 * equal to the amount requested.
13251 			 */
13252 			orig_bp->b_resid = orig_bp->b_bcount;
13253 		} else {
13254 			/*
13255 			 * We did not get all the data that we requested
13256 			 * from the device, but we will try to return what
13257 			 * portion we did get.
13258 			 */
13259 			orig_bp->b_resid = shortfall;
13260 		}
13261 		ASSERT(copy_length >= orig_bp->b_resid);
13262 		copy_length  -= orig_bp->b_resid;
13263 	}
13264 
13265 	/* Propagate the error code from the shadow buf to the original buf */
13266 	bioerror(orig_bp, bp->b_error);
13267 
13268 	if (is_write) {
13269 		goto freebuf_done;	/* No data copying for a WRITE */
13270 	}
13271 
13272 	if (has_wmap) {
13273 		/*
13274 		 * This is a READ command from the READ phase of a
13275 		 * read-modify-write request. We have to copy the data given
13276 		 * by the user OVER the data returned by the READ command,
13277 		 * then convert the command from a READ to a WRITE and send
13278 		 * it back to the target.
13279 		 */
13280 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13281 		    copy_length);
13282 
13283 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
13284 
13285 		/*
13286 		 * Dispatch the WRITE command to the taskq thread, which
13287 		 * will in turn send the command to the target. When the
13288 		 * WRITE command completes, we (sd_mapblocksize_iodone())
13289 		 * will get called again as part of the iodone chain
13290 		 * processing for it. Note that we will still be dealing
13291 		 * with the shadow buf at that point.
13292 		 */
13293 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13294 		    KM_NOSLEEP) != 0) {
13295 			/*
13296 			 * Dispatch was successful so we are done. Return
13297 			 * without going any higher up the iodone chain. Do
13298 			 * not free up any layer-private data until after the
13299 			 * WRITE completes.
13300 			 */
13301 			return;
13302 		}
13303 
13304 		/*
13305 		 * Dispatch of the WRITE command failed; set up the error
13306 		 * condition and send this IO back up the iodone chain.
13307 		 */
13308 		bioerror(orig_bp, EIO);
13309 		orig_bp->b_resid = orig_bp->b_bcount;
13310 
13311 	} else {
13312 		/*
13313 		 * This is a regular READ request (ie, not a RMW). Copy the
13314 		 * data from the shadow buf into the original buf. The
13315 		 * copy_offset compensates for any "misalignment" between the
13316 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
13317 		 * original buf (with its un->un_sys_blocksize blocks).
13318 		 */
13319 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13320 		    copy_length);
13321 	}
13322 
13323 freebuf_done:
13324 
13325 	/*
13326 	 * At this point we still have both the shadow buf AND the original
13327 	 * buf to deal with, as well as the layer-private data area in each.
13328 	 * Local variables are as follows:
13329 	 *
13330 	 * bp -- points to shadow buf
13331 	 * xp -- points to xbuf of shadow buf
13332 	 * bsp -- points to layer-private data area of shadow buf
13333 	 * orig_bp -- points to original buf
13334 	 *
13335 	 * First free the shadow buf and its associated xbuf, then free the
13336 	 * layer-private data area from the shadow buf. There is no need to
13337 	 * restore xb_private in the shadow xbuf.
13338 	 */
13339 	sd_shadow_buf_free(bp);
13340 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13341 
13342 	/*
13343 	 * Now update the local variables to point to the original buf, xbuf,
13344 	 * and layer-private area.
13345 	 */
13346 	bp = orig_bp;
13347 	xp = SD_GET_XBUF(bp);
13348 	ASSERT(xp != NULL);
13349 	ASSERT(xp == orig_xp);
13350 	bsp = xp->xb_private;
13351 	ASSERT(bsp != NULL);
13352 
13353 done:
13354 	/*
13355 	 * Restore xb_private to whatever it was set to by the next higher
13356 	 * layer in the chain, then free the layer-private data area.
13357 	 */
13358 	xp->xb_private = bsp->mbs_oprivate;
13359 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13360 
13361 exit:
13362 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13363 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13364 
13365 	SD_NEXT_IODONE(index, un, bp);
13366 }
13367 
13368 
13369 /*
13370  *    Function: sd_checksum_iostart
13371  *
13372  * Description: A stub function for a layer that's currently not used.
13373  *		For now just a placeholder.
13374  *
13375  *     Context: Kernel thread context
13376  */
13377 
13378 static void
13379 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13380 {
13381 	ASSERT(un != NULL);
13382 	ASSERT(bp != NULL);
13383 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13384 	SD_NEXT_IOSTART(index, un, bp);
13385 }
13386 
13387 
13388 /*
13389  *    Function: sd_checksum_iodone
13390  *
13391  * Description: A stub function for a layer that's currently not used.
13392  *		For now just a placeholder.
13393  *
13394  *     Context: May be called under interrupt context
13395  */
13396 
13397 static void
13398 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13399 {
13400 	ASSERT(un != NULL);
13401 	ASSERT(bp != NULL);
13402 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13403 	SD_NEXT_IODONE(index, un, bp);
13404 }
13405 
13406 
13407 /*
13408  *    Function: sd_checksum_uscsi_iostart
13409  *
13410  * Description: A stub function for a layer that's currently not used.
13411  *		For now just a placeholder.
13412  *
13413  *     Context: Kernel thread context
13414  */
13415 
13416 static void
13417 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13418 {
13419 	ASSERT(un != NULL);
13420 	ASSERT(bp != NULL);
13421 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13422 	SD_NEXT_IOSTART(index, un, bp);
13423 }
13424 
13425 
13426 /*
13427  *    Function: sd_checksum_uscsi_iodone
13428  *
13429  * Description: A stub function for a layer that's currently not used.
13430  *		For now just a placeholder.
13431  *
13432  *     Context: May be called under interrupt context
13433  */
13434 
13435 static void
13436 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13437 {
13438 	ASSERT(un != NULL);
13439 	ASSERT(bp != NULL);
13440 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13441 	SD_NEXT_IODONE(index, un, bp);
13442 }
13443 
13444 
13445 /*
13446  *    Function: sd_pm_iostart
13447  *
13448  * Description: iostart-side routine for Power mangement.
13449  *
13450  *     Context: Kernel thread context
13451  */
13452 
13453 static void
13454 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13455 {
13456 	ASSERT(un != NULL);
13457 	ASSERT(bp != NULL);
13458 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13459 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13460 
13461 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13462 
13463 	if (sd_pm_entry(un) != DDI_SUCCESS) {
13464 		/*
13465 		 * Set up to return the failed buf back up the 'iodone'
13466 		 * side of the calling chain.
13467 		 */
13468 		bioerror(bp, EIO);
13469 		bp->b_resid = bp->b_bcount;
13470 
13471 		SD_BEGIN_IODONE(index, un, bp);
13472 
13473 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13474 		return;
13475 	}
13476 
13477 	SD_NEXT_IOSTART(index, un, bp);
13478 
13479 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13480 }
13481 
13482 
13483 /*
13484  *    Function: sd_pm_iodone
13485  *
13486  * Description: iodone-side routine for power mangement.
13487  *
13488  *     Context: may be called from interrupt context
13489  */
13490 
13491 static void
13492 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13493 {
13494 	ASSERT(un != NULL);
13495 	ASSERT(bp != NULL);
13496 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13497 
13498 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13499 
13500 	/*
13501 	 * After attach the following flag is only read, so don't
13502 	 * take the penalty of acquiring a mutex for it.
13503 	 */
13504 	if (un->un_f_pm_is_enabled == TRUE) {
13505 		sd_pm_exit(un);
13506 	}
13507 
13508 	SD_NEXT_IODONE(index, un, bp);
13509 
13510 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13511 }
13512 
13513 
13514 /*
13515  *    Function: sd_core_iostart
13516  *
13517  * Description: Primary driver function for enqueuing buf(9S) structs from
13518  *		the system and initiating IO to the target device
13519  *
13520  *     Context: Kernel thread context. Can sleep.
13521  *
13522  * Assumptions:  - The given xp->xb_blkno is absolute
13523  *		   (ie, relative to the start of the device).
13524  *		 - The IO is to be done using the native blocksize of
13525  *		   the device, as specified in un->un_tgt_blocksize.
13526  */
13527 /* ARGSUSED */
13528 static void
13529 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13530 {
13531 	struct sd_xbuf *xp;
13532 
13533 	ASSERT(un != NULL);
13534 	ASSERT(bp != NULL);
13535 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13536 	ASSERT(bp->b_resid == 0);
13537 
13538 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13539 
13540 	xp = SD_GET_XBUF(bp);
13541 	ASSERT(xp != NULL);
13542 
13543 	mutex_enter(SD_MUTEX(un));
13544 
13545 	/*
13546 	 * If we are currently in the failfast state, fail any new IO
13547 	 * that has B_FAILFAST set, then return.
13548 	 */
13549 	if ((bp->b_flags & B_FAILFAST) &&
13550 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13551 		mutex_exit(SD_MUTEX(un));
13552 		bioerror(bp, EIO);
13553 		bp->b_resid = bp->b_bcount;
13554 		SD_BEGIN_IODONE(index, un, bp);
13555 		return;
13556 	}
13557 
13558 	if (SD_IS_DIRECT_PRIORITY(xp)) {
13559 		/*
13560 		 * Priority command -- transport it immediately.
13561 		 *
13562 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13563 		 * because all direct priority commands should be associated
13564 		 * with error recovery actions which we don't want to retry.
13565 		 */
13566 		sd_start_cmds(un, bp);
13567 	} else {
13568 		/*
13569 		 * Normal command -- add it to the wait queue, then start
13570 		 * transporting commands from the wait queue.
13571 		 */
13572 		sd_add_buf_to_waitq(un, bp);
13573 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13574 		sd_start_cmds(un, NULL);
13575 	}
13576 
13577 	mutex_exit(SD_MUTEX(un));
13578 
13579 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13580 }
13581 
13582 
13583 /*
13584  *    Function: sd_init_cdb_limits
13585  *
13586  * Description: This is to handle scsi_pkt initialization differences
13587  *		between the driver platforms.
13588  *
13589  *		Legacy behaviors:
13590  *
13591  *		If the block number or the sector count exceeds the
13592  *		capabilities of a Group 0 command, shift over to a
13593  *		Group 1 command. We don't blindly use Group 1
13594  *		commands because a) some drives (CDC Wren IVs) get a
13595  *		bit confused, and b) there is probably a fair amount
13596  *		of speed difference for a target to receive and decode
13597  *		a 10 byte command instead of a 6 byte command.
13598  *
13599  *		The xfer time difference of 6 vs 10 byte CDBs is
13600  *		still significant so this code is still worthwhile.
13601  *		10 byte CDBs are very inefficient with the fas HBA driver
13602  *		and older disks. Each CDB byte took 1 usec with some
13603  *		popular disks.
13604  *
13605  *     Context: Must be called at attach time
13606  */
13607 
13608 static void
13609 sd_init_cdb_limits(struct sd_lun *un)
13610 {
13611 	int hba_cdb_limit;
13612 
13613 	/*
13614 	 * Use CDB_GROUP1 commands for most devices except for
13615 	 * parallel SCSI fixed drives in which case we get better
13616 	 * performance using CDB_GROUP0 commands (where applicable).
13617 	 */
13618 	un->un_mincdb = SD_CDB_GROUP1;
13619 #if !defined(__fibre)
13620 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13621 	    !un->un_f_has_removable_media) {
13622 		un->un_mincdb = SD_CDB_GROUP0;
13623 	}
13624 #endif
13625 
13626 	/*
13627 	 * Try to read the max-cdb-length supported by HBA.
13628 	 */
13629 	un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13630 	if (0 >= un->un_max_hba_cdb) {
13631 		un->un_max_hba_cdb = CDB_GROUP4;
13632 		hba_cdb_limit = SD_CDB_GROUP4;
13633 	} else if (0 < un->un_max_hba_cdb &&
13634 	    un->un_max_hba_cdb < CDB_GROUP1) {
13635 		hba_cdb_limit = SD_CDB_GROUP0;
13636 	} else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13637 	    un->un_max_hba_cdb < CDB_GROUP5) {
13638 		hba_cdb_limit = SD_CDB_GROUP1;
13639 	} else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13640 	    un->un_max_hba_cdb < CDB_GROUP4) {
13641 		hba_cdb_limit = SD_CDB_GROUP5;
13642 	} else {
13643 		hba_cdb_limit = SD_CDB_GROUP4;
13644 	}
13645 
13646 	/*
13647 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13648 	 * commands for fixed disks unless we are building for a 32 bit
13649 	 * kernel.
13650 	 */
13651 #ifdef _LP64
13652 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13653 	    min(hba_cdb_limit, SD_CDB_GROUP4);
13654 #else
13655 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13656 	    min(hba_cdb_limit, SD_CDB_GROUP1);
13657 #endif
13658 
13659 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13660 	    ? sizeof (struct scsi_arq_status) : 1);
13661 	if (!ISCD(un))
13662 		un->un_cmd_timeout = (ushort_t)sd_io_time;
13663 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13664 }
13665 
13666 
13667 /*
13668  *    Function: sd_initpkt_for_buf
13669  *
13670  * Description: Allocate and initialize for transport a scsi_pkt struct,
13671  *		based upon the info specified in the given buf struct.
13672  *
13673  *		Assumes the xb_blkno in the request is absolute (ie,
13674  *		relative to the start of the device (NOT partition!).
13675  *		Also assumes that the request is using the native block
13676  *		size of the device (as returned by the READ CAPACITY
13677  *		command).
13678  *
13679  * Return Code: SD_PKT_ALLOC_SUCCESS
13680  *		SD_PKT_ALLOC_FAILURE
13681  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13682  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13683  *
13684  *     Context: Kernel thread and may be called from software interrupt context
13685  *		as part of a sdrunout callback. This function may not block or
13686  *		call routines that block
13687  */
13688 
13689 static int
13690 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13691 {
13692 	struct sd_xbuf	*xp;
13693 	struct scsi_pkt *pktp = NULL;
13694 	struct sd_lun	*un;
13695 	size_t		blockcount;
13696 	daddr_t		startblock;
13697 	int		rval;
13698 	int		cmd_flags;
13699 
13700 	ASSERT(bp != NULL);
13701 	ASSERT(pktpp != NULL);
13702 	xp = SD_GET_XBUF(bp);
13703 	ASSERT(xp != NULL);
13704 	un = SD_GET_UN(bp);
13705 	ASSERT(un != NULL);
13706 	ASSERT(mutex_owned(SD_MUTEX(un)));
13707 	ASSERT(bp->b_resid == 0);
13708 
13709 	SD_TRACE(SD_LOG_IO_CORE, un,
13710 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13711 
13712 	mutex_exit(SD_MUTEX(un));
13713 
13714 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13715 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13716 		/*
13717 		 * Already have a scsi_pkt -- just need DMA resources.
13718 		 * We must recompute the CDB in case the mapping returns
13719 		 * a nonzero pkt_resid.
13720 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13721 		 * that is being retried, the unmap/remap of the DMA resouces
13722 		 * will result in the entire transfer starting over again
13723 		 * from the very first block.
13724 		 */
13725 		ASSERT(xp->xb_pktp != NULL);
13726 		pktp = xp->xb_pktp;
13727 	} else {
13728 		pktp = NULL;
13729 	}
13730 #endif /* __i386 || __amd64 */
13731 
13732 	startblock = xp->xb_blkno;	/* Absolute block num. */
13733 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13734 
13735 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13736 
13737 	/*
13738 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13739 	 * call scsi_init_pkt, and build the CDB.
13740 	 */
13741 	rval = sd_setup_rw_pkt(un, &pktp, bp,
13742 	    cmd_flags, sdrunout, (caddr_t)un,
13743 	    startblock, blockcount);
13744 
13745 	if (rval == 0) {
13746 		/*
13747 		 * Success.
13748 		 *
13749 		 * If partial DMA is being used and required for this transfer.
13750 		 * set it up here.
13751 		 */
13752 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13753 		    (pktp->pkt_resid != 0)) {
13754 
13755 			/*
13756 			 * Save the CDB length and pkt_resid for the
13757 			 * next xfer
13758 			 */
13759 			xp->xb_dma_resid = pktp->pkt_resid;
13760 
13761 			/* rezero resid */
13762 			pktp->pkt_resid = 0;
13763 
13764 		} else {
13765 			xp->xb_dma_resid = 0;
13766 		}
13767 
13768 		pktp->pkt_flags = un->un_tagflags;
13769 		pktp->pkt_time  = un->un_cmd_timeout;
13770 		pktp->pkt_comp  = sdintr;
13771 
13772 		pktp->pkt_private = bp;
13773 		*pktpp = pktp;
13774 
13775 		SD_TRACE(SD_LOG_IO_CORE, un,
13776 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13777 
13778 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13779 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13780 #endif
13781 
13782 		mutex_enter(SD_MUTEX(un));
13783 		return (SD_PKT_ALLOC_SUCCESS);
13784 
13785 	}
13786 
13787 	/*
13788 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13789 	 * from sd_setup_rw_pkt.
13790 	 */
13791 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13792 
13793 	if (rval == SD_PKT_ALLOC_FAILURE) {
13794 		*pktpp = NULL;
13795 		/*
13796 		 * Set the driver state to RWAIT to indicate the driver
13797 		 * is waiting on resource allocations. The driver will not
13798 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13799 		 */
13800 		mutex_enter(SD_MUTEX(un));
13801 		New_state(un, SD_STATE_RWAIT);
13802 
13803 		SD_ERROR(SD_LOG_IO_CORE, un,
13804 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13805 
13806 		if ((bp->b_flags & B_ERROR) != 0) {
13807 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13808 		}
13809 		return (SD_PKT_ALLOC_FAILURE);
13810 	} else {
13811 		/*
13812 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13813 		 *
13814 		 * This should never happen.  Maybe someone messed with the
13815 		 * kernel's minphys?
13816 		 */
13817 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13818 		    "Request rejected: too large for CDB: "
13819 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13820 		SD_ERROR(SD_LOG_IO_CORE, un,
13821 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13822 		mutex_enter(SD_MUTEX(un));
13823 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13824 
13825 	}
13826 }
13827 
13828 
13829 /*
13830  *    Function: sd_destroypkt_for_buf
13831  *
13832  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13833  *
13834  *     Context: Kernel thread or interrupt context
13835  */
13836 
13837 static void
13838 sd_destroypkt_for_buf(struct buf *bp)
13839 {
13840 	ASSERT(bp != NULL);
13841 	ASSERT(SD_GET_UN(bp) != NULL);
13842 
13843 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13844 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13845 
13846 	ASSERT(SD_GET_PKTP(bp) != NULL);
13847 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13848 
13849 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13850 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13851 }
13852 
13853 /*
13854  *    Function: sd_setup_rw_pkt
13855  *
13856  * Description: Determines appropriate CDB group for the requested LBA
13857  *		and transfer length, calls scsi_init_pkt, and builds
13858  *		the CDB.  Do not use for partial DMA transfers except
13859  *		for the initial transfer since the CDB size must
13860  *		remain constant.
13861  *
13862  *     Context: Kernel thread and may be called from software interrupt
13863  *		context as part of a sdrunout callback. This function may not
13864  *		block or call routines that block
13865  */
13866 
13867 
13868 int
13869 sd_setup_rw_pkt(struct sd_lun *un,
13870     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13871     int (*callback)(caddr_t), caddr_t callback_arg,
13872     diskaddr_t lba, uint32_t blockcount)
13873 {
13874 	struct scsi_pkt *return_pktp;
13875 	union scsi_cdb *cdbp;
13876 	struct sd_cdbinfo *cp = NULL;
13877 	int i;
13878 
13879 	/*
13880 	 * See which size CDB to use, based upon the request.
13881 	 */
13882 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13883 
13884 		/*
13885 		 * Check lba and block count against sd_cdbtab limits.
13886 		 * In the partial DMA case, we have to use the same size
13887 		 * CDB for all the transfers.  Check lba + blockcount
13888 		 * against the max LBA so we know that segment of the
13889 		 * transfer can use the CDB we select.
13890 		 */
13891 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13892 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13893 
13894 			/*
13895 			 * The command will fit into the CDB type
13896 			 * specified by sd_cdbtab[i].
13897 			 */
13898 			cp = sd_cdbtab + i;
13899 
13900 			/*
13901 			 * Call scsi_init_pkt so we can fill in the
13902 			 * CDB.
13903 			 */
13904 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13905 			    bp, cp->sc_grpcode, un->un_status_len, 0,
13906 			    flags, callback, callback_arg);
13907 
13908 			if (return_pktp != NULL) {
13909 
13910 				/*
13911 				 * Return new value of pkt
13912 				 */
13913 				*pktpp = return_pktp;
13914 
13915 				/*
13916 				 * To be safe, zero the CDB insuring there is
13917 				 * no leftover data from a previous command.
13918 				 */
13919 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13920 
13921 				/*
13922 				 * Handle partial DMA mapping
13923 				 */
13924 				if (return_pktp->pkt_resid != 0) {
13925 
13926 					/*
13927 					 * Not going to xfer as many blocks as
13928 					 * originally expected
13929 					 */
13930 					blockcount -=
13931 					    SD_BYTES2TGTBLOCKS(un,
13932 					    return_pktp->pkt_resid);
13933 				}
13934 
13935 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13936 
13937 				/*
13938 				 * Set command byte based on the CDB
13939 				 * type we matched.
13940 				 */
13941 				cdbp->scc_cmd = cp->sc_grpmask |
13942 				    ((bp->b_flags & B_READ) ?
13943 				    SCMD_READ : SCMD_WRITE);
13944 
13945 				SD_FILL_SCSI1_LUN(un, return_pktp);
13946 
13947 				/*
13948 				 * Fill in LBA and length
13949 				 */
13950 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13951 				    (cp->sc_grpcode == CDB_GROUP4) ||
13952 				    (cp->sc_grpcode == CDB_GROUP0) ||
13953 				    (cp->sc_grpcode == CDB_GROUP5));
13954 
13955 				if (cp->sc_grpcode == CDB_GROUP1) {
13956 					FORMG1ADDR(cdbp, lba);
13957 					FORMG1COUNT(cdbp, blockcount);
13958 					return (0);
13959 				} else if (cp->sc_grpcode == CDB_GROUP4) {
13960 					FORMG4LONGADDR(cdbp, lba);
13961 					FORMG4COUNT(cdbp, blockcount);
13962 					return (0);
13963 				} else if (cp->sc_grpcode == CDB_GROUP0) {
13964 					FORMG0ADDR(cdbp, lba);
13965 					FORMG0COUNT(cdbp, blockcount);
13966 					return (0);
13967 				} else if (cp->sc_grpcode == CDB_GROUP5) {
13968 					FORMG5ADDR(cdbp, lba);
13969 					FORMG5COUNT(cdbp, blockcount);
13970 					return (0);
13971 				}
13972 
13973 				/*
13974 				 * It should be impossible to not match one
13975 				 * of the CDB types above, so we should never
13976 				 * reach this point.  Set the CDB command byte
13977 				 * to test-unit-ready to avoid writing
13978 				 * to somewhere we don't intend.
13979 				 */
13980 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13981 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13982 			} else {
13983 				/*
13984 				 * Couldn't get scsi_pkt
13985 				 */
13986 				return (SD_PKT_ALLOC_FAILURE);
13987 			}
13988 		}
13989 	}
13990 
13991 	/*
13992 	 * None of the available CDB types were suitable.  This really
13993 	 * should never happen:  on a 64 bit system we support
13994 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
13995 	 * and on a 32 bit system we will refuse to bind to a device
13996 	 * larger than 2TB so addresses will never be larger than 32 bits.
13997 	 */
13998 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13999 }
14000 
14001 /*
14002  *    Function: sd_setup_next_rw_pkt
14003  *
14004  * Description: Setup packet for partial DMA transfers, except for the
14005  * 		initial transfer.  sd_setup_rw_pkt should be used for
14006  *		the initial transfer.
14007  *
14008  *     Context: Kernel thread and may be called from interrupt context.
14009  */
14010 
14011 int
14012 sd_setup_next_rw_pkt(struct sd_lun *un,
14013     struct scsi_pkt *pktp, struct buf *bp,
14014     diskaddr_t lba, uint32_t blockcount)
14015 {
14016 	uchar_t com;
14017 	union scsi_cdb *cdbp;
14018 	uchar_t cdb_group_id;
14019 
14020 	ASSERT(pktp != NULL);
14021 	ASSERT(pktp->pkt_cdbp != NULL);
14022 
14023 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
14024 	com = cdbp->scc_cmd;
14025 	cdb_group_id = CDB_GROUPID(com);
14026 
14027 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
14028 	    (cdb_group_id == CDB_GROUPID_1) ||
14029 	    (cdb_group_id == CDB_GROUPID_4) ||
14030 	    (cdb_group_id == CDB_GROUPID_5));
14031 
14032 	/*
14033 	 * Move pkt to the next portion of the xfer.
14034 	 * func is NULL_FUNC so we do not have to release
14035 	 * the disk mutex here.
14036 	 */
14037 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
14038 	    NULL_FUNC, NULL) == pktp) {
14039 		/* Success.  Handle partial DMA */
14040 		if (pktp->pkt_resid != 0) {
14041 			blockcount -=
14042 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
14043 		}
14044 
14045 		cdbp->scc_cmd = com;
14046 		SD_FILL_SCSI1_LUN(un, pktp);
14047 		if (cdb_group_id == CDB_GROUPID_1) {
14048 			FORMG1ADDR(cdbp, lba);
14049 			FORMG1COUNT(cdbp, blockcount);
14050 			return (0);
14051 		} else if (cdb_group_id == CDB_GROUPID_4) {
14052 			FORMG4LONGADDR(cdbp, lba);
14053 			FORMG4COUNT(cdbp, blockcount);
14054 			return (0);
14055 		} else if (cdb_group_id == CDB_GROUPID_0) {
14056 			FORMG0ADDR(cdbp, lba);
14057 			FORMG0COUNT(cdbp, blockcount);
14058 			return (0);
14059 		} else if (cdb_group_id == CDB_GROUPID_5) {
14060 			FORMG5ADDR(cdbp, lba);
14061 			FORMG5COUNT(cdbp, blockcount);
14062 			return (0);
14063 		}
14064 
14065 		/* Unreachable */
14066 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14067 	}
14068 
14069 	/*
14070 	 * Error setting up next portion of cmd transfer.
14071 	 * Something is definitely very wrong and this
14072 	 * should not happen.
14073 	 */
14074 	return (SD_PKT_ALLOC_FAILURE);
14075 }
14076 
14077 /*
14078  *    Function: sd_initpkt_for_uscsi
14079  *
14080  * Description: Allocate and initialize for transport a scsi_pkt struct,
14081  *		based upon the info specified in the given uscsi_cmd struct.
14082  *
14083  * Return Code: SD_PKT_ALLOC_SUCCESS
14084  *		SD_PKT_ALLOC_FAILURE
14085  *		SD_PKT_ALLOC_FAILURE_NO_DMA
14086  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
14087  *
14088  *     Context: Kernel thread and may be called from software interrupt context
14089  *		as part of a sdrunout callback. This function may not block or
14090  *		call routines that block
14091  */
14092 
14093 static int
14094 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
14095 {
14096 	struct uscsi_cmd *uscmd;
14097 	struct sd_xbuf	*xp;
14098 	struct scsi_pkt	*pktp;
14099 	struct sd_lun	*un;
14100 	uint32_t	flags = 0;
14101 
14102 	ASSERT(bp != NULL);
14103 	ASSERT(pktpp != NULL);
14104 	xp = SD_GET_XBUF(bp);
14105 	ASSERT(xp != NULL);
14106 	un = SD_GET_UN(bp);
14107 	ASSERT(un != NULL);
14108 	ASSERT(mutex_owned(SD_MUTEX(un)));
14109 
14110 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14111 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14112 	ASSERT(uscmd != NULL);
14113 
14114 	SD_TRACE(SD_LOG_IO_CORE, un,
14115 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
14116 
14117 	/*
14118 	 * Allocate the scsi_pkt for the command.
14119 	 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
14120 	 *	 during scsi_init_pkt time and will continue to use the
14121 	 *	 same path as long as the same scsi_pkt is used without
14122 	 *	 intervening scsi_dma_free(). Since uscsi command does
14123 	 *	 not call scsi_dmafree() before retry failed command, it
14124 	 *	 is necessary to make sure PKT_DMA_PARTIAL flag is NOT
14125 	 *	 set such that scsi_vhci can use other available path for
14126 	 *	 retry. Besides, ucsci command does not allow DMA breakup,
14127 	 *	 so there is no need to set PKT_DMA_PARTIAL flag.
14128 	 */
14129 	if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14130 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14131 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14132 		    ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
14133 		    - sizeof (struct scsi_extended_sense)), 0,
14134 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
14135 		    sdrunout, (caddr_t)un);
14136 	} else {
14137 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14138 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14139 		    sizeof (struct scsi_arq_status), 0,
14140 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14141 		    sdrunout, (caddr_t)un);
14142 	}
14143 
14144 	if (pktp == NULL) {
14145 		*pktpp = NULL;
14146 		/*
14147 		 * Set the driver state to RWAIT to indicate the driver
14148 		 * is waiting on resource allocations. The driver will not
14149 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
14150 		 */
14151 		New_state(un, SD_STATE_RWAIT);
14152 
14153 		SD_ERROR(SD_LOG_IO_CORE, un,
14154 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14155 
14156 		if ((bp->b_flags & B_ERROR) != 0) {
14157 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14158 		}
14159 		return (SD_PKT_ALLOC_FAILURE);
14160 	}
14161 
14162 	/*
14163 	 * We do not do DMA breakup for USCSI commands, so return failure
14164 	 * here if all the needed DMA resources were not allocated.
14165 	 */
14166 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14167 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14168 		scsi_destroy_pkt(pktp);
14169 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14170 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14171 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14172 	}
14173 
14174 	/* Init the cdb from the given uscsi struct */
14175 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14176 	    uscmd->uscsi_cdb[0], 0, 0, 0);
14177 
14178 	SD_FILL_SCSI1_LUN(un, pktp);
14179 
14180 	/*
14181 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
14182 	 * for listing of the supported flags.
14183 	 */
14184 
14185 	if (uscmd->uscsi_flags & USCSI_SILENT) {
14186 		flags |= FLAG_SILENT;
14187 	}
14188 
14189 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14190 		flags |= FLAG_DIAGNOSE;
14191 	}
14192 
14193 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14194 		flags |= FLAG_ISOLATE;
14195 	}
14196 
14197 	if (un->un_f_is_fibre == FALSE) {
14198 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14199 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14200 		}
14201 	}
14202 
14203 	/*
14204 	 * Set the pkt flags here so we save time later.
14205 	 * Note: These flags are NOT in the uscsi man page!!!
14206 	 */
14207 	if (uscmd->uscsi_flags & USCSI_HEAD) {
14208 		flags |= FLAG_HEAD;
14209 	}
14210 
14211 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
14212 		flags |= FLAG_NOINTR;
14213 	}
14214 
14215 	/*
14216 	 * For tagged queueing, things get a bit complicated.
14217 	 * Check first for head of queue and last for ordered queue.
14218 	 * If neither head nor order, use the default driver tag flags.
14219 	 */
14220 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14221 		if (uscmd->uscsi_flags & USCSI_HTAG) {
14222 			flags |= FLAG_HTAG;
14223 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
14224 			flags |= FLAG_OTAG;
14225 		} else {
14226 			flags |= un->un_tagflags & FLAG_TAGMASK;
14227 		}
14228 	}
14229 
14230 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
14231 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14232 	}
14233 
14234 	pktp->pkt_flags = flags;
14235 
14236 	/* Transfer uscsi information to scsi_pkt */
14237 	(void) scsi_uscsi_pktinit(uscmd, pktp);
14238 
14239 	/* Copy the caller's CDB into the pkt... */
14240 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14241 
14242 	if (uscmd->uscsi_timeout == 0) {
14243 		pktp->pkt_time = un->un_uscsi_timeout;
14244 	} else {
14245 		pktp->pkt_time = uscmd->uscsi_timeout;
14246 	}
14247 
14248 	/* need it later to identify USCSI request in sdintr */
14249 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
14250 
14251 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
14252 
14253 	pktp->pkt_private = bp;
14254 	pktp->pkt_comp = sdintr;
14255 	*pktpp = pktp;
14256 
14257 	SD_TRACE(SD_LOG_IO_CORE, un,
14258 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14259 
14260 	return (SD_PKT_ALLOC_SUCCESS);
14261 }
14262 
14263 
14264 /*
14265  *    Function: sd_destroypkt_for_uscsi
14266  *
14267  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14268  *		IOs.. Also saves relevant info into the associated uscsi_cmd
14269  *		struct.
14270  *
14271  *     Context: May be called under interrupt context
14272  */
14273 
14274 static void
14275 sd_destroypkt_for_uscsi(struct buf *bp)
14276 {
14277 	struct uscsi_cmd *uscmd;
14278 	struct sd_xbuf	*xp;
14279 	struct scsi_pkt	*pktp;
14280 	struct sd_lun	*un;
14281 	struct sd_uscsi_info *suip;
14282 
14283 	ASSERT(bp != NULL);
14284 	xp = SD_GET_XBUF(bp);
14285 	ASSERT(xp != NULL);
14286 	un = SD_GET_UN(bp);
14287 	ASSERT(un != NULL);
14288 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14289 	pktp = SD_GET_PKTP(bp);
14290 	ASSERT(pktp != NULL);
14291 
14292 	SD_TRACE(SD_LOG_IO_CORE, un,
14293 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14294 
14295 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14296 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14297 	ASSERT(uscmd != NULL);
14298 
14299 	/* Save the status and the residual into the uscsi_cmd struct */
14300 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14301 	uscmd->uscsi_resid  = bp->b_resid;
14302 
14303 	/* Transfer scsi_pkt information to uscsi */
14304 	(void) scsi_uscsi_pktfini(pktp, uscmd);
14305 
14306 	/*
14307 	 * If enabled, copy any saved sense data into the area specified
14308 	 * by the uscsi command.
14309 	 */
14310 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14311 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14312 		/*
14313 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
14314 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14315 		 */
14316 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
14317 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
14318 		if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14319 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14320 			    MAX_SENSE_LENGTH);
14321 		} else {
14322 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14323 			    SENSE_LENGTH);
14324 		}
14325 	}
14326 	/*
14327 	 * The following assignments are for SCSI FMA.
14328 	 */
14329 	ASSERT(xp->xb_private != NULL);
14330 	suip = (struct sd_uscsi_info *)xp->xb_private;
14331 	suip->ui_pkt_reason = pktp->pkt_reason;
14332 	suip->ui_pkt_state = pktp->pkt_state;
14333 	suip->ui_pkt_statistics = pktp->pkt_statistics;
14334 	suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
14335 
14336 	/* We are done with the scsi_pkt; free it now */
14337 	ASSERT(SD_GET_PKTP(bp) != NULL);
14338 	scsi_destroy_pkt(SD_GET_PKTP(bp));
14339 
14340 	SD_TRACE(SD_LOG_IO_CORE, un,
14341 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14342 }
14343 
14344 
14345 /*
14346  *    Function: sd_bioclone_alloc
14347  *
14348  * Description: Allocate a buf(9S) and init it as per the given buf
14349  *		and the various arguments.  The associated sd_xbuf
14350  *		struct is (nearly) duplicated.  The struct buf *bp
14351  *		argument is saved in new_xp->xb_private.
14352  *
14353  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14354  *		datalen - size of data area for the shadow bp
14355  *		blkno - starting LBA
14356  *		func - function pointer for b_iodone in the shadow buf. (May
14357  *			be NULL if none.)
14358  *
14359  * Return Code: Pointer to allocates buf(9S) struct
14360  *
14361  *     Context: Can sleep.
14362  */
14363 
14364 static struct buf *
14365 sd_bioclone_alloc(struct buf *bp, size_t datalen,
14366 	daddr_t blkno, int (*func)(struct buf *))
14367 {
14368 	struct	sd_lun	*un;
14369 	struct	sd_xbuf	*xp;
14370 	struct	sd_xbuf	*new_xp;
14371 	struct	buf	*new_bp;
14372 
14373 	ASSERT(bp != NULL);
14374 	xp = SD_GET_XBUF(bp);
14375 	ASSERT(xp != NULL);
14376 	un = SD_GET_UN(bp);
14377 	ASSERT(un != NULL);
14378 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14379 
14380 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14381 	    NULL, KM_SLEEP);
14382 
14383 	new_bp->b_lblkno	= blkno;
14384 
14385 	/*
14386 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14387 	 * original xbuf into it.
14388 	 */
14389 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14390 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14391 
14392 	/*
14393 	 * The given bp is automatically saved in the xb_private member
14394 	 * of the new xbuf.  Callers are allowed to depend on this.
14395 	 */
14396 	new_xp->xb_private = bp;
14397 
14398 	new_bp->b_private  = new_xp;
14399 
14400 	return (new_bp);
14401 }
14402 
14403 /*
14404  *    Function: sd_shadow_buf_alloc
14405  *
14406  * Description: Allocate a buf(9S) and init it as per the given buf
14407  *		and the various arguments.  The associated sd_xbuf
14408  *		struct is (nearly) duplicated.  The struct buf *bp
14409  *		argument is saved in new_xp->xb_private.
14410  *
14411  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14412  *		datalen - size of data area for the shadow bp
14413  *		bflags - B_READ or B_WRITE (pseudo flag)
14414  *		blkno - starting LBA
14415  *		func - function pointer for b_iodone in the shadow buf. (May
14416  *			be NULL if none.)
14417  *
14418  * Return Code: Pointer to allocates buf(9S) struct
14419  *
14420  *     Context: Can sleep.
14421  */
14422 
14423 static struct buf *
14424 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14425 	daddr_t blkno, int (*func)(struct buf *))
14426 {
14427 	struct	sd_lun	*un;
14428 	struct	sd_xbuf	*xp;
14429 	struct	sd_xbuf	*new_xp;
14430 	struct	buf	*new_bp;
14431 
14432 	ASSERT(bp != NULL);
14433 	xp = SD_GET_XBUF(bp);
14434 	ASSERT(xp != NULL);
14435 	un = SD_GET_UN(bp);
14436 	ASSERT(un != NULL);
14437 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14438 
14439 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14440 		bp_mapin(bp);
14441 	}
14442 
14443 	bflags &= (B_READ | B_WRITE);
14444 #if defined(__i386) || defined(__amd64)
14445 	new_bp = getrbuf(KM_SLEEP);
14446 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14447 	new_bp->b_bcount = datalen;
14448 	new_bp->b_flags = bflags |
14449 	    (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14450 #else
14451 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14452 	    datalen, bflags, SLEEP_FUNC, NULL);
14453 #endif
14454 	new_bp->av_forw	= NULL;
14455 	new_bp->av_back	= NULL;
14456 	new_bp->b_dev	= bp->b_dev;
14457 	new_bp->b_blkno	= blkno;
14458 	new_bp->b_iodone = func;
14459 	new_bp->b_edev	= bp->b_edev;
14460 	new_bp->b_resid	= 0;
14461 
14462 	/* We need to preserve the B_FAILFAST flag */
14463 	if (bp->b_flags & B_FAILFAST) {
14464 		new_bp->b_flags |= B_FAILFAST;
14465 	}
14466 
14467 	/*
14468 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14469 	 * original xbuf into it.
14470 	 */
14471 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14472 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14473 
14474 	/* Need later to copy data between the shadow buf & original buf! */
14475 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14476 
14477 	/*
14478 	 * The given bp is automatically saved in the xb_private member
14479 	 * of the new xbuf.  Callers are allowed to depend on this.
14480 	 */
14481 	new_xp->xb_private = bp;
14482 
14483 	new_bp->b_private  = new_xp;
14484 
14485 	return (new_bp);
14486 }
14487 
14488 /*
14489  *    Function: sd_bioclone_free
14490  *
14491  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14492  *		in the larger than partition operation.
14493  *
14494  *     Context: May be called under interrupt context
14495  */
14496 
14497 static void
14498 sd_bioclone_free(struct buf *bp)
14499 {
14500 	struct sd_xbuf	*xp;
14501 
14502 	ASSERT(bp != NULL);
14503 	xp = SD_GET_XBUF(bp);
14504 	ASSERT(xp != NULL);
14505 
14506 	/*
14507 	 * Call bp_mapout() before freeing the buf,  in case a lower
14508 	 * layer or HBA  had done a bp_mapin().  we must do this here
14509 	 * as we are the "originator" of the shadow buf.
14510 	 */
14511 	bp_mapout(bp);
14512 
14513 	/*
14514 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14515 	 * never gets confused by a stale value in this field. (Just a little
14516 	 * extra defensiveness here.)
14517 	 */
14518 	bp->b_iodone = NULL;
14519 
14520 	freerbuf(bp);
14521 
14522 	kmem_free(xp, sizeof (struct sd_xbuf));
14523 }
14524 
14525 /*
14526  *    Function: sd_shadow_buf_free
14527  *
14528  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14529  *
14530  *     Context: May be called under interrupt context
14531  */
14532 
14533 static void
14534 sd_shadow_buf_free(struct buf *bp)
14535 {
14536 	struct sd_xbuf	*xp;
14537 
14538 	ASSERT(bp != NULL);
14539 	xp = SD_GET_XBUF(bp);
14540 	ASSERT(xp != NULL);
14541 
14542 #if defined(__sparc)
14543 	/*
14544 	 * Call bp_mapout() before freeing the buf,  in case a lower
14545 	 * layer or HBA  had done a bp_mapin().  we must do this here
14546 	 * as we are the "originator" of the shadow buf.
14547 	 */
14548 	bp_mapout(bp);
14549 #endif
14550 
14551 	/*
14552 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14553 	 * never gets confused by a stale value in this field. (Just a little
14554 	 * extra defensiveness here.)
14555 	 */
14556 	bp->b_iodone = NULL;
14557 
14558 #if defined(__i386) || defined(__amd64)
14559 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
14560 	freerbuf(bp);
14561 #else
14562 	scsi_free_consistent_buf(bp);
14563 #endif
14564 
14565 	kmem_free(xp, sizeof (struct sd_xbuf));
14566 }
14567 
14568 
14569 /*
14570  *    Function: sd_print_transport_rejected_message
14571  *
14572  * Description: This implements the ludicrously complex rules for printing
14573  *		a "transport rejected" message.  This is to address the
14574  *		specific problem of having a flood of this error message
14575  *		produced when a failover occurs.
14576  *
14577  *     Context: Any.
14578  */
14579 
14580 static void
14581 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14582 	int code)
14583 {
14584 	ASSERT(un != NULL);
14585 	ASSERT(mutex_owned(SD_MUTEX(un)));
14586 	ASSERT(xp != NULL);
14587 
14588 	/*
14589 	 * Print the "transport rejected" message under the following
14590 	 * conditions:
14591 	 *
14592 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14593 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14594 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14595 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14596 	 *   scsi_transport(9F) (which indicates that the target might have
14597 	 *   gone off-line).  This uses the un->un_tran_fatal_count
14598 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14599 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14600 	 *   from scsi_transport().
14601 	 *
14602 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14603 	 * the preceeding cases in order for the message to be printed.
14604 	 */
14605 	if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14606 	    (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14607 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14608 		    (code != TRAN_FATAL_ERROR) ||
14609 		    (un->un_tran_fatal_count == 1)) {
14610 			switch (code) {
14611 			case TRAN_BADPKT:
14612 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14613 				    "transport rejected bad packet\n");
14614 				break;
14615 			case TRAN_FATAL_ERROR:
14616 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14617 				    "transport rejected fatal error\n");
14618 				break;
14619 			default:
14620 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14621 				    "transport rejected (%d)\n", code);
14622 				break;
14623 			}
14624 		}
14625 	}
14626 }
14627 
14628 
14629 /*
14630  *    Function: sd_add_buf_to_waitq
14631  *
14632  * Description: Add the given buf(9S) struct to the wait queue for the
14633  *		instance.  If sorting is enabled, then the buf is added
14634  *		to the queue via an elevator sort algorithm (a la
14635  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14636  *		If sorting is not enabled, then the buf is just added
14637  *		to the end of the wait queue.
14638  *
14639  * Return Code: void
14640  *
14641  *     Context: Does not sleep/block, therefore technically can be called
14642  *		from any context.  However if sorting is enabled then the
14643  *		execution time is indeterminate, and may take long if
14644  *		the wait queue grows large.
14645  */
14646 
14647 static void
14648 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14649 {
14650 	struct buf *ap;
14651 
14652 	ASSERT(bp != NULL);
14653 	ASSERT(un != NULL);
14654 	ASSERT(mutex_owned(SD_MUTEX(un)));
14655 
14656 	/* If the queue is empty, add the buf as the only entry & return. */
14657 	if (un->un_waitq_headp == NULL) {
14658 		ASSERT(un->un_waitq_tailp == NULL);
14659 		un->un_waitq_headp = un->un_waitq_tailp = bp;
14660 		bp->av_forw = NULL;
14661 		return;
14662 	}
14663 
14664 	ASSERT(un->un_waitq_tailp != NULL);
14665 
14666 	/*
14667 	 * If sorting is disabled, just add the buf to the tail end of
14668 	 * the wait queue and return.
14669 	 */
14670 	if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14671 		un->un_waitq_tailp->av_forw = bp;
14672 		un->un_waitq_tailp = bp;
14673 		bp->av_forw = NULL;
14674 		return;
14675 	}
14676 
14677 	/*
14678 	 * Sort thru the list of requests currently on the wait queue
14679 	 * and add the new buf request at the appropriate position.
14680 	 *
14681 	 * The un->un_waitq_headp is an activity chain pointer on which
14682 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14683 	 * first queue holds those requests which are positioned after
14684 	 * the current SD_GET_BLKNO() (in the first request); the second holds
14685 	 * requests which came in after their SD_GET_BLKNO() number was passed.
14686 	 * Thus we implement a one way scan, retracting after reaching
14687 	 * the end of the drive to the first request on the second
14688 	 * queue, at which time it becomes the first queue.
14689 	 * A one-way scan is natural because of the way UNIX read-ahead
14690 	 * blocks are allocated.
14691 	 *
14692 	 * If we lie after the first request, then we must locate the
14693 	 * second request list and add ourselves to it.
14694 	 */
14695 	ap = un->un_waitq_headp;
14696 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14697 		while (ap->av_forw != NULL) {
14698 			/*
14699 			 * Look for an "inversion" in the (normally
14700 			 * ascending) block numbers. This indicates
14701 			 * the start of the second request list.
14702 			 */
14703 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14704 				/*
14705 				 * Search the second request list for the
14706 				 * first request at a larger block number.
14707 				 * We go before that; however if there is
14708 				 * no such request, we go at the end.
14709 				 */
14710 				do {
14711 					if (SD_GET_BLKNO(bp) <
14712 					    SD_GET_BLKNO(ap->av_forw)) {
14713 						goto insert;
14714 					}
14715 					ap = ap->av_forw;
14716 				} while (ap->av_forw != NULL);
14717 				goto insert;		/* after last */
14718 			}
14719 			ap = ap->av_forw;
14720 		}
14721 
14722 		/*
14723 		 * No inversions... we will go after the last, and
14724 		 * be the first request in the second request list.
14725 		 */
14726 		goto insert;
14727 	}
14728 
14729 	/*
14730 	 * Request is at/after the current request...
14731 	 * sort in the first request list.
14732 	 */
14733 	while (ap->av_forw != NULL) {
14734 		/*
14735 		 * We want to go after the current request (1) if
14736 		 * there is an inversion after it (i.e. it is the end
14737 		 * of the first request list), or (2) if the next
14738 		 * request is a larger block no. than our request.
14739 		 */
14740 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14741 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14742 			goto insert;
14743 		}
14744 		ap = ap->av_forw;
14745 	}
14746 
14747 	/*
14748 	 * Neither a second list nor a larger request, therefore
14749 	 * we go at the end of the first list (which is the same
14750 	 * as the end of the whole schebang).
14751 	 */
14752 insert:
14753 	bp->av_forw = ap->av_forw;
14754 	ap->av_forw = bp;
14755 
14756 	/*
14757 	 * If we inserted onto the tail end of the waitq, make sure the
14758 	 * tail pointer is updated.
14759 	 */
14760 	if (ap == un->un_waitq_tailp) {
14761 		un->un_waitq_tailp = bp;
14762 	}
14763 }
14764 
14765 
14766 /*
14767  *    Function: sd_start_cmds
14768  *
14769  * Description: Remove and transport cmds from the driver queues.
14770  *
14771  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14772  *
14773  *		immed_bp - ptr to a buf to be transported immediately. Only
14774  *		the immed_bp is transported; bufs on the waitq are not
14775  *		processed and the un_retry_bp is not checked.  If immed_bp is
14776  *		NULL, then normal queue processing is performed.
14777  *
14778  *     Context: May be called from kernel thread context, interrupt context,
14779  *		or runout callback context. This function may not block or
14780  *		call routines that block.
14781  */
14782 
14783 static void
14784 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14785 {
14786 	struct	sd_xbuf	*xp;
14787 	struct	buf	*bp;
14788 	void	(*statp)(kstat_io_t *);
14789 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14790 	void	(*saved_statp)(kstat_io_t *);
14791 #endif
14792 	int	rval;
14793 	struct sd_fm_internal *sfip = NULL;
14794 
14795 	ASSERT(un != NULL);
14796 	ASSERT(mutex_owned(SD_MUTEX(un)));
14797 	ASSERT(un->un_ncmds_in_transport >= 0);
14798 	ASSERT(un->un_throttle >= 0);
14799 
14800 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14801 
14802 	do {
14803 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14804 		saved_statp = NULL;
14805 #endif
14806 
14807 		/*
14808 		 * If we are syncing or dumping, fail the command to
14809 		 * avoid recursively calling back into scsi_transport().
14810 		 * The dump I/O itself uses a separate code path so this
14811 		 * only prevents non-dump I/O from being sent while dumping.
14812 		 * File system sync takes place before dumping begins.
14813 		 * During panic, filesystem I/O is allowed provided
14814 		 * un_in_callback is <= 1.  This is to prevent recursion
14815 		 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14816 		 * sd_start_cmds and so on.  See panic.c for more information
14817 		 * about the states the system can be in during panic.
14818 		 */
14819 		if ((un->un_state == SD_STATE_DUMPING) ||
14820 		    (ddi_in_panic() && (un->un_in_callback > 1))) {
14821 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14822 			    "sd_start_cmds: panicking\n");
14823 			goto exit;
14824 		}
14825 
14826 		if ((bp = immed_bp) != NULL) {
14827 			/*
14828 			 * We have a bp that must be transported immediately.
14829 			 * It's OK to transport the immed_bp here without doing
14830 			 * the throttle limit check because the immed_bp is
14831 			 * always used in a retry/recovery case. This means
14832 			 * that we know we are not at the throttle limit by
14833 			 * virtue of the fact that to get here we must have
14834 			 * already gotten a command back via sdintr(). This also
14835 			 * relies on (1) the command on un_retry_bp preventing
14836 			 * further commands from the waitq from being issued;
14837 			 * and (2) the code in sd_retry_command checking the
14838 			 * throttle limit before issuing a delayed or immediate
14839 			 * retry. This holds even if the throttle limit is
14840 			 * currently ratcheted down from its maximum value.
14841 			 */
14842 			statp = kstat_runq_enter;
14843 			if (bp == un->un_retry_bp) {
14844 				ASSERT((un->un_retry_statp == NULL) ||
14845 				    (un->un_retry_statp == kstat_waitq_enter) ||
14846 				    (un->un_retry_statp ==
14847 				    kstat_runq_back_to_waitq));
14848 				/*
14849 				 * If the waitq kstat was incremented when
14850 				 * sd_set_retry_bp() queued this bp for a retry,
14851 				 * then we must set up statp so that the waitq
14852 				 * count will get decremented correctly below.
14853 				 * Also we must clear un->un_retry_statp to
14854 				 * ensure that we do not act on a stale value
14855 				 * in this field.
14856 				 */
14857 				if ((un->un_retry_statp == kstat_waitq_enter) ||
14858 				    (un->un_retry_statp ==
14859 				    kstat_runq_back_to_waitq)) {
14860 					statp = kstat_waitq_to_runq;
14861 				}
14862 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14863 				saved_statp = un->un_retry_statp;
14864 #endif
14865 				un->un_retry_statp = NULL;
14866 
14867 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14868 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14869 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
14870 				    un, un->un_retry_bp, un->un_throttle,
14871 				    un->un_ncmds_in_transport);
14872 			} else {
14873 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14874 				    "processing priority bp:0x%p\n", bp);
14875 			}
14876 
14877 		} else if ((bp = un->un_waitq_headp) != NULL) {
14878 			/*
14879 			 * A command on the waitq is ready to go, but do not
14880 			 * send it if:
14881 			 *
14882 			 * (1) the throttle limit has been reached, or
14883 			 * (2) a retry is pending, or
14884 			 * (3) a START_STOP_UNIT callback pending, or
14885 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14886 			 *	command is pending.
14887 			 *
14888 			 * For all of these conditions, IO processing will
14889 			 * restart after the condition is cleared.
14890 			 */
14891 			if (un->un_ncmds_in_transport >= un->un_throttle) {
14892 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14893 				    "sd_start_cmds: exiting, "
14894 				    "throttle limit reached!\n");
14895 				goto exit;
14896 			}
14897 			if (un->un_retry_bp != NULL) {
14898 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14899 				    "sd_start_cmds: exiting, retry pending!\n");
14900 				goto exit;
14901 			}
14902 			if (un->un_startstop_timeid != NULL) {
14903 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14904 				    "sd_start_cmds: exiting, "
14905 				    "START_STOP pending!\n");
14906 				goto exit;
14907 			}
14908 			if (un->un_direct_priority_timeid != NULL) {
14909 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14910 				    "sd_start_cmds: exiting, "
14911 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14912 				goto exit;
14913 			}
14914 
14915 			/* Dequeue the command */
14916 			un->un_waitq_headp = bp->av_forw;
14917 			if (un->un_waitq_headp == NULL) {
14918 				un->un_waitq_tailp = NULL;
14919 			}
14920 			bp->av_forw = NULL;
14921 			statp = kstat_waitq_to_runq;
14922 			SD_TRACE(SD_LOG_IO_CORE, un,
14923 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14924 
14925 		} else {
14926 			/* No work to do so bail out now */
14927 			SD_TRACE(SD_LOG_IO_CORE, un,
14928 			    "sd_start_cmds: no more work, exiting!\n");
14929 			goto exit;
14930 		}
14931 
14932 		/*
14933 		 * Reset the state to normal. This is the mechanism by which
14934 		 * the state transitions from either SD_STATE_RWAIT or
14935 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14936 		 * If state is SD_STATE_PM_CHANGING then this command is
14937 		 * part of the device power control and the state must
14938 		 * not be put back to normal. Doing so would would
14939 		 * allow new commands to proceed when they shouldn't,
14940 		 * the device may be going off.
14941 		 */
14942 		if ((un->un_state != SD_STATE_SUSPENDED) &&
14943 		    (un->un_state != SD_STATE_PM_CHANGING)) {
14944 			New_state(un, SD_STATE_NORMAL);
14945 		}
14946 
14947 		xp = SD_GET_XBUF(bp);
14948 		ASSERT(xp != NULL);
14949 
14950 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14951 		/*
14952 		 * Allocate the scsi_pkt if we need one, or attach DMA
14953 		 * resources if we have a scsi_pkt that needs them. The
14954 		 * latter should only occur for commands that are being
14955 		 * retried.
14956 		 */
14957 		if ((xp->xb_pktp == NULL) ||
14958 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14959 #else
14960 		if (xp->xb_pktp == NULL) {
14961 #endif
14962 			/*
14963 			 * There is no scsi_pkt allocated for this buf. Call
14964 			 * the initpkt function to allocate & init one.
14965 			 *
14966 			 * The scsi_init_pkt runout callback functionality is
14967 			 * implemented as follows:
14968 			 *
14969 			 * 1) The initpkt function always calls
14970 			 *    scsi_init_pkt(9F) with sdrunout specified as the
14971 			 *    callback routine.
14972 			 * 2) A successful packet allocation is initialized and
14973 			 *    the I/O is transported.
14974 			 * 3) The I/O associated with an allocation resource
14975 			 *    failure is left on its queue to be retried via
14976 			 *    runout or the next I/O.
14977 			 * 4) The I/O associated with a DMA error is removed
14978 			 *    from the queue and failed with EIO. Processing of
14979 			 *    the transport queues is also halted to be
14980 			 *    restarted via runout or the next I/O.
14981 			 * 5) The I/O associated with a CDB size or packet
14982 			 *    size error is removed from the queue and failed
14983 			 *    with EIO. Processing of the transport queues is
14984 			 *    continued.
14985 			 *
14986 			 * Note: there is no interface for canceling a runout
14987 			 * callback. To prevent the driver from detaching or
14988 			 * suspending while a runout is pending the driver
14989 			 * state is set to SD_STATE_RWAIT
14990 			 *
14991 			 * Note: using the scsi_init_pkt callback facility can
14992 			 * result in an I/O request persisting at the head of
14993 			 * the list which cannot be satisfied even after
14994 			 * multiple retries. In the future the driver may
14995 			 * implement some kind of maximum runout count before
14996 			 * failing an I/O.
14997 			 *
14998 			 * Note: the use of funcp below may seem superfluous,
14999 			 * but it helps warlock figure out the correct
15000 			 * initpkt function calls (see [s]sd.wlcmd).
15001 			 */
15002 			struct scsi_pkt	*pktp;
15003 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
15004 
15005 			ASSERT(bp != un->un_rqs_bp);
15006 
15007 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
15008 			switch ((*funcp)(bp, &pktp)) {
15009 			case  SD_PKT_ALLOC_SUCCESS:
15010 				xp->xb_pktp = pktp;
15011 				SD_TRACE(SD_LOG_IO_CORE, un,
15012 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
15013 				    pktp);
15014 				goto got_pkt;
15015 
15016 			case SD_PKT_ALLOC_FAILURE:
15017 				/*
15018 				 * Temporary (hopefully) resource depletion.
15019 				 * Since retries and RQS commands always have a
15020 				 * scsi_pkt allocated, these cases should never
15021 				 * get here. So the only cases this needs to
15022 				 * handle is a bp from the waitq (which we put
15023 				 * back onto the waitq for sdrunout), or a bp
15024 				 * sent as an immed_bp (which we just fail).
15025 				 */
15026 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15027 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
15028 
15029 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15030 
15031 				if (bp == immed_bp) {
15032 					/*
15033 					 * If SD_XB_DMA_FREED is clear, then
15034 					 * this is a failure to allocate a
15035 					 * scsi_pkt, and we must fail the
15036 					 * command.
15037 					 */
15038 					if ((xp->xb_pkt_flags &
15039 					    SD_XB_DMA_FREED) == 0) {
15040 						break;
15041 					}
15042 
15043 					/*
15044 					 * If this immediate command is NOT our
15045 					 * un_retry_bp, then we must fail it.
15046 					 */
15047 					if (bp != un->un_retry_bp) {
15048 						break;
15049 					}
15050 
15051 					/*
15052 					 * We get here if this cmd is our
15053 					 * un_retry_bp that was DMAFREED, but
15054 					 * scsi_init_pkt() failed to reallocate
15055 					 * DMA resources when we attempted to
15056 					 * retry it. This can happen when an
15057 					 * mpxio failover is in progress, but
15058 					 * we don't want to just fail the
15059 					 * command in this case.
15060 					 *
15061 					 * Use timeout(9F) to restart it after
15062 					 * a 100ms delay.  We don't want to
15063 					 * let sdrunout() restart it, because
15064 					 * sdrunout() is just supposed to start
15065 					 * commands that are sitting on the
15066 					 * wait queue.  The un_retry_bp stays
15067 					 * set until the command completes, but
15068 					 * sdrunout can be called many times
15069 					 * before that happens.  Since sdrunout
15070 					 * cannot tell if the un_retry_bp is
15071 					 * already in the transport, it could
15072 					 * end up calling scsi_transport() for
15073 					 * the un_retry_bp multiple times.
15074 					 *
15075 					 * Also: don't schedule the callback
15076 					 * if some other callback is already
15077 					 * pending.
15078 					 */
15079 					if (un->un_retry_statp == NULL) {
15080 						/*
15081 						 * restore the kstat pointer to
15082 						 * keep kstat counts coherent
15083 						 * when we do retry the command.
15084 						 */
15085 						un->un_retry_statp =
15086 						    saved_statp;
15087 					}
15088 
15089 					if ((un->un_startstop_timeid == NULL) &&
15090 					    (un->un_retry_timeid == NULL) &&
15091 					    (un->un_direct_priority_timeid ==
15092 					    NULL)) {
15093 
15094 						un->un_retry_timeid =
15095 						    timeout(
15096 						    sd_start_retry_command,
15097 						    un, SD_RESTART_TIMEOUT);
15098 					}
15099 					goto exit;
15100 				}
15101 
15102 #else
15103 				if (bp == immed_bp) {
15104 					break;	/* Just fail the command */
15105 				}
15106 #endif
15107 
15108 				/* Add the buf back to the head of the waitq */
15109 				bp->av_forw = un->un_waitq_headp;
15110 				un->un_waitq_headp = bp;
15111 				if (un->un_waitq_tailp == NULL) {
15112 					un->un_waitq_tailp = bp;
15113 				}
15114 				goto exit;
15115 
15116 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
15117 				/*
15118 				 * HBA DMA resource failure. Fail the command
15119 				 * and continue processing of the queues.
15120 				 */
15121 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15122 				    "sd_start_cmds: "
15123 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15124 				break;
15125 
15126 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15127 				/*
15128 				 * Note:x86: Partial DMA mapping not supported
15129 				 * for USCSI commands, and all the needed DMA
15130 				 * resources were not allocated.
15131 				 */
15132 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15133 				    "sd_start_cmds: "
15134 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15135 				break;
15136 
15137 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15138 				/*
15139 				 * Note:x86: Request cannot fit into CDB based
15140 				 * on lba and len.
15141 				 */
15142 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15143 				    "sd_start_cmds: "
15144 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15145 				break;
15146 
15147 			default:
15148 				/* Should NEVER get here! */
15149 				panic("scsi_initpkt error");
15150 				/*NOTREACHED*/
15151 			}
15152 
15153 			/*
15154 			 * Fatal error in allocating a scsi_pkt for this buf.
15155 			 * Update kstats & return the buf with an error code.
15156 			 * We must use sd_return_failed_command_no_restart() to
15157 			 * avoid a recursive call back into sd_start_cmds().
15158 			 * However this also means that we must keep processing
15159 			 * the waitq here in order to avoid stalling.
15160 			 */
15161 			if (statp == kstat_waitq_to_runq) {
15162 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15163 			}
15164 			sd_return_failed_command_no_restart(un, bp, EIO);
15165 			if (bp == immed_bp) {
15166 				/* immed_bp is gone by now, so clear this */
15167 				immed_bp = NULL;
15168 			}
15169 			continue;
15170 		}
15171 got_pkt:
15172 		if (bp == immed_bp) {
15173 			/* goto the head of the class.... */
15174 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15175 		}
15176 
15177 		un->un_ncmds_in_transport++;
15178 		SD_UPDATE_KSTATS(un, statp, bp);
15179 
15180 		/*
15181 		 * Call scsi_transport() to send the command to the target.
15182 		 * According to SCSA architecture, we must drop the mutex here
15183 		 * before calling scsi_transport() in order to avoid deadlock.
15184 		 * Note that the scsi_pkt's completion routine can be executed
15185 		 * (from interrupt context) even before the call to
15186 		 * scsi_transport() returns.
15187 		 */
15188 		SD_TRACE(SD_LOG_IO_CORE, un,
15189 		    "sd_start_cmds: calling scsi_transport()\n");
15190 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15191 
15192 		mutex_exit(SD_MUTEX(un));
15193 		rval = scsi_transport(xp->xb_pktp);
15194 		mutex_enter(SD_MUTEX(un));
15195 
15196 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15197 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
15198 
15199 		switch (rval) {
15200 		case TRAN_ACCEPT:
15201 			/* Clear this with every pkt accepted by the HBA */
15202 			un->un_tran_fatal_count = 0;
15203 			break;	/* Success; try the next cmd (if any) */
15204 
15205 		case TRAN_BUSY:
15206 			un->un_ncmds_in_transport--;
15207 			ASSERT(un->un_ncmds_in_transport >= 0);
15208 
15209 			/*
15210 			 * Don't retry request sense, the sense data
15211 			 * is lost when another request is sent.
15212 			 * Free up the rqs buf and retry
15213 			 * the original failed cmd.  Update kstat.
15214 			 */
15215 			if (bp == un->un_rqs_bp) {
15216 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15217 				bp = sd_mark_rqs_idle(un, xp);
15218 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15219 				    NULL, NULL, EIO, un->un_busy_timeout / 500,
15220 				    kstat_waitq_enter);
15221 				goto exit;
15222 			}
15223 
15224 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15225 			/*
15226 			 * Free the DMA resources for the  scsi_pkt. This will
15227 			 * allow mpxio to select another path the next time
15228 			 * we call scsi_transport() with this scsi_pkt.
15229 			 * See sdintr() for the rationalization behind this.
15230 			 */
15231 			if ((un->un_f_is_fibre == TRUE) &&
15232 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15233 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15234 				scsi_dmafree(xp->xb_pktp);
15235 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15236 			}
15237 #endif
15238 
15239 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15240 				/*
15241 				 * Commands that are SD_PATH_DIRECT_PRIORITY
15242 				 * are for error recovery situations. These do
15243 				 * not use the normal command waitq, so if they
15244 				 * get a TRAN_BUSY we cannot put them back onto
15245 				 * the waitq for later retry. One possible
15246 				 * problem is that there could already be some
15247 				 * other command on un_retry_bp that is waiting
15248 				 * for this one to complete, so we would be
15249 				 * deadlocked if we put this command back onto
15250 				 * the waitq for later retry (since un_retry_bp
15251 				 * must complete before the driver gets back to
15252 				 * commands on the waitq).
15253 				 *
15254 				 * To avoid deadlock we must schedule a callback
15255 				 * that will restart this command after a set
15256 				 * interval.  This should keep retrying for as
15257 				 * long as the underlying transport keeps
15258 				 * returning TRAN_BUSY (just like for other
15259 				 * commands).  Use the same timeout interval as
15260 				 * for the ordinary TRAN_BUSY retry.
15261 				 */
15262 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15263 				    "sd_start_cmds: scsi_transport() returned "
15264 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15265 
15266 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15267 				un->un_direct_priority_timeid =
15268 				    timeout(sd_start_direct_priority_command,
15269 				    bp, un->un_busy_timeout / 500);
15270 
15271 				goto exit;
15272 			}
15273 
15274 			/*
15275 			 * For TRAN_BUSY, we want to reduce the throttle value,
15276 			 * unless we are retrying a command.
15277 			 */
15278 			if (bp != un->un_retry_bp) {
15279 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15280 			}
15281 
15282 			/*
15283 			 * Set up the bp to be tried again 10 ms later.
15284 			 * Note:x86: Is there a timeout value in the sd_lun
15285 			 * for this condition?
15286 			 */
15287 			sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15288 			    kstat_runq_back_to_waitq);
15289 			goto exit;
15290 
15291 		case TRAN_FATAL_ERROR:
15292 			un->un_tran_fatal_count++;
15293 			/* FALLTHRU */
15294 
15295 		case TRAN_BADPKT:
15296 		default:
15297 			un->un_ncmds_in_transport--;
15298 			ASSERT(un->un_ncmds_in_transport >= 0);
15299 
15300 			/*
15301 			 * If this is our REQUEST SENSE command with a
15302 			 * transport error, we must get back the pointers
15303 			 * to the original buf, and mark the REQUEST
15304 			 * SENSE command as "available".
15305 			 */
15306 			if (bp == un->un_rqs_bp) {
15307 				bp = sd_mark_rqs_idle(un, xp);
15308 				xp = SD_GET_XBUF(bp);
15309 			} else {
15310 				/*
15311 				 * Legacy behavior: do not update transport
15312 				 * error count for request sense commands.
15313 				 */
15314 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
15315 			}
15316 
15317 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15318 			sd_print_transport_rejected_message(un, xp, rval);
15319 
15320 			/*
15321 			 * This command will be terminated by SD driver due
15322 			 * to a fatal transport error. We should post
15323 			 * ereport.io.scsi.cmd.disk.tran with driver-assessment
15324 			 * of "fail" for any command to indicate this
15325 			 * situation.
15326 			 */
15327 			if (xp->xb_ena > 0) {
15328 				ASSERT(un->un_fm_private != NULL);
15329 				sfip = un->un_fm_private;
15330 				sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15331 				sd_ssc_extract_info(&sfip->fm_ssc, un,
15332 				    xp->xb_pktp, bp, xp);
15333 				sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15334 			}
15335 
15336 			/*
15337 			 * We must use sd_return_failed_command_no_restart() to
15338 			 * avoid a recursive call back into sd_start_cmds().
15339 			 * However this also means that we must keep processing
15340 			 * the waitq here in order to avoid stalling.
15341 			 */
15342 			sd_return_failed_command_no_restart(un, bp, EIO);
15343 
15344 			/*
15345 			 * Notify any threads waiting in sd_ddi_suspend() that
15346 			 * a command completion has occurred.
15347 			 */
15348 			if (un->un_state == SD_STATE_SUSPENDED) {
15349 				cv_broadcast(&un->un_disk_busy_cv);
15350 			}
15351 
15352 			if (bp == immed_bp) {
15353 				/* immed_bp is gone by now, so clear this */
15354 				immed_bp = NULL;
15355 			}
15356 			break;
15357 		}
15358 
15359 	} while (immed_bp == NULL);
15360 
15361 exit:
15362 	ASSERT(mutex_owned(SD_MUTEX(un)));
15363 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15364 }
15365 
15366 
15367 /*
15368  *    Function: sd_return_command
15369  *
15370  * Description: Returns a command to its originator (with or without an
15371  *		error).  Also starts commands waiting to be transported
15372  *		to the target.
15373  *
15374  *     Context: May be called from interrupt, kernel, or timeout context
15375  */
15376 
15377 static void
15378 sd_return_command(struct sd_lun *un, struct buf *bp)
15379 {
15380 	struct sd_xbuf *xp;
15381 	struct scsi_pkt *pktp;
15382 	struct sd_fm_internal *sfip;
15383 
15384 	ASSERT(bp != NULL);
15385 	ASSERT(un != NULL);
15386 	ASSERT(mutex_owned(SD_MUTEX(un)));
15387 	ASSERT(bp != un->un_rqs_bp);
15388 	xp = SD_GET_XBUF(bp);
15389 	ASSERT(xp != NULL);
15390 
15391 	pktp = SD_GET_PKTP(bp);
15392 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15393 	ASSERT(sfip != NULL);
15394 
15395 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15396 
15397 	/*
15398 	 * Note: check for the "sdrestart failed" case.
15399 	 */
15400 	if ((un->un_partial_dma_supported == 1) &&
15401 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15402 	    (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15403 	    (xp->xb_pktp->pkt_resid == 0)) {
15404 
15405 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15406 			/*
15407 			 * Successfully set up next portion of cmd
15408 			 * transfer, try sending it
15409 			 */
15410 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15411 			    NULL, NULL, 0, (clock_t)0, NULL);
15412 			sd_start_cmds(un, NULL);
15413 			return;	/* Note:x86: need a return here? */
15414 		}
15415 	}
15416 
15417 	/*
15418 	 * If this is the failfast bp, clear it from un_failfast_bp. This
15419 	 * can happen if upon being re-tried the failfast bp either
15420 	 * succeeded or encountered another error (possibly even a different
15421 	 * error than the one that precipitated the failfast state, but in
15422 	 * that case it would have had to exhaust retries as well). Regardless,
15423 	 * this should not occur whenever the instance is in the active
15424 	 * failfast state.
15425 	 */
15426 	if (bp == un->un_failfast_bp) {
15427 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15428 		un->un_failfast_bp = NULL;
15429 	}
15430 
15431 	/*
15432 	 * Clear the failfast state upon successful completion of ANY cmd.
15433 	 */
15434 	if (bp->b_error == 0) {
15435 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15436 		/*
15437 		 * If this is a successful command, but used to be retried,
15438 		 * we will take it as a recovered command and post an
15439 		 * ereport with driver-assessment of "recovered".
15440 		 */
15441 		if (xp->xb_ena > 0) {
15442 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15443 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15444 		}
15445 	} else {
15446 		/*
15447 		 * If this is a failed non-USCSI command we will post an
15448 		 * ereport with driver-assessment set accordingly("fail" or
15449 		 * "fatal").
15450 		 */
15451 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15452 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15453 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15454 		}
15455 	}
15456 
15457 	/*
15458 	 * This is used if the command was retried one or more times. Show that
15459 	 * we are done with it, and allow processing of the waitq to resume.
15460 	 */
15461 	if (bp == un->un_retry_bp) {
15462 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15463 		    "sd_return_command: un:0x%p: "
15464 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15465 		un->un_retry_bp = NULL;
15466 		un->un_retry_statp = NULL;
15467 	}
15468 
15469 	SD_UPDATE_RDWR_STATS(un, bp);
15470 	SD_UPDATE_PARTITION_STATS(un, bp);
15471 
15472 	switch (un->un_state) {
15473 	case SD_STATE_SUSPENDED:
15474 		/*
15475 		 * Notify any threads waiting in sd_ddi_suspend() that
15476 		 * a command completion has occurred.
15477 		 */
15478 		cv_broadcast(&un->un_disk_busy_cv);
15479 		break;
15480 	default:
15481 		sd_start_cmds(un, NULL);
15482 		break;
15483 	}
15484 
15485 	/* Return this command up the iodone chain to its originator. */
15486 	mutex_exit(SD_MUTEX(un));
15487 
15488 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15489 	xp->xb_pktp = NULL;
15490 
15491 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15492 
15493 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15494 	mutex_enter(SD_MUTEX(un));
15495 
15496 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15497 }
15498 
15499 
15500 /*
15501  *    Function: sd_return_failed_command
15502  *
15503  * Description: Command completion when an error occurred.
15504  *
15505  *     Context: May be called from interrupt context
15506  */
15507 
15508 static void
15509 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15510 {
15511 	ASSERT(bp != NULL);
15512 	ASSERT(un != NULL);
15513 	ASSERT(mutex_owned(SD_MUTEX(un)));
15514 
15515 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15516 	    "sd_return_failed_command: entry\n");
15517 
15518 	/*
15519 	 * b_resid could already be nonzero due to a partial data
15520 	 * transfer, so do not change it here.
15521 	 */
15522 	SD_BIOERROR(bp, errcode);
15523 
15524 	sd_return_command(un, bp);
15525 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15526 	    "sd_return_failed_command: exit\n");
15527 }
15528 
15529 
15530 /*
15531  *    Function: sd_return_failed_command_no_restart
15532  *
15533  * Description: Same as sd_return_failed_command, but ensures that no
15534  *		call back into sd_start_cmds will be issued.
15535  *
15536  *     Context: May be called from interrupt context
15537  */
15538 
15539 static void
15540 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15541 	int errcode)
15542 {
15543 	struct sd_xbuf *xp;
15544 
15545 	ASSERT(bp != NULL);
15546 	ASSERT(un != NULL);
15547 	ASSERT(mutex_owned(SD_MUTEX(un)));
15548 	xp = SD_GET_XBUF(bp);
15549 	ASSERT(xp != NULL);
15550 	ASSERT(errcode != 0);
15551 
15552 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15553 	    "sd_return_failed_command_no_restart: entry\n");
15554 
15555 	/*
15556 	 * b_resid could already be nonzero due to a partial data
15557 	 * transfer, so do not change it here.
15558 	 */
15559 	SD_BIOERROR(bp, errcode);
15560 
15561 	/*
15562 	 * If this is the failfast bp, clear it. This can happen if the
15563 	 * failfast bp encounterd a fatal error when we attempted to
15564 	 * re-try it (such as a scsi_transport(9F) failure).  However
15565 	 * we should NOT be in an active failfast state if the failfast
15566 	 * bp is not NULL.
15567 	 */
15568 	if (bp == un->un_failfast_bp) {
15569 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15570 		un->un_failfast_bp = NULL;
15571 	}
15572 
15573 	if (bp == un->un_retry_bp) {
15574 		/*
15575 		 * This command was retried one or more times. Show that we are
15576 		 * done with it, and allow processing of the waitq to resume.
15577 		 */
15578 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15579 		    "sd_return_failed_command_no_restart: "
15580 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15581 		un->un_retry_bp = NULL;
15582 		un->un_retry_statp = NULL;
15583 	}
15584 
15585 	SD_UPDATE_RDWR_STATS(un, bp);
15586 	SD_UPDATE_PARTITION_STATS(un, bp);
15587 
15588 	mutex_exit(SD_MUTEX(un));
15589 
15590 	if (xp->xb_pktp != NULL) {
15591 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15592 		xp->xb_pktp = NULL;
15593 	}
15594 
15595 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15596 
15597 	mutex_enter(SD_MUTEX(un));
15598 
15599 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15600 	    "sd_return_failed_command_no_restart: exit\n");
15601 }
15602 
15603 
15604 /*
15605  *    Function: sd_retry_command
15606  *
15607  * Description: queue up a command for retry, or (optionally) fail it
15608  *		if retry counts are exhausted.
15609  *
15610  *   Arguments: un - Pointer to the sd_lun struct for the target.
15611  *
15612  *		bp - Pointer to the buf for the command to be retried.
15613  *
15614  *		retry_check_flag - Flag to see which (if any) of the retry
15615  *		   counts should be decremented/checked. If the indicated
15616  *		   retry count is exhausted, then the command will not be
15617  *		   retried; it will be failed instead. This should use a
15618  *		   value equal to one of the following:
15619  *
15620  *			SD_RETRIES_NOCHECK
15621  *			SD_RESD_RETRIES_STANDARD
15622  *			SD_RETRIES_VICTIM
15623  *
15624  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15625  *		   if the check should be made to see of FLAG_ISOLATE is set
15626  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
15627  *		   not retried, it is simply failed.
15628  *
15629  *		user_funcp - Ptr to function to call before dispatching the
15630  *		   command. May be NULL if no action needs to be performed.
15631  *		   (Primarily intended for printing messages.)
15632  *
15633  *		user_arg - Optional argument to be passed along to
15634  *		   the user_funcp call.
15635  *
15636  *		failure_code - errno return code to set in the bp if the
15637  *		   command is going to be failed.
15638  *
15639  *		retry_delay - Retry delay interval in (clock_t) units. May
15640  *		   be zero which indicates that the retry should be retried
15641  *		   immediately (ie, without an intervening delay).
15642  *
15643  *		statp - Ptr to kstat function to be updated if the command
15644  *		   is queued for a delayed retry. May be NULL if no kstat
15645  *		   update is desired.
15646  *
15647  *     Context: May be called from interrupt context.
15648  */
15649 
15650 static void
15651 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15652 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
15653 	code), void *user_arg, int failure_code,  clock_t retry_delay,
15654 	void (*statp)(kstat_io_t *))
15655 {
15656 	struct sd_xbuf	*xp;
15657 	struct scsi_pkt	*pktp;
15658 	struct sd_fm_internal *sfip;
15659 
15660 	ASSERT(un != NULL);
15661 	ASSERT(mutex_owned(SD_MUTEX(un)));
15662 	ASSERT(bp != NULL);
15663 	xp = SD_GET_XBUF(bp);
15664 	ASSERT(xp != NULL);
15665 	pktp = SD_GET_PKTP(bp);
15666 	ASSERT(pktp != NULL);
15667 
15668 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15669 	ASSERT(sfip != NULL);
15670 
15671 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15672 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15673 
15674 	/*
15675 	 * If we are syncing or dumping, fail the command to avoid
15676 	 * recursively calling back into scsi_transport().
15677 	 */
15678 	if (ddi_in_panic()) {
15679 		goto fail_command_no_log;
15680 	}
15681 
15682 	/*
15683 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15684 	 * log an error and fail the command.
15685 	 */
15686 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15687 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15688 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
15689 		sd_dump_memory(un, SD_LOG_IO, "CDB",
15690 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15691 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15692 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15693 		goto fail_command;
15694 	}
15695 
15696 	/*
15697 	 * If we are suspended, then put the command onto head of the
15698 	 * wait queue since we don't want to start more commands, and
15699 	 * clear the un_retry_bp. Next time when we are resumed, will
15700 	 * handle the command in the wait queue.
15701 	 */
15702 	switch (un->un_state) {
15703 	case SD_STATE_SUSPENDED:
15704 	case SD_STATE_DUMPING:
15705 		bp->av_forw = un->un_waitq_headp;
15706 		un->un_waitq_headp = bp;
15707 		if (un->un_waitq_tailp == NULL) {
15708 			un->un_waitq_tailp = bp;
15709 		}
15710 		if (bp == un->un_retry_bp) {
15711 			un->un_retry_bp = NULL;
15712 			un->un_retry_statp = NULL;
15713 		}
15714 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15715 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15716 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15717 		return;
15718 	default:
15719 		break;
15720 	}
15721 
15722 	/*
15723 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
15724 	 * is set; if it is then we do not want to retry the command.
15725 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15726 	 */
15727 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15728 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15729 			goto fail_command;
15730 		}
15731 	}
15732 
15733 
15734 	/*
15735 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15736 	 * command timeout or a selection timeout has occurred. This means
15737 	 * that we were unable to establish an kind of communication with
15738 	 * the target, and subsequent retries and/or commands are likely
15739 	 * to encounter similar results and take a long time to complete.
15740 	 *
15741 	 * If this is a failfast error condition, we need to update the
15742 	 * failfast state, even if this bp does not have B_FAILFAST set.
15743 	 */
15744 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
15745 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15746 			ASSERT(un->un_failfast_bp == NULL);
15747 			/*
15748 			 * If we are already in the active failfast state, and
15749 			 * another failfast error condition has been detected,
15750 			 * then fail this command if it has B_FAILFAST set.
15751 			 * If B_FAILFAST is clear, then maintain the legacy
15752 			 * behavior of retrying heroically, even tho this will
15753 			 * take a lot more time to fail the command.
15754 			 */
15755 			if (bp->b_flags & B_FAILFAST) {
15756 				goto fail_command;
15757 			}
15758 		} else {
15759 			/*
15760 			 * We're not in the active failfast state, but we
15761 			 * have a failfast error condition, so we must begin
15762 			 * transition to the next state. We do this regardless
15763 			 * of whether or not this bp has B_FAILFAST set.
15764 			 */
15765 			if (un->un_failfast_bp == NULL) {
15766 				/*
15767 				 * This is the first bp to meet a failfast
15768 				 * condition so save it on un_failfast_bp &
15769 				 * do normal retry processing. Do not enter
15770 				 * active failfast state yet. This marks
15771 				 * entry into the "failfast pending" state.
15772 				 */
15773 				un->un_failfast_bp = bp;
15774 
15775 			} else if (un->un_failfast_bp == bp) {
15776 				/*
15777 				 * This is the second time *this* bp has
15778 				 * encountered a failfast error condition,
15779 				 * so enter active failfast state & flush
15780 				 * queues as appropriate.
15781 				 */
15782 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
15783 				un->un_failfast_bp = NULL;
15784 				sd_failfast_flushq(un);
15785 
15786 				/*
15787 				 * Fail this bp now if B_FAILFAST set;
15788 				 * otherwise continue with retries. (It would
15789 				 * be pretty ironic if this bp succeeded on a
15790 				 * subsequent retry after we just flushed all
15791 				 * the queues).
15792 				 */
15793 				if (bp->b_flags & B_FAILFAST) {
15794 					goto fail_command;
15795 				}
15796 
15797 #if !defined(lint) && !defined(__lint)
15798 			} else {
15799 				/*
15800 				 * If neither of the preceeding conditionals
15801 				 * was true, it means that there is some
15802 				 * *other* bp that has met an inital failfast
15803 				 * condition and is currently either being
15804 				 * retried or is waiting to be retried. In
15805 				 * that case we should perform normal retry
15806 				 * processing on *this* bp, since there is a
15807 				 * chance that the current failfast condition
15808 				 * is transient and recoverable. If that does
15809 				 * not turn out to be the case, then retries
15810 				 * will be cleared when the wait queue is
15811 				 * flushed anyway.
15812 				 */
15813 #endif
15814 			}
15815 		}
15816 	} else {
15817 		/*
15818 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
15819 		 * likely were able to at least establish some level of
15820 		 * communication with the target and subsequent commands
15821 		 * and/or retries are likely to get through to the target,
15822 		 * In this case we want to be aggressive about clearing
15823 		 * the failfast state. Note that this does not affect
15824 		 * the "failfast pending" condition.
15825 		 */
15826 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15827 	}
15828 
15829 
15830 	/*
15831 	 * Check the specified retry count to see if we can still do
15832 	 * any retries with this pkt before we should fail it.
15833 	 */
15834 	switch (retry_check_flag & SD_RETRIES_MASK) {
15835 	case SD_RETRIES_VICTIM:
15836 		/*
15837 		 * Check the victim retry count. If exhausted, then fall
15838 		 * thru & check against the standard retry count.
15839 		 */
15840 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15841 			/* Increment count & proceed with the retry */
15842 			xp->xb_victim_retry_count++;
15843 			break;
15844 		}
15845 		/* Victim retries exhausted, fall back to std. retries... */
15846 		/* FALLTHRU */
15847 
15848 	case SD_RETRIES_STANDARD:
15849 		if (xp->xb_retry_count >= un->un_retry_count) {
15850 			/* Retries exhausted, fail the command */
15851 			SD_TRACE(SD_LOG_IO_CORE, un,
15852 			    "sd_retry_command: retries exhausted!\n");
15853 			/*
15854 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
15855 			 * commands with nonzero pkt_resid.
15856 			 */
15857 			if ((pktp->pkt_reason == CMD_CMPLT) &&
15858 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15859 			    (pktp->pkt_resid != 0)) {
15860 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15861 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15862 					SD_UPDATE_B_RESID(bp, pktp);
15863 				}
15864 			}
15865 			goto fail_command;
15866 		}
15867 		xp->xb_retry_count++;
15868 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15869 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15870 		break;
15871 
15872 	case SD_RETRIES_UA:
15873 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15874 			/* Retries exhausted, fail the command */
15875 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15876 			    "Unit Attention retries exhausted. "
15877 			    "Check the target.\n");
15878 			goto fail_command;
15879 		}
15880 		xp->xb_ua_retry_count++;
15881 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15882 		    "sd_retry_command: retry count:%d\n",
15883 		    xp->xb_ua_retry_count);
15884 		break;
15885 
15886 	case SD_RETRIES_BUSY:
15887 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
15888 			/* Retries exhausted, fail the command */
15889 			SD_TRACE(SD_LOG_IO_CORE, un,
15890 			    "sd_retry_command: retries exhausted!\n");
15891 			goto fail_command;
15892 		}
15893 		xp->xb_retry_count++;
15894 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15895 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15896 		break;
15897 
15898 	case SD_RETRIES_NOCHECK:
15899 	default:
15900 		/* No retry count to check. Just proceed with the retry */
15901 		break;
15902 	}
15903 
15904 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15905 
15906 	/*
15907 	 * If this is a non-USCSI command being retried
15908 	 * during execution last time, we should post an ereport with
15909 	 * driver-assessment of the value "retry".
15910 	 * For partial DMA, request sense and STATUS_QFULL, there are no
15911 	 * hardware errors, we bypass ereport posting.
15912 	 */
15913 	if (failure_code != 0) {
15914 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15915 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15916 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15917 		}
15918 	}
15919 
15920 	/*
15921 	 * If we were given a zero timeout, we must attempt to retry the
15922 	 * command immediately (ie, without a delay).
15923 	 */
15924 	if (retry_delay == 0) {
15925 		/*
15926 		 * Check some limiting conditions to see if we can actually
15927 		 * do the immediate retry.  If we cannot, then we must
15928 		 * fall back to queueing up a delayed retry.
15929 		 */
15930 		if (un->un_ncmds_in_transport >= un->un_throttle) {
15931 			/*
15932 			 * We are at the throttle limit for the target,
15933 			 * fall back to delayed retry.
15934 			 */
15935 			retry_delay = un->un_busy_timeout;
15936 			statp = kstat_waitq_enter;
15937 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15938 			    "sd_retry_command: immed. retry hit "
15939 			    "throttle!\n");
15940 		} else {
15941 			/*
15942 			 * We're clear to proceed with the immediate retry.
15943 			 * First call the user-provided function (if any)
15944 			 */
15945 			if (user_funcp != NULL) {
15946 				(*user_funcp)(un, bp, user_arg,
15947 				    SD_IMMEDIATE_RETRY_ISSUED);
15948 #ifdef __lock_lint
15949 				sd_print_incomplete_msg(un, bp, user_arg,
15950 				    SD_IMMEDIATE_RETRY_ISSUED);
15951 				sd_print_cmd_incomplete_msg(un, bp, user_arg,
15952 				    SD_IMMEDIATE_RETRY_ISSUED);
15953 				sd_print_sense_failed_msg(un, bp, user_arg,
15954 				    SD_IMMEDIATE_RETRY_ISSUED);
15955 #endif
15956 			}
15957 
15958 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15959 			    "sd_retry_command: issuing immediate retry\n");
15960 
15961 			/*
15962 			 * Call sd_start_cmds() to transport the command to
15963 			 * the target.
15964 			 */
15965 			sd_start_cmds(un, bp);
15966 
15967 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15968 			    "sd_retry_command exit\n");
15969 			return;
15970 		}
15971 	}
15972 
15973 	/*
15974 	 * Set up to retry the command after a delay.
15975 	 * First call the user-provided function (if any)
15976 	 */
15977 	if (user_funcp != NULL) {
15978 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15979 	}
15980 
15981 	sd_set_retry_bp(un, bp, retry_delay, statp);
15982 
15983 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15984 	return;
15985 
15986 fail_command:
15987 
15988 	if (user_funcp != NULL) {
15989 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15990 	}
15991 
15992 fail_command_no_log:
15993 
15994 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15995 	    "sd_retry_command: returning failed command\n");
15996 
15997 	sd_return_failed_command(un, bp, failure_code);
15998 
15999 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
16000 }
16001 
16002 
16003 /*
16004  *    Function: sd_set_retry_bp
16005  *
16006  * Description: Set up the given bp for retry.
16007  *
16008  *   Arguments: un - ptr to associated softstate
16009  *		bp - ptr to buf(9S) for the command
16010  *		retry_delay - time interval before issuing retry (may be 0)
16011  *		statp - optional pointer to kstat function
16012  *
16013  *     Context: May be called under interrupt context
16014  */
16015 
16016 static void
16017 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
16018 	void (*statp)(kstat_io_t *))
16019 {
16020 	ASSERT(un != NULL);
16021 	ASSERT(mutex_owned(SD_MUTEX(un)));
16022 	ASSERT(bp != NULL);
16023 
16024 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16025 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
16026 
16027 	/*
16028 	 * Indicate that the command is being retried. This will not allow any
16029 	 * other commands on the wait queue to be transported to the target
16030 	 * until this command has been completed (success or failure). The
16031 	 * "retry command" is not transported to the target until the given
16032 	 * time delay expires, unless the user specified a 0 retry_delay.
16033 	 *
16034 	 * Note: the timeout(9F) callback routine is what actually calls
16035 	 * sd_start_cmds() to transport the command, with the exception of a
16036 	 * zero retry_delay. The only current implementor of a zero retry delay
16037 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
16038 	 */
16039 	if (un->un_retry_bp == NULL) {
16040 		ASSERT(un->un_retry_statp == NULL);
16041 		un->un_retry_bp = bp;
16042 
16043 		/*
16044 		 * If the user has not specified a delay the command should
16045 		 * be queued and no timeout should be scheduled.
16046 		 */
16047 		if (retry_delay == 0) {
16048 			/*
16049 			 * Save the kstat pointer that will be used in the
16050 			 * call to SD_UPDATE_KSTATS() below, so that
16051 			 * sd_start_cmds() can correctly decrement the waitq
16052 			 * count when it is time to transport this command.
16053 			 */
16054 			un->un_retry_statp = statp;
16055 			goto done;
16056 		}
16057 	}
16058 
16059 	if (un->un_retry_bp == bp) {
16060 		/*
16061 		 * Save the kstat pointer that will be used in the call to
16062 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
16063 		 * correctly decrement the waitq count when it is time to
16064 		 * transport this command.
16065 		 */
16066 		un->un_retry_statp = statp;
16067 
16068 		/*
16069 		 * Schedule a timeout if:
16070 		 *   1) The user has specified a delay.
16071 		 *   2) There is not a START_STOP_UNIT callback pending.
16072 		 *
16073 		 * If no delay has been specified, then it is up to the caller
16074 		 * to ensure that IO processing continues without stalling.
16075 		 * Effectively, this means that the caller will issue the
16076 		 * required call to sd_start_cmds(). The START_STOP_UNIT
16077 		 * callback does this after the START STOP UNIT command has
16078 		 * completed. In either of these cases we should not schedule
16079 		 * a timeout callback here.  Also don't schedule the timeout if
16080 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
16081 		 */
16082 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
16083 		    (un->un_direct_priority_timeid == NULL)) {
16084 			un->un_retry_timeid =
16085 			    timeout(sd_start_retry_command, un, retry_delay);
16086 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16087 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
16088 			    " bp:0x%p un_retry_timeid:0x%p\n",
16089 			    un, bp, un->un_retry_timeid);
16090 		}
16091 	} else {
16092 		/*
16093 		 * We only get in here if there is already another command
16094 		 * waiting to be retried.  In this case, we just put the
16095 		 * given command onto the wait queue, so it can be transported
16096 		 * after the current retry command has completed.
16097 		 *
16098 		 * Also we have to make sure that if the command at the head
16099 		 * of the wait queue is the un_failfast_bp, that we do not
16100 		 * put ahead of it any other commands that are to be retried.
16101 		 */
16102 		if ((un->un_failfast_bp != NULL) &&
16103 		    (un->un_failfast_bp == un->un_waitq_headp)) {
16104 			/*
16105 			 * Enqueue this command AFTER the first command on
16106 			 * the wait queue (which is also un_failfast_bp).
16107 			 */
16108 			bp->av_forw = un->un_waitq_headp->av_forw;
16109 			un->un_waitq_headp->av_forw = bp;
16110 			if (un->un_waitq_headp == un->un_waitq_tailp) {
16111 				un->un_waitq_tailp = bp;
16112 			}
16113 		} else {
16114 			/* Enqueue this command at the head of the waitq. */
16115 			bp->av_forw = un->un_waitq_headp;
16116 			un->un_waitq_headp = bp;
16117 			if (un->un_waitq_tailp == NULL) {
16118 				un->un_waitq_tailp = bp;
16119 			}
16120 		}
16121 
16122 		if (statp == NULL) {
16123 			statp = kstat_waitq_enter;
16124 		}
16125 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16126 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
16127 	}
16128 
16129 done:
16130 	if (statp != NULL) {
16131 		SD_UPDATE_KSTATS(un, statp, bp);
16132 	}
16133 
16134 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16135 	    "sd_set_retry_bp: exit un:0x%p\n", un);
16136 }
16137 
16138 
16139 /*
16140  *    Function: sd_start_retry_command
16141  *
16142  * Description: Start the command that has been waiting on the target's
16143  *		retry queue.  Called from timeout(9F) context after the
16144  *		retry delay interval has expired.
16145  *
16146  *   Arguments: arg - pointer to associated softstate for the device.
16147  *
16148  *     Context: timeout(9F) thread context.  May not sleep.
16149  */
16150 
16151 static void
16152 sd_start_retry_command(void *arg)
16153 {
16154 	struct sd_lun *un = arg;
16155 
16156 	ASSERT(un != NULL);
16157 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16158 
16159 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16160 	    "sd_start_retry_command: entry\n");
16161 
16162 	mutex_enter(SD_MUTEX(un));
16163 
16164 	un->un_retry_timeid = NULL;
16165 
16166 	if (un->un_retry_bp != NULL) {
16167 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16168 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16169 		    un, un->un_retry_bp);
16170 		sd_start_cmds(un, un->un_retry_bp);
16171 	}
16172 
16173 	mutex_exit(SD_MUTEX(un));
16174 
16175 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16176 	    "sd_start_retry_command: exit\n");
16177 }
16178 
16179 /*
16180  *    Function: sd_rmw_msg_print_handler
16181  *
16182  * Description: If RMW mode is enabled and warning message is triggered
16183  *              print I/O count during a fixed interval.
16184  *
16185  *   Arguments: arg - pointer to associated softstate for the device.
16186  *
16187  *     Context: timeout(9F) thread context. May not sleep.
16188  */
16189 static void
16190 sd_rmw_msg_print_handler(void *arg)
16191 {
16192 	struct sd_lun *un = arg;
16193 
16194 	ASSERT(un != NULL);
16195 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16196 
16197 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16198 	    "sd_rmw_msg_print_handler: entry\n");
16199 
16200 	mutex_enter(SD_MUTEX(un));
16201 
16202 	if (un->un_rmw_incre_count > 0) {
16203 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16204 		    "%"PRIu64" I/O requests are not aligned with %d disk "
16205 		    "sector size in %ld seconds. They are handled through "
16206 		    "Read Modify Write but the performance is very low!\n",
16207 		    un->un_rmw_incre_count, un->un_tgt_blocksize,
16208 		    drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
16209 		un->un_rmw_incre_count = 0;
16210 		un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
16211 		    un, SD_RMW_MSG_PRINT_TIMEOUT);
16212 	} else {
16213 		un->un_rmw_msg_timeid = NULL;
16214 	}
16215 
16216 	mutex_exit(SD_MUTEX(un));
16217 
16218 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16219 	    "sd_rmw_msg_print_handler: exit\n");
16220 }
16221 
16222 /*
16223  *    Function: sd_start_direct_priority_command
16224  *
16225  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16226  *		received TRAN_BUSY when we called scsi_transport() to send it
16227  *		to the underlying HBA. This function is called from timeout(9F)
16228  *		context after the delay interval has expired.
16229  *
16230  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
16231  *
16232  *     Context: timeout(9F) thread context.  May not sleep.
16233  */
16234 
16235 static void
16236 sd_start_direct_priority_command(void *arg)
16237 {
16238 	struct buf	*priority_bp = arg;
16239 	struct sd_lun	*un;
16240 
16241 	ASSERT(priority_bp != NULL);
16242 	un = SD_GET_UN(priority_bp);
16243 	ASSERT(un != NULL);
16244 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16245 
16246 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16247 	    "sd_start_direct_priority_command: entry\n");
16248 
16249 	mutex_enter(SD_MUTEX(un));
16250 	un->un_direct_priority_timeid = NULL;
16251 	sd_start_cmds(un, priority_bp);
16252 	mutex_exit(SD_MUTEX(un));
16253 
16254 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16255 	    "sd_start_direct_priority_command: exit\n");
16256 }
16257 
16258 
16259 /*
16260  *    Function: sd_send_request_sense_command
16261  *
16262  * Description: Sends a REQUEST SENSE command to the target
16263  *
16264  *     Context: May be called from interrupt context.
16265  */
16266 
16267 static void
16268 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16269 	struct scsi_pkt *pktp)
16270 {
16271 	ASSERT(bp != NULL);
16272 	ASSERT(un != NULL);
16273 	ASSERT(mutex_owned(SD_MUTEX(un)));
16274 
16275 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16276 	    "entry: buf:0x%p\n", bp);
16277 
16278 	/*
16279 	 * If we are syncing or dumping, then fail the command to avoid a
16280 	 * recursive callback into scsi_transport(). Also fail the command
16281 	 * if we are suspended (legacy behavior).
16282 	 */
16283 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16284 	    (un->un_state == SD_STATE_DUMPING)) {
16285 		sd_return_failed_command(un, bp, EIO);
16286 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16287 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
16288 		return;
16289 	}
16290 
16291 	/*
16292 	 * Retry the failed command and don't issue the request sense if:
16293 	 *    1) the sense buf is busy
16294 	 *    2) we have 1 or more outstanding commands on the target
16295 	 *    (the sense data will be cleared or invalidated any way)
16296 	 *
16297 	 * Note: There could be an issue with not checking a retry limit here,
16298 	 * the problem is determining which retry limit to check.
16299 	 */
16300 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16301 		/* Don't retry if the command is flagged as non-retryable */
16302 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16303 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16304 			    NULL, NULL, 0, un->un_busy_timeout,
16305 			    kstat_waitq_enter);
16306 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16307 			    "sd_send_request_sense_command: "
16308 			    "at full throttle, retrying exit\n");
16309 		} else {
16310 			sd_return_failed_command(un, bp, EIO);
16311 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16312 			    "sd_send_request_sense_command: "
16313 			    "at full throttle, non-retryable exit\n");
16314 		}
16315 		return;
16316 	}
16317 
16318 	sd_mark_rqs_busy(un, bp);
16319 	sd_start_cmds(un, un->un_rqs_bp);
16320 
16321 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16322 	    "sd_send_request_sense_command: exit\n");
16323 }
16324 
16325 
16326 /*
16327  *    Function: sd_mark_rqs_busy
16328  *
16329  * Description: Indicate that the request sense bp for this instance is
16330  *		in use.
16331  *
16332  *     Context: May be called under interrupt context
16333  */
16334 
16335 static void
16336 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16337 {
16338 	struct sd_xbuf	*sense_xp;
16339 
16340 	ASSERT(un != NULL);
16341 	ASSERT(bp != NULL);
16342 	ASSERT(mutex_owned(SD_MUTEX(un)));
16343 	ASSERT(un->un_sense_isbusy == 0);
16344 
16345 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16346 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16347 
16348 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16349 	ASSERT(sense_xp != NULL);
16350 
16351 	SD_INFO(SD_LOG_IO, un,
16352 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16353 
16354 	ASSERT(sense_xp->xb_pktp != NULL);
16355 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16356 	    == (FLAG_SENSING | FLAG_HEAD));
16357 
16358 	un->un_sense_isbusy = 1;
16359 	un->un_rqs_bp->b_resid = 0;
16360 	sense_xp->xb_pktp->pkt_resid  = 0;
16361 	sense_xp->xb_pktp->pkt_reason = 0;
16362 
16363 	/* So we can get back the bp at interrupt time! */
16364 	sense_xp->xb_sense_bp = bp;
16365 
16366 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16367 
16368 	/*
16369 	 * Mark this buf as awaiting sense data. (This is already set in
16370 	 * the pkt_flags for the RQS packet.)
16371 	 */
16372 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16373 
16374 	/* Request sense down same path */
16375 	if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16376 	    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16377 		sense_xp->xb_pktp->pkt_path_instance =
16378 		    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16379 
16380 	sense_xp->xb_retry_count	= 0;
16381 	sense_xp->xb_victim_retry_count = 0;
16382 	sense_xp->xb_ua_retry_count	= 0;
16383 	sense_xp->xb_nr_retry_count 	= 0;
16384 	sense_xp->xb_dma_resid  = 0;
16385 
16386 	/* Clean up the fields for auto-request sense */
16387 	sense_xp->xb_sense_status = 0;
16388 	sense_xp->xb_sense_state  = 0;
16389 	sense_xp->xb_sense_resid  = 0;
16390 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16391 
16392 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16393 }
16394 
16395 
16396 /*
16397  *    Function: sd_mark_rqs_idle
16398  *
16399  * Description: SD_MUTEX must be held continuously through this routine
16400  *		to prevent reuse of the rqs struct before the caller can
16401  *		complete it's processing.
16402  *
16403  * Return Code: Pointer to the RQS buf
16404  *
16405  *     Context: May be called under interrupt context
16406  */
16407 
16408 static struct buf *
16409 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16410 {
16411 	struct buf *bp;
16412 	ASSERT(un != NULL);
16413 	ASSERT(sense_xp != NULL);
16414 	ASSERT(mutex_owned(SD_MUTEX(un)));
16415 	ASSERT(un->un_sense_isbusy != 0);
16416 
16417 	un->un_sense_isbusy = 0;
16418 	bp = sense_xp->xb_sense_bp;
16419 	sense_xp->xb_sense_bp = NULL;
16420 
16421 	/* This pkt is no longer interested in getting sense data */
16422 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16423 
16424 	return (bp);
16425 }
16426 
16427 
16428 
16429 /*
16430  *    Function: sd_alloc_rqs
16431  *
16432  * Description: Set up the unit to receive auto request sense data
16433  *
16434  * Return Code: DDI_SUCCESS or DDI_FAILURE
16435  *
16436  *     Context: Called under attach(9E) context
16437  */
16438 
16439 static int
16440 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16441 {
16442 	struct sd_xbuf *xp;
16443 
16444 	ASSERT(un != NULL);
16445 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16446 	ASSERT(un->un_rqs_bp == NULL);
16447 	ASSERT(un->un_rqs_pktp == NULL);
16448 
16449 	/*
16450 	 * First allocate the required buf and scsi_pkt structs, then set up
16451 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
16452 	 */
16453 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16454 	    MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16455 	if (un->un_rqs_bp == NULL) {
16456 		return (DDI_FAILURE);
16457 	}
16458 
16459 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16460 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16461 
16462 	if (un->un_rqs_pktp == NULL) {
16463 		sd_free_rqs(un);
16464 		return (DDI_FAILURE);
16465 	}
16466 
16467 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16468 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16469 	    SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16470 
16471 	SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16472 
16473 	/* Set up the other needed members in the ARQ scsi_pkt. */
16474 	un->un_rqs_pktp->pkt_comp   = sdintr;
16475 	un->un_rqs_pktp->pkt_time   = sd_io_time;
16476 	un->un_rqs_pktp->pkt_flags |=
16477 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
16478 
16479 	/*
16480 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16481 	 * provide any intpkt, destroypkt routines as we take care of
16482 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16483 	 */
16484 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16485 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16486 	xp->xb_pktp = un->un_rqs_pktp;
16487 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
16488 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16489 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16490 
16491 	/*
16492 	 * Save the pointer to the request sense private bp so it can
16493 	 * be retrieved in sdintr.
16494 	 */
16495 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16496 	ASSERT(un->un_rqs_bp->b_private == xp);
16497 
16498 	/*
16499 	 * See if the HBA supports auto-request sense for the specified
16500 	 * target/lun. If it does, then try to enable it (if not already
16501 	 * enabled).
16502 	 *
16503 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16504 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16505 	 * return success.  However, in both of these cases ARQ is always
16506 	 * enabled and scsi_ifgetcap will always return true. The best approach
16507 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16508 	 *
16509 	 * The 3rd case is the HBA (adp) always return enabled on
16510 	 * scsi_ifgetgetcap even when it's not enable, the best approach
16511 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16512 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16513 	 */
16514 
16515 	if (un->un_f_is_fibre == TRUE) {
16516 		un->un_f_arq_enabled = TRUE;
16517 	} else {
16518 #if defined(__i386) || defined(__amd64)
16519 		/*
16520 		 * Circumvent the Adaptec bug, remove this code when
16521 		 * the bug is fixed
16522 		 */
16523 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16524 #endif
16525 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16526 		case 0:
16527 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16528 			    "sd_alloc_rqs: HBA supports ARQ\n");
16529 			/*
16530 			 * ARQ is supported by this HBA but currently is not
16531 			 * enabled. Attempt to enable it and if successful then
16532 			 * mark this instance as ARQ enabled.
16533 			 */
16534 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16535 			    == 1) {
16536 				/* Successfully enabled ARQ in the HBA */
16537 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16538 				    "sd_alloc_rqs: ARQ enabled\n");
16539 				un->un_f_arq_enabled = TRUE;
16540 			} else {
16541 				/* Could not enable ARQ in the HBA */
16542 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16543 				    "sd_alloc_rqs: failed ARQ enable\n");
16544 				un->un_f_arq_enabled = FALSE;
16545 			}
16546 			break;
16547 		case 1:
16548 			/*
16549 			 * ARQ is supported by this HBA and is already enabled.
16550 			 * Just mark ARQ as enabled for this instance.
16551 			 */
16552 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16553 			    "sd_alloc_rqs: ARQ already enabled\n");
16554 			un->un_f_arq_enabled = TRUE;
16555 			break;
16556 		default:
16557 			/*
16558 			 * ARQ is not supported by this HBA; disable it for this
16559 			 * instance.
16560 			 */
16561 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16562 			    "sd_alloc_rqs: HBA does not support ARQ\n");
16563 			un->un_f_arq_enabled = FALSE;
16564 			break;
16565 		}
16566 	}
16567 
16568 	return (DDI_SUCCESS);
16569 }
16570 
16571 
16572 /*
16573  *    Function: sd_free_rqs
16574  *
16575  * Description: Cleanup for the pre-instance RQS command.
16576  *
16577  *     Context: Kernel thread context
16578  */
16579 
16580 static void
16581 sd_free_rqs(struct sd_lun *un)
16582 {
16583 	ASSERT(un != NULL);
16584 
16585 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16586 
16587 	/*
16588 	 * If consistent memory is bound to a scsi_pkt, the pkt
16589 	 * has to be destroyed *before* freeing the consistent memory.
16590 	 * Don't change the sequence of this operations.
16591 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
16592 	 * after it was freed in scsi_free_consistent_buf().
16593 	 */
16594 	if (un->un_rqs_pktp != NULL) {
16595 		scsi_destroy_pkt(un->un_rqs_pktp);
16596 		un->un_rqs_pktp = NULL;
16597 	}
16598 
16599 	if (un->un_rqs_bp != NULL) {
16600 		struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16601 		if (xp != NULL) {
16602 			kmem_free(xp, sizeof (struct sd_xbuf));
16603 		}
16604 		scsi_free_consistent_buf(un->un_rqs_bp);
16605 		un->un_rqs_bp = NULL;
16606 	}
16607 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16608 }
16609 
16610 
16611 
16612 /*
16613  *    Function: sd_reduce_throttle
16614  *
16615  * Description: Reduces the maximum # of outstanding commands on a
16616  *		target to the current number of outstanding commands.
16617  *		Queues a tiemout(9F) callback to restore the limit
16618  *		after a specified interval has elapsed.
16619  *		Typically used when we get a TRAN_BUSY return code
16620  *		back from scsi_transport().
16621  *
16622  *   Arguments: un - ptr to the sd_lun softstate struct
16623  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16624  *
16625  *     Context: May be called from interrupt context
16626  */
16627 
16628 static void
16629 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16630 {
16631 	ASSERT(un != NULL);
16632 	ASSERT(mutex_owned(SD_MUTEX(un)));
16633 	ASSERT(un->un_ncmds_in_transport >= 0);
16634 
16635 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16636 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16637 	    un, un->un_throttle, un->un_ncmds_in_transport);
16638 
16639 	if (un->un_throttle > 1) {
16640 		if (un->un_f_use_adaptive_throttle == TRUE) {
16641 			switch (throttle_type) {
16642 			case SD_THROTTLE_TRAN_BUSY:
16643 				if (un->un_busy_throttle == 0) {
16644 					un->un_busy_throttle = un->un_throttle;
16645 				}
16646 				break;
16647 			case SD_THROTTLE_QFULL:
16648 				un->un_busy_throttle = 0;
16649 				break;
16650 			default:
16651 				ASSERT(FALSE);
16652 			}
16653 
16654 			if (un->un_ncmds_in_transport > 0) {
16655 				un->un_throttle = un->un_ncmds_in_transport;
16656 			}
16657 
16658 		} else {
16659 			if (un->un_ncmds_in_transport == 0) {
16660 				un->un_throttle = 1;
16661 			} else {
16662 				un->un_throttle = un->un_ncmds_in_transport;
16663 			}
16664 		}
16665 	}
16666 
16667 	/* Reschedule the timeout if none is currently active */
16668 	if (un->un_reset_throttle_timeid == NULL) {
16669 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16670 		    un, SD_THROTTLE_RESET_INTERVAL);
16671 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16672 		    "sd_reduce_throttle: timeout scheduled!\n");
16673 	}
16674 
16675 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16676 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16677 }
16678 
16679 
16680 
16681 /*
16682  *    Function: sd_restore_throttle
16683  *
16684  * Description: Callback function for timeout(9F).  Resets the current
16685  *		value of un->un_throttle to its default.
16686  *
16687  *   Arguments: arg - pointer to associated softstate for the device.
16688  *
16689  *     Context: May be called from interrupt context
16690  */
16691 
16692 static void
16693 sd_restore_throttle(void *arg)
16694 {
16695 	struct sd_lun	*un = arg;
16696 
16697 	ASSERT(un != NULL);
16698 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16699 
16700 	mutex_enter(SD_MUTEX(un));
16701 
16702 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16703 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16704 
16705 	un->un_reset_throttle_timeid = NULL;
16706 
16707 	if (un->un_f_use_adaptive_throttle == TRUE) {
16708 		/*
16709 		 * If un_busy_throttle is nonzero, then it contains the
16710 		 * value that un_throttle was when we got a TRAN_BUSY back
16711 		 * from scsi_transport(). We want to revert back to this
16712 		 * value.
16713 		 *
16714 		 * In the QFULL case, the throttle limit will incrementally
16715 		 * increase until it reaches max throttle.
16716 		 */
16717 		if (un->un_busy_throttle > 0) {
16718 			un->un_throttle = un->un_busy_throttle;
16719 			un->un_busy_throttle = 0;
16720 		} else {
16721 			/*
16722 			 * increase throttle by 10% open gate slowly, schedule
16723 			 * another restore if saved throttle has not been
16724 			 * reached
16725 			 */
16726 			short throttle;
16727 			if (sd_qfull_throttle_enable) {
16728 				throttle = un->un_throttle +
16729 				    max((un->un_throttle / 10), 1);
16730 				un->un_throttle =
16731 				    (throttle < un->un_saved_throttle) ?
16732 				    throttle : un->un_saved_throttle;
16733 				if (un->un_throttle < un->un_saved_throttle) {
16734 					un->un_reset_throttle_timeid =
16735 					    timeout(sd_restore_throttle,
16736 					    un,
16737 					    SD_QFULL_THROTTLE_RESET_INTERVAL);
16738 				}
16739 			}
16740 		}
16741 
16742 		/*
16743 		 * If un_throttle has fallen below the low-water mark, we
16744 		 * restore the maximum value here (and allow it to ratchet
16745 		 * down again if necessary).
16746 		 */
16747 		if (un->un_throttle < un->un_min_throttle) {
16748 			un->un_throttle = un->un_saved_throttle;
16749 		}
16750 	} else {
16751 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16752 		    "restoring limit from 0x%x to 0x%x\n",
16753 		    un->un_throttle, un->un_saved_throttle);
16754 		un->un_throttle = un->un_saved_throttle;
16755 	}
16756 
16757 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16758 	    "sd_restore_throttle: calling sd_start_cmds!\n");
16759 
16760 	sd_start_cmds(un, NULL);
16761 
16762 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16763 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16764 	    un, un->un_throttle);
16765 
16766 	mutex_exit(SD_MUTEX(un));
16767 
16768 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16769 }
16770 
16771 /*
16772  *    Function: sdrunout
16773  *
16774  * Description: Callback routine for scsi_init_pkt when a resource allocation
16775  *		fails.
16776  *
16777  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16778  *		soft state instance.
16779  *
16780  * Return Code: The scsi_init_pkt routine allows for the callback function to
16781  *		return a 0 indicating the callback should be rescheduled or a 1
16782  *		indicating not to reschedule. This routine always returns 1
16783  *		because the driver always provides a callback function to
16784  *		scsi_init_pkt. This results in a callback always being scheduled
16785  *		(via the scsi_init_pkt callback implementation) if a resource
16786  *		failure occurs.
16787  *
16788  *     Context: This callback function may not block or call routines that block
16789  *
16790  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16791  *		request persisting at the head of the list which cannot be
16792  *		satisfied even after multiple retries. In the future the driver
16793  *		may implement some time of maximum runout count before failing
16794  *		an I/O.
16795  */
16796 
16797 static int
16798 sdrunout(caddr_t arg)
16799 {
16800 	struct sd_lun	*un = (struct sd_lun *)arg;
16801 
16802 	ASSERT(un != NULL);
16803 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16804 
16805 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16806 
16807 	mutex_enter(SD_MUTEX(un));
16808 	sd_start_cmds(un, NULL);
16809 	mutex_exit(SD_MUTEX(un));
16810 	/*
16811 	 * This callback routine always returns 1 (i.e. do not reschedule)
16812 	 * because we always specify sdrunout as the callback handler for
16813 	 * scsi_init_pkt inside the call to sd_start_cmds.
16814 	 */
16815 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16816 	return (1);
16817 }
16818 
16819 
16820 /*
16821  *    Function: sdintr
16822  *
16823  * Description: Completion callback routine for scsi_pkt(9S) structs
16824  *		sent to the HBA driver via scsi_transport(9F).
16825  *
16826  *     Context: Interrupt context
16827  */
16828 
16829 static void
16830 sdintr(struct scsi_pkt *pktp)
16831 {
16832 	struct buf	*bp;
16833 	struct sd_xbuf	*xp;
16834 	struct sd_lun	*un;
16835 	size_t		actual_len;
16836 	sd_ssc_t	*sscp;
16837 
16838 	ASSERT(pktp != NULL);
16839 	bp = (struct buf *)pktp->pkt_private;
16840 	ASSERT(bp != NULL);
16841 	xp = SD_GET_XBUF(bp);
16842 	ASSERT(xp != NULL);
16843 	ASSERT(xp->xb_pktp != NULL);
16844 	un = SD_GET_UN(bp);
16845 	ASSERT(un != NULL);
16846 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16847 
16848 #ifdef SD_FAULT_INJECTION
16849 
16850 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16851 	/* SD FaultInjection */
16852 	sd_faultinjection(pktp);
16853 
16854 #endif /* SD_FAULT_INJECTION */
16855 
16856 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16857 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
16858 
16859 	mutex_enter(SD_MUTEX(un));
16860 
16861 	ASSERT(un->un_fm_private != NULL);
16862 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16863 	ASSERT(sscp != NULL);
16864 
16865 	/* Reduce the count of the #commands currently in transport */
16866 	un->un_ncmds_in_transport--;
16867 	ASSERT(un->un_ncmds_in_transport >= 0);
16868 
16869 	/* Increment counter to indicate that the callback routine is active */
16870 	un->un_in_callback++;
16871 
16872 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16873 
16874 #ifdef	SDDEBUG
16875 	if (bp == un->un_retry_bp) {
16876 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16877 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16878 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
16879 	}
16880 #endif
16881 
16882 	/*
16883 	 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16884 	 * state if needed.
16885 	 */
16886 	if (pktp->pkt_reason == CMD_DEV_GONE) {
16887 		/* Prevent multiple console messages for the same failure. */
16888 		if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16889 			un->un_last_pkt_reason = CMD_DEV_GONE;
16890 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16891 			    "Command failed to complete...Device is gone\n");
16892 		}
16893 		if (un->un_mediastate != DKIO_DEV_GONE) {
16894 			un->un_mediastate = DKIO_DEV_GONE;
16895 			cv_broadcast(&un->un_state_cv);
16896 		}
16897 		/*
16898 		 * If the command happens to be the REQUEST SENSE command,
16899 		 * free up the rqs buf and fail the original command.
16900 		 */
16901 		if (bp == un->un_rqs_bp) {
16902 			bp = sd_mark_rqs_idle(un, xp);
16903 		}
16904 		sd_return_failed_command(un, bp, EIO);
16905 		goto exit;
16906 	}
16907 
16908 	if (pktp->pkt_state & STATE_XARQ_DONE) {
16909 		SD_TRACE(SD_LOG_COMMON, un,
16910 		    "sdintr: extra sense data received. pkt=%p\n", pktp);
16911 	}
16912 
16913 	/*
16914 	 * First see if the pkt has auto-request sense data with it....
16915 	 * Look at the packet state first so we don't take a performance
16916 	 * hit looking at the arq enabled flag unless absolutely necessary.
16917 	 */
16918 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16919 	    (un->un_f_arq_enabled == TRUE)) {
16920 		/*
16921 		 * The HBA did an auto request sense for this command so check
16922 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16923 		 * driver command that should not be retried.
16924 		 */
16925 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16926 			/*
16927 			 * Save the relevant sense info into the xp for the
16928 			 * original cmd.
16929 			 */
16930 			struct scsi_arq_status *asp;
16931 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16932 			xp->xb_sense_status =
16933 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
16934 			xp->xb_sense_state  = asp->sts_rqpkt_state;
16935 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16936 			if (pktp->pkt_state & STATE_XARQ_DONE) {
16937 				actual_len = MAX_SENSE_LENGTH -
16938 				    xp->xb_sense_resid;
16939 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16940 				    MAX_SENSE_LENGTH);
16941 			} else {
16942 				if (xp->xb_sense_resid > SENSE_LENGTH) {
16943 					actual_len = MAX_SENSE_LENGTH -
16944 					    xp->xb_sense_resid;
16945 				} else {
16946 					actual_len = SENSE_LENGTH -
16947 					    xp->xb_sense_resid;
16948 				}
16949 				if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16950 					if ((((struct uscsi_cmd *)
16951 					    (xp->xb_pktinfo))->uscsi_rqlen) >
16952 					    actual_len) {
16953 						xp->xb_sense_resid =
16954 						    (((struct uscsi_cmd *)
16955 						    (xp->xb_pktinfo))->
16956 						    uscsi_rqlen) - actual_len;
16957 					} else {
16958 						xp->xb_sense_resid = 0;
16959 					}
16960 				}
16961 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16962 				    SENSE_LENGTH);
16963 			}
16964 
16965 			/* fail the command */
16966 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16967 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
16968 			sd_return_failed_command(un, bp, EIO);
16969 			goto exit;
16970 		}
16971 
16972 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16973 		/*
16974 		 * We want to either retry or fail this command, so free
16975 		 * the DMA resources here.  If we retry the command then
16976 		 * the DMA resources will be reallocated in sd_start_cmds().
16977 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16978 		 * causes the *entire* transfer to start over again from the
16979 		 * beginning of the request, even for PARTIAL chunks that
16980 		 * have already transferred successfully.
16981 		 */
16982 		if ((un->un_f_is_fibre == TRUE) &&
16983 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16984 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16985 			scsi_dmafree(pktp);
16986 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16987 		}
16988 #endif
16989 
16990 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16991 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
16992 
16993 		sd_handle_auto_request_sense(un, bp, xp, pktp);
16994 		goto exit;
16995 	}
16996 
16997 	/* Next see if this is the REQUEST SENSE pkt for the instance */
16998 	if (pktp->pkt_flags & FLAG_SENSING)  {
16999 		/* This pktp is from the unit's REQUEST_SENSE command */
17000 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17001 		    "sdintr: sd_handle_request_sense\n");
17002 		sd_handle_request_sense(un, bp, xp, pktp);
17003 		goto exit;
17004 	}
17005 
17006 	/*
17007 	 * Check to see if the command successfully completed as requested;
17008 	 * this is the most common case (and also the hot performance path).
17009 	 *
17010 	 * Requirements for successful completion are:
17011 	 * pkt_reason is CMD_CMPLT and packet status is status good.
17012 	 * In addition:
17013 	 * - A residual of zero indicates successful completion no matter what
17014 	 *   the command is.
17015 	 * - If the residual is not zero and the command is not a read or
17016 	 *   write, then it's still defined as successful completion. In other
17017 	 *   words, if the command is a read or write the residual must be
17018 	 *   zero for successful completion.
17019 	 * - If the residual is not zero and the command is a read or
17020 	 *   write, and it's a USCSICMD, then it's still defined as
17021 	 *   successful completion.
17022 	 */
17023 	if ((pktp->pkt_reason == CMD_CMPLT) &&
17024 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
17025 
17026 		/*
17027 		 * Since this command is returned with a good status, we
17028 		 * can reset the count for Sonoma failover.
17029 		 */
17030 		un->un_sonoma_failure_count = 0;
17031 
17032 		/*
17033 		 * Return all USCSI commands on good status
17034 		 */
17035 		if (pktp->pkt_resid == 0) {
17036 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17037 			    "sdintr: returning command for resid == 0\n");
17038 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
17039 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
17040 			SD_UPDATE_B_RESID(bp, pktp);
17041 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17042 			    "sdintr: returning command for resid != 0\n");
17043 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17044 			SD_UPDATE_B_RESID(bp, pktp);
17045 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17046 			    "sdintr: returning uscsi command\n");
17047 		} else {
17048 			goto not_successful;
17049 		}
17050 		sd_return_command(un, bp);
17051 
17052 		/*
17053 		 * Decrement counter to indicate that the callback routine
17054 		 * is done.
17055 		 */
17056 		un->un_in_callback--;
17057 		ASSERT(un->un_in_callback >= 0);
17058 		mutex_exit(SD_MUTEX(un));
17059 
17060 		return;
17061 	}
17062 
17063 not_successful:
17064 
17065 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
17066 	/*
17067 	 * The following is based upon knowledge of the underlying transport
17068 	 * and its use of DMA resources.  This code should be removed when
17069 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
17070 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
17071 	 * and sd_start_cmds().
17072 	 *
17073 	 * Free any DMA resources associated with this command if there
17074 	 * is a chance it could be retried or enqueued for later retry.
17075 	 * If we keep the DMA binding then mpxio cannot reissue the
17076 	 * command on another path whenever a path failure occurs.
17077 	 *
17078 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
17079 	 * causes the *entire* transfer to start over again from the
17080 	 * beginning of the request, even for PARTIAL chunks that
17081 	 * have already transferred successfully.
17082 	 *
17083 	 * This is only done for non-uscsi commands (and also skipped for the
17084 	 * driver's internal RQS command). Also just do this for Fibre Channel
17085 	 * devices as these are the only ones that support mpxio.
17086 	 */
17087 	if ((un->un_f_is_fibre == TRUE) &&
17088 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17089 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
17090 		scsi_dmafree(pktp);
17091 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17092 	}
17093 #endif
17094 
17095 	/*
17096 	 * The command did not successfully complete as requested so check
17097 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17098 	 * driver command that should not be retried so just return. If
17099 	 * FLAG_DIAGNOSE is not set the error will be processed below.
17100 	 */
17101 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17102 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17103 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
17104 		/*
17105 		 * Issue a request sense if a check condition caused the error
17106 		 * (we handle the auto request sense case above), otherwise
17107 		 * just fail the command.
17108 		 */
17109 		if ((pktp->pkt_reason == CMD_CMPLT) &&
17110 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
17111 			sd_send_request_sense_command(un, bp, pktp);
17112 		} else {
17113 			sd_return_failed_command(un, bp, EIO);
17114 		}
17115 		goto exit;
17116 	}
17117 
17118 	/*
17119 	 * The command did not successfully complete as requested so process
17120 	 * the error, retry, and/or attempt recovery.
17121 	 */
17122 	switch (pktp->pkt_reason) {
17123 	case CMD_CMPLT:
17124 		switch (SD_GET_PKT_STATUS(pktp)) {
17125 		case STATUS_GOOD:
17126 			/*
17127 			 * The command completed successfully with a non-zero
17128 			 * residual
17129 			 */
17130 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17131 			    "sdintr: STATUS_GOOD \n");
17132 			sd_pkt_status_good(un, bp, xp, pktp);
17133 			break;
17134 
17135 		case STATUS_CHECK:
17136 		case STATUS_TERMINATED:
17137 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17138 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17139 			sd_pkt_status_check_condition(un, bp, xp, pktp);
17140 			break;
17141 
17142 		case STATUS_BUSY:
17143 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17144 			    "sdintr: STATUS_BUSY\n");
17145 			sd_pkt_status_busy(un, bp, xp, pktp);
17146 			break;
17147 
17148 		case STATUS_RESERVATION_CONFLICT:
17149 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17150 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
17151 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17152 			break;
17153 
17154 		case STATUS_QFULL:
17155 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17156 			    "sdintr: STATUS_QFULL\n");
17157 			sd_pkt_status_qfull(un, bp, xp, pktp);
17158 			break;
17159 
17160 		case STATUS_MET:
17161 		case STATUS_INTERMEDIATE:
17162 		case STATUS_SCSI2:
17163 		case STATUS_INTERMEDIATE_MET:
17164 		case STATUS_ACA_ACTIVE:
17165 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17166 			    "Unexpected SCSI status received: 0x%x\n",
17167 			    SD_GET_PKT_STATUS(pktp));
17168 			/*
17169 			 * Mark the ssc_flags when detected invalid status
17170 			 * code for non-USCSI command.
17171 			 */
17172 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17173 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17174 				    0, "stat-code");
17175 			}
17176 			sd_return_failed_command(un, bp, EIO);
17177 			break;
17178 
17179 		default:
17180 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17181 			    "Invalid SCSI status received: 0x%x\n",
17182 			    SD_GET_PKT_STATUS(pktp));
17183 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17184 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17185 				    0, "stat-code");
17186 			}
17187 			sd_return_failed_command(un, bp, EIO);
17188 			break;
17189 
17190 		}
17191 		break;
17192 
17193 	case CMD_INCOMPLETE:
17194 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17195 		    "sdintr:  CMD_INCOMPLETE\n");
17196 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17197 		break;
17198 	case CMD_TRAN_ERR:
17199 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17200 		    "sdintr: CMD_TRAN_ERR\n");
17201 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17202 		break;
17203 	case CMD_RESET:
17204 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17205 		    "sdintr: CMD_RESET \n");
17206 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17207 		break;
17208 	case CMD_ABORTED:
17209 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17210 		    "sdintr: CMD_ABORTED \n");
17211 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17212 		break;
17213 	case CMD_TIMEOUT:
17214 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17215 		    "sdintr: CMD_TIMEOUT\n");
17216 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17217 		break;
17218 	case CMD_UNX_BUS_FREE:
17219 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17220 		    "sdintr: CMD_UNX_BUS_FREE \n");
17221 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17222 		break;
17223 	case CMD_TAG_REJECT:
17224 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17225 		    "sdintr: CMD_TAG_REJECT\n");
17226 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17227 		break;
17228 	default:
17229 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17230 		    "sdintr: default\n");
17231 		/*
17232 		 * Mark the ssc_flags for detecting invliad pkt_reason.
17233 		 */
17234 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17235 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17236 			    0, "pkt-reason");
17237 		}
17238 		sd_pkt_reason_default(un, bp, xp, pktp);
17239 		break;
17240 	}
17241 
17242 exit:
17243 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17244 
17245 	/* Decrement counter to indicate that the callback routine is done. */
17246 	un->un_in_callback--;
17247 	ASSERT(un->un_in_callback >= 0);
17248 
17249 	/*
17250 	 * At this point, the pkt has been dispatched, ie, it is either
17251 	 * being re-tried or has been returned to its caller and should
17252 	 * not be referenced.
17253 	 */
17254 
17255 	mutex_exit(SD_MUTEX(un));
17256 }
17257 
17258 
17259 /*
17260  *    Function: sd_print_incomplete_msg
17261  *
17262  * Description: Prints the error message for a CMD_INCOMPLETE error.
17263  *
17264  *   Arguments: un - ptr to associated softstate for the device.
17265  *		bp - ptr to the buf(9S) for the command.
17266  *		arg - message string ptr
17267  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17268  *			or SD_NO_RETRY_ISSUED.
17269  *
17270  *     Context: May be called under interrupt context
17271  */
17272 
17273 static void
17274 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17275 {
17276 	struct scsi_pkt	*pktp;
17277 	char	*msgp;
17278 	char	*cmdp = arg;
17279 
17280 	ASSERT(un != NULL);
17281 	ASSERT(mutex_owned(SD_MUTEX(un)));
17282 	ASSERT(bp != NULL);
17283 	ASSERT(arg != NULL);
17284 	pktp = SD_GET_PKTP(bp);
17285 	ASSERT(pktp != NULL);
17286 
17287 	switch (code) {
17288 	case SD_DELAYED_RETRY_ISSUED:
17289 	case SD_IMMEDIATE_RETRY_ISSUED:
17290 		msgp = "retrying";
17291 		break;
17292 	case SD_NO_RETRY_ISSUED:
17293 	default:
17294 		msgp = "giving up";
17295 		break;
17296 	}
17297 
17298 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17299 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17300 		    "incomplete %s- %s\n", cmdp, msgp);
17301 	}
17302 }
17303 
17304 
17305 
17306 /*
17307  *    Function: sd_pkt_status_good
17308  *
17309  * Description: Processing for a STATUS_GOOD code in pkt_status.
17310  *
17311  *     Context: May be called under interrupt context
17312  */
17313 
17314 static void
17315 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17316 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17317 {
17318 	char	*cmdp;
17319 
17320 	ASSERT(un != NULL);
17321 	ASSERT(mutex_owned(SD_MUTEX(un)));
17322 	ASSERT(bp != NULL);
17323 	ASSERT(xp != NULL);
17324 	ASSERT(pktp != NULL);
17325 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
17326 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17327 	ASSERT(pktp->pkt_resid != 0);
17328 
17329 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17330 
17331 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17332 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17333 	case SCMD_READ:
17334 		cmdp = "read";
17335 		break;
17336 	case SCMD_WRITE:
17337 		cmdp = "write";
17338 		break;
17339 	default:
17340 		SD_UPDATE_B_RESID(bp, pktp);
17341 		sd_return_command(un, bp);
17342 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17343 		return;
17344 	}
17345 
17346 	/*
17347 	 * See if we can retry the read/write, preferrably immediately.
17348 	 * If retries are exhaused, then sd_retry_command() will update
17349 	 * the b_resid count.
17350 	 */
17351 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17352 	    cmdp, EIO, (clock_t)0, NULL);
17353 
17354 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17355 }
17356 
17357 
17358 
17359 
17360 
17361 /*
17362  *    Function: sd_handle_request_sense
17363  *
17364  * Description: Processing for non-auto Request Sense command.
17365  *
17366  *   Arguments: un - ptr to associated softstate
17367  *		sense_bp - ptr to buf(9S) for the RQS command
17368  *		sense_xp - ptr to the sd_xbuf for the RQS command
17369  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17370  *
17371  *     Context: May be called under interrupt context
17372  */
17373 
17374 static void
17375 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17376 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17377 {
17378 	struct buf	*cmd_bp;	/* buf for the original command */
17379 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
17380 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
17381 	size_t		actual_len;	/* actual sense data length */
17382 
17383 	ASSERT(un != NULL);
17384 	ASSERT(mutex_owned(SD_MUTEX(un)));
17385 	ASSERT(sense_bp != NULL);
17386 	ASSERT(sense_xp != NULL);
17387 	ASSERT(sense_pktp != NULL);
17388 
17389 	/*
17390 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
17391 	 * RQS command and not the original command.
17392 	 */
17393 	ASSERT(sense_pktp == un->un_rqs_pktp);
17394 	ASSERT(sense_bp   == un->un_rqs_bp);
17395 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17396 	    (FLAG_SENSING | FLAG_HEAD));
17397 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17398 	    FLAG_SENSING) == FLAG_SENSING);
17399 
17400 	/* These are the bp, xp, and pktp for the original command */
17401 	cmd_bp = sense_xp->xb_sense_bp;
17402 	cmd_xp = SD_GET_XBUF(cmd_bp);
17403 	cmd_pktp = SD_GET_PKTP(cmd_bp);
17404 
17405 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
17406 		/*
17407 		 * The REQUEST SENSE command failed.  Release the REQUEST
17408 		 * SENSE command for re-use, get back the bp for the original
17409 		 * command, and attempt to re-try the original command if
17410 		 * FLAG_DIAGNOSE is not set in the original packet.
17411 		 */
17412 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17413 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17414 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17415 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17416 			    NULL, NULL, EIO, (clock_t)0, NULL);
17417 			return;
17418 		}
17419 	}
17420 
17421 	/*
17422 	 * Save the relevant sense info into the xp for the original cmd.
17423 	 *
17424 	 * Note: if the request sense failed the state info will be zero
17425 	 * as set in sd_mark_rqs_busy()
17426 	 */
17427 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17428 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17429 	actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17430 	if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17431 	    (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17432 	    SENSE_LENGTH)) {
17433 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17434 		    MAX_SENSE_LENGTH);
17435 		cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17436 	} else {
17437 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17438 		    SENSE_LENGTH);
17439 		if (actual_len < SENSE_LENGTH) {
17440 			cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17441 		} else {
17442 			cmd_xp->xb_sense_resid = 0;
17443 		}
17444 	}
17445 
17446 	/*
17447 	 *  Free up the RQS command....
17448 	 *  NOTE:
17449 	 *	Must do this BEFORE calling sd_validate_sense_data!
17450 	 *	sd_validate_sense_data may return the original command in
17451 	 *	which case the pkt will be freed and the flags can no
17452 	 *	longer be touched.
17453 	 *	SD_MUTEX is held through this process until the command
17454 	 *	is dispatched based upon the sense data, so there are
17455 	 *	no race conditions.
17456 	 */
17457 	(void) sd_mark_rqs_idle(un, sense_xp);
17458 
17459 	/*
17460 	 * For a retryable command see if we have valid sense data, if so then
17461 	 * turn it over to sd_decode_sense() to figure out the right course of
17462 	 * action. Just fail a non-retryable command.
17463 	 */
17464 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17465 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17466 		    SD_SENSE_DATA_IS_VALID) {
17467 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17468 		}
17469 	} else {
17470 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17471 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17472 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17473 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17474 		sd_return_failed_command(un, cmd_bp, EIO);
17475 	}
17476 }
17477 
17478 
17479 
17480 
17481 /*
17482  *    Function: sd_handle_auto_request_sense
17483  *
17484  * Description: Processing for auto-request sense information.
17485  *
17486  *   Arguments: un - ptr to associated softstate
17487  *		bp - ptr to buf(9S) for the command
17488  *		xp - ptr to the sd_xbuf for the command
17489  *		pktp - ptr to the scsi_pkt(9S) for the command
17490  *
17491  *     Context: May be called under interrupt context
17492  */
17493 
17494 static void
17495 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17496 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17497 {
17498 	struct scsi_arq_status *asp;
17499 	size_t actual_len;
17500 
17501 	ASSERT(un != NULL);
17502 	ASSERT(mutex_owned(SD_MUTEX(un)));
17503 	ASSERT(bp != NULL);
17504 	ASSERT(xp != NULL);
17505 	ASSERT(pktp != NULL);
17506 	ASSERT(pktp != un->un_rqs_pktp);
17507 	ASSERT(bp   != un->un_rqs_bp);
17508 
17509 	/*
17510 	 * For auto-request sense, we get a scsi_arq_status back from
17511 	 * the HBA, with the sense data in the sts_sensedata member.
17512 	 * The pkt_scbp of the packet points to this scsi_arq_status.
17513 	 */
17514 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17515 
17516 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17517 		/*
17518 		 * The auto REQUEST SENSE failed; see if we can re-try
17519 		 * the original command.
17520 		 */
17521 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17522 		    "auto request sense failed (reason=%s)\n",
17523 		    scsi_rname(asp->sts_rqpkt_reason));
17524 
17525 		sd_reset_target(un, pktp);
17526 
17527 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17528 		    NULL, NULL, EIO, (clock_t)0, NULL);
17529 		return;
17530 	}
17531 
17532 	/* Save the relevant sense info into the xp for the original cmd. */
17533 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17534 	xp->xb_sense_state  = asp->sts_rqpkt_state;
17535 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17536 	if (xp->xb_sense_state & STATE_XARQ_DONE) {
17537 		actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17538 		bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17539 		    MAX_SENSE_LENGTH);
17540 	} else {
17541 		if (xp->xb_sense_resid > SENSE_LENGTH) {
17542 			actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17543 		} else {
17544 			actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17545 		}
17546 		if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17547 			if ((((struct uscsi_cmd *)
17548 			    (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17549 				xp->xb_sense_resid = (((struct uscsi_cmd *)
17550 				    (xp->xb_pktinfo))->uscsi_rqlen) -
17551 				    actual_len;
17552 			} else {
17553 				xp->xb_sense_resid = 0;
17554 			}
17555 		}
17556 		bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17557 	}
17558 
17559 	/*
17560 	 * See if we have valid sense data, if so then turn it over to
17561 	 * sd_decode_sense() to figure out the right course of action.
17562 	 */
17563 	if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17564 	    SD_SENSE_DATA_IS_VALID) {
17565 		sd_decode_sense(un, bp, xp, pktp);
17566 	}
17567 }
17568 
17569 
17570 /*
17571  *    Function: sd_print_sense_failed_msg
17572  *
17573  * Description: Print log message when RQS has failed.
17574  *
17575  *   Arguments: un - ptr to associated softstate
17576  *		bp - ptr to buf(9S) for the command
17577  *		arg - generic message string ptr
17578  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17579  *			or SD_NO_RETRY_ISSUED
17580  *
17581  *     Context: May be called from interrupt context
17582  */
17583 
17584 static void
17585 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17586 	int code)
17587 {
17588 	char	*msgp = arg;
17589 
17590 	ASSERT(un != NULL);
17591 	ASSERT(mutex_owned(SD_MUTEX(un)));
17592 	ASSERT(bp != NULL);
17593 
17594 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17595 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17596 	}
17597 }
17598 
17599 
17600 /*
17601  *    Function: sd_validate_sense_data
17602  *
17603  * Description: Check the given sense data for validity.
17604  *		If the sense data is not valid, the command will
17605  *		be either failed or retried!
17606  *
17607  * Return Code: SD_SENSE_DATA_IS_INVALID
17608  *		SD_SENSE_DATA_IS_VALID
17609  *
17610  *     Context: May be called from interrupt context
17611  */
17612 
17613 static int
17614 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17615 	size_t actual_len)
17616 {
17617 	struct scsi_extended_sense *esp;
17618 	struct	scsi_pkt *pktp;
17619 	char	*msgp = NULL;
17620 	sd_ssc_t *sscp;
17621 
17622 	ASSERT(un != NULL);
17623 	ASSERT(mutex_owned(SD_MUTEX(un)));
17624 	ASSERT(bp != NULL);
17625 	ASSERT(bp != un->un_rqs_bp);
17626 	ASSERT(xp != NULL);
17627 	ASSERT(un->un_fm_private != NULL);
17628 
17629 	pktp = SD_GET_PKTP(bp);
17630 	ASSERT(pktp != NULL);
17631 
17632 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17633 	ASSERT(sscp != NULL);
17634 
17635 	/*
17636 	 * Check the status of the RQS command (auto or manual).
17637 	 */
17638 	switch (xp->xb_sense_status & STATUS_MASK) {
17639 	case STATUS_GOOD:
17640 		break;
17641 
17642 	case STATUS_RESERVATION_CONFLICT:
17643 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17644 		return (SD_SENSE_DATA_IS_INVALID);
17645 
17646 	case STATUS_BUSY:
17647 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17648 		    "Busy Status on REQUEST SENSE\n");
17649 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17650 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17651 		return (SD_SENSE_DATA_IS_INVALID);
17652 
17653 	case STATUS_QFULL:
17654 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17655 		    "QFULL Status on REQUEST SENSE\n");
17656 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17657 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17658 		return (SD_SENSE_DATA_IS_INVALID);
17659 
17660 	case STATUS_CHECK:
17661 	case STATUS_TERMINATED:
17662 		msgp = "Check Condition on REQUEST SENSE\n";
17663 		goto sense_failed;
17664 
17665 	default:
17666 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17667 		goto sense_failed;
17668 	}
17669 
17670 	/*
17671 	 * See if we got the minimum required amount of sense data.
17672 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17673 	 * or less.
17674 	 */
17675 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17676 	    (actual_len == 0)) {
17677 		msgp = "Request Sense couldn't get sense data\n";
17678 		goto sense_failed;
17679 	}
17680 
17681 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
17682 		msgp = "Not enough sense information\n";
17683 		/* Mark the ssc_flags for detecting invalid sense data */
17684 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17685 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17686 			    "sense-data");
17687 		}
17688 		goto sense_failed;
17689 	}
17690 
17691 	/*
17692 	 * We require the extended sense data
17693 	 */
17694 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17695 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
17696 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17697 			static char tmp[8];
17698 			static char buf[148];
17699 			char *p = (char *)(xp->xb_sense_data);
17700 			int i;
17701 
17702 			mutex_enter(&sd_sense_mutex);
17703 			(void) strcpy(buf, "undecodable sense information:");
17704 			for (i = 0; i < actual_len; i++) {
17705 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17706 				(void) strcpy(&buf[strlen(buf)], tmp);
17707 			}
17708 			i = strlen(buf);
17709 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
17710 
17711 			if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17712 				scsi_log(SD_DEVINFO(un), sd_label,
17713 				    CE_WARN, buf);
17714 			}
17715 			mutex_exit(&sd_sense_mutex);
17716 		}
17717 
17718 		/* Mark the ssc_flags for detecting invalid sense data */
17719 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17720 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17721 			    "sense-data");
17722 		}
17723 
17724 		/* Note: Legacy behavior, fail the command with no retry */
17725 		sd_return_failed_command(un, bp, EIO);
17726 		return (SD_SENSE_DATA_IS_INVALID);
17727 	}
17728 
17729 	/*
17730 	 * Check that es_code is valid (es_class concatenated with es_code
17731 	 * make up the "response code" field.  es_class will always be 7, so
17732 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17733 	 * format.
17734 	 */
17735 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17736 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17737 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17738 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17739 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17740 		/* Mark the ssc_flags for detecting invalid sense data */
17741 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17742 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17743 			    "sense-data");
17744 		}
17745 		goto sense_failed;
17746 	}
17747 
17748 	return (SD_SENSE_DATA_IS_VALID);
17749 
17750 sense_failed:
17751 	/*
17752 	 * If the request sense failed (for whatever reason), attempt
17753 	 * to retry the original command.
17754 	 */
17755 #if defined(__i386) || defined(__amd64)
17756 	/*
17757 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17758 	 * sddef.h for Sparc platform, and x86 uses 1 binary
17759 	 * for both SCSI/FC.
17760 	 * The SD_RETRY_DELAY value need to be adjusted here
17761 	 * when SD_RETRY_DELAY change in sddef.h
17762 	 */
17763 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17764 	    sd_print_sense_failed_msg, msgp, EIO,
17765 	    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17766 #else
17767 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17768 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17769 #endif
17770 
17771 	return (SD_SENSE_DATA_IS_INVALID);
17772 }
17773 
17774 /*
17775  *    Function: sd_decode_sense
17776  *
17777  * Description: Take recovery action(s) when SCSI Sense Data is received.
17778  *
17779  *     Context: Interrupt context.
17780  */
17781 
17782 static void
17783 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17784 	struct scsi_pkt *pktp)
17785 {
17786 	uint8_t sense_key;
17787 
17788 	ASSERT(un != NULL);
17789 	ASSERT(mutex_owned(SD_MUTEX(un)));
17790 	ASSERT(bp != NULL);
17791 	ASSERT(bp != un->un_rqs_bp);
17792 	ASSERT(xp != NULL);
17793 	ASSERT(pktp != NULL);
17794 
17795 	sense_key = scsi_sense_key(xp->xb_sense_data);
17796 
17797 	switch (sense_key) {
17798 	case KEY_NO_SENSE:
17799 		sd_sense_key_no_sense(un, bp, xp, pktp);
17800 		break;
17801 	case KEY_RECOVERABLE_ERROR:
17802 		sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17803 		    bp, xp, pktp);
17804 		break;
17805 	case KEY_NOT_READY:
17806 		sd_sense_key_not_ready(un, xp->xb_sense_data,
17807 		    bp, xp, pktp);
17808 		break;
17809 	case KEY_MEDIUM_ERROR:
17810 	case KEY_HARDWARE_ERROR:
17811 		sd_sense_key_medium_or_hardware_error(un,
17812 		    xp->xb_sense_data, bp, xp, pktp);
17813 		break;
17814 	case KEY_ILLEGAL_REQUEST:
17815 		sd_sense_key_illegal_request(un, bp, xp, pktp);
17816 		break;
17817 	case KEY_UNIT_ATTENTION:
17818 		sd_sense_key_unit_attention(un, xp->xb_sense_data,
17819 		    bp, xp, pktp);
17820 		break;
17821 	case KEY_WRITE_PROTECT:
17822 	case KEY_VOLUME_OVERFLOW:
17823 	case KEY_MISCOMPARE:
17824 		sd_sense_key_fail_command(un, bp, xp, pktp);
17825 		break;
17826 	case KEY_BLANK_CHECK:
17827 		sd_sense_key_blank_check(un, bp, xp, pktp);
17828 		break;
17829 	case KEY_ABORTED_COMMAND:
17830 		sd_sense_key_aborted_command(un, bp, xp, pktp);
17831 		break;
17832 	case KEY_VENDOR_UNIQUE:
17833 	case KEY_COPY_ABORTED:
17834 	case KEY_EQUAL:
17835 	case KEY_RESERVED:
17836 	default:
17837 		sd_sense_key_default(un, xp->xb_sense_data,
17838 		    bp, xp, pktp);
17839 		break;
17840 	}
17841 }
17842 
17843 
17844 /*
17845  *    Function: sd_dump_memory
17846  *
17847  * Description: Debug logging routine to print the contents of a user provided
17848  *		buffer. The output of the buffer is broken up into 256 byte
17849  *		segments due to a size constraint of the scsi_log.
17850  *		implementation.
17851  *
17852  *   Arguments: un - ptr to softstate
17853  *		comp - component mask
17854  *		title - "title" string to preceed data when printed
17855  *		data - ptr to data block to be printed
17856  *		len - size of data block to be printed
17857  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17858  *
17859  *     Context: May be called from interrupt context
17860  */
17861 
17862 #define	SD_DUMP_MEMORY_BUF_SIZE	256
17863 
17864 static char *sd_dump_format_string[] = {
17865 		" 0x%02x",
17866 		" %c"
17867 };
17868 
17869 static void
17870 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17871     int len, int fmt)
17872 {
17873 	int	i, j;
17874 	int	avail_count;
17875 	int	start_offset;
17876 	int	end_offset;
17877 	size_t	entry_len;
17878 	char	*bufp;
17879 	char	*local_buf;
17880 	char	*format_string;
17881 
17882 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17883 
17884 	/*
17885 	 * In the debug version of the driver, this function is called from a
17886 	 * number of places which are NOPs in the release driver.
17887 	 * The debug driver therefore has additional methods of filtering
17888 	 * debug output.
17889 	 */
17890 #ifdef SDDEBUG
17891 	/*
17892 	 * In the debug version of the driver we can reduce the amount of debug
17893 	 * messages by setting sd_error_level to something other than
17894 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17895 	 * sd_component_mask.
17896 	 */
17897 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17898 	    (sd_error_level != SCSI_ERR_ALL)) {
17899 		return;
17900 	}
17901 	if (((sd_component_mask & comp) == 0) ||
17902 	    (sd_error_level != SCSI_ERR_ALL)) {
17903 		return;
17904 	}
17905 #else
17906 	if (sd_error_level != SCSI_ERR_ALL) {
17907 		return;
17908 	}
17909 #endif
17910 
17911 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17912 	bufp = local_buf;
17913 	/*
17914 	 * Available length is the length of local_buf[], minus the
17915 	 * length of the title string, minus one for the ":", minus
17916 	 * one for the newline, minus one for the NULL terminator.
17917 	 * This gives the #bytes available for holding the printed
17918 	 * values from the given data buffer.
17919 	 */
17920 	if (fmt == SD_LOG_HEX) {
17921 		format_string = sd_dump_format_string[0];
17922 	} else /* SD_LOG_CHAR */ {
17923 		format_string = sd_dump_format_string[1];
17924 	}
17925 	/*
17926 	 * Available count is the number of elements from the given
17927 	 * data buffer that we can fit into the available length.
17928 	 * This is based upon the size of the format string used.
17929 	 * Make one entry and find it's size.
17930 	 */
17931 	(void) sprintf(bufp, format_string, data[0]);
17932 	entry_len = strlen(bufp);
17933 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17934 
17935 	j = 0;
17936 	while (j < len) {
17937 		bufp = local_buf;
17938 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17939 		start_offset = j;
17940 
17941 		end_offset = start_offset + avail_count;
17942 
17943 		(void) sprintf(bufp, "%s:", title);
17944 		bufp += strlen(bufp);
17945 		for (i = start_offset; ((i < end_offset) && (j < len));
17946 		    i++, j++) {
17947 			(void) sprintf(bufp, format_string, data[i]);
17948 			bufp += entry_len;
17949 		}
17950 		(void) sprintf(bufp, "\n");
17951 
17952 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17953 	}
17954 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17955 }
17956 
17957 /*
17958  *    Function: sd_print_sense_msg
17959  *
17960  * Description: Log a message based upon the given sense data.
17961  *
17962  *   Arguments: un - ptr to associated softstate
17963  *		bp - ptr to buf(9S) for the command
17964  *		arg - ptr to associate sd_sense_info struct
17965  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17966  *			or SD_NO_RETRY_ISSUED
17967  *
17968  *     Context: May be called from interrupt context
17969  */
17970 
17971 static void
17972 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17973 {
17974 	struct sd_xbuf	*xp;
17975 	struct scsi_pkt	*pktp;
17976 	uint8_t *sensep;
17977 	daddr_t request_blkno;
17978 	diskaddr_t err_blkno;
17979 	int severity;
17980 	int pfa_flag;
17981 	extern struct scsi_key_strings scsi_cmds[];
17982 
17983 	ASSERT(un != NULL);
17984 	ASSERT(mutex_owned(SD_MUTEX(un)));
17985 	ASSERT(bp != NULL);
17986 	xp = SD_GET_XBUF(bp);
17987 	ASSERT(xp != NULL);
17988 	pktp = SD_GET_PKTP(bp);
17989 	ASSERT(pktp != NULL);
17990 	ASSERT(arg != NULL);
17991 
17992 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17993 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17994 
17995 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
17996 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17997 		severity = SCSI_ERR_RETRYABLE;
17998 	}
17999 
18000 	/* Use absolute block number for the request block number */
18001 	request_blkno = xp->xb_blkno;
18002 
18003 	/*
18004 	 * Now try to get the error block number from the sense data
18005 	 */
18006 	sensep = xp->xb_sense_data;
18007 
18008 	if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
18009 	    (uint64_t *)&err_blkno)) {
18010 		/*
18011 		 * We retrieved the error block number from the information
18012 		 * portion of the sense data.
18013 		 *
18014 		 * For USCSI commands we are better off using the error
18015 		 * block no. as the requested block no. (This is the best
18016 		 * we can estimate.)
18017 		 */
18018 		if ((SD_IS_BUFIO(xp) == FALSE) &&
18019 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
18020 			request_blkno = err_blkno;
18021 		}
18022 	} else {
18023 		/*
18024 		 * Without the es_valid bit set (for fixed format) or an
18025 		 * information descriptor (for descriptor format) we cannot
18026 		 * be certain of the error blkno, so just use the
18027 		 * request_blkno.
18028 		 */
18029 		err_blkno = (diskaddr_t)request_blkno;
18030 	}
18031 
18032 	/*
18033 	 * The following will log the buffer contents for the release driver
18034 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
18035 	 * level is set to verbose.
18036 	 */
18037 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
18038 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
18039 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
18040 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
18041 
18042 	if (pfa_flag == FALSE) {
18043 		/* This is normally only set for USCSI */
18044 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
18045 			return;
18046 		}
18047 
18048 		if ((SD_IS_BUFIO(xp) == TRUE) &&
18049 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
18050 		    (severity < sd_error_level))) {
18051 			return;
18052 		}
18053 	}
18054 	/*
18055 	 * Check for Sonoma Failover and keep a count of how many failed I/O's
18056 	 */
18057 	if ((SD_IS_LSI(un)) &&
18058 	    (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
18059 	    (scsi_sense_asc(sensep) == 0x94) &&
18060 	    (scsi_sense_ascq(sensep) == 0x01)) {
18061 		un->un_sonoma_failure_count++;
18062 		if (un->un_sonoma_failure_count > 1) {
18063 			return;
18064 		}
18065 	}
18066 
18067 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
18068 	    ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
18069 	    (pktp->pkt_resid == 0))) {
18070 		scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
18071 		    request_blkno, err_blkno, scsi_cmds,
18072 		    (struct scsi_extended_sense *)sensep,
18073 		    un->un_additional_codes, NULL);
18074 	}
18075 }
18076 
18077 /*
18078  *    Function: sd_sense_key_no_sense
18079  *
18080  * Description: Recovery action when sense data was not received.
18081  *
18082  *     Context: May be called from interrupt context
18083  */
18084 
18085 static void
18086 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
18087 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18088 {
18089 	struct sd_sense_info	si;
18090 
18091 	ASSERT(un != NULL);
18092 	ASSERT(mutex_owned(SD_MUTEX(un)));
18093 	ASSERT(bp != NULL);
18094 	ASSERT(xp != NULL);
18095 	ASSERT(pktp != NULL);
18096 
18097 	si.ssi_severity = SCSI_ERR_FATAL;
18098 	si.ssi_pfa_flag = FALSE;
18099 
18100 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
18101 
18102 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18103 	    &si, EIO, (clock_t)0, NULL);
18104 }
18105 
18106 
18107 /*
18108  *    Function: sd_sense_key_recoverable_error
18109  *
18110  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
18111  *
18112  *     Context: May be called from interrupt context
18113  */
18114 
18115 static void
18116 sd_sense_key_recoverable_error(struct sd_lun *un,
18117 	uint8_t *sense_datap,
18118 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18119 {
18120 	struct sd_sense_info	si;
18121 	uint8_t asc = scsi_sense_asc(sense_datap);
18122 	uint8_t ascq = scsi_sense_ascq(sense_datap);
18123 
18124 	ASSERT(un != NULL);
18125 	ASSERT(mutex_owned(SD_MUTEX(un)));
18126 	ASSERT(bp != NULL);
18127 	ASSERT(xp != NULL);
18128 	ASSERT(pktp != NULL);
18129 
18130 	/*
18131 	 * 0x00, 0x1D: ATA PASSTHROUGH INFORMATION AVAILABLE
18132 	 */
18133 	if (asc == 0x00 && ascq == 0x1D) {
18134 		sd_return_command(un, bp);
18135 		return;
18136 	}
18137 
18138 	/*
18139 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
18140 	 */
18141 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
18142 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18143 		si.ssi_severity = SCSI_ERR_INFO;
18144 		si.ssi_pfa_flag = TRUE;
18145 	} else {
18146 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
18147 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18148 		si.ssi_severity = SCSI_ERR_RECOVERED;
18149 		si.ssi_pfa_flag = FALSE;
18150 	}
18151 
18152 	if (pktp->pkt_resid == 0) {
18153 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18154 		sd_return_command(un, bp);
18155 		return;
18156 	}
18157 
18158 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18159 	    &si, EIO, (clock_t)0, NULL);
18160 }
18161 
18162 
18163 
18164 
18165 /*
18166  *    Function: sd_sense_key_not_ready
18167  *
18168  * Description: Recovery actions for a SCSI "Not Ready" sense key.
18169  *
18170  *     Context: May be called from interrupt context
18171  */
18172 
18173 static void
18174 sd_sense_key_not_ready(struct sd_lun *un,
18175 	uint8_t *sense_datap,
18176 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18177 {
18178 	struct sd_sense_info	si;
18179 	uint8_t asc = scsi_sense_asc(sense_datap);
18180 	uint8_t ascq = scsi_sense_ascq(sense_datap);
18181 
18182 	ASSERT(un != NULL);
18183 	ASSERT(mutex_owned(SD_MUTEX(un)));
18184 	ASSERT(bp != NULL);
18185 	ASSERT(xp != NULL);
18186 	ASSERT(pktp != NULL);
18187 
18188 	si.ssi_severity = SCSI_ERR_FATAL;
18189 	si.ssi_pfa_flag = FALSE;
18190 
18191 	/*
18192 	 * Update error stats after first NOT READY error. Disks may have
18193 	 * been powered down and may need to be restarted.  For CDROMs,
18194 	 * report NOT READY errors only if media is present.
18195 	 */
18196 	if ((ISCD(un) && (asc == 0x3A)) ||
18197 	    (xp->xb_nr_retry_count > 0)) {
18198 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18199 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18200 	}
18201 
18202 	/*
18203 	 * Just fail if the "not ready" retry limit has been reached.
18204 	 */
18205 	if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18206 		/* Special check for error message printing for removables. */
18207 		if (un->un_f_has_removable_media && (asc == 0x04) &&
18208 		    (ascq >= 0x04)) {
18209 			si.ssi_severity = SCSI_ERR_ALL;
18210 		}
18211 		goto fail_command;
18212 	}
18213 
18214 	/*
18215 	 * Check the ASC and ASCQ in the sense data as needed, to determine
18216 	 * what to do.
18217 	 */
18218 	switch (asc) {
18219 	case 0x04:	/* LOGICAL UNIT NOT READY */
18220 		/*
18221 		 * disk drives that don't spin up result in a very long delay
18222 		 * in format without warning messages. We will log a message
18223 		 * if the error level is set to verbose.
18224 		 */
18225 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18226 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18227 			    "logical unit not ready, resetting disk\n");
18228 		}
18229 
18230 		/*
18231 		 * There are different requirements for CDROMs and disks for
18232 		 * the number of retries.  If a CD-ROM is giving this, it is
18233 		 * probably reading TOC and is in the process of getting
18234 		 * ready, so we should keep on trying for a long time to make
18235 		 * sure that all types of media are taken in account (for
18236 		 * some media the drive takes a long time to read TOC).  For
18237 		 * disks we do not want to retry this too many times as this
18238 		 * can cause a long hang in format when the drive refuses to
18239 		 * spin up (a very common failure).
18240 		 */
18241 		switch (ascq) {
18242 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
18243 			/*
18244 			 * Disk drives frequently refuse to spin up which
18245 			 * results in a very long hang in format without
18246 			 * warning messages.
18247 			 *
18248 			 * Note: This code preserves the legacy behavior of
18249 			 * comparing xb_nr_retry_count against zero for fibre
18250 			 * channel targets instead of comparing against the
18251 			 * un_reset_retry_count value.  The reason for this
18252 			 * discrepancy has been so utterly lost beneath the
18253 			 * Sands of Time that even Indiana Jones could not
18254 			 * find it.
18255 			 */
18256 			if (un->un_f_is_fibre == TRUE) {
18257 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18258 				    (xp->xb_nr_retry_count > 0)) &&
18259 				    (un->un_startstop_timeid == NULL)) {
18260 					scsi_log(SD_DEVINFO(un), sd_label,
18261 					    CE_WARN, "logical unit not ready, "
18262 					    "resetting disk\n");
18263 					sd_reset_target(un, pktp);
18264 				}
18265 			} else {
18266 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18267 				    (xp->xb_nr_retry_count >
18268 				    un->un_reset_retry_count)) &&
18269 				    (un->un_startstop_timeid == NULL)) {
18270 					scsi_log(SD_DEVINFO(un), sd_label,
18271 					    CE_WARN, "logical unit not ready, "
18272 					    "resetting disk\n");
18273 					sd_reset_target(un, pktp);
18274 				}
18275 			}
18276 			break;
18277 
18278 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
18279 			/*
18280 			 * If the target is in the process of becoming
18281 			 * ready, just proceed with the retry. This can
18282 			 * happen with CD-ROMs that take a long time to
18283 			 * read TOC after a power cycle or reset.
18284 			 */
18285 			goto do_retry;
18286 
18287 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18288 			break;
18289 
18290 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18291 			/*
18292 			 * Retries cannot help here so just fail right away.
18293 			 */
18294 			goto fail_command;
18295 
18296 		case 0x88:
18297 			/*
18298 			 * Vendor-unique code for T3/T4: it indicates a
18299 			 * path problem in a mutipathed config, but as far as
18300 			 * the target driver is concerned it equates to a fatal
18301 			 * error, so we should just fail the command right away
18302 			 * (without printing anything to the console). If this
18303 			 * is not a T3/T4, fall thru to the default recovery
18304 			 * action.
18305 			 * T3/T4 is FC only, don't need to check is_fibre
18306 			 */
18307 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
18308 				sd_return_failed_command(un, bp, EIO);
18309 				return;
18310 			}
18311 			/* FALLTHRU */
18312 
18313 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
18314 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
18315 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
18316 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
18317 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
18318 		default:    /* Possible future codes in SCSI spec? */
18319 			/*
18320 			 * For removable-media devices, do not retry if
18321 			 * ASCQ > 2 as these result mostly from USCSI commands
18322 			 * on MMC devices issued to check status of an
18323 			 * operation initiated in immediate mode.  Also for
18324 			 * ASCQ >= 4 do not print console messages as these
18325 			 * mainly represent a user-initiated operation
18326 			 * instead of a system failure.
18327 			 */
18328 			if (un->un_f_has_removable_media) {
18329 				si.ssi_severity = SCSI_ERR_ALL;
18330 				goto fail_command;
18331 			}
18332 			break;
18333 		}
18334 
18335 		/*
18336 		 * As part of our recovery attempt for the NOT READY
18337 		 * condition, we issue a START STOP UNIT command. However
18338 		 * we want to wait for a short delay before attempting this
18339 		 * as there may still be more commands coming back from the
18340 		 * target with the check condition. To do this we use
18341 		 * timeout(9F) to call sd_start_stop_unit_callback() after
18342 		 * the delay interval expires. (sd_start_stop_unit_callback()
18343 		 * dispatches sd_start_stop_unit_task(), which will issue
18344 		 * the actual START STOP UNIT command. The delay interval
18345 		 * is one-half of the delay that we will use to retry the
18346 		 * command that generated the NOT READY condition.
18347 		 *
18348 		 * Note that we could just dispatch sd_start_stop_unit_task()
18349 		 * from here and allow it to sleep for the delay interval,
18350 		 * but then we would be tying up the taskq thread
18351 		 * uncesessarily for the duration of the delay.
18352 		 *
18353 		 * Do not issue the START STOP UNIT if the current command
18354 		 * is already a START STOP UNIT.
18355 		 */
18356 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18357 			break;
18358 		}
18359 
18360 		/*
18361 		 * Do not schedule the timeout if one is already pending.
18362 		 */
18363 		if (un->un_startstop_timeid != NULL) {
18364 			SD_INFO(SD_LOG_ERROR, un,
18365 			    "sd_sense_key_not_ready: restart already issued to"
18366 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18367 			    ddi_get_instance(SD_DEVINFO(un)));
18368 			break;
18369 		}
18370 
18371 		/*
18372 		 * Schedule the START STOP UNIT command, then queue the command
18373 		 * for a retry.
18374 		 *
18375 		 * Note: A timeout is not scheduled for this retry because we
18376 		 * want the retry to be serial with the START_STOP_UNIT. The
18377 		 * retry will be started when the START_STOP_UNIT is completed
18378 		 * in sd_start_stop_unit_task.
18379 		 */
18380 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18381 		    un, un->un_busy_timeout / 2);
18382 		xp->xb_nr_retry_count++;
18383 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18384 		return;
18385 
18386 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18387 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18388 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18389 			    "unit does not respond to selection\n");
18390 		}
18391 		break;
18392 
18393 	case 0x3A:	/* MEDIUM NOT PRESENT */
18394 		if (sd_error_level >= SCSI_ERR_FATAL) {
18395 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18396 			    "Caddy not inserted in drive\n");
18397 		}
18398 
18399 		sr_ejected(un);
18400 		un->un_mediastate = DKIO_EJECTED;
18401 		/* The state has changed, inform the media watch routines */
18402 		cv_broadcast(&un->un_state_cv);
18403 		/* Just fail if no media is present in the drive. */
18404 		goto fail_command;
18405 
18406 	default:
18407 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18408 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18409 			    "Unit not Ready. Additional sense code 0x%x\n",
18410 			    asc);
18411 		}
18412 		break;
18413 	}
18414 
18415 do_retry:
18416 
18417 	/*
18418 	 * Retry the command, as some targets may report NOT READY for
18419 	 * several seconds after being reset.
18420 	 */
18421 	xp->xb_nr_retry_count++;
18422 	si.ssi_severity = SCSI_ERR_RETRYABLE;
18423 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18424 	    &si, EIO, un->un_busy_timeout, NULL);
18425 
18426 	return;
18427 
18428 fail_command:
18429 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18430 	sd_return_failed_command(un, bp, EIO);
18431 }
18432 
18433 
18434 
18435 /*
18436  *    Function: sd_sense_key_medium_or_hardware_error
18437  *
18438  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18439  *		sense key.
18440  *
18441  *     Context: May be called from interrupt context
18442  */
18443 
18444 static void
18445 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
18446 	uint8_t *sense_datap,
18447 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18448 {
18449 	struct sd_sense_info	si;
18450 	uint8_t sense_key = scsi_sense_key(sense_datap);
18451 	uint8_t asc = scsi_sense_asc(sense_datap);
18452 
18453 	ASSERT(un != NULL);
18454 	ASSERT(mutex_owned(SD_MUTEX(un)));
18455 	ASSERT(bp != NULL);
18456 	ASSERT(xp != NULL);
18457 	ASSERT(pktp != NULL);
18458 
18459 	si.ssi_severity = SCSI_ERR_FATAL;
18460 	si.ssi_pfa_flag = FALSE;
18461 
18462 	if (sense_key == KEY_MEDIUM_ERROR) {
18463 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18464 	}
18465 
18466 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18467 
18468 	if ((un->un_reset_retry_count != 0) &&
18469 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
18470 		mutex_exit(SD_MUTEX(un));
18471 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18472 		if (un->un_f_allow_bus_device_reset == TRUE) {
18473 
18474 			boolean_t try_resetting_target = B_TRUE;
18475 
18476 			/*
18477 			 * We need to be able to handle specific ASC when we are
18478 			 * handling a KEY_HARDWARE_ERROR. In particular
18479 			 * taking the default action of resetting the target may
18480 			 * not be the appropriate way to attempt recovery.
18481 			 * Resetting a target because of a single LUN failure
18482 			 * victimizes all LUNs on that target.
18483 			 *
18484 			 * This is true for the LSI arrays, if an LSI
18485 			 * array controller returns an ASC of 0x84 (LUN Dead) we
18486 			 * should trust it.
18487 			 */
18488 
18489 			if (sense_key == KEY_HARDWARE_ERROR) {
18490 				switch (asc) {
18491 				case 0x84:
18492 					if (SD_IS_LSI(un)) {
18493 						try_resetting_target = B_FALSE;
18494 					}
18495 					break;
18496 				default:
18497 					break;
18498 				}
18499 			}
18500 
18501 			if (try_resetting_target == B_TRUE) {
18502 				int reset_retval = 0;
18503 				if (un->un_f_lun_reset_enabled == TRUE) {
18504 					SD_TRACE(SD_LOG_IO_CORE, un,
18505 					    "sd_sense_key_medium_or_hardware_"
18506 					    "error: issuing RESET_LUN\n");
18507 					reset_retval =
18508 					    scsi_reset(SD_ADDRESS(un),
18509 					    RESET_LUN);
18510 				}
18511 				if (reset_retval == 0) {
18512 					SD_TRACE(SD_LOG_IO_CORE, un,
18513 					    "sd_sense_key_medium_or_hardware_"
18514 					    "error: issuing RESET_TARGET\n");
18515 					(void) scsi_reset(SD_ADDRESS(un),
18516 					    RESET_TARGET);
18517 				}
18518 			}
18519 		}
18520 		mutex_enter(SD_MUTEX(un));
18521 	}
18522 
18523 	/*
18524 	 * This really ought to be a fatal error, but we will retry anyway
18525 	 * as some drives report this as a spurious error.
18526 	 */
18527 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18528 	    &si, EIO, (clock_t)0, NULL);
18529 }
18530 
18531 
18532 
18533 /*
18534  *    Function: sd_sense_key_illegal_request
18535  *
18536  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18537  *
18538  *     Context: May be called from interrupt context
18539  */
18540 
18541 static void
18542 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18543 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18544 {
18545 	struct sd_sense_info	si;
18546 
18547 	ASSERT(un != NULL);
18548 	ASSERT(mutex_owned(SD_MUTEX(un)));
18549 	ASSERT(bp != NULL);
18550 	ASSERT(xp != NULL);
18551 	ASSERT(pktp != NULL);
18552 
18553 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18554 
18555 	si.ssi_severity = SCSI_ERR_INFO;
18556 	si.ssi_pfa_flag = FALSE;
18557 
18558 	/* Pointless to retry if the target thinks it's an illegal request */
18559 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18560 	sd_return_failed_command(un, bp, EIO);
18561 }
18562 
18563 
18564 
18565 
18566 /*
18567  *    Function: sd_sense_key_unit_attention
18568  *
18569  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18570  *
18571  *     Context: May be called from interrupt context
18572  */
18573 
18574 static void
18575 sd_sense_key_unit_attention(struct sd_lun *un,
18576 	uint8_t *sense_datap,
18577 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18578 {
18579 	/*
18580 	 * For UNIT ATTENTION we allow retries for one minute. Devices
18581 	 * like Sonoma can return UNIT ATTENTION close to a minute
18582 	 * under certain conditions.
18583 	 */
18584 	int	retry_check_flag = SD_RETRIES_UA;
18585 	boolean_t	kstat_updated = B_FALSE;
18586 	struct	sd_sense_info		si;
18587 	uint8_t asc = scsi_sense_asc(sense_datap);
18588 	uint8_t	ascq = scsi_sense_ascq(sense_datap);
18589 
18590 	ASSERT(un != NULL);
18591 	ASSERT(mutex_owned(SD_MUTEX(un)));
18592 	ASSERT(bp != NULL);
18593 	ASSERT(xp != NULL);
18594 	ASSERT(pktp != NULL);
18595 
18596 	si.ssi_severity = SCSI_ERR_INFO;
18597 	si.ssi_pfa_flag = FALSE;
18598 
18599 
18600 	switch (asc) {
18601 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18602 		if (sd_report_pfa != 0) {
18603 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18604 			si.ssi_pfa_flag = TRUE;
18605 			retry_check_flag = SD_RETRIES_STANDARD;
18606 			goto do_retry;
18607 		}
18608 
18609 		break;
18610 
18611 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18612 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18613 			un->un_resvd_status |=
18614 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
18615 		}
18616 #ifdef _LP64
18617 		if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18618 			if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18619 			    un, KM_NOSLEEP) == 0) {
18620 				/*
18621 				 * If we can't dispatch the task we'll just
18622 				 * live without descriptor sense.  We can
18623 				 * try again on the next "unit attention"
18624 				 */
18625 				SD_ERROR(SD_LOG_ERROR, un,
18626 				    "sd_sense_key_unit_attention: "
18627 				    "Could not dispatch "
18628 				    "sd_reenable_dsense_task\n");
18629 			}
18630 		}
18631 #endif /* _LP64 */
18632 		/* FALLTHRU */
18633 
18634 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18635 		if (!un->un_f_has_removable_media) {
18636 			break;
18637 		}
18638 
18639 		/*
18640 		 * When we get a unit attention from a removable-media device,
18641 		 * it may be in a state that will take a long time to recover
18642 		 * (e.g., from a reset).  Since we are executing in interrupt
18643 		 * context here, we cannot wait around for the device to come
18644 		 * back. So hand this command off to sd_media_change_task()
18645 		 * for deferred processing under taskq thread context. (Note
18646 		 * that the command still may be failed if a problem is
18647 		 * encountered at a later time.)
18648 		 */
18649 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18650 		    KM_NOSLEEP) == 0) {
18651 			/*
18652 			 * Cannot dispatch the request so fail the command.
18653 			 */
18654 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
18655 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18656 			si.ssi_severity = SCSI_ERR_FATAL;
18657 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18658 			sd_return_failed_command(un, bp, EIO);
18659 		}
18660 
18661 		/*
18662 		 * If failed to dispatch sd_media_change_task(), we already
18663 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
18664 		 * we should update kstat later if it encounters an error. So,
18665 		 * we update kstat_updated flag here.
18666 		 */
18667 		kstat_updated = B_TRUE;
18668 
18669 		/*
18670 		 * Either the command has been successfully dispatched to a
18671 		 * task Q for retrying, or the dispatch failed. In either case
18672 		 * do NOT retry again by calling sd_retry_command. This sets up
18673 		 * two retries of the same command and when one completes and
18674 		 * frees the resources the other will access freed memory,
18675 		 * a bad thing.
18676 		 */
18677 		return;
18678 
18679 	default:
18680 		break;
18681 	}
18682 
18683 	/*
18684 	 * ASC  ASCQ
18685 	 *  2A   09	Capacity data has changed
18686 	 *  2A   01	Mode parameters changed
18687 	 *  3F   0E	Reported luns data has changed
18688 	 * Arrays that support logical unit expansion should report
18689 	 * capacity changes(2Ah/09). Mode parameters changed and
18690 	 * reported luns data has changed are the approximation.
18691 	 */
18692 	if (((asc == 0x2a) && (ascq == 0x09)) ||
18693 	    ((asc == 0x2a) && (ascq == 0x01)) ||
18694 	    ((asc == 0x3f) && (ascq == 0x0e))) {
18695 		if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18696 		    KM_NOSLEEP) == 0) {
18697 			SD_ERROR(SD_LOG_ERROR, un,
18698 			    "sd_sense_key_unit_attention: "
18699 			    "Could not dispatch sd_target_change_task\n");
18700 		}
18701 	}
18702 
18703 	/*
18704 	 * Update kstat if we haven't done that.
18705 	 */
18706 	if (!kstat_updated) {
18707 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18708 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18709 	}
18710 
18711 do_retry:
18712 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18713 	    EIO, SD_UA_RETRY_DELAY, NULL);
18714 }
18715 
18716 
18717 
18718 /*
18719  *    Function: sd_sense_key_fail_command
18720  *
18721  * Description: Use to fail a command when we don't like the sense key that
18722  *		was returned.
18723  *
18724  *     Context: May be called from interrupt context
18725  */
18726 
18727 static void
18728 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
18729 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18730 {
18731 	struct sd_sense_info	si;
18732 
18733 	ASSERT(un != NULL);
18734 	ASSERT(mutex_owned(SD_MUTEX(un)));
18735 	ASSERT(bp != NULL);
18736 	ASSERT(xp != NULL);
18737 	ASSERT(pktp != NULL);
18738 
18739 	si.ssi_severity = SCSI_ERR_FATAL;
18740 	si.ssi_pfa_flag = FALSE;
18741 
18742 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18743 	sd_return_failed_command(un, bp, EIO);
18744 }
18745 
18746 
18747 
18748 /*
18749  *    Function: sd_sense_key_blank_check
18750  *
18751  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18752  *		Has no monetary connotation.
18753  *
18754  *     Context: May be called from interrupt context
18755  */
18756 
18757 static void
18758 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
18759 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18760 {
18761 	struct sd_sense_info	si;
18762 
18763 	ASSERT(un != NULL);
18764 	ASSERT(mutex_owned(SD_MUTEX(un)));
18765 	ASSERT(bp != NULL);
18766 	ASSERT(xp != NULL);
18767 	ASSERT(pktp != NULL);
18768 
18769 	/*
18770 	 * Blank check is not fatal for removable devices, therefore
18771 	 * it does not require a console message.
18772 	 */
18773 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18774 	    SCSI_ERR_FATAL;
18775 	si.ssi_pfa_flag = FALSE;
18776 
18777 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18778 	sd_return_failed_command(un, bp, EIO);
18779 }
18780 
18781 
18782 
18783 
18784 /*
18785  *    Function: sd_sense_key_aborted_command
18786  *
18787  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18788  *
18789  *     Context: May be called from interrupt context
18790  */
18791 
18792 static void
18793 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18794 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18795 {
18796 	struct sd_sense_info	si;
18797 
18798 	ASSERT(un != NULL);
18799 	ASSERT(mutex_owned(SD_MUTEX(un)));
18800 	ASSERT(bp != NULL);
18801 	ASSERT(xp != NULL);
18802 	ASSERT(pktp != NULL);
18803 
18804 	si.ssi_severity = SCSI_ERR_FATAL;
18805 	si.ssi_pfa_flag = FALSE;
18806 
18807 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18808 
18809 	/*
18810 	 * This really ought to be a fatal error, but we will retry anyway
18811 	 * as some drives report this as a spurious error.
18812 	 */
18813 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18814 	    &si, EIO, drv_usectohz(100000), NULL);
18815 }
18816 
18817 
18818 
18819 /*
18820  *    Function: sd_sense_key_default
18821  *
18822  * Description: Default recovery action for several SCSI sense keys (basically
18823  *		attempts a retry).
18824  *
18825  *     Context: May be called from interrupt context
18826  */
18827 
18828 static void
18829 sd_sense_key_default(struct sd_lun *un,
18830 	uint8_t *sense_datap,
18831 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18832 {
18833 	struct sd_sense_info	si;
18834 	uint8_t sense_key = scsi_sense_key(sense_datap);
18835 
18836 	ASSERT(un != NULL);
18837 	ASSERT(mutex_owned(SD_MUTEX(un)));
18838 	ASSERT(bp != NULL);
18839 	ASSERT(xp != NULL);
18840 	ASSERT(pktp != NULL);
18841 
18842 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18843 
18844 	/*
18845 	 * Undecoded sense key.	Attempt retries and hope that will fix
18846 	 * the problem.  Otherwise, we're dead.
18847 	 */
18848 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18849 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18850 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18851 	}
18852 
18853 	si.ssi_severity = SCSI_ERR_FATAL;
18854 	si.ssi_pfa_flag = FALSE;
18855 
18856 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18857 	    &si, EIO, (clock_t)0, NULL);
18858 }
18859 
18860 
18861 
18862 /*
18863  *    Function: sd_print_retry_msg
18864  *
18865  * Description: Print a message indicating the retry action being taken.
18866  *
18867  *   Arguments: un - ptr to associated softstate
18868  *		bp - ptr to buf(9S) for the command
18869  *		arg - not used.
18870  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18871  *			or SD_NO_RETRY_ISSUED
18872  *
18873  *     Context: May be called from interrupt context
18874  */
18875 /* ARGSUSED */
18876 static void
18877 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18878 {
18879 	struct sd_xbuf	*xp;
18880 	struct scsi_pkt *pktp;
18881 	char *reasonp;
18882 	char *msgp;
18883 
18884 	ASSERT(un != NULL);
18885 	ASSERT(mutex_owned(SD_MUTEX(un)));
18886 	ASSERT(bp != NULL);
18887 	pktp = SD_GET_PKTP(bp);
18888 	ASSERT(pktp != NULL);
18889 	xp = SD_GET_XBUF(bp);
18890 	ASSERT(xp != NULL);
18891 
18892 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18893 	mutex_enter(&un->un_pm_mutex);
18894 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18895 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18896 	    (pktp->pkt_flags & FLAG_SILENT)) {
18897 		mutex_exit(&un->un_pm_mutex);
18898 		goto update_pkt_reason;
18899 	}
18900 	mutex_exit(&un->un_pm_mutex);
18901 
18902 	/*
18903 	 * Suppress messages if they are all the same pkt_reason; with
18904 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18905 	 * If we are in panic, then suppress the retry messages.
18906 	 */
18907 	switch (flag) {
18908 	case SD_NO_RETRY_ISSUED:
18909 		msgp = "giving up";
18910 		break;
18911 	case SD_IMMEDIATE_RETRY_ISSUED:
18912 	case SD_DELAYED_RETRY_ISSUED:
18913 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18914 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18915 		    (sd_error_level != SCSI_ERR_ALL))) {
18916 			return;
18917 		}
18918 		msgp = "retrying command";
18919 		break;
18920 	default:
18921 		goto update_pkt_reason;
18922 	}
18923 
18924 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18925 	    scsi_rname(pktp->pkt_reason));
18926 
18927 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18928 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18929 		    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18930 	}
18931 
18932 update_pkt_reason:
18933 	/*
18934 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18935 	 * This is to prevent multiple console messages for the same failure
18936 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18937 	 * when the command is retried successfully because there still may be
18938 	 * more commands coming back with the same value of pktp->pkt_reason.
18939 	 */
18940 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18941 		un->un_last_pkt_reason = pktp->pkt_reason;
18942 	}
18943 }
18944 
18945 
18946 /*
18947  *    Function: sd_print_cmd_incomplete_msg
18948  *
18949  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18950  *
18951  *   Arguments: un - ptr to associated softstate
18952  *		bp - ptr to buf(9S) for the command
18953  *		arg - passed to sd_print_retry_msg()
18954  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18955  *			or SD_NO_RETRY_ISSUED
18956  *
18957  *     Context: May be called from interrupt context
18958  */
18959 
18960 static void
18961 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18962 	int code)
18963 {
18964 	dev_info_t	*dip;
18965 
18966 	ASSERT(un != NULL);
18967 	ASSERT(mutex_owned(SD_MUTEX(un)));
18968 	ASSERT(bp != NULL);
18969 
18970 	switch (code) {
18971 	case SD_NO_RETRY_ISSUED:
18972 		/* Command was failed. Someone turned off this target? */
18973 		if (un->un_state != SD_STATE_OFFLINE) {
18974 			/*
18975 			 * Suppress message if we are detaching and
18976 			 * device has been disconnected
18977 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18978 			 * private interface and not part of the DDI
18979 			 */
18980 			dip = un->un_sd->sd_dev;
18981 			if (!(DEVI_IS_DETACHING(dip) &&
18982 			    DEVI_IS_DEVICE_REMOVED(dip))) {
18983 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18984 				"disk not responding to selection\n");
18985 			}
18986 			New_state(un, SD_STATE_OFFLINE);
18987 		}
18988 		break;
18989 
18990 	case SD_DELAYED_RETRY_ISSUED:
18991 	case SD_IMMEDIATE_RETRY_ISSUED:
18992 	default:
18993 		/* Command was successfully queued for retry */
18994 		sd_print_retry_msg(un, bp, arg, code);
18995 		break;
18996 	}
18997 }
18998 
18999 
19000 /*
19001  *    Function: sd_pkt_reason_cmd_incomplete
19002  *
19003  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
19004  *
19005  *     Context: May be called from interrupt context
19006  */
19007 
19008 static void
19009 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
19010 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19011 {
19012 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
19013 
19014 	ASSERT(un != NULL);
19015 	ASSERT(mutex_owned(SD_MUTEX(un)));
19016 	ASSERT(bp != NULL);
19017 	ASSERT(xp != NULL);
19018 	ASSERT(pktp != NULL);
19019 
19020 	/* Do not do a reset if selection did not complete */
19021 	/* Note: Should this not just check the bit? */
19022 	if (pktp->pkt_state != STATE_GOT_BUS) {
19023 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
19024 		sd_reset_target(un, pktp);
19025 	}
19026 
19027 	/*
19028 	 * If the target was not successfully selected, then set
19029 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
19030 	 * with the target, and further retries and/or commands are
19031 	 * likely to take a long time.
19032 	 */
19033 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
19034 		flag |= SD_RETRIES_FAILFAST;
19035 	}
19036 
19037 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19038 
19039 	sd_retry_command(un, bp, flag,
19040 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19041 }
19042 
19043 
19044 
19045 /*
19046  *    Function: sd_pkt_reason_cmd_tran_err
19047  *
19048  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
19049  *
19050  *     Context: May be called from interrupt context
19051  */
19052 
19053 static void
19054 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
19055 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19056 {
19057 	ASSERT(un != NULL);
19058 	ASSERT(mutex_owned(SD_MUTEX(un)));
19059 	ASSERT(bp != NULL);
19060 	ASSERT(xp != NULL);
19061 	ASSERT(pktp != NULL);
19062 
19063 	/*
19064 	 * Do not reset if we got a parity error, or if
19065 	 * selection did not complete.
19066 	 */
19067 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19068 	/* Note: Should this not just check the bit for pkt_state? */
19069 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
19070 	    (pktp->pkt_state != STATE_GOT_BUS)) {
19071 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
19072 		sd_reset_target(un, pktp);
19073 	}
19074 
19075 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19076 
19077 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19078 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19079 }
19080 
19081 
19082 
19083 /*
19084  *    Function: sd_pkt_reason_cmd_reset
19085  *
19086  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
19087  *
19088  *     Context: May be called from interrupt context
19089  */
19090 
19091 static void
19092 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
19093 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19094 {
19095 	ASSERT(un != NULL);
19096 	ASSERT(mutex_owned(SD_MUTEX(un)));
19097 	ASSERT(bp != NULL);
19098 	ASSERT(xp != NULL);
19099 	ASSERT(pktp != NULL);
19100 
19101 	/* The target may still be running the command, so try to reset. */
19102 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19103 	sd_reset_target(un, pktp);
19104 
19105 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19106 
19107 	/*
19108 	 * If pkt_reason is CMD_RESET chances are that this pkt got
19109 	 * reset because another target on this bus caused it. The target
19110 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19111 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19112 	 */
19113 
19114 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19115 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19116 }
19117 
19118 
19119 
19120 
19121 /*
19122  *    Function: sd_pkt_reason_cmd_aborted
19123  *
19124  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
19125  *
19126  *     Context: May be called from interrupt context
19127  */
19128 
19129 static void
19130 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
19131 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19132 {
19133 	ASSERT(un != NULL);
19134 	ASSERT(mutex_owned(SD_MUTEX(un)));
19135 	ASSERT(bp != NULL);
19136 	ASSERT(xp != NULL);
19137 	ASSERT(pktp != NULL);
19138 
19139 	/* The target may still be running the command, so try to reset. */
19140 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19141 	sd_reset_target(un, pktp);
19142 
19143 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19144 
19145 	/*
19146 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
19147 	 * aborted because another target on this bus caused it. The target
19148 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19149 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19150 	 */
19151 
19152 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19153 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19154 }
19155 
19156 
19157 
19158 /*
19159  *    Function: sd_pkt_reason_cmd_timeout
19160  *
19161  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19162  *
19163  *     Context: May be called from interrupt context
19164  */
19165 
19166 static void
19167 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
19168 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19169 {
19170 	ASSERT(un != NULL);
19171 	ASSERT(mutex_owned(SD_MUTEX(un)));
19172 	ASSERT(bp != NULL);
19173 	ASSERT(xp != NULL);
19174 	ASSERT(pktp != NULL);
19175 
19176 
19177 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19178 	sd_reset_target(un, pktp);
19179 
19180 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19181 
19182 	/*
19183 	 * A command timeout indicates that we could not establish
19184 	 * communication with the target, so set SD_RETRIES_FAILFAST
19185 	 * as further retries/commands are likely to take a long time.
19186 	 */
19187 	sd_retry_command(un, bp,
19188 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19189 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19190 }
19191 
19192 
19193 
19194 /*
19195  *    Function: sd_pkt_reason_cmd_unx_bus_free
19196  *
19197  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19198  *
19199  *     Context: May be called from interrupt context
19200  */
19201 
19202 static void
19203 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19204 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19205 {
19206 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19207 
19208 	ASSERT(un != NULL);
19209 	ASSERT(mutex_owned(SD_MUTEX(un)));
19210 	ASSERT(bp != NULL);
19211 	ASSERT(xp != NULL);
19212 	ASSERT(pktp != NULL);
19213 
19214 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19215 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19216 
19217 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19218 	    sd_print_retry_msg : NULL;
19219 
19220 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19221 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19222 }
19223 
19224 
19225 /*
19226  *    Function: sd_pkt_reason_cmd_tag_reject
19227  *
19228  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19229  *
19230  *     Context: May be called from interrupt context
19231  */
19232 
19233 static void
19234 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19235 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19236 {
19237 	ASSERT(un != NULL);
19238 	ASSERT(mutex_owned(SD_MUTEX(un)));
19239 	ASSERT(bp != NULL);
19240 	ASSERT(xp != NULL);
19241 	ASSERT(pktp != NULL);
19242 
19243 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19244 	pktp->pkt_flags = 0;
19245 	un->un_tagflags = 0;
19246 	if (un->un_f_opt_queueing == TRUE) {
19247 		un->un_throttle = min(un->un_throttle, 3);
19248 	} else {
19249 		un->un_throttle = 1;
19250 	}
19251 	mutex_exit(SD_MUTEX(un));
19252 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19253 	mutex_enter(SD_MUTEX(un));
19254 
19255 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19256 
19257 	/* Legacy behavior not to check retry counts here. */
19258 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19259 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19260 }
19261 
19262 
19263 /*
19264  *    Function: sd_pkt_reason_default
19265  *
19266  * Description: Default recovery actions for SCSA pkt_reason values that
19267  *		do not have more explicit recovery actions.
19268  *
19269  *     Context: May be called from interrupt context
19270  */
19271 
19272 static void
19273 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
19274 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19275 {
19276 	ASSERT(un != NULL);
19277 	ASSERT(mutex_owned(SD_MUTEX(un)));
19278 	ASSERT(bp != NULL);
19279 	ASSERT(xp != NULL);
19280 	ASSERT(pktp != NULL);
19281 
19282 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19283 	sd_reset_target(un, pktp);
19284 
19285 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19286 
19287 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19288 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19289 }
19290 
19291 
19292 
19293 /*
19294  *    Function: sd_pkt_status_check_condition
19295  *
19296  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19297  *
19298  *     Context: May be called from interrupt context
19299  */
19300 
19301 static void
19302 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19303 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19304 {
19305 	ASSERT(un != NULL);
19306 	ASSERT(mutex_owned(SD_MUTEX(un)));
19307 	ASSERT(bp != NULL);
19308 	ASSERT(xp != NULL);
19309 	ASSERT(pktp != NULL);
19310 
19311 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19312 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
19313 
19314 	/*
19315 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19316 	 * command will be retried after the request sense). Otherwise, retry
19317 	 * the command. Note: we are issuing the request sense even though the
19318 	 * retry limit may have been reached for the failed command.
19319 	 */
19320 	if (un->un_f_arq_enabled == FALSE) {
19321 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19322 		    "no ARQ, sending request sense command\n");
19323 		sd_send_request_sense_command(un, bp, pktp);
19324 	} else {
19325 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19326 		    "ARQ,retrying request sense command\n");
19327 #if defined(__i386) || defined(__amd64)
19328 		/*
19329 		 * The SD_RETRY_DELAY value need to be adjusted here
19330 		 * when SD_RETRY_DELAY change in sddef.h
19331 		 */
19332 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19333 		    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19334 		    NULL);
19335 #else
19336 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19337 		    EIO, SD_RETRY_DELAY, NULL);
19338 #endif
19339 	}
19340 
19341 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19342 }
19343 
19344 
19345 /*
19346  *    Function: sd_pkt_status_busy
19347  *
19348  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19349  *
19350  *     Context: May be called from interrupt context
19351  */
19352 
19353 static void
19354 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19355 	struct scsi_pkt *pktp)
19356 {
19357 	ASSERT(un != NULL);
19358 	ASSERT(mutex_owned(SD_MUTEX(un)));
19359 	ASSERT(bp != NULL);
19360 	ASSERT(xp != NULL);
19361 	ASSERT(pktp != NULL);
19362 
19363 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19364 	    "sd_pkt_status_busy: entry\n");
19365 
19366 	/* If retries are exhausted, just fail the command. */
19367 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
19368 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19369 		    "device busy too long\n");
19370 		sd_return_failed_command(un, bp, EIO);
19371 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19372 		    "sd_pkt_status_busy: exit\n");
19373 		return;
19374 	}
19375 	xp->xb_retry_count++;
19376 
19377 	/*
19378 	 * Try to reset the target. However, we do not want to perform
19379 	 * more than one reset if the device continues to fail. The reset
19380 	 * will be performed when the retry count reaches the reset
19381 	 * threshold.  This threshold should be set such that at least
19382 	 * one retry is issued before the reset is performed.
19383 	 */
19384 	if (xp->xb_retry_count ==
19385 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19386 		int rval = 0;
19387 		mutex_exit(SD_MUTEX(un));
19388 		if (un->un_f_allow_bus_device_reset == TRUE) {
19389 			/*
19390 			 * First try to reset the LUN; if we cannot then
19391 			 * try to reset the target.
19392 			 */
19393 			if (un->un_f_lun_reset_enabled == TRUE) {
19394 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19395 				    "sd_pkt_status_busy: RESET_LUN\n");
19396 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19397 			}
19398 			if (rval == 0) {
19399 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19400 				    "sd_pkt_status_busy: RESET_TARGET\n");
19401 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19402 			}
19403 		}
19404 		if (rval == 0) {
19405 			/*
19406 			 * If the RESET_LUN and/or RESET_TARGET failed,
19407 			 * try RESET_ALL
19408 			 */
19409 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19410 			    "sd_pkt_status_busy: RESET_ALL\n");
19411 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19412 		}
19413 		mutex_enter(SD_MUTEX(un));
19414 		if (rval == 0) {
19415 			/*
19416 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19417 			 * At this point we give up & fail the command.
19418 			 */
19419 			sd_return_failed_command(un, bp, EIO);
19420 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19421 			    "sd_pkt_status_busy: exit (failed cmd)\n");
19422 			return;
19423 		}
19424 	}
19425 
19426 	/*
19427 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19428 	 * we have already checked the retry counts above.
19429 	 */
19430 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19431 	    EIO, un->un_busy_timeout, NULL);
19432 
19433 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19434 	    "sd_pkt_status_busy: exit\n");
19435 }
19436 
19437 
19438 /*
19439  *    Function: sd_pkt_status_reservation_conflict
19440  *
19441  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19442  *		command status.
19443  *
19444  *     Context: May be called from interrupt context
19445  */
19446 
19447 static void
19448 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19449 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19450 {
19451 	ASSERT(un != NULL);
19452 	ASSERT(mutex_owned(SD_MUTEX(un)));
19453 	ASSERT(bp != NULL);
19454 	ASSERT(xp != NULL);
19455 	ASSERT(pktp != NULL);
19456 
19457 	/*
19458 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19459 	 * conflict could be due to various reasons like incorrect keys, not
19460 	 * registered or not reserved etc. So, we return EACCES to the caller.
19461 	 */
19462 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19463 		int cmd = SD_GET_PKT_OPCODE(pktp);
19464 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19465 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19466 			sd_return_failed_command(un, bp, EACCES);
19467 			return;
19468 		}
19469 	}
19470 
19471 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19472 
19473 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19474 		if (sd_failfast_enable != 0) {
19475 			/* By definition, we must panic here.... */
19476 			sd_panic_for_res_conflict(un);
19477 			/*NOTREACHED*/
19478 		}
19479 		SD_ERROR(SD_LOG_IO, un,
19480 		    "sd_handle_resv_conflict: Disk Reserved\n");
19481 		sd_return_failed_command(un, bp, EACCES);
19482 		return;
19483 	}
19484 
19485 	/*
19486 	 * 1147670: retry only if sd_retry_on_reservation_conflict
19487 	 * property is set (default is 1). Retries will not succeed
19488 	 * on a disk reserved by another initiator. HA systems
19489 	 * may reset this via sd.conf to avoid these retries.
19490 	 *
19491 	 * Note: The legacy return code for this failure is EIO, however EACCES
19492 	 * seems more appropriate for a reservation conflict.
19493 	 */
19494 	if (sd_retry_on_reservation_conflict == 0) {
19495 		SD_ERROR(SD_LOG_IO, un,
19496 		    "sd_handle_resv_conflict: Device Reserved\n");
19497 		sd_return_failed_command(un, bp, EIO);
19498 		return;
19499 	}
19500 
19501 	/*
19502 	 * Retry the command if we can.
19503 	 *
19504 	 * Note: The legacy return code for this failure is EIO, however EACCES
19505 	 * seems more appropriate for a reservation conflict.
19506 	 */
19507 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19508 	    (clock_t)2, NULL);
19509 }
19510 
19511 
19512 
19513 /*
19514  *    Function: sd_pkt_status_qfull
19515  *
19516  * Description: Handle a QUEUE FULL condition from the target.  This can
19517  *		occur if the HBA does not handle the queue full condition.
19518  *		(Basically this means third-party HBAs as Sun HBAs will
19519  *		handle the queue full condition.)  Note that if there are
19520  *		some commands already in the transport, then the queue full
19521  *		has occurred because the queue for this nexus is actually
19522  *		full. If there are no commands in the transport, then the
19523  *		queue full is resulting from some other initiator or lun
19524  *		consuming all the resources at the target.
19525  *
19526  *     Context: May be called from interrupt context
19527  */
19528 
19529 static void
19530 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
19531 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19532 {
19533 	ASSERT(un != NULL);
19534 	ASSERT(mutex_owned(SD_MUTEX(un)));
19535 	ASSERT(bp != NULL);
19536 	ASSERT(xp != NULL);
19537 	ASSERT(pktp != NULL);
19538 
19539 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19540 	    "sd_pkt_status_qfull: entry\n");
19541 
19542 	/*
19543 	 * Just lower the QFULL throttle and retry the command.  Note that
19544 	 * we do not limit the number of retries here.
19545 	 */
19546 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19547 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19548 	    SD_RESTART_TIMEOUT, NULL);
19549 
19550 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19551 	    "sd_pkt_status_qfull: exit\n");
19552 }
19553 
19554 
19555 /*
19556  *    Function: sd_reset_target
19557  *
19558  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19559  *		RESET_TARGET, or RESET_ALL.
19560  *
19561  *     Context: May be called under interrupt context.
19562  */
19563 
19564 static void
19565 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19566 {
19567 	int rval = 0;
19568 
19569 	ASSERT(un != NULL);
19570 	ASSERT(mutex_owned(SD_MUTEX(un)));
19571 	ASSERT(pktp != NULL);
19572 
19573 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19574 
19575 	/*
19576 	 * No need to reset if the transport layer has already done so.
19577 	 */
19578 	if ((pktp->pkt_statistics &
19579 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19580 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19581 		    "sd_reset_target: no reset\n");
19582 		return;
19583 	}
19584 
19585 	mutex_exit(SD_MUTEX(un));
19586 
19587 	if (un->un_f_allow_bus_device_reset == TRUE) {
19588 		if (un->un_f_lun_reset_enabled == TRUE) {
19589 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19590 			    "sd_reset_target: RESET_LUN\n");
19591 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19592 		}
19593 		if (rval == 0) {
19594 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19595 			    "sd_reset_target: RESET_TARGET\n");
19596 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19597 		}
19598 	}
19599 
19600 	if (rval == 0) {
19601 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19602 		    "sd_reset_target: RESET_ALL\n");
19603 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19604 	}
19605 
19606 	mutex_enter(SD_MUTEX(un));
19607 
19608 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19609 }
19610 
19611 /*
19612  *    Function: sd_target_change_task
19613  *
19614  * Description: Handle dynamic target change
19615  *
19616  *     Context: Executes in a taskq() thread context
19617  */
19618 static void
19619 sd_target_change_task(void *arg)
19620 {
19621 	struct sd_lun		*un = arg;
19622 	uint64_t		capacity;
19623 	diskaddr_t		label_cap;
19624 	uint_t			lbasize;
19625 	sd_ssc_t		*ssc;
19626 
19627 	ASSERT(un != NULL);
19628 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19629 
19630 	if ((un->un_f_blockcount_is_valid == FALSE) ||
19631 	    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19632 		return;
19633 	}
19634 
19635 	ssc = sd_ssc_init(un);
19636 
19637 	if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19638 	    &lbasize, SD_PATH_DIRECT) != 0) {
19639 		SD_ERROR(SD_LOG_ERROR, un,
19640 		    "sd_target_change_task: fail to read capacity\n");
19641 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19642 		goto task_exit;
19643 	}
19644 
19645 	mutex_enter(SD_MUTEX(un));
19646 	if (capacity <= un->un_blockcount) {
19647 		mutex_exit(SD_MUTEX(un));
19648 		goto task_exit;
19649 	}
19650 
19651 	sd_update_block_info(un, lbasize, capacity);
19652 	mutex_exit(SD_MUTEX(un));
19653 
19654 	/*
19655 	 * If lun is EFI labeled and lun capacity is greater than the
19656 	 * capacity contained in the label, log a sys event.
19657 	 */
19658 	if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19659 	    (void*)SD_PATH_DIRECT) == 0) {
19660 		mutex_enter(SD_MUTEX(un));
19661 		if (un->un_f_blockcount_is_valid &&
19662 		    un->un_blockcount > label_cap) {
19663 			mutex_exit(SD_MUTEX(un));
19664 			sd_log_lun_expansion_event(un, KM_SLEEP);
19665 		} else {
19666 			mutex_exit(SD_MUTEX(un));
19667 		}
19668 	}
19669 
19670 task_exit:
19671 	sd_ssc_fini(ssc);
19672 }
19673 
19674 
19675 /*
19676  *    Function: sd_log_dev_status_event
19677  *
19678  * Description: Log EC_dev_status sysevent
19679  *
19680  *     Context: Never called from interrupt context
19681  */
19682 static void
19683 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19684 {
19685 	int err;
19686 	char			*path;
19687 	nvlist_t		*attr_list;
19688 
19689 	/* Allocate and build sysevent attribute list */
19690 	err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19691 	if (err != 0) {
19692 		SD_ERROR(SD_LOG_ERROR, un,
19693 		    "sd_log_dev_status_event: fail to allocate space\n");
19694 		return;
19695 	}
19696 
19697 	path = kmem_alloc(MAXPATHLEN, km_flag);
19698 	if (path == NULL) {
19699 		nvlist_free(attr_list);
19700 		SD_ERROR(SD_LOG_ERROR, un,
19701 		    "sd_log_dev_status_event: fail to allocate space\n");
19702 		return;
19703 	}
19704 	/*
19705 	 * Add path attribute to identify the lun.
19706 	 * We are using minor node 'a' as the sysevent attribute.
19707 	 */
19708 	(void) snprintf(path, MAXPATHLEN, "/devices");
19709 	(void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19710 	(void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19711 	    ":a");
19712 
19713 	err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19714 	if (err != 0) {
19715 		nvlist_free(attr_list);
19716 		kmem_free(path, MAXPATHLEN);
19717 		SD_ERROR(SD_LOG_ERROR, un,
19718 		    "sd_log_dev_status_event: fail to add attribute\n");
19719 		return;
19720 	}
19721 
19722 	/* Log dynamic lun expansion sysevent */
19723 	err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19724 	    esc, attr_list, NULL, km_flag);
19725 	if (err != DDI_SUCCESS) {
19726 		SD_ERROR(SD_LOG_ERROR, un,
19727 		    "sd_log_dev_status_event: fail to log sysevent\n");
19728 	}
19729 
19730 	nvlist_free(attr_list);
19731 	kmem_free(path, MAXPATHLEN);
19732 }
19733 
19734 
19735 /*
19736  *    Function: sd_log_lun_expansion_event
19737  *
19738  * Description: Log lun expansion sys event
19739  *
19740  *     Context: Never called from interrupt context
19741  */
19742 static void
19743 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19744 {
19745 	sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19746 }
19747 
19748 
19749 /*
19750  *    Function: sd_log_eject_request_event
19751  *
19752  * Description: Log eject request sysevent
19753  *
19754  *     Context: Never called from interrupt context
19755  */
19756 static void
19757 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19758 {
19759 	sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19760 }
19761 
19762 
19763 /*
19764  *    Function: sd_media_change_task
19765  *
19766  * Description: Recovery action for CDROM to become available.
19767  *
19768  *     Context: Executes in a taskq() thread context
19769  */
19770 
19771 static void
19772 sd_media_change_task(void *arg)
19773 {
19774 	struct	scsi_pkt	*pktp = arg;
19775 	struct	sd_lun		*un;
19776 	struct	buf		*bp;
19777 	struct	sd_xbuf		*xp;
19778 	int	err		= 0;
19779 	int	retry_count	= 0;
19780 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
19781 	struct	sd_sense_info	si;
19782 
19783 	ASSERT(pktp != NULL);
19784 	bp = (struct buf *)pktp->pkt_private;
19785 	ASSERT(bp != NULL);
19786 	xp = SD_GET_XBUF(bp);
19787 	ASSERT(xp != NULL);
19788 	un = SD_GET_UN(bp);
19789 	ASSERT(un != NULL);
19790 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19791 	ASSERT(un->un_f_monitor_media_state);
19792 
19793 	si.ssi_severity = SCSI_ERR_INFO;
19794 	si.ssi_pfa_flag = FALSE;
19795 
19796 	/*
19797 	 * When a reset is issued on a CDROM, it takes a long time to
19798 	 * recover. First few attempts to read capacity and other things
19799 	 * related to handling unit attention fail (with a ASC 0x4 and
19800 	 * ASCQ 0x1). In that case we want to do enough retries and we want
19801 	 * to limit the retries in other cases of genuine failures like
19802 	 * no media in drive.
19803 	 */
19804 	while (retry_count++ < retry_limit) {
19805 		if ((err = sd_handle_mchange(un)) == 0) {
19806 			break;
19807 		}
19808 		if (err == EAGAIN) {
19809 			retry_limit = SD_UNIT_ATTENTION_RETRY;
19810 		}
19811 		/* Sleep for 0.5 sec. & try again */
19812 		delay(drv_usectohz(500000));
19813 	}
19814 
19815 	/*
19816 	 * Dispatch (retry or fail) the original command here,
19817 	 * along with appropriate console messages....
19818 	 *
19819 	 * Must grab the mutex before calling sd_retry_command,
19820 	 * sd_print_sense_msg and sd_return_failed_command.
19821 	 */
19822 	mutex_enter(SD_MUTEX(un));
19823 	if (err != SD_CMD_SUCCESS) {
19824 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
19825 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19826 		si.ssi_severity = SCSI_ERR_FATAL;
19827 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19828 		sd_return_failed_command(un, bp, EIO);
19829 	} else {
19830 		sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19831 		    &si, EIO, (clock_t)0, NULL);
19832 	}
19833 	mutex_exit(SD_MUTEX(un));
19834 }
19835 
19836 
19837 
19838 /*
19839  *    Function: sd_handle_mchange
19840  *
19841  * Description: Perform geometry validation & other recovery when CDROM
19842  *		has been removed from drive.
19843  *
19844  * Return Code: 0 for success
19845  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
19846  *		sd_send_scsi_READ_CAPACITY()
19847  *
19848  *     Context: Executes in a taskq() thread context
19849  */
19850 
19851 static int
19852 sd_handle_mchange(struct sd_lun *un)
19853 {
19854 	uint64_t	capacity;
19855 	uint32_t	lbasize;
19856 	int		rval;
19857 	sd_ssc_t	*ssc;
19858 
19859 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19860 	ASSERT(un->un_f_monitor_media_state);
19861 
19862 	ssc = sd_ssc_init(un);
19863 	rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19864 	    SD_PATH_DIRECT_PRIORITY);
19865 
19866 	if (rval != 0)
19867 		goto failed;
19868 
19869 	mutex_enter(SD_MUTEX(un));
19870 	sd_update_block_info(un, lbasize, capacity);
19871 
19872 	if (un->un_errstats != NULL) {
19873 		struct	sd_errstats *stp =
19874 		    (struct sd_errstats *)un->un_errstats->ks_data;
19875 		stp->sd_capacity.value.ui64 = (uint64_t)
19876 		    ((uint64_t)un->un_blockcount *
19877 		    (uint64_t)un->un_tgt_blocksize);
19878 	}
19879 
19880 	/*
19881 	 * Check if the media in the device is writable or not
19882 	 */
19883 	if (ISCD(un)) {
19884 		sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19885 	}
19886 
19887 	/*
19888 	 * Note: Maybe let the strategy/partitioning chain worry about getting
19889 	 * valid geometry.
19890 	 */
19891 	mutex_exit(SD_MUTEX(un));
19892 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19893 
19894 
19895 	if (cmlb_validate(un->un_cmlbhandle, 0,
19896 	    (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19897 		sd_ssc_fini(ssc);
19898 		return (EIO);
19899 	} else {
19900 		if (un->un_f_pkstats_enabled) {
19901 			sd_set_pstats(un);
19902 			SD_TRACE(SD_LOG_IO_PARTITION, un,
19903 			    "sd_handle_mchange: un:0x%p pstats created and "
19904 			    "set\n", un);
19905 		}
19906 	}
19907 
19908 	/*
19909 	 * Try to lock the door
19910 	 */
19911 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19912 	    SD_PATH_DIRECT_PRIORITY);
19913 failed:
19914 	if (rval != 0)
19915 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19916 	sd_ssc_fini(ssc);
19917 	return (rval);
19918 }
19919 
19920 
19921 /*
19922  *    Function: sd_send_scsi_DOORLOCK
19923  *
19924  * Description: Issue the scsi DOOR LOCK command
19925  *
19926  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
19927  *                      structure for this target.
19928  *		flag  - SD_REMOVAL_ALLOW
19929  *			SD_REMOVAL_PREVENT
19930  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19931  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19932  *			to use the USCSI "direct" chain and bypass the normal
19933  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19934  *			command is issued as part of an error recovery action.
19935  *
19936  * Return Code: 0   - Success
19937  *		errno return code from sd_ssc_send()
19938  *
19939  *     Context: Can sleep.
19940  */
19941 
19942 static int
19943 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19944 {
19945 	struct scsi_extended_sense	sense_buf;
19946 	union scsi_cdb		cdb;
19947 	struct uscsi_cmd	ucmd_buf;
19948 	int			status;
19949 	struct sd_lun		*un;
19950 
19951 	ASSERT(ssc != NULL);
19952 	un = ssc->ssc_un;
19953 	ASSERT(un != NULL);
19954 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19955 
19956 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19957 
19958 	/* already determined doorlock is not supported, fake success */
19959 	if (un->un_f_doorlock_supported == FALSE) {
19960 		return (0);
19961 	}
19962 
19963 	/*
19964 	 * If we are ejecting and see an SD_REMOVAL_PREVENT
19965 	 * ignore the command so we can complete the eject
19966 	 * operation.
19967 	 */
19968 	if (flag == SD_REMOVAL_PREVENT) {
19969 		mutex_enter(SD_MUTEX(un));
19970 		if (un->un_f_ejecting == TRUE) {
19971 			mutex_exit(SD_MUTEX(un));
19972 			return (EAGAIN);
19973 		}
19974 		mutex_exit(SD_MUTEX(un));
19975 	}
19976 
19977 	bzero(&cdb, sizeof (cdb));
19978 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19979 
19980 	cdb.scc_cmd = SCMD_DOORLOCK;
19981 	cdb.cdb_opaque[4] = (uchar_t)flag;
19982 
19983 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19984 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19985 	ucmd_buf.uscsi_bufaddr	= NULL;
19986 	ucmd_buf.uscsi_buflen	= 0;
19987 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19988 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19989 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19990 	ucmd_buf.uscsi_timeout	= 15;
19991 
19992 	SD_TRACE(SD_LOG_IO, un,
19993 	    "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19994 
19995 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19996 	    UIO_SYSSPACE, path_flag);
19997 
19998 	if (status == 0)
19999 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20000 
20001 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
20002 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20003 	    (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
20004 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20005 
20006 		/* fake success and skip subsequent doorlock commands */
20007 		un->un_f_doorlock_supported = FALSE;
20008 		return (0);
20009 	}
20010 
20011 	return (status);
20012 }
20013 
20014 /*
20015  *    Function: sd_send_scsi_READ_CAPACITY
20016  *
20017  * Description: This routine uses the scsi READ CAPACITY command to determine
20018  *		the device capacity in number of blocks and the device native
20019  *		block size. If this function returns a failure, then the
20020  *		values in *capp and *lbap are undefined.  If the capacity
20021  *		returned is 0xffffffff then the lun is too large for a
20022  *		normal READ CAPACITY command and the results of a
20023  *		READ CAPACITY 16 will be used instead.
20024  *
20025  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20026  *		capp - ptr to unsigned 64-bit variable to receive the
20027  *			capacity value from the command.
20028  *		lbap - ptr to unsigned 32-bit varaible to receive the
20029  *			block size value from the command
20030  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20031  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20032  *			to use the USCSI "direct" chain and bypass the normal
20033  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20034  *			command is issued as part of an error recovery action.
20035  *
20036  * Return Code: 0   - Success
20037  *		EIO - IO error
20038  *		EACCES - Reservation conflict detected
20039  *		EAGAIN - Device is becoming ready
20040  *		errno return code from sd_ssc_send()
20041  *
20042  *     Context: Can sleep.  Blocks until command completes.
20043  */
20044 
20045 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
20046 
20047 static int
20048 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20049 	int path_flag)
20050 {
20051 	struct	scsi_extended_sense	sense_buf;
20052 	struct	uscsi_cmd	ucmd_buf;
20053 	union	scsi_cdb	cdb;
20054 	uint32_t		*capacity_buf;
20055 	uint64_t		capacity;
20056 	uint32_t		lbasize;
20057 	uint32_t		pbsize;
20058 	int			status;
20059 	struct sd_lun		*un;
20060 
20061 	ASSERT(ssc != NULL);
20062 
20063 	un = ssc->ssc_un;
20064 	ASSERT(un != NULL);
20065 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20066 	ASSERT(capp != NULL);
20067 	ASSERT(lbap != NULL);
20068 
20069 	SD_TRACE(SD_LOG_IO, un,
20070 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20071 
20072 	/*
20073 	 * First send a READ_CAPACITY command to the target.
20074 	 * (This command is mandatory under SCSI-2.)
20075 	 *
20076 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
20077 	 * Medium Indicator bit is cleared.  The address field must be
20078 	 * zero if the PMI bit is zero.
20079 	 */
20080 	bzero(&cdb, sizeof (cdb));
20081 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20082 
20083 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
20084 
20085 	cdb.scc_cmd = SCMD_READ_CAPACITY;
20086 
20087 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20088 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20089 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
20090 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
20091 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20092 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20093 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20094 	ucmd_buf.uscsi_timeout	= 60;
20095 
20096 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20097 	    UIO_SYSSPACE, path_flag);
20098 
20099 	switch (status) {
20100 	case 0:
20101 		/* Return failure if we did not get valid capacity data. */
20102 		if (ucmd_buf.uscsi_resid != 0) {
20103 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20104 			    "sd_send_scsi_READ_CAPACITY received invalid "
20105 			    "capacity data");
20106 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20107 			return (EIO);
20108 		}
20109 		/*
20110 		 * Read capacity and block size from the READ CAPACITY 10 data.
20111 		 * This data may be adjusted later due to device specific
20112 		 * issues.
20113 		 *
20114 		 * According to the SCSI spec, the READ CAPACITY 10
20115 		 * command returns the following:
20116 		 *
20117 		 *  bytes 0-3: Maximum logical block address available.
20118 		 *		(MSB in byte:0 & LSB in byte:3)
20119 		 *
20120 		 *  bytes 4-7: Block length in bytes
20121 		 *		(MSB in byte:4 & LSB in byte:7)
20122 		 *
20123 		 */
20124 		capacity = BE_32(capacity_buf[0]);
20125 		lbasize = BE_32(capacity_buf[1]);
20126 
20127 		/*
20128 		 * Done with capacity_buf
20129 		 */
20130 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20131 
20132 		/*
20133 		 * if the reported capacity is set to all 0xf's, then
20134 		 * this disk is too large and requires SBC-2 commands.
20135 		 * Reissue the request using READ CAPACITY 16.
20136 		 */
20137 		if (capacity == 0xffffffff) {
20138 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20139 			status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
20140 			    &lbasize, &pbsize, path_flag);
20141 			if (status != 0) {
20142 				return (status);
20143 			} else {
20144 				goto rc16_done;
20145 			}
20146 		}
20147 		break;	/* Success! */
20148 	case EIO:
20149 		switch (ucmd_buf.uscsi_status) {
20150 		case STATUS_RESERVATION_CONFLICT:
20151 			status = EACCES;
20152 			break;
20153 		case STATUS_CHECK:
20154 			/*
20155 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20156 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20157 			 */
20158 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20159 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20160 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20161 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20162 				return (EAGAIN);
20163 			}
20164 			break;
20165 		default:
20166 			break;
20167 		}
20168 		/* FALLTHRU */
20169 	default:
20170 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20171 		return (status);
20172 	}
20173 
20174 	/*
20175 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20176 	 * (2352 and 0 are common) so for these devices always force the value
20177 	 * to 2048 as required by the ATAPI specs.
20178 	 */
20179 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20180 		lbasize = 2048;
20181 	}
20182 
20183 	/*
20184 	 * Get the maximum LBA value from the READ CAPACITY data.
20185 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20186 	 * was cleared when issuing the command. This means that the LBA
20187 	 * returned from the device is the LBA of the last logical block
20188 	 * on the logical unit.  The actual logical block count will be
20189 	 * this value plus one.
20190 	 */
20191 	capacity += 1;
20192 
20193 	/*
20194 	 * Currently, for removable media, the capacity is saved in terms
20195 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20196 	 */
20197 	if (un->un_f_has_removable_media)
20198 		capacity *= (lbasize / un->un_sys_blocksize);
20199 
20200 rc16_done:
20201 
20202 	/*
20203 	 * Copy the values from the READ CAPACITY command into the space
20204 	 * provided by the caller.
20205 	 */
20206 	*capp = capacity;
20207 	*lbap = lbasize;
20208 
20209 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20210 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
20211 
20212 	/*
20213 	 * Both the lbasize and capacity from the device must be nonzero,
20214 	 * otherwise we assume that the values are not valid and return
20215 	 * failure to the caller. (4203735)
20216 	 */
20217 	if ((capacity == 0) || (lbasize == 0)) {
20218 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20219 		    "sd_send_scsi_READ_CAPACITY received invalid value "
20220 		    "capacity %llu lbasize %d", capacity, lbasize);
20221 		return (EIO);
20222 	}
20223 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20224 	return (0);
20225 }
20226 
20227 /*
20228  *    Function: sd_send_scsi_READ_CAPACITY_16
20229  *
20230  * Description: This routine uses the scsi READ CAPACITY 16 command to
20231  *		determine the device capacity in number of blocks and the
20232  *		device native block size.  If this function returns a failure,
20233  *		then the values in *capp and *lbap are undefined.
20234  *		This routine should be called by sd_send_scsi_READ_CAPACITY
20235  *              which will apply any device specific adjustments to capacity
20236  *              and lbasize. One exception is it is also called by
20237  *              sd_get_media_info_ext. In that function, there is no need to
20238  *              adjust the capacity and lbasize.
20239  *
20240  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20241  *		capp - ptr to unsigned 64-bit variable to receive the
20242  *			capacity value from the command.
20243  *		lbap - ptr to unsigned 32-bit varaible to receive the
20244  *			block size value from the command
20245  *              psp  - ptr to unsigned 32-bit variable to receive the
20246  *                      physical block size value from the command
20247  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20248  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20249  *			to use the USCSI "direct" chain and bypass the normal
20250  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
20251  *			this command is issued as part of an error recovery
20252  *			action.
20253  *
20254  * Return Code: 0   - Success
20255  *		EIO - IO error
20256  *		EACCES - Reservation conflict detected
20257  *		EAGAIN - Device is becoming ready
20258  *		errno return code from sd_ssc_send()
20259  *
20260  *     Context: Can sleep.  Blocks until command completes.
20261  */
20262 
20263 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
20264 
20265 static int
20266 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
20267 	uint32_t *lbap, uint32_t *psp, int path_flag)
20268 {
20269 	struct	scsi_extended_sense	sense_buf;
20270 	struct	uscsi_cmd	ucmd_buf;
20271 	union	scsi_cdb	cdb;
20272 	uint64_t		*capacity16_buf;
20273 	uint64_t		capacity;
20274 	uint32_t		lbasize;
20275 	uint32_t		pbsize;
20276 	uint32_t		lbpb_exp;
20277 	int			status;
20278 	struct sd_lun		*un;
20279 
20280 	ASSERT(ssc != NULL);
20281 
20282 	un = ssc->ssc_un;
20283 	ASSERT(un != NULL);
20284 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20285 	ASSERT(capp != NULL);
20286 	ASSERT(lbap != NULL);
20287 
20288 	SD_TRACE(SD_LOG_IO, un,
20289 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20290 
20291 	/*
20292 	 * First send a READ_CAPACITY_16 command to the target.
20293 	 *
20294 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
20295 	 * Medium Indicator bit is cleared.  The address field must be
20296 	 * zero if the PMI bit is zero.
20297 	 */
20298 	bzero(&cdb, sizeof (cdb));
20299 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20300 
20301 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20302 
20303 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20304 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
20305 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
20306 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
20307 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20308 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20309 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20310 	ucmd_buf.uscsi_timeout	= 60;
20311 
20312 	/*
20313 	 * Read Capacity (16) is a Service Action In command.  One
20314 	 * command byte (0x9E) is overloaded for multiple operations,
20315 	 * with the second CDB byte specifying the desired operation
20316 	 */
20317 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20318 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20319 
20320 	/*
20321 	 * Fill in allocation length field
20322 	 */
20323 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20324 
20325 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20326 	    UIO_SYSSPACE, path_flag);
20327 
20328 	switch (status) {
20329 	case 0:
20330 		/* Return failure if we did not get valid capacity data. */
20331 		if (ucmd_buf.uscsi_resid > 20) {
20332 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20333 			    "sd_send_scsi_READ_CAPACITY_16 received invalid "
20334 			    "capacity data");
20335 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20336 			return (EIO);
20337 		}
20338 
20339 		/*
20340 		 * Read capacity and block size from the READ CAPACITY 16 data.
20341 		 * This data may be adjusted later due to device specific
20342 		 * issues.
20343 		 *
20344 		 * According to the SCSI spec, the READ CAPACITY 16
20345 		 * command returns the following:
20346 		 *
20347 		 *  bytes 0-7: Maximum logical block address available.
20348 		 *		(MSB in byte:0 & LSB in byte:7)
20349 		 *
20350 		 *  bytes 8-11: Block length in bytes
20351 		 *		(MSB in byte:8 & LSB in byte:11)
20352 		 *
20353 		 *  byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20354 		 */
20355 		capacity = BE_64(capacity16_buf[0]);
20356 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20357 		lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20358 
20359 		pbsize = lbasize << lbpb_exp;
20360 
20361 		/*
20362 		 * Done with capacity16_buf
20363 		 */
20364 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20365 
20366 		/*
20367 		 * if the reported capacity is set to all 0xf's, then
20368 		 * this disk is too large.  This could only happen with
20369 		 * a device that supports LBAs larger than 64 bits which
20370 		 * are not defined by any current T10 standards.
20371 		 */
20372 		if (capacity == 0xffffffffffffffff) {
20373 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20374 			    "disk is too large");
20375 			return (EIO);
20376 		}
20377 		break;	/* Success! */
20378 	case EIO:
20379 		switch (ucmd_buf.uscsi_status) {
20380 		case STATUS_RESERVATION_CONFLICT:
20381 			status = EACCES;
20382 			break;
20383 		case STATUS_CHECK:
20384 			/*
20385 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20386 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20387 			 */
20388 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20389 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20390 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20391 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20392 				return (EAGAIN);
20393 			}
20394 			break;
20395 		default:
20396 			break;
20397 		}
20398 		/* FALLTHRU */
20399 	default:
20400 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20401 		return (status);
20402 	}
20403 
20404 	/*
20405 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20406 	 * (2352 and 0 are common) so for these devices always force the value
20407 	 * to 2048 as required by the ATAPI specs.
20408 	 */
20409 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20410 		lbasize = 2048;
20411 	}
20412 
20413 	/*
20414 	 * Get the maximum LBA value from the READ CAPACITY 16 data.
20415 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20416 	 * was cleared when issuing the command. This means that the LBA
20417 	 * returned from the device is the LBA of the last logical block
20418 	 * on the logical unit.  The actual logical block count will be
20419 	 * this value plus one.
20420 	 */
20421 	capacity += 1;
20422 
20423 	/*
20424 	 * Currently, for removable media, the capacity is saved in terms
20425 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20426 	 */
20427 	if (un->un_f_has_removable_media)
20428 		capacity *= (lbasize / un->un_sys_blocksize);
20429 
20430 	*capp = capacity;
20431 	*lbap = lbasize;
20432 	*psp = pbsize;
20433 
20434 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20435 	    "capacity:0x%llx  lbasize:0x%x, pbsize: 0x%x\n",
20436 	    capacity, lbasize, pbsize);
20437 
20438 	if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
20439 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20440 		    "sd_send_scsi_READ_CAPACITY_16 received invalid value "
20441 		    "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
20442 		return (EIO);
20443 	}
20444 
20445 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20446 	return (0);
20447 }
20448 
20449 
20450 /*
20451  *    Function: sd_send_scsi_START_STOP_UNIT
20452  *
20453  * Description: Issue a scsi START STOP UNIT command to the target.
20454  *
20455  *   Arguments: ssc    - ssc contatins pointer to driver soft state (unit)
20456  *                       structure for this target.
20457  *      pc_flag - SD_POWER_CONDITION
20458  *                SD_START_STOP
20459  *		flag  - SD_TARGET_START
20460  *			SD_TARGET_STOP
20461  *			SD_TARGET_EJECT
20462  *			SD_TARGET_CLOSE
20463  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20464  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20465  *			to use the USCSI "direct" chain and bypass the normal
20466  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20467  *			command is issued as part of an error recovery action.
20468  *
20469  * Return Code: 0   - Success
20470  *		EIO - IO error
20471  *		EACCES - Reservation conflict detected
20472  *		ENXIO  - Not Ready, medium not present
20473  *		errno return code from sd_ssc_send()
20474  *
20475  *     Context: Can sleep.
20476  */
20477 
20478 static int
20479 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20480     int path_flag)
20481 {
20482 	struct	scsi_extended_sense	sense_buf;
20483 	union scsi_cdb		cdb;
20484 	struct uscsi_cmd	ucmd_buf;
20485 	int			status;
20486 	struct sd_lun		*un;
20487 
20488 	ASSERT(ssc != NULL);
20489 	un = ssc->ssc_un;
20490 	ASSERT(un != NULL);
20491 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20492 
20493 	SD_TRACE(SD_LOG_IO, un,
20494 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20495 
20496 	if (un->un_f_check_start_stop &&
20497 	    (pc_flag == SD_START_STOP) &&
20498 	    ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20499 	    (un->un_f_start_stop_supported != TRUE)) {
20500 		return (0);
20501 	}
20502 
20503 	/*
20504 	 * If we are performing an eject operation and
20505 	 * we receive any command other than SD_TARGET_EJECT
20506 	 * we should immediately return.
20507 	 */
20508 	if (flag != SD_TARGET_EJECT) {
20509 		mutex_enter(SD_MUTEX(un));
20510 		if (un->un_f_ejecting == TRUE) {
20511 			mutex_exit(SD_MUTEX(un));
20512 			return (EAGAIN);
20513 		}
20514 		mutex_exit(SD_MUTEX(un));
20515 	}
20516 
20517 	bzero(&cdb, sizeof (cdb));
20518 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20519 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20520 
20521 	cdb.scc_cmd = SCMD_START_STOP;
20522 	cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20523 	    (uchar_t)(flag << 4) : (uchar_t)flag;
20524 
20525 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20526 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20527 	ucmd_buf.uscsi_bufaddr	= NULL;
20528 	ucmd_buf.uscsi_buflen	= 0;
20529 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20530 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20531 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20532 	ucmd_buf.uscsi_timeout	= 200;
20533 
20534 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20535 	    UIO_SYSSPACE, path_flag);
20536 
20537 	switch (status) {
20538 	case 0:
20539 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20540 		break;	/* Success! */
20541 	case EIO:
20542 		switch (ucmd_buf.uscsi_status) {
20543 		case STATUS_RESERVATION_CONFLICT:
20544 			status = EACCES;
20545 			break;
20546 		case STATUS_CHECK:
20547 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20548 				switch (scsi_sense_key(
20549 				    (uint8_t *)&sense_buf)) {
20550 				case KEY_ILLEGAL_REQUEST:
20551 					status = ENOTSUP;
20552 					break;
20553 				case KEY_NOT_READY:
20554 					if (scsi_sense_asc(
20555 					    (uint8_t *)&sense_buf)
20556 					    == 0x3A) {
20557 						status = ENXIO;
20558 					}
20559 					break;
20560 				default:
20561 					break;
20562 				}
20563 			}
20564 			break;
20565 		default:
20566 			break;
20567 		}
20568 		break;
20569 	default:
20570 		break;
20571 	}
20572 
20573 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20574 
20575 	return (status);
20576 }
20577 
20578 
20579 /*
20580  *    Function: sd_start_stop_unit_callback
20581  *
20582  * Description: timeout(9F) callback to begin recovery process for a
20583  *		device that has spun down.
20584  *
20585  *   Arguments: arg - pointer to associated softstate struct.
20586  *
20587  *     Context: Executes in a timeout(9F) thread context
20588  */
20589 
20590 static void
20591 sd_start_stop_unit_callback(void *arg)
20592 {
20593 	struct sd_lun	*un = arg;
20594 	ASSERT(un != NULL);
20595 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20596 
20597 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20598 
20599 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20600 }
20601 
20602 
20603 /*
20604  *    Function: sd_start_stop_unit_task
20605  *
20606  * Description: Recovery procedure when a drive is spun down.
20607  *
20608  *   Arguments: arg - pointer to associated softstate struct.
20609  *
20610  *     Context: Executes in a taskq() thread context
20611  */
20612 
20613 static void
20614 sd_start_stop_unit_task(void *arg)
20615 {
20616 	struct sd_lun	*un = arg;
20617 	sd_ssc_t	*ssc;
20618 	int		power_level;
20619 	int		rval;
20620 
20621 	ASSERT(un != NULL);
20622 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20623 
20624 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20625 
20626 	/*
20627 	 * Some unformatted drives report not ready error, no need to
20628 	 * restart if format has been initiated.
20629 	 */
20630 	mutex_enter(SD_MUTEX(un));
20631 	if (un->un_f_format_in_progress == TRUE) {
20632 		mutex_exit(SD_MUTEX(un));
20633 		return;
20634 	}
20635 	mutex_exit(SD_MUTEX(un));
20636 
20637 	ssc = sd_ssc_init(un);
20638 	/*
20639 	 * When a START STOP command is issued from here, it is part of a
20640 	 * failure recovery operation and must be issued before any other
20641 	 * commands, including any pending retries. Thus it must be sent
20642 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20643 	 * succeeds or not, we will start I/O after the attempt.
20644 	 * If power condition is supported and the current power level
20645 	 * is capable of performing I/O, we should set the power condition
20646 	 * to that level. Otherwise, set the power condition to ACTIVE.
20647 	 */
20648 	if (un->un_f_power_condition_supported) {
20649 		mutex_enter(SD_MUTEX(un));
20650 		ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20651 		power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20652 		    > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20653 		mutex_exit(SD_MUTEX(un));
20654 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20655 		    sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20656 	} else {
20657 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20658 		    SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20659 	}
20660 
20661 	if (rval != 0)
20662 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20663 	sd_ssc_fini(ssc);
20664 	/*
20665 	 * The above call blocks until the START_STOP_UNIT command completes.
20666 	 * Now that it has completed, we must re-try the original IO that
20667 	 * received the NOT READY condition in the first place. There are
20668 	 * three possible conditions here:
20669 	 *
20670 	 *  (1) The original IO is on un_retry_bp.
20671 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
20672 	 *	is NULL.
20673 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
20674 	 *	points to some other, unrelated bp.
20675 	 *
20676 	 * For each case, we must call sd_start_cmds() with un_retry_bp
20677 	 * as the argument. If un_retry_bp is NULL, this will initiate
20678 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
20679 	 * then this will process the bp on un_retry_bp. That may or may not
20680 	 * be the original IO, but that does not matter: the important thing
20681 	 * is to keep the IO processing going at this point.
20682 	 *
20683 	 * Note: This is a very specific error recovery sequence associated
20684 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20685 	 * serialize the I/O with completion of the spin-up.
20686 	 */
20687 	mutex_enter(SD_MUTEX(un));
20688 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20689 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20690 	    un, un->un_retry_bp);
20691 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
20692 	sd_start_cmds(un, un->un_retry_bp);
20693 	mutex_exit(SD_MUTEX(un));
20694 
20695 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20696 }
20697 
20698 
20699 /*
20700  *    Function: sd_send_scsi_INQUIRY
20701  *
20702  * Description: Issue the scsi INQUIRY command.
20703  *
20704  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20705  *                      structure for this target.
20706  *		bufaddr
20707  *		buflen
20708  *		evpd
20709  *		page_code
20710  *		page_length
20711  *
20712  * Return Code: 0   - Success
20713  *		errno return code from sd_ssc_send()
20714  *
20715  *     Context: Can sleep. Does not return until command is completed.
20716  */
20717 
20718 static int
20719 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20720 	uchar_t evpd, uchar_t page_code, size_t *residp)
20721 {
20722 	union scsi_cdb		cdb;
20723 	struct uscsi_cmd	ucmd_buf;
20724 	int			status;
20725 	struct sd_lun		*un;
20726 
20727 	ASSERT(ssc != NULL);
20728 	un = ssc->ssc_un;
20729 	ASSERT(un != NULL);
20730 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20731 	ASSERT(bufaddr != NULL);
20732 
20733 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20734 
20735 	bzero(&cdb, sizeof (cdb));
20736 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20737 	bzero(bufaddr, buflen);
20738 
20739 	cdb.scc_cmd = SCMD_INQUIRY;
20740 	cdb.cdb_opaque[1] = evpd;
20741 	cdb.cdb_opaque[2] = page_code;
20742 	FORMG0COUNT(&cdb, buflen);
20743 
20744 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20745 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20746 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20747 	ucmd_buf.uscsi_buflen	= buflen;
20748 	ucmd_buf.uscsi_rqbuf	= NULL;
20749 	ucmd_buf.uscsi_rqlen	= 0;
20750 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
20751 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
20752 
20753 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20754 	    UIO_SYSSPACE, SD_PATH_DIRECT);
20755 
20756 	/*
20757 	 * Only handle status == 0, the upper-level caller
20758 	 * will put different assessment based on the context.
20759 	 */
20760 	if (status == 0)
20761 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20762 
20763 	if ((status == 0) && (residp != NULL)) {
20764 		*residp = ucmd_buf.uscsi_resid;
20765 	}
20766 
20767 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20768 
20769 	return (status);
20770 }
20771 
20772 
20773 /*
20774  *    Function: sd_send_scsi_TEST_UNIT_READY
20775  *
20776  * Description: Issue the scsi TEST UNIT READY command.
20777  *		This routine can be told to set the flag USCSI_DIAGNOSE to
20778  *		prevent retrying failed commands. Use this when the intent
20779  *		is either to check for device readiness, to clear a Unit
20780  *		Attention, or to clear any outstanding sense data.
20781  *		However under specific conditions the expected behavior
20782  *		is for retries to bring a device ready, so use the flag
20783  *		with caution.
20784  *
20785  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20786  *                      structure for this target.
20787  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20788  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20789  *			0: dont check for media present, do retries on cmd.
20790  *
20791  * Return Code: 0   - Success
20792  *		EIO - IO error
20793  *		EACCES - Reservation conflict detected
20794  *		ENXIO  - Not Ready, medium not present
20795  *		errno return code from sd_ssc_send()
20796  *
20797  *     Context: Can sleep. Does not return until command is completed.
20798  */
20799 
20800 static int
20801 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20802 {
20803 	struct	scsi_extended_sense	sense_buf;
20804 	union scsi_cdb		cdb;
20805 	struct uscsi_cmd	ucmd_buf;
20806 	int			status;
20807 	struct sd_lun		*un;
20808 
20809 	ASSERT(ssc != NULL);
20810 	un = ssc->ssc_un;
20811 	ASSERT(un != NULL);
20812 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20813 
20814 	SD_TRACE(SD_LOG_IO, un,
20815 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20816 
20817 	/*
20818 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20819 	 * timeouts when they receive a TUR and the queue is not empty. Check
20820 	 * the configuration flag set during attach (indicating the drive has
20821 	 * this firmware bug) and un_ncmds_in_transport before issuing the
20822 	 * TUR. If there are
20823 	 * pending commands return success, this is a bit arbitrary but is ok
20824 	 * for non-removables (i.e. the eliteI disks) and non-clustering
20825 	 * configurations.
20826 	 */
20827 	if (un->un_f_cfg_tur_check == TRUE) {
20828 		mutex_enter(SD_MUTEX(un));
20829 		if (un->un_ncmds_in_transport != 0) {
20830 			mutex_exit(SD_MUTEX(un));
20831 			return (0);
20832 		}
20833 		mutex_exit(SD_MUTEX(un));
20834 	}
20835 
20836 	bzero(&cdb, sizeof (cdb));
20837 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20838 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20839 
20840 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20841 
20842 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20843 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20844 	ucmd_buf.uscsi_bufaddr	= NULL;
20845 	ucmd_buf.uscsi_buflen	= 0;
20846 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20847 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20848 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20849 
20850 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20851 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
20852 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20853 	}
20854 	ucmd_buf.uscsi_timeout	= 60;
20855 
20856 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20857 	    UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20858 	    SD_PATH_STANDARD));
20859 
20860 	switch (status) {
20861 	case 0:
20862 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20863 		break;	/* Success! */
20864 	case EIO:
20865 		switch (ucmd_buf.uscsi_status) {
20866 		case STATUS_RESERVATION_CONFLICT:
20867 			status = EACCES;
20868 			break;
20869 		case STATUS_CHECK:
20870 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20871 				break;
20872 			}
20873 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20874 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20875 			    KEY_NOT_READY) &&
20876 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20877 				status = ENXIO;
20878 			}
20879 			break;
20880 		default:
20881 			break;
20882 		}
20883 		break;
20884 	default:
20885 		break;
20886 	}
20887 
20888 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20889 
20890 	return (status);
20891 }
20892 
20893 /*
20894  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20895  *
20896  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20897  *
20898  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20899  *                      structure for this target.
20900  *
20901  * Return Code: 0   - Success
20902  *		EACCES
20903  *		ENOTSUP
20904  *		errno return code from sd_ssc_send()
20905  *
20906  *     Context: Can sleep. Does not return until command is completed.
20907  */
20908 
20909 static int
20910 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t  usr_cmd,
20911 	uint16_t data_len, uchar_t *data_bufp)
20912 {
20913 	struct scsi_extended_sense	sense_buf;
20914 	union scsi_cdb		cdb;
20915 	struct uscsi_cmd	ucmd_buf;
20916 	int			status;
20917 	int			no_caller_buf = FALSE;
20918 	struct sd_lun		*un;
20919 
20920 	ASSERT(ssc != NULL);
20921 	un = ssc->ssc_un;
20922 	ASSERT(un != NULL);
20923 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20924 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20925 
20926 	SD_TRACE(SD_LOG_IO, un,
20927 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20928 
20929 	bzero(&cdb, sizeof (cdb));
20930 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20931 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20932 	if (data_bufp == NULL) {
20933 		/* Allocate a default buf if the caller did not give one */
20934 		ASSERT(data_len == 0);
20935 		data_len  = MHIOC_RESV_KEY_SIZE;
20936 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20937 		no_caller_buf = TRUE;
20938 	}
20939 
20940 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20941 	cdb.cdb_opaque[1] = usr_cmd;
20942 	FORMG1COUNT(&cdb, data_len);
20943 
20944 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20945 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20946 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
20947 	ucmd_buf.uscsi_buflen	= data_len;
20948 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20949 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20950 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20951 	ucmd_buf.uscsi_timeout	= 60;
20952 
20953 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20954 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20955 
20956 	switch (status) {
20957 	case 0:
20958 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20959 
20960 		break;	/* Success! */
20961 	case EIO:
20962 		switch (ucmd_buf.uscsi_status) {
20963 		case STATUS_RESERVATION_CONFLICT:
20964 			status = EACCES;
20965 			break;
20966 		case STATUS_CHECK:
20967 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20968 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20969 			    KEY_ILLEGAL_REQUEST)) {
20970 				status = ENOTSUP;
20971 			}
20972 			break;
20973 		default:
20974 			break;
20975 		}
20976 		break;
20977 	default:
20978 		break;
20979 	}
20980 
20981 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20982 
20983 	if (no_caller_buf == TRUE) {
20984 		kmem_free(data_bufp, data_len);
20985 	}
20986 
20987 	return (status);
20988 }
20989 
20990 
20991 /*
20992  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20993  *
20994  * Description: This routine is the driver entry point for handling CD-ROM
20995  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20996  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20997  *		device.
20998  *
20999  *   Arguments: ssc  -  ssc contains un - pointer to soft state struct
21000  *                      for the target.
21001  *		usr_cmd SCSI-3 reservation facility command (one of
21002  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
21003  *			SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
21004  *		usr_bufp - user provided pointer register, reserve descriptor or
21005  *			preempt and abort structure (mhioc_register_t,
21006  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
21007  *
21008  * Return Code: 0   - Success
21009  *		EACCES
21010  *		ENOTSUP
21011  *		errno return code from sd_ssc_send()
21012  *
21013  *     Context: Can sleep. Does not return until command is completed.
21014  */
21015 
21016 static int
21017 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
21018 	uchar_t	*usr_bufp)
21019 {
21020 	struct scsi_extended_sense	sense_buf;
21021 	union scsi_cdb		cdb;
21022 	struct uscsi_cmd	ucmd_buf;
21023 	int			status;
21024 	uchar_t			data_len = sizeof (sd_prout_t);
21025 	sd_prout_t		*prp;
21026 	struct sd_lun		*un;
21027 
21028 	ASSERT(ssc != NULL);
21029 	un = ssc->ssc_un;
21030 	ASSERT(un != NULL);
21031 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21032 	ASSERT(data_len == 24);	/* required by scsi spec */
21033 
21034 	SD_TRACE(SD_LOG_IO, un,
21035 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
21036 
21037 	if (usr_bufp == NULL) {
21038 		return (EINVAL);
21039 	}
21040 
21041 	bzero(&cdb, sizeof (cdb));
21042 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21043 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21044 	prp = kmem_zalloc(data_len, KM_SLEEP);
21045 
21046 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
21047 	cdb.cdb_opaque[1] = usr_cmd;
21048 	FORMG1COUNT(&cdb, data_len);
21049 
21050 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21051 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21052 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
21053 	ucmd_buf.uscsi_buflen	= data_len;
21054 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21055 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21056 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21057 	ucmd_buf.uscsi_timeout	= 60;
21058 
21059 	switch (usr_cmd) {
21060 	case SD_SCSI3_REGISTER: {
21061 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
21062 
21063 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21064 		bcopy(ptr->newkey.key, prp->service_key,
21065 		    MHIOC_RESV_KEY_SIZE);
21066 		prp->aptpl = ptr->aptpl;
21067 		break;
21068 	}
21069 	case SD_SCSI3_CLEAR: {
21070 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21071 
21072 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21073 		break;
21074 	}
21075 	case SD_SCSI3_RESERVE:
21076 	case SD_SCSI3_RELEASE: {
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 		prp->scope_address = BE_32(ptr->scope_specific_addr);
21081 		cdb.cdb_opaque[2] = ptr->type;
21082 		break;
21083 	}
21084 	case SD_SCSI3_PREEMPTANDABORT: {
21085 		mhioc_preemptandabort_t *ptr =
21086 		    (mhioc_preemptandabort_t *)usr_bufp;
21087 
21088 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21089 		bcopy(ptr->victim_key.key, prp->service_key,
21090 		    MHIOC_RESV_KEY_SIZE);
21091 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
21092 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
21093 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
21094 		break;
21095 	}
21096 	case SD_SCSI3_REGISTERANDIGNOREKEY:
21097 	{
21098 		mhioc_registerandignorekey_t *ptr;
21099 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
21100 		bcopy(ptr->newkey.key,
21101 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
21102 		prp->aptpl = ptr->aptpl;
21103 		break;
21104 	}
21105 	default:
21106 		ASSERT(FALSE);
21107 		break;
21108 	}
21109 
21110 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21111 	    UIO_SYSSPACE, SD_PATH_STANDARD);
21112 
21113 	switch (status) {
21114 	case 0:
21115 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21116 		break;	/* Success! */
21117 	case EIO:
21118 		switch (ucmd_buf.uscsi_status) {
21119 		case STATUS_RESERVATION_CONFLICT:
21120 			status = EACCES;
21121 			break;
21122 		case STATUS_CHECK:
21123 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21124 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
21125 			    KEY_ILLEGAL_REQUEST)) {
21126 				status = ENOTSUP;
21127 			}
21128 			break;
21129 		default:
21130 			break;
21131 		}
21132 		break;
21133 	default:
21134 		break;
21135 	}
21136 
21137 	kmem_free(prp, data_len);
21138 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
21139 	return (status);
21140 }
21141 
21142 
21143 /*
21144  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
21145  *
21146  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
21147  *
21148  *   Arguments: un - pointer to the target's soft state struct
21149  *              dkc - pointer to the callback structure
21150  *
21151  * Return Code: 0 - success
21152  *		errno-type error code
21153  *
21154  *     Context: kernel thread context only.
21155  *
21156  *  _______________________________________________________________
21157  * | dkc_flag &   | dkc_callback | DKIOCFLUSHWRITECACHE            |
21158  * |FLUSH_VOLATILE|              | operation                       |
21159  * |______________|______________|_________________________________|
21160  * | 0            | NULL         | Synchronous flush on both       |
21161  * |              |              | volatile and non-volatile cache |
21162  * |______________|______________|_________________________________|
21163  * | 1            | NULL         | Synchronous flush on volatile   |
21164  * |              |              | cache; disk drivers may suppress|
21165  * |              |              | flush if disk table indicates   |
21166  * |              |              | non-volatile cache              |
21167  * |______________|______________|_________________________________|
21168  * | 0            | !NULL        | Asynchronous flush on both      |
21169  * |              |              | volatile and non-volatile cache;|
21170  * |______________|______________|_________________________________|
21171  * | 1            | !NULL        | Asynchronous flush on volatile  |
21172  * |              |              | cache; disk drivers may suppress|
21173  * |              |              | flush if disk table indicates   |
21174  * |              |              | non-volatile cache              |
21175  * |______________|______________|_________________________________|
21176  *
21177  */
21178 
21179 static int
21180 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
21181 {
21182 	struct sd_uscsi_info	*uip;
21183 	struct uscsi_cmd	*uscmd;
21184 	union scsi_cdb		*cdb;
21185 	struct buf		*bp;
21186 	int			rval = 0;
21187 	int			is_async;
21188 
21189 	SD_TRACE(SD_LOG_IO, un,
21190 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
21191 
21192 	ASSERT(un != NULL);
21193 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21194 
21195 	if (dkc == NULL || dkc->dkc_callback == NULL) {
21196 		is_async = FALSE;
21197 	} else {
21198 		is_async = TRUE;
21199 	}
21200 
21201 	mutex_enter(SD_MUTEX(un));
21202 	/* check whether cache flush should be suppressed */
21203 	if (un->un_f_suppress_cache_flush == TRUE) {
21204 		mutex_exit(SD_MUTEX(un));
21205 		/*
21206 		 * suppress the cache flush if the device is told to do
21207 		 * so by sd.conf or disk table
21208 		 */
21209 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
21210 		    skip the cache flush since suppress_cache_flush is %d!\n",
21211 		    un->un_f_suppress_cache_flush);
21212 
21213 		if (is_async == TRUE) {
21214 			/* invoke callback for asynchronous flush */
21215 			(*dkc->dkc_callback)(dkc->dkc_cookie, 0);
21216 		}
21217 		return (rval);
21218 	}
21219 	mutex_exit(SD_MUTEX(un));
21220 
21221 	/*
21222 	 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
21223 	 * set properly
21224 	 */
21225 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
21226 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
21227 
21228 	mutex_enter(SD_MUTEX(un));
21229 	if (dkc != NULL && un->un_f_sync_nv_supported &&
21230 	    (dkc->dkc_flag & FLUSH_VOLATILE)) {
21231 		/*
21232 		 * if the device supports SYNC_NV bit, turn on
21233 		 * the SYNC_NV bit to only flush volatile cache
21234 		 */
21235 		cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21236 	}
21237 	mutex_exit(SD_MUTEX(un));
21238 
21239 	/*
21240 	 * First get some memory for the uscsi_cmd struct and cdb
21241 	 * and initialize for SYNCHRONIZE_CACHE cmd.
21242 	 */
21243 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21244 	uscmd->uscsi_cdblen = CDB_GROUP1;
21245 	uscmd->uscsi_cdb = (caddr_t)cdb;
21246 	uscmd->uscsi_bufaddr = NULL;
21247 	uscmd->uscsi_buflen = 0;
21248 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21249 	uscmd->uscsi_rqlen = SENSE_LENGTH;
21250 	uscmd->uscsi_rqresid = SENSE_LENGTH;
21251 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21252 	uscmd->uscsi_timeout = sd_io_time;
21253 
21254 	/*
21255 	 * Allocate an sd_uscsi_info struct and fill it with the info
21256 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
21257 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
21258 	 * since we allocate the buf here in this function, we do not
21259 	 * need to preserve the prior contents of b_private.
21260 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21261 	 */
21262 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21263 	uip->ui_flags = SD_PATH_DIRECT;
21264 	uip->ui_cmdp  = uscmd;
21265 
21266 	bp = getrbuf(KM_SLEEP);
21267 	bp->b_private = uip;
21268 
21269 	/*
21270 	 * Setup buffer to carry uscsi request.
21271 	 */
21272 	bp->b_flags  = B_BUSY;
21273 	bp->b_bcount = 0;
21274 	bp->b_blkno  = 0;
21275 
21276 	if (is_async == TRUE) {
21277 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21278 		uip->ui_dkc = *dkc;
21279 	}
21280 
21281 	bp->b_edev = SD_GET_DEV(un);
21282 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
21283 
21284 	/*
21285 	 * Unset un_f_sync_cache_required flag
21286 	 */
21287 	mutex_enter(SD_MUTEX(un));
21288 	un->un_f_sync_cache_required = FALSE;
21289 	mutex_exit(SD_MUTEX(un));
21290 
21291 	(void) sd_uscsi_strategy(bp);
21292 
21293 	/*
21294 	 * If synchronous request, wait for completion
21295 	 * If async just return and let b_iodone callback
21296 	 * cleanup.
21297 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
21298 	 * but it was also incremented in sd_uscsi_strategy(), so
21299 	 * we should be ok.
21300 	 */
21301 	if (is_async == FALSE) {
21302 		(void) biowait(bp);
21303 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21304 	}
21305 
21306 	return (rval);
21307 }
21308 
21309 
21310 static int
21311 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21312 {
21313 	struct sd_uscsi_info *uip;
21314 	struct uscsi_cmd *uscmd;
21315 	uint8_t *sense_buf;
21316 	struct sd_lun *un;
21317 	int status;
21318 	union scsi_cdb *cdb;
21319 
21320 	uip = (struct sd_uscsi_info *)(bp->b_private);
21321 	ASSERT(uip != NULL);
21322 
21323 	uscmd = uip->ui_cmdp;
21324 	ASSERT(uscmd != NULL);
21325 
21326 	sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21327 	ASSERT(sense_buf != NULL);
21328 
21329 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21330 	ASSERT(un != NULL);
21331 
21332 	cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21333 
21334 	status = geterror(bp);
21335 	switch (status) {
21336 	case 0:
21337 		break;	/* Success! */
21338 	case EIO:
21339 		switch (uscmd->uscsi_status) {
21340 		case STATUS_RESERVATION_CONFLICT:
21341 			/* Ignore reservation conflict */
21342 			status = 0;
21343 			goto done;
21344 
21345 		case STATUS_CHECK:
21346 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21347 			    (scsi_sense_key(sense_buf) ==
21348 			    KEY_ILLEGAL_REQUEST)) {
21349 				/* Ignore Illegal Request error */
21350 				if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21351 					mutex_enter(SD_MUTEX(un));
21352 					un->un_f_sync_nv_supported = FALSE;
21353 					mutex_exit(SD_MUTEX(un));
21354 					status = 0;
21355 					SD_TRACE(SD_LOG_IO, un,
21356 					    "un_f_sync_nv_supported \
21357 					    is set to false.\n");
21358 					goto done;
21359 				}
21360 
21361 				mutex_enter(SD_MUTEX(un));
21362 				un->un_f_sync_cache_supported = FALSE;
21363 				mutex_exit(SD_MUTEX(un));
21364 				SD_TRACE(SD_LOG_IO, un,
21365 				    "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21366 				    un_f_sync_cache_supported set to false \
21367 				    with asc = %x, ascq = %x\n",
21368 				    scsi_sense_asc(sense_buf),
21369 				    scsi_sense_ascq(sense_buf));
21370 				status = ENOTSUP;
21371 				goto done;
21372 			}
21373 			break;
21374 		default:
21375 			break;
21376 		}
21377 		/* FALLTHRU */
21378 	default:
21379 		/*
21380 		 * Turn on the un_f_sync_cache_required flag
21381 		 * since the SYNC CACHE command failed
21382 		 */
21383 		mutex_enter(SD_MUTEX(un));
21384 		un->un_f_sync_cache_required = TRUE;
21385 		mutex_exit(SD_MUTEX(un));
21386 
21387 		/*
21388 		 * Don't log an error message if this device
21389 		 * has removable media.
21390 		 */
21391 		if (!un->un_f_has_removable_media) {
21392 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21393 			    "SYNCHRONIZE CACHE command failed (%d)\n", status);
21394 		}
21395 		break;
21396 	}
21397 
21398 done:
21399 	if (uip->ui_dkc.dkc_callback != NULL) {
21400 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21401 	}
21402 
21403 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
21404 	freerbuf(bp);
21405 	kmem_free(uip, sizeof (struct sd_uscsi_info));
21406 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21407 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21408 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
21409 
21410 	return (status);
21411 }
21412 
21413 
21414 /*
21415  *    Function: sd_send_scsi_GET_CONFIGURATION
21416  *
21417  * Description: Issues the get configuration command to the device.
21418  *		Called from sd_check_for_writable_cd & sd_get_media_info
21419  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21420  *   Arguments: ssc
21421  *		ucmdbuf
21422  *		rqbuf
21423  *		rqbuflen
21424  *		bufaddr
21425  *		buflen
21426  *		path_flag
21427  *
21428  * Return Code: 0   - Success
21429  *		errno return code from sd_ssc_send()
21430  *
21431  *     Context: Can sleep. Does not return until command is completed.
21432  *
21433  */
21434 
21435 static int
21436 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21437 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21438 	int path_flag)
21439 {
21440 	char	cdb[CDB_GROUP1];
21441 	int	status;
21442 	struct sd_lun	*un;
21443 
21444 	ASSERT(ssc != NULL);
21445 	un = ssc->ssc_un;
21446 	ASSERT(un != NULL);
21447 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21448 	ASSERT(bufaddr != NULL);
21449 	ASSERT(ucmdbuf != NULL);
21450 	ASSERT(rqbuf != NULL);
21451 
21452 	SD_TRACE(SD_LOG_IO, un,
21453 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21454 
21455 	bzero(cdb, sizeof (cdb));
21456 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21457 	bzero(rqbuf, rqbuflen);
21458 	bzero(bufaddr, buflen);
21459 
21460 	/*
21461 	 * Set up cdb field for the get configuration command.
21462 	 */
21463 	cdb[0] = SCMD_GET_CONFIGURATION;
21464 	cdb[1] = 0x02;  /* Requested Type */
21465 	cdb[8] = SD_PROFILE_HEADER_LEN;
21466 	ucmdbuf->uscsi_cdb = cdb;
21467 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21468 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21469 	ucmdbuf->uscsi_buflen = buflen;
21470 	ucmdbuf->uscsi_timeout = sd_io_time;
21471 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21472 	ucmdbuf->uscsi_rqlen = rqbuflen;
21473 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21474 
21475 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21476 	    UIO_SYSSPACE, path_flag);
21477 
21478 	switch (status) {
21479 	case 0:
21480 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21481 		break;  /* Success! */
21482 	case EIO:
21483 		switch (ucmdbuf->uscsi_status) {
21484 		case STATUS_RESERVATION_CONFLICT:
21485 			status = EACCES;
21486 			break;
21487 		default:
21488 			break;
21489 		}
21490 		break;
21491 	default:
21492 		break;
21493 	}
21494 
21495 	if (status == 0) {
21496 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21497 		    "sd_send_scsi_GET_CONFIGURATION: data",
21498 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21499 	}
21500 
21501 	SD_TRACE(SD_LOG_IO, un,
21502 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
21503 
21504 	return (status);
21505 }
21506 
21507 /*
21508  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
21509  *
21510  * Description: Issues the get configuration command to the device to
21511  *              retrieve a specific feature. Called from
21512  *		sd_check_for_writable_cd & sd_set_mmc_caps.
21513  *   Arguments: ssc
21514  *              ucmdbuf
21515  *              rqbuf
21516  *              rqbuflen
21517  *              bufaddr
21518  *              buflen
21519  *		feature
21520  *
21521  * Return Code: 0   - Success
21522  *              errno return code from sd_ssc_send()
21523  *
21524  *     Context: Can sleep. Does not return until command is completed.
21525  *
21526  */
21527 static int
21528 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
21529 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
21530 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag)
21531 {
21532 	char    cdb[CDB_GROUP1];
21533 	int	status;
21534 	struct sd_lun	*un;
21535 
21536 	ASSERT(ssc != NULL);
21537 	un = ssc->ssc_un;
21538 	ASSERT(un != NULL);
21539 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21540 	ASSERT(bufaddr != NULL);
21541 	ASSERT(ucmdbuf != NULL);
21542 	ASSERT(rqbuf != NULL);
21543 
21544 	SD_TRACE(SD_LOG_IO, un,
21545 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21546 
21547 	bzero(cdb, sizeof (cdb));
21548 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21549 	bzero(rqbuf, rqbuflen);
21550 	bzero(bufaddr, buflen);
21551 
21552 	/*
21553 	 * Set up cdb field for the get configuration command.
21554 	 */
21555 	cdb[0] = SCMD_GET_CONFIGURATION;
21556 	cdb[1] = 0x02;  /* Requested Type */
21557 	cdb[3] = feature;
21558 	cdb[8] = buflen;
21559 	ucmdbuf->uscsi_cdb = cdb;
21560 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21561 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21562 	ucmdbuf->uscsi_buflen = buflen;
21563 	ucmdbuf->uscsi_timeout = sd_io_time;
21564 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21565 	ucmdbuf->uscsi_rqlen = rqbuflen;
21566 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21567 
21568 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21569 	    UIO_SYSSPACE, path_flag);
21570 
21571 	switch (status) {
21572 	case 0:
21573 
21574 		break;  /* Success! */
21575 	case EIO:
21576 		switch (ucmdbuf->uscsi_status) {
21577 		case STATUS_RESERVATION_CONFLICT:
21578 			status = EACCES;
21579 			break;
21580 		default:
21581 			break;
21582 		}
21583 		break;
21584 	default:
21585 		break;
21586 	}
21587 
21588 	if (status == 0) {
21589 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21590 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
21591 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21592 	}
21593 
21594 	SD_TRACE(SD_LOG_IO, un,
21595 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21596 
21597 	return (status);
21598 }
21599 
21600 
21601 /*
21602  *    Function: sd_send_scsi_MODE_SENSE
21603  *
21604  * Description: Utility function for issuing a scsi MODE SENSE command.
21605  *		Note: This routine uses a consistent implementation for Group0,
21606  *		Group1, and Group2 commands across all platforms. ATAPI devices
21607  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21608  *
21609  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21610  *                      structure for this target.
21611  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21612  *			  CDB_GROUP[1|2] (10 byte).
21613  *		bufaddr - buffer for page data retrieved from the target.
21614  *		buflen - size of page to be retrieved.
21615  *		page_code - page code of data to be retrieved from the target.
21616  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21617  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21618  *			to use the USCSI "direct" chain and bypass the normal
21619  *			command waitq.
21620  *
21621  * Return Code: 0   - Success
21622  *		errno return code from sd_ssc_send()
21623  *
21624  *     Context: Can sleep. Does not return until command is completed.
21625  */
21626 
21627 static int
21628 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21629 	size_t buflen,  uchar_t page_code, int path_flag)
21630 {
21631 	struct	scsi_extended_sense	sense_buf;
21632 	union scsi_cdb		cdb;
21633 	struct uscsi_cmd	ucmd_buf;
21634 	int			status;
21635 	int			headlen;
21636 	struct sd_lun		*un;
21637 
21638 	ASSERT(ssc != NULL);
21639 	un = ssc->ssc_un;
21640 	ASSERT(un != NULL);
21641 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21642 	ASSERT(bufaddr != NULL);
21643 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21644 	    (cdbsize == CDB_GROUP2));
21645 
21646 	SD_TRACE(SD_LOG_IO, un,
21647 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21648 
21649 	bzero(&cdb, sizeof (cdb));
21650 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21651 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21652 	bzero(bufaddr, buflen);
21653 
21654 	if (cdbsize == CDB_GROUP0) {
21655 		cdb.scc_cmd = SCMD_MODE_SENSE;
21656 		cdb.cdb_opaque[2] = page_code;
21657 		FORMG0COUNT(&cdb, buflen);
21658 		headlen = MODE_HEADER_LENGTH;
21659 	} else {
21660 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21661 		cdb.cdb_opaque[2] = page_code;
21662 		FORMG1COUNT(&cdb, buflen);
21663 		headlen = MODE_HEADER_LENGTH_GRP2;
21664 	}
21665 
21666 	ASSERT(headlen <= buflen);
21667 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21668 
21669 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21670 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21671 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21672 	ucmd_buf.uscsi_buflen	= buflen;
21673 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21674 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21675 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21676 	ucmd_buf.uscsi_timeout	= 60;
21677 
21678 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21679 	    UIO_SYSSPACE, path_flag);
21680 
21681 	switch (status) {
21682 	case 0:
21683 		/*
21684 		 * sr_check_wp() uses 0x3f page code and check the header of
21685 		 * mode page to determine if target device is write-protected.
21686 		 * But some USB devices return 0 bytes for 0x3f page code. For
21687 		 * this case, make sure that mode page header is returned at
21688 		 * least.
21689 		 */
21690 		if (buflen - ucmd_buf.uscsi_resid <  headlen) {
21691 			status = EIO;
21692 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21693 			    "mode page header is not returned");
21694 		}
21695 		break;	/* Success! */
21696 	case EIO:
21697 		switch (ucmd_buf.uscsi_status) {
21698 		case STATUS_RESERVATION_CONFLICT:
21699 			status = EACCES;
21700 			break;
21701 		default:
21702 			break;
21703 		}
21704 		break;
21705 	default:
21706 		break;
21707 	}
21708 
21709 	if (status == 0) {
21710 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21711 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21712 	}
21713 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21714 
21715 	return (status);
21716 }
21717 
21718 
21719 /*
21720  *    Function: sd_send_scsi_MODE_SELECT
21721  *
21722  * Description: Utility function for issuing a scsi MODE SELECT command.
21723  *		Note: This routine uses a consistent implementation for Group0,
21724  *		Group1, and Group2 commands across all platforms. ATAPI devices
21725  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21726  *
21727  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21728  *                      structure for this target.
21729  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21730  *			  CDB_GROUP[1|2] (10 byte).
21731  *		bufaddr - buffer for page data retrieved from the target.
21732  *		buflen - size of page to be retrieved.
21733  *		save_page - boolean to determin if SP bit should be set.
21734  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21735  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21736  *			to use the USCSI "direct" chain and bypass the normal
21737  *			command waitq.
21738  *
21739  * Return Code: 0   - Success
21740  *		errno return code from sd_ssc_send()
21741  *
21742  *     Context: Can sleep. Does not return until command is completed.
21743  */
21744 
21745 static int
21746 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21747 	size_t buflen,  uchar_t save_page, int path_flag)
21748 {
21749 	struct	scsi_extended_sense	sense_buf;
21750 	union scsi_cdb		cdb;
21751 	struct uscsi_cmd	ucmd_buf;
21752 	int			status;
21753 	struct sd_lun		*un;
21754 
21755 	ASSERT(ssc != NULL);
21756 	un = ssc->ssc_un;
21757 	ASSERT(un != NULL);
21758 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21759 	ASSERT(bufaddr != NULL);
21760 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21761 	    (cdbsize == CDB_GROUP2));
21762 
21763 	SD_TRACE(SD_LOG_IO, un,
21764 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21765 
21766 	bzero(&cdb, sizeof (cdb));
21767 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21768 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21769 
21770 	/* Set the PF bit for many third party drives */
21771 	cdb.cdb_opaque[1] = 0x10;
21772 
21773 	/* Set the savepage(SP) bit if given */
21774 	if (save_page == SD_SAVE_PAGE) {
21775 		cdb.cdb_opaque[1] |= 0x01;
21776 	}
21777 
21778 	if (cdbsize == CDB_GROUP0) {
21779 		cdb.scc_cmd = SCMD_MODE_SELECT;
21780 		FORMG0COUNT(&cdb, buflen);
21781 	} else {
21782 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21783 		FORMG1COUNT(&cdb, buflen);
21784 	}
21785 
21786 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21787 
21788 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21789 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21790 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21791 	ucmd_buf.uscsi_buflen	= buflen;
21792 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21793 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21794 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21795 	ucmd_buf.uscsi_timeout	= 60;
21796 
21797 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21798 	    UIO_SYSSPACE, path_flag);
21799 
21800 	switch (status) {
21801 	case 0:
21802 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21803 		break;	/* Success! */
21804 	case EIO:
21805 		switch (ucmd_buf.uscsi_status) {
21806 		case STATUS_RESERVATION_CONFLICT:
21807 			status = EACCES;
21808 			break;
21809 		default:
21810 			break;
21811 		}
21812 		break;
21813 	default:
21814 		break;
21815 	}
21816 
21817 	if (status == 0) {
21818 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21819 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21820 	}
21821 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21822 
21823 	return (status);
21824 }
21825 
21826 
21827 /*
21828  *    Function: sd_send_scsi_RDWR
21829  *
21830  * Description: Issue a scsi READ or WRITE command with the given parameters.
21831  *
21832  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21833  *                      structure for this target.
21834  *		cmd:	 SCMD_READ or SCMD_WRITE
21835  *		bufaddr: Address of caller's buffer to receive the RDWR data
21836  *		buflen:  Length of caller's buffer receive the RDWR data.
21837  *		start_block: Block number for the start of the RDWR operation.
21838  *			 (Assumes target-native block size.)
21839  *		residp:  Pointer to variable to receive the redisual of the
21840  *			 RDWR operation (may be NULL of no residual requested).
21841  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21842  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21843  *			to use the USCSI "direct" chain and bypass the normal
21844  *			command waitq.
21845  *
21846  * Return Code: 0   - Success
21847  *		errno return code from sd_ssc_send()
21848  *
21849  *     Context: Can sleep. Does not return until command is completed.
21850  */
21851 
21852 static int
21853 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21854 	size_t buflen, daddr_t start_block, int path_flag)
21855 {
21856 	struct	scsi_extended_sense	sense_buf;
21857 	union scsi_cdb		cdb;
21858 	struct uscsi_cmd	ucmd_buf;
21859 	uint32_t		block_count;
21860 	int			status;
21861 	int			cdbsize;
21862 	uchar_t			flag;
21863 	struct sd_lun		*un;
21864 
21865 	ASSERT(ssc != NULL);
21866 	un = ssc->ssc_un;
21867 	ASSERT(un != NULL);
21868 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21869 	ASSERT(bufaddr != NULL);
21870 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21871 
21872 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21873 
21874 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21875 		return (EINVAL);
21876 	}
21877 
21878 	mutex_enter(SD_MUTEX(un));
21879 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21880 	mutex_exit(SD_MUTEX(un));
21881 
21882 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21883 
21884 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21885 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21886 	    bufaddr, buflen, start_block, block_count);
21887 
21888 	bzero(&cdb, sizeof (cdb));
21889 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21890 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21891 
21892 	/* Compute CDB size to use */
21893 	if (start_block > 0xffffffff)
21894 		cdbsize = CDB_GROUP4;
21895 	else if ((start_block & 0xFFE00000) ||
21896 	    (un->un_f_cfg_is_atapi == TRUE))
21897 		cdbsize = CDB_GROUP1;
21898 	else
21899 		cdbsize = CDB_GROUP0;
21900 
21901 	switch (cdbsize) {
21902 	case CDB_GROUP0:	/* 6-byte CDBs */
21903 		cdb.scc_cmd = cmd;
21904 		FORMG0ADDR(&cdb, start_block);
21905 		FORMG0COUNT(&cdb, block_count);
21906 		break;
21907 	case CDB_GROUP1:	/* 10-byte CDBs */
21908 		cdb.scc_cmd = cmd | SCMD_GROUP1;
21909 		FORMG1ADDR(&cdb, start_block);
21910 		FORMG1COUNT(&cdb, block_count);
21911 		break;
21912 	case CDB_GROUP4:	/* 16-byte CDBs */
21913 		cdb.scc_cmd = cmd | SCMD_GROUP4;
21914 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21915 		FORMG4COUNT(&cdb, block_count);
21916 		break;
21917 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
21918 	default:
21919 		/* All others reserved */
21920 		return (EINVAL);
21921 	}
21922 
21923 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21924 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21925 
21926 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21927 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21928 	ucmd_buf.uscsi_bufaddr	= bufaddr;
21929 	ucmd_buf.uscsi_buflen	= buflen;
21930 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21931 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21932 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
21933 	ucmd_buf.uscsi_timeout	= 60;
21934 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21935 	    UIO_SYSSPACE, path_flag);
21936 
21937 	switch (status) {
21938 	case 0:
21939 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21940 		break;	/* Success! */
21941 	case EIO:
21942 		switch (ucmd_buf.uscsi_status) {
21943 		case STATUS_RESERVATION_CONFLICT:
21944 			status = EACCES;
21945 			break;
21946 		default:
21947 			break;
21948 		}
21949 		break;
21950 	default:
21951 		break;
21952 	}
21953 
21954 	if (status == 0) {
21955 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21956 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21957 	}
21958 
21959 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21960 
21961 	return (status);
21962 }
21963 
21964 
21965 /*
21966  *    Function: sd_send_scsi_LOG_SENSE
21967  *
21968  * Description: Issue a scsi LOG_SENSE command with the given parameters.
21969  *
21970  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21971  *                      structure for this target.
21972  *
21973  * Return Code: 0   - Success
21974  *		errno return code from sd_ssc_send()
21975  *
21976  *     Context: Can sleep. Does not return until command is completed.
21977  */
21978 
21979 static int
21980 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21981 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
21982 	int path_flag)
21983 
21984 {
21985 	struct scsi_extended_sense	sense_buf;
21986 	union scsi_cdb		cdb;
21987 	struct uscsi_cmd	ucmd_buf;
21988 	int			status;
21989 	struct sd_lun		*un;
21990 
21991 	ASSERT(ssc != NULL);
21992 	un = ssc->ssc_un;
21993 	ASSERT(un != NULL);
21994 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21995 
21996 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21997 
21998 	bzero(&cdb, sizeof (cdb));
21999 	bzero(&ucmd_buf, sizeof (ucmd_buf));
22000 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
22001 
22002 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
22003 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
22004 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
22005 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
22006 	FORMG1COUNT(&cdb, buflen);
22007 
22008 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22009 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
22010 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
22011 	ucmd_buf.uscsi_buflen	= buflen;
22012 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
22013 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
22014 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
22015 	ucmd_buf.uscsi_timeout	= 60;
22016 
22017 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22018 	    UIO_SYSSPACE, path_flag);
22019 
22020 	switch (status) {
22021 	case 0:
22022 		break;
22023 	case EIO:
22024 		switch (ucmd_buf.uscsi_status) {
22025 		case STATUS_RESERVATION_CONFLICT:
22026 			status = EACCES;
22027 			break;
22028 		case STATUS_CHECK:
22029 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
22030 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
22031 				KEY_ILLEGAL_REQUEST) &&
22032 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
22033 				/*
22034 				 * ASC 0x24: INVALID FIELD IN CDB
22035 				 */
22036 				switch (page_code) {
22037 				case START_STOP_CYCLE_PAGE:
22038 					/*
22039 					 * The start stop cycle counter is
22040 					 * implemented as page 0x31 in earlier
22041 					 * generation disks. In new generation
22042 					 * disks the start stop cycle counter is
22043 					 * implemented as page 0xE. To properly
22044 					 * handle this case if an attempt for
22045 					 * log page 0xE is made and fails we
22046 					 * will try again using page 0x31.
22047 					 *
22048 					 * Network storage BU committed to
22049 					 * maintain the page 0x31 for this
22050 					 * purpose and will not have any other
22051 					 * page implemented with page code 0x31
22052 					 * until all disks transition to the
22053 					 * standard page.
22054 					 */
22055 					mutex_enter(SD_MUTEX(un));
22056 					un->un_start_stop_cycle_page =
22057 					    START_STOP_CYCLE_VU_PAGE;
22058 					cdb.cdb_opaque[2] =
22059 					    (char)(page_control << 6) |
22060 					    un->un_start_stop_cycle_page;
22061 					mutex_exit(SD_MUTEX(un));
22062 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22063 					status = sd_ssc_send(
22064 					    ssc, &ucmd_buf, FKIOCTL,
22065 					    UIO_SYSSPACE, path_flag);
22066 
22067 					break;
22068 				case TEMPERATURE_PAGE:
22069 					status = ENOTTY;
22070 					break;
22071 				default:
22072 					break;
22073 				}
22074 			}
22075 			break;
22076 		default:
22077 			break;
22078 		}
22079 		break;
22080 	default:
22081 		break;
22082 	}
22083 
22084 	if (status == 0) {
22085 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22086 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
22087 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22088 	}
22089 
22090 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
22091 
22092 	return (status);
22093 }
22094 
22095 
22096 /*
22097  *    Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
22098  *
22099  * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
22100  *
22101  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22102  *                      structure for this target.
22103  *		bufaddr
22104  *		buflen
22105  *		class_req
22106  *
22107  * Return Code: 0   - Success
22108  *		errno return code from sd_ssc_send()
22109  *
22110  *     Context: Can sleep. Does not return until command is completed.
22111  */
22112 
22113 static int
22114 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
22115 	size_t buflen, uchar_t class_req)
22116 {
22117 	union scsi_cdb		cdb;
22118 	struct uscsi_cmd	ucmd_buf;
22119 	int			status;
22120 	struct sd_lun		*un;
22121 
22122 	ASSERT(ssc != NULL);
22123 	un = ssc->ssc_un;
22124 	ASSERT(un != NULL);
22125 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22126 	ASSERT(bufaddr != NULL);
22127 
22128 	SD_TRACE(SD_LOG_IO, un,
22129 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22130 
22131 	bzero(&cdb, sizeof (cdb));
22132 	bzero(&ucmd_buf, sizeof (ucmd_buf));
22133 	bzero(bufaddr, buflen);
22134 
22135 	cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22136 	cdb.cdb_opaque[1] = 1; /* polled */
22137 	cdb.cdb_opaque[4] = class_req;
22138 	FORMG1COUNT(&cdb, buflen);
22139 
22140 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22141 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
22142 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
22143 	ucmd_buf.uscsi_buflen	= buflen;
22144 	ucmd_buf.uscsi_rqbuf	= NULL;
22145 	ucmd_buf.uscsi_rqlen	= 0;
22146 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
22147 	ucmd_buf.uscsi_timeout	= 60;
22148 
22149 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22150 	    UIO_SYSSPACE, SD_PATH_DIRECT);
22151 
22152 	/*
22153 	 * Only handle status == 0, the upper-level caller
22154 	 * will put different assessment based on the context.
22155 	 */
22156 	if (status == 0) {
22157 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22158 
22159 		if (ucmd_buf.uscsi_resid != 0) {
22160 			status = EIO;
22161 		}
22162 	}
22163 
22164 	SD_TRACE(SD_LOG_IO, un,
22165 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22166 
22167 	return (status);
22168 }
22169 
22170 
22171 static boolean_t
22172 sd_gesn_media_data_valid(uchar_t *data)
22173 {
22174 	uint16_t			len;
22175 
22176 	len = (data[1] << 8) | data[0];
22177 	return ((len >= 6) &&
22178 	    ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
22179 	    ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
22180 	    ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
22181 }
22182 
22183 
22184 /*
22185  *    Function: sdioctl
22186  *
22187  * Description: Driver's ioctl(9e) entry point function.
22188  *
22189  *   Arguments: dev     - device number
22190  *		cmd     - ioctl operation to be performed
22191  *		arg     - user argument, contains data to be set or reference
22192  *			  parameter for get
22193  *		flag    - bit flag, indicating open settings, 32/64 bit type
22194  *		cred_p  - user credential pointer
22195  *		rval_p  - calling process return value (OPT)
22196  *
22197  * Return Code: EINVAL
22198  *		ENOTTY
22199  *		ENXIO
22200  *		EIO
22201  *		EFAULT
22202  *		ENOTSUP
22203  *		EPERM
22204  *
22205  *     Context: Called from the device switch at normal priority.
22206  */
22207 
22208 static int
22209 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22210 {
22211 	struct sd_lun	*un = NULL;
22212 	int		err = 0;
22213 	int		i = 0;
22214 	cred_t		*cr;
22215 	int		tmprval = EINVAL;
22216 	boolean_t	is_valid;
22217 	sd_ssc_t	*ssc;
22218 
22219 	/*
22220 	 * All device accesses go thru sdstrategy where we check on suspend
22221 	 * status
22222 	 */
22223 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22224 		return (ENXIO);
22225 	}
22226 
22227 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22228 
22229 	/* Initialize sd_ssc_t for internal uscsi commands */
22230 	ssc = sd_ssc_init(un);
22231 
22232 	is_valid = SD_IS_VALID_LABEL(un);
22233 
22234 	/*
22235 	 * Moved this wait from sd_uscsi_strategy to here for
22236 	 * reasons of deadlock prevention. Internal driver commands,
22237 	 * specifically those to change a devices power level, result
22238 	 * in a call to sd_uscsi_strategy.
22239 	 */
22240 	mutex_enter(SD_MUTEX(un));
22241 	while ((un->un_state == SD_STATE_SUSPENDED) ||
22242 	    (un->un_state == SD_STATE_PM_CHANGING)) {
22243 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22244 	}
22245 	/*
22246 	 * Twiddling the counter here protects commands from now
22247 	 * through to the top of sd_uscsi_strategy. Without the
22248 	 * counter inc. a power down, for example, could get in
22249 	 * after the above check for state is made and before
22250 	 * execution gets to the top of sd_uscsi_strategy.
22251 	 * That would cause problems.
22252 	 */
22253 	un->un_ncmds_in_driver++;
22254 
22255 	if (!is_valid &&
22256 	    (flag & (FNDELAY | FNONBLOCK))) {
22257 		switch (cmd) {
22258 		case DKIOCGGEOM:	/* SD_PATH_DIRECT */
22259 		case DKIOCGVTOC:
22260 		case DKIOCGEXTVTOC:
22261 		case DKIOCGAPART:
22262 		case DKIOCPARTINFO:
22263 		case DKIOCEXTPARTINFO:
22264 		case DKIOCSGEOM:
22265 		case DKIOCSAPART:
22266 		case DKIOCGETEFI:
22267 		case DKIOCPARTITION:
22268 		case DKIOCSVTOC:
22269 		case DKIOCSEXTVTOC:
22270 		case DKIOCSETEFI:
22271 		case DKIOCGMBOOT:
22272 		case DKIOCSMBOOT:
22273 		case DKIOCG_PHYGEOM:
22274 		case DKIOCG_VIRTGEOM:
22275 #if defined(__i386) || defined(__amd64)
22276 		case DKIOCSETEXTPART:
22277 #endif
22278 			/* let cmlb handle it */
22279 			goto skip_ready_valid;
22280 
22281 		case CDROMPAUSE:
22282 		case CDROMRESUME:
22283 		case CDROMPLAYMSF:
22284 		case CDROMPLAYTRKIND:
22285 		case CDROMREADTOCHDR:
22286 		case CDROMREADTOCENTRY:
22287 		case CDROMSTOP:
22288 		case CDROMSTART:
22289 		case CDROMVOLCTRL:
22290 		case CDROMSUBCHNL:
22291 		case CDROMREADMODE2:
22292 		case CDROMREADMODE1:
22293 		case CDROMREADOFFSET:
22294 		case CDROMSBLKMODE:
22295 		case CDROMGBLKMODE:
22296 		case CDROMGDRVSPEED:
22297 		case CDROMSDRVSPEED:
22298 		case CDROMCDDA:
22299 		case CDROMCDXA:
22300 		case CDROMSUBCODE:
22301 			if (!ISCD(un)) {
22302 				un->un_ncmds_in_driver--;
22303 				ASSERT(un->un_ncmds_in_driver >= 0);
22304 				mutex_exit(SD_MUTEX(un));
22305 				err = ENOTTY;
22306 				goto done_without_assess;
22307 			}
22308 			break;
22309 		case FDEJECT:
22310 		case DKIOCEJECT:
22311 		case CDROMEJECT:
22312 			if (!un->un_f_eject_media_supported) {
22313 				un->un_ncmds_in_driver--;
22314 				ASSERT(un->un_ncmds_in_driver >= 0);
22315 				mutex_exit(SD_MUTEX(un));
22316 				err = ENOTTY;
22317 				goto done_without_assess;
22318 			}
22319 			break;
22320 		case DKIOCFLUSHWRITECACHE:
22321 			mutex_exit(SD_MUTEX(un));
22322 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22323 			if (err != 0) {
22324 				mutex_enter(SD_MUTEX(un));
22325 				un->un_ncmds_in_driver--;
22326 				ASSERT(un->un_ncmds_in_driver >= 0);
22327 				mutex_exit(SD_MUTEX(un));
22328 				err = EIO;
22329 				goto done_quick_assess;
22330 			}
22331 			mutex_enter(SD_MUTEX(un));
22332 			/* FALLTHROUGH */
22333 		case DKIOCREMOVABLE:
22334 		case DKIOCHOTPLUGGABLE:
22335 		case DKIOCINFO:
22336 		case DKIOCGMEDIAINFO:
22337 		case DKIOCGMEDIAINFOEXT:
22338 		case DKIOCSOLIDSTATE:
22339 		case MHIOCENFAILFAST:
22340 		case MHIOCSTATUS:
22341 		case MHIOCTKOWN:
22342 		case MHIOCRELEASE:
22343 		case MHIOCGRP_INKEYS:
22344 		case MHIOCGRP_INRESV:
22345 		case MHIOCGRP_REGISTER:
22346 		case MHIOCGRP_CLEAR:
22347 		case MHIOCGRP_RESERVE:
22348 		case MHIOCGRP_PREEMPTANDABORT:
22349 		case MHIOCGRP_REGISTERANDIGNOREKEY:
22350 		case CDROMCLOSETRAY:
22351 		case USCSICMD:
22352 			goto skip_ready_valid;
22353 		default:
22354 			break;
22355 		}
22356 
22357 		mutex_exit(SD_MUTEX(un));
22358 		err = sd_ready_and_valid(ssc, SDPART(dev));
22359 		mutex_enter(SD_MUTEX(un));
22360 
22361 		if (err != SD_READY_VALID) {
22362 			switch (cmd) {
22363 			case DKIOCSTATE:
22364 			case CDROMGDRVSPEED:
22365 			case CDROMSDRVSPEED:
22366 			case FDEJECT:	/* for eject command */
22367 			case DKIOCEJECT:
22368 			case CDROMEJECT:
22369 			case DKIOCREMOVABLE:
22370 			case DKIOCHOTPLUGGABLE:
22371 				break;
22372 			default:
22373 				if (un->un_f_has_removable_media) {
22374 					err = ENXIO;
22375 				} else {
22376 				/* Do not map SD_RESERVED_BY_OTHERS to EIO */
22377 					if (err == SD_RESERVED_BY_OTHERS) {
22378 						err = EACCES;
22379 					} else {
22380 						err = EIO;
22381 					}
22382 				}
22383 				un->un_ncmds_in_driver--;
22384 				ASSERT(un->un_ncmds_in_driver >= 0);
22385 				mutex_exit(SD_MUTEX(un));
22386 
22387 				goto done_without_assess;
22388 			}
22389 		}
22390 	}
22391 
22392 skip_ready_valid:
22393 	mutex_exit(SD_MUTEX(un));
22394 
22395 	switch (cmd) {
22396 	case DKIOCINFO:
22397 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22398 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22399 		break;
22400 
22401 	case DKIOCGMEDIAINFO:
22402 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22403 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
22404 		break;
22405 
22406 	case DKIOCGMEDIAINFOEXT:
22407 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22408 		err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22409 		break;
22410 
22411 	case DKIOCGGEOM:
22412 	case DKIOCGVTOC:
22413 	case DKIOCGEXTVTOC:
22414 	case DKIOCGAPART:
22415 	case DKIOCPARTINFO:
22416 	case DKIOCEXTPARTINFO:
22417 	case DKIOCSGEOM:
22418 	case DKIOCSAPART:
22419 	case DKIOCGETEFI:
22420 	case DKIOCPARTITION:
22421 	case DKIOCSVTOC:
22422 	case DKIOCSEXTVTOC:
22423 	case DKIOCSETEFI:
22424 	case DKIOCGMBOOT:
22425 	case DKIOCSMBOOT:
22426 	case DKIOCG_PHYGEOM:
22427 	case DKIOCG_VIRTGEOM:
22428 #if defined(__i386) || defined(__amd64)
22429 	case DKIOCSETEXTPART:
22430 #endif
22431 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22432 
22433 		/* TUR should spin up */
22434 
22435 		if (un->un_f_has_removable_media)
22436 			err = sd_send_scsi_TEST_UNIT_READY(ssc,
22437 			    SD_CHECK_FOR_MEDIA);
22438 
22439 		else
22440 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22441 
22442 		if (err != 0)
22443 			goto done_with_assess;
22444 
22445 		err = cmlb_ioctl(un->un_cmlbhandle, dev,
22446 		    cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22447 
22448 		if ((err == 0) &&
22449 		    ((cmd == DKIOCSETEFI) ||
22450 		    (un->un_f_pkstats_enabled) &&
22451 		    (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22452 		    cmd == DKIOCSEXTVTOC))) {
22453 
22454 			tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22455 			    (void *)SD_PATH_DIRECT);
22456 			if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22457 				sd_set_pstats(un);
22458 				SD_TRACE(SD_LOG_IO_PARTITION, un,
22459 				    "sd_ioctl: un:0x%p pstats created and "
22460 				    "set\n", un);
22461 			}
22462 		}
22463 
22464 		if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22465 		    ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22466 
22467 			mutex_enter(SD_MUTEX(un));
22468 			if (un->un_f_devid_supported &&
22469 			    (un->un_f_opt_fab_devid == TRUE)) {
22470 				if (un->un_devid == NULL) {
22471 					sd_register_devid(ssc, SD_DEVINFO(un),
22472 					    SD_TARGET_IS_UNRESERVED);
22473 				} else {
22474 					/*
22475 					 * The device id for this disk
22476 					 * has been fabricated. The
22477 					 * device id must be preserved
22478 					 * by writing it back out to
22479 					 * disk.
22480 					 */
22481 					if (sd_write_deviceid(ssc) != 0) {
22482 						ddi_devid_free(un->un_devid);
22483 						un->un_devid = NULL;
22484 					}
22485 				}
22486 			}
22487 			mutex_exit(SD_MUTEX(un));
22488 		}
22489 
22490 		break;
22491 
22492 	case DKIOCLOCK:
22493 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22494 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22495 		    SD_PATH_STANDARD);
22496 		goto done_with_assess;
22497 
22498 	case DKIOCUNLOCK:
22499 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22500 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22501 		    SD_PATH_STANDARD);
22502 		goto done_with_assess;
22503 
22504 	case DKIOCSTATE: {
22505 		enum dkio_state		state;
22506 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22507 
22508 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22509 			err = EFAULT;
22510 		} else {
22511 			err = sd_check_media(dev, state);
22512 			if (err == 0) {
22513 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
22514 				    sizeof (int), flag) != 0)
22515 					err = EFAULT;
22516 			}
22517 		}
22518 		break;
22519 	}
22520 
22521 	case DKIOCREMOVABLE:
22522 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22523 		i = un->un_f_has_removable_media ? 1 : 0;
22524 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22525 			err = EFAULT;
22526 		} else {
22527 			err = 0;
22528 		}
22529 		break;
22530 
22531 	case DKIOCSOLIDSTATE:
22532 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSOLIDSTATE\n");
22533 		i = un->un_f_is_solid_state ? 1 : 0;
22534 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22535 			err = EFAULT;
22536 		} else {
22537 			err = 0;
22538 		}
22539 		break;
22540 
22541 	case DKIOCHOTPLUGGABLE:
22542 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22543 		i = un->un_f_is_hotpluggable ? 1 : 0;
22544 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22545 			err = EFAULT;
22546 		} else {
22547 			err = 0;
22548 		}
22549 		break;
22550 
22551 	case DKIOCREADONLY:
22552 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
22553 		i = 0;
22554 		if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
22555 		    (sr_check_wp(dev) != 0)) {
22556 			i = 1;
22557 		}
22558 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22559 			err = EFAULT;
22560 		} else {
22561 			err = 0;
22562 		}
22563 		break;
22564 
22565 	case DKIOCGTEMPERATURE:
22566 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22567 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22568 		break;
22569 
22570 	case MHIOCENFAILFAST:
22571 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22572 		if ((err = drv_priv(cred_p)) == 0) {
22573 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22574 		}
22575 		break;
22576 
22577 	case MHIOCTKOWN:
22578 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22579 		if ((err = drv_priv(cred_p)) == 0) {
22580 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22581 		}
22582 		break;
22583 
22584 	case MHIOCRELEASE:
22585 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22586 		if ((err = drv_priv(cred_p)) == 0) {
22587 			err = sd_mhdioc_release(dev);
22588 		}
22589 		break;
22590 
22591 	case MHIOCSTATUS:
22592 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22593 		if ((err = drv_priv(cred_p)) == 0) {
22594 			switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22595 			case 0:
22596 				err = 0;
22597 				break;
22598 			case EACCES:
22599 				*rval_p = 1;
22600 				err = 0;
22601 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22602 				break;
22603 			default:
22604 				err = EIO;
22605 				goto done_with_assess;
22606 			}
22607 		}
22608 		break;
22609 
22610 	case MHIOCQRESERVE:
22611 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22612 		if ((err = drv_priv(cred_p)) == 0) {
22613 			err = sd_reserve_release(dev, SD_RESERVE);
22614 		}
22615 		break;
22616 
22617 	case MHIOCREREGISTERDEVID:
22618 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22619 		if (drv_priv(cred_p) == EPERM) {
22620 			err = EPERM;
22621 		} else if (!un->un_f_devid_supported) {
22622 			err = ENOTTY;
22623 		} else {
22624 			err = sd_mhdioc_register_devid(dev);
22625 		}
22626 		break;
22627 
22628 	case MHIOCGRP_INKEYS:
22629 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22630 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22631 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22632 				err = ENOTSUP;
22633 			} else {
22634 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22635 				    flag);
22636 			}
22637 		}
22638 		break;
22639 
22640 	case MHIOCGRP_INRESV:
22641 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22642 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22643 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22644 				err = ENOTSUP;
22645 			} else {
22646 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22647 			}
22648 		}
22649 		break;
22650 
22651 	case MHIOCGRP_REGISTER:
22652 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22653 		if ((err = drv_priv(cred_p)) != EPERM) {
22654 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22655 				err = ENOTSUP;
22656 			} else if (arg != NULL) {
22657 				mhioc_register_t reg;
22658 				if (ddi_copyin((void *)arg, &reg,
22659 				    sizeof (mhioc_register_t), flag) != 0) {
22660 					err = EFAULT;
22661 				} else {
22662 					err =
22663 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22664 					    ssc, SD_SCSI3_REGISTER,
22665 					    (uchar_t *)&reg);
22666 					if (err != 0)
22667 						goto done_with_assess;
22668 				}
22669 			}
22670 		}
22671 		break;
22672 
22673 	case MHIOCGRP_CLEAR:
22674 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
22675 		if ((err = drv_priv(cred_p)) != EPERM) {
22676 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22677 				err = ENOTSUP;
22678 			} else if (arg != NULL) {
22679 				mhioc_register_t reg;
22680 				if (ddi_copyin((void *)arg, &reg,
22681 				    sizeof (mhioc_register_t), flag) != 0) {
22682 					err = EFAULT;
22683 				} else {
22684 					err =
22685 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22686 					    ssc, SD_SCSI3_CLEAR,
22687 					    (uchar_t *)&reg);
22688 					if (err != 0)
22689 						goto done_with_assess;
22690 				}
22691 			}
22692 		}
22693 		break;
22694 
22695 	case MHIOCGRP_RESERVE:
22696 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22697 		if ((err = drv_priv(cred_p)) != EPERM) {
22698 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22699 				err = ENOTSUP;
22700 			} else if (arg != NULL) {
22701 				mhioc_resv_desc_t resv_desc;
22702 				if (ddi_copyin((void *)arg, &resv_desc,
22703 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
22704 					err = EFAULT;
22705 				} else {
22706 					err =
22707 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22708 					    ssc, SD_SCSI3_RESERVE,
22709 					    (uchar_t *)&resv_desc);
22710 					if (err != 0)
22711 						goto done_with_assess;
22712 				}
22713 			}
22714 		}
22715 		break;
22716 
22717 	case MHIOCGRP_PREEMPTANDABORT:
22718 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22719 		if ((err = drv_priv(cred_p)) != EPERM) {
22720 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22721 				err = ENOTSUP;
22722 			} else if (arg != NULL) {
22723 				mhioc_preemptandabort_t preempt_abort;
22724 				if (ddi_copyin((void *)arg, &preempt_abort,
22725 				    sizeof (mhioc_preemptandabort_t),
22726 				    flag) != 0) {
22727 					err = EFAULT;
22728 				} else {
22729 					err =
22730 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22731 					    ssc, SD_SCSI3_PREEMPTANDABORT,
22732 					    (uchar_t *)&preempt_abort);
22733 					if (err != 0)
22734 						goto done_with_assess;
22735 				}
22736 			}
22737 		}
22738 		break;
22739 
22740 	case MHIOCGRP_REGISTERANDIGNOREKEY:
22741 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22742 		if ((err = drv_priv(cred_p)) != EPERM) {
22743 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22744 				err = ENOTSUP;
22745 			} else if (arg != NULL) {
22746 				mhioc_registerandignorekey_t r_and_i;
22747 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
22748 				    sizeof (mhioc_registerandignorekey_t),
22749 				    flag) != 0) {
22750 					err = EFAULT;
22751 				} else {
22752 					err =
22753 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22754 					    ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22755 					    (uchar_t *)&r_and_i);
22756 					if (err != 0)
22757 						goto done_with_assess;
22758 				}
22759 			}
22760 		}
22761 		break;
22762 
22763 	case USCSICMD:
22764 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22765 		cr = ddi_get_cred();
22766 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22767 			err = EPERM;
22768 		} else {
22769 			enum uio_seg	uioseg;
22770 
22771 			uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22772 			    UIO_USERSPACE;
22773 			if (un->un_f_format_in_progress == TRUE) {
22774 				err = EAGAIN;
22775 				break;
22776 			}
22777 
22778 			err = sd_ssc_send(ssc,
22779 			    (struct uscsi_cmd *)arg,
22780 			    flag, uioseg, SD_PATH_STANDARD);
22781 			if (err != 0)
22782 				goto done_with_assess;
22783 			else
22784 				sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22785 		}
22786 		break;
22787 
22788 	case CDROMPAUSE:
22789 	case CDROMRESUME:
22790 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22791 		if (!ISCD(un)) {
22792 			err = ENOTTY;
22793 		} else {
22794 			err = sr_pause_resume(dev, cmd);
22795 		}
22796 		break;
22797 
22798 	case CDROMPLAYMSF:
22799 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22800 		if (!ISCD(un)) {
22801 			err = ENOTTY;
22802 		} else {
22803 			err = sr_play_msf(dev, (caddr_t)arg, flag);
22804 		}
22805 		break;
22806 
22807 	case CDROMPLAYTRKIND:
22808 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22809 #if defined(__i386) || defined(__amd64)
22810 		/*
22811 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22812 		 */
22813 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22814 #else
22815 		if (!ISCD(un)) {
22816 #endif
22817 			err = ENOTTY;
22818 		} else {
22819 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
22820 		}
22821 		break;
22822 
22823 	case CDROMREADTOCHDR:
22824 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22825 		if (!ISCD(un)) {
22826 			err = ENOTTY;
22827 		} else {
22828 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22829 		}
22830 		break;
22831 
22832 	case CDROMREADTOCENTRY:
22833 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22834 		if (!ISCD(un)) {
22835 			err = ENOTTY;
22836 		} else {
22837 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22838 		}
22839 		break;
22840 
22841 	case CDROMSTOP:
22842 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22843 		if (!ISCD(un)) {
22844 			err = ENOTTY;
22845 		} else {
22846 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22847 			    SD_TARGET_STOP, SD_PATH_STANDARD);
22848 			goto done_with_assess;
22849 		}
22850 		break;
22851 
22852 	case CDROMSTART:
22853 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22854 		if (!ISCD(un)) {
22855 			err = ENOTTY;
22856 		} else {
22857 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22858 			    SD_TARGET_START, SD_PATH_STANDARD);
22859 			goto done_with_assess;
22860 		}
22861 		break;
22862 
22863 	case CDROMCLOSETRAY:
22864 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22865 		if (!ISCD(un)) {
22866 			err = ENOTTY;
22867 		} else {
22868 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22869 			    SD_TARGET_CLOSE, SD_PATH_STANDARD);
22870 			goto done_with_assess;
22871 		}
22872 		break;
22873 
22874 	case FDEJECT:	/* for eject command */
22875 	case DKIOCEJECT:
22876 	case CDROMEJECT:
22877 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22878 		if (!un->un_f_eject_media_supported) {
22879 			err = ENOTTY;
22880 		} else {
22881 			err = sr_eject(dev);
22882 		}
22883 		break;
22884 
22885 	case CDROMVOLCTRL:
22886 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22887 		if (!ISCD(un)) {
22888 			err = ENOTTY;
22889 		} else {
22890 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22891 		}
22892 		break;
22893 
22894 	case CDROMSUBCHNL:
22895 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22896 		if (!ISCD(un)) {
22897 			err = ENOTTY;
22898 		} else {
22899 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22900 		}
22901 		break;
22902 
22903 	case CDROMREADMODE2:
22904 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22905 		if (!ISCD(un)) {
22906 			err = ENOTTY;
22907 		} else if (un->un_f_cfg_is_atapi == TRUE) {
22908 			/*
22909 			 * If the drive supports READ CD, use that instead of
22910 			 * switching the LBA size via a MODE SELECT
22911 			 * Block Descriptor
22912 			 */
22913 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22914 		} else {
22915 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
22916 		}
22917 		break;
22918 
22919 	case CDROMREADMODE1:
22920 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22921 		if (!ISCD(un)) {
22922 			err = ENOTTY;
22923 		} else {
22924 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
22925 		}
22926 		break;
22927 
22928 	case CDROMREADOFFSET:
22929 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22930 		if (!ISCD(un)) {
22931 			err = ENOTTY;
22932 		} else {
22933 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22934 			    flag);
22935 		}
22936 		break;
22937 
22938 	case CDROMSBLKMODE:
22939 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22940 		/*
22941 		 * There is no means of changing block size in case of atapi
22942 		 * drives, thus return ENOTTY if drive type is atapi
22943 		 */
22944 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22945 			err = ENOTTY;
22946 		} else if (un->un_f_mmc_cap == TRUE) {
22947 
22948 			/*
22949 			 * MMC Devices do not support changing the
22950 			 * logical block size
22951 			 *
22952 			 * Note: EINVAL is being returned instead of ENOTTY to
22953 			 * maintain consistancy with the original mmc
22954 			 * driver update.
22955 			 */
22956 			err = EINVAL;
22957 		} else {
22958 			mutex_enter(SD_MUTEX(un));
22959 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22960 			    (un->un_ncmds_in_transport > 0)) {
22961 				mutex_exit(SD_MUTEX(un));
22962 				err = EINVAL;
22963 			} else {
22964 				mutex_exit(SD_MUTEX(un));
22965 				err = sr_change_blkmode(dev, cmd, arg, flag);
22966 			}
22967 		}
22968 		break;
22969 
22970 	case CDROMGBLKMODE:
22971 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22972 		if (!ISCD(un)) {
22973 			err = ENOTTY;
22974 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
22975 		    (un->un_f_blockcount_is_valid != FALSE)) {
22976 			/*
22977 			 * Drive is an ATAPI drive so return target block
22978 			 * size for ATAPI drives since we cannot change the
22979 			 * blocksize on ATAPI drives. Used primarily to detect
22980 			 * if an ATAPI cdrom is present.
22981 			 */
22982 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22983 			    sizeof (int), flag) != 0) {
22984 				err = EFAULT;
22985 			} else {
22986 				err = 0;
22987 			}
22988 
22989 		} else {
22990 			/*
22991 			 * Drive supports changing block sizes via a Mode
22992 			 * Select.
22993 			 */
22994 			err = sr_change_blkmode(dev, cmd, arg, flag);
22995 		}
22996 		break;
22997 
22998 	case CDROMGDRVSPEED:
22999 	case CDROMSDRVSPEED:
23000 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
23001 		if (!ISCD(un)) {
23002 			err = ENOTTY;
23003 		} else if (un->un_f_mmc_cap == TRUE) {
23004 			/*
23005 			 * Note: In the future the driver implementation
23006 			 * for getting and
23007 			 * setting cd speed should entail:
23008 			 * 1) If non-mmc try the Toshiba mode page
23009 			 *    (sr_change_speed)
23010 			 * 2) If mmc but no support for Real Time Streaming try
23011 			 *    the SET CD SPEED (0xBB) command
23012 			 *   (sr_atapi_change_speed)
23013 			 * 3) If mmc and support for Real Time Streaming
23014 			 *    try the GET PERFORMANCE and SET STREAMING
23015 			 *    commands (not yet implemented, 4380808)
23016 			 */
23017 			/*
23018 			 * As per recent MMC spec, CD-ROM speed is variable
23019 			 * and changes with LBA. Since there is no such
23020 			 * things as drive speed now, fail this ioctl.
23021 			 *
23022 			 * Note: EINVAL is returned for consistancy of original
23023 			 * implementation which included support for getting
23024 			 * the drive speed of mmc devices but not setting
23025 			 * the drive speed. Thus EINVAL would be returned
23026 			 * if a set request was made for an mmc device.
23027 			 * We no longer support get or set speed for
23028 			 * mmc but need to remain consistent with regard
23029 			 * to the error code returned.
23030 			 */
23031 			err = EINVAL;
23032 		} else if (un->un_f_cfg_is_atapi == TRUE) {
23033 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
23034 		} else {
23035 			err = sr_change_speed(dev, cmd, arg, flag);
23036 		}
23037 		break;
23038 
23039 	case CDROMCDDA:
23040 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
23041 		if (!ISCD(un)) {
23042 			err = ENOTTY;
23043 		} else {
23044 			err = sr_read_cdda(dev, (void *)arg, flag);
23045 		}
23046 		break;
23047 
23048 	case CDROMCDXA:
23049 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
23050 		if (!ISCD(un)) {
23051 			err = ENOTTY;
23052 		} else {
23053 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
23054 		}
23055 		break;
23056 
23057 	case CDROMSUBCODE:
23058 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
23059 		if (!ISCD(un)) {
23060 			err = ENOTTY;
23061 		} else {
23062 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
23063 		}
23064 		break;
23065 
23066 
23067 #ifdef SDDEBUG
23068 /* RESET/ABORTS testing ioctls */
23069 	case DKIOCRESET: {
23070 		int	reset_level;
23071 
23072 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
23073 			err = EFAULT;
23074 		} else {
23075 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
23076 			    "reset_level = 0x%lx\n", reset_level);
23077 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
23078 				err = 0;
23079 			} else {
23080 				err = EIO;
23081 			}
23082 		}
23083 		break;
23084 	}
23085 
23086 	case DKIOCABORT:
23087 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23088 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
23089 			err = 0;
23090 		} else {
23091 			err = EIO;
23092 		}
23093 		break;
23094 #endif
23095 
23096 #ifdef SD_FAULT_INJECTION
23097 /* SDIOC FaultInjection testing ioctls */
23098 	case SDIOCSTART:
23099 	case SDIOCSTOP:
23100 	case SDIOCINSERTPKT:
23101 	case SDIOCINSERTXB:
23102 	case SDIOCINSERTUN:
23103 	case SDIOCINSERTARQ:
23104 	case SDIOCPUSH:
23105 	case SDIOCRETRIEVE:
23106 	case SDIOCRUN:
23107 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23108 		    "SDIOC detected cmd:0x%X:\n", cmd);
23109 		/* call error generator */
23110 		sd_faultinjection_ioctl(cmd, arg, un);
23111 		err = 0;
23112 		break;
23113 
23114 #endif /* SD_FAULT_INJECTION */
23115 
23116 	case DKIOCFLUSHWRITECACHE:
23117 		{
23118 			struct dk_callback *dkc = (struct dk_callback *)arg;
23119 
23120 			mutex_enter(SD_MUTEX(un));
23121 			if (!un->un_f_sync_cache_supported ||
23122 			    !un->un_f_write_cache_enabled) {
23123 				err = un->un_f_sync_cache_supported ?
23124 				    0 : ENOTSUP;
23125 				mutex_exit(SD_MUTEX(un));
23126 				if ((flag & FKIOCTL) && dkc != NULL &&
23127 				    dkc->dkc_callback != NULL) {
23128 					(*dkc->dkc_callback)(dkc->dkc_cookie,
23129 					    err);
23130 					/*
23131 					 * Did callback and reported error.
23132 					 * Since we did a callback, ioctl
23133 					 * should return 0.
23134 					 */
23135 					err = 0;
23136 				}
23137 				break;
23138 			}
23139 			mutex_exit(SD_MUTEX(un));
23140 
23141 			if ((flag & FKIOCTL) && dkc != NULL &&
23142 			    dkc->dkc_callback != NULL) {
23143 				/* async SYNC CACHE request */
23144 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23145 			} else {
23146 				/* synchronous SYNC CACHE request */
23147 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23148 			}
23149 		}
23150 		break;
23151 
23152 	case DKIOCGETWCE: {
23153 
23154 		int wce;
23155 
23156 		if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23157 			break;
23158 		}
23159 
23160 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23161 			err = EFAULT;
23162 		}
23163 		break;
23164 	}
23165 
23166 	case DKIOCSETWCE: {
23167 
23168 		int wce, sync_supported;
23169 		int cur_wce = 0;
23170 
23171 		if (!un->un_f_cache_mode_changeable) {
23172 			err = EINVAL;
23173 			break;
23174 		}
23175 
23176 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
23177 			err = EFAULT;
23178 			break;
23179 		}
23180 
23181 		/*
23182 		 * Synchronize multiple threads trying to enable
23183 		 * or disable the cache via the un_f_wcc_cv
23184 		 * condition variable.
23185 		 */
23186 		mutex_enter(SD_MUTEX(un));
23187 
23188 		/*
23189 		 * Don't allow the cache to be enabled if the
23190 		 * config file has it disabled.
23191 		 */
23192 		if (un->un_f_opt_disable_cache && wce) {
23193 			mutex_exit(SD_MUTEX(un));
23194 			err = EINVAL;
23195 			break;
23196 		}
23197 
23198 		/*
23199 		 * Wait for write cache change in progress
23200 		 * bit to be clear before proceeding.
23201 		 */
23202 		while (un->un_f_wcc_inprog)
23203 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
23204 
23205 		un->un_f_wcc_inprog = 1;
23206 
23207 		mutex_exit(SD_MUTEX(un));
23208 
23209 		/*
23210 		 * Get the current write cache state
23211 		 */
23212 		if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
23213 			mutex_enter(SD_MUTEX(un));
23214 			un->un_f_wcc_inprog = 0;
23215 			cv_broadcast(&un->un_wcc_cv);
23216 			mutex_exit(SD_MUTEX(un));
23217 			break;
23218 		}
23219 
23220 		mutex_enter(SD_MUTEX(un));
23221 		un->un_f_write_cache_enabled = (cur_wce != 0);
23222 
23223 		if (un->un_f_write_cache_enabled && wce == 0) {
23224 			/*
23225 			 * Disable the write cache.  Don't clear
23226 			 * un_f_write_cache_enabled until after
23227 			 * the mode select and flush are complete.
23228 			 */
23229 			sync_supported = un->un_f_sync_cache_supported;
23230 
23231 			/*
23232 			 * If cache flush is suppressed, we assume that the
23233 			 * controller firmware will take care of managing the
23234 			 * write cache for us: no need to explicitly
23235 			 * disable it.
23236 			 */
23237 			if (!un->un_f_suppress_cache_flush) {
23238 				mutex_exit(SD_MUTEX(un));
23239 				if ((err = sd_cache_control(ssc,
23240 				    SD_CACHE_NOCHANGE,
23241 				    SD_CACHE_DISABLE)) == 0 &&
23242 				    sync_supported) {
23243 					err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
23244 					    NULL);
23245 				}
23246 			} else {
23247 				mutex_exit(SD_MUTEX(un));
23248 			}
23249 
23250 			mutex_enter(SD_MUTEX(un));
23251 			if (err == 0) {
23252 				un->un_f_write_cache_enabled = 0;
23253 			}
23254 
23255 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
23256 			/*
23257 			 * Set un_f_write_cache_enabled first, so there is
23258 			 * no window where the cache is enabled, but the
23259 			 * bit says it isn't.
23260 			 */
23261 			un->un_f_write_cache_enabled = 1;
23262 
23263 			/*
23264 			 * If cache flush is suppressed, we assume that the
23265 			 * controller firmware will take care of managing the
23266 			 * write cache for us: no need to explicitly
23267 			 * enable it.
23268 			 */
23269 			if (!un->un_f_suppress_cache_flush) {
23270 				mutex_exit(SD_MUTEX(un));
23271 				err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23272 				    SD_CACHE_ENABLE);
23273 			} else {
23274 				mutex_exit(SD_MUTEX(un));
23275 			}
23276 
23277 			mutex_enter(SD_MUTEX(un));
23278 
23279 			if (err) {
23280 				un->un_f_write_cache_enabled = 0;
23281 			}
23282 		}
23283 
23284 		un->un_f_wcc_inprog = 0;
23285 		cv_broadcast(&un->un_wcc_cv);
23286 		mutex_exit(SD_MUTEX(un));
23287 		break;
23288 	}
23289 
23290 	default:
23291 		err = ENOTTY;
23292 		break;
23293 	}
23294 	mutex_enter(SD_MUTEX(un));
23295 	un->un_ncmds_in_driver--;
23296 	ASSERT(un->un_ncmds_in_driver >= 0);
23297 	mutex_exit(SD_MUTEX(un));
23298 
23299 
23300 done_without_assess:
23301 	sd_ssc_fini(ssc);
23302 
23303 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23304 	return (err);
23305 
23306 done_with_assess:
23307 	mutex_enter(SD_MUTEX(un));
23308 	un->un_ncmds_in_driver--;
23309 	ASSERT(un->un_ncmds_in_driver >= 0);
23310 	mutex_exit(SD_MUTEX(un));
23311 
23312 done_quick_assess:
23313 	if (err != 0)
23314 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23315 	/* Uninitialize sd_ssc_t pointer */
23316 	sd_ssc_fini(ssc);
23317 
23318 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23319 	return (err);
23320 }
23321 
23322 
23323 /*
23324  *    Function: sd_dkio_ctrl_info
23325  *
23326  * Description: This routine is the driver entry point for handling controller
23327  *		information ioctl requests (DKIOCINFO).
23328  *
23329  *   Arguments: dev  - the device number
23330  *		arg  - pointer to user provided dk_cinfo structure
23331  *		       specifying the controller type and attributes.
23332  *		flag - this argument is a pass through to ddi_copyxxx()
23333  *		       directly from the mode argument of ioctl().
23334  *
23335  * Return Code: 0
23336  *		EFAULT
23337  *		ENXIO
23338  */
23339 
23340 static int
23341 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23342 {
23343 	struct sd_lun	*un = NULL;
23344 	struct dk_cinfo	*info;
23345 	dev_info_t	*pdip;
23346 	int		lun, tgt;
23347 
23348 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23349 		return (ENXIO);
23350 	}
23351 
23352 	info = (struct dk_cinfo *)
23353 	    kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23354 
23355 	switch (un->un_ctype) {
23356 	case CTYPE_CDROM:
23357 		info->dki_ctype = DKC_CDROM;
23358 		break;
23359 	default:
23360 		info->dki_ctype = DKC_SCSI_CCS;
23361 		break;
23362 	}
23363 	pdip = ddi_get_parent(SD_DEVINFO(un));
23364 	info->dki_cnum = ddi_get_instance(pdip);
23365 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23366 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
23367 	} else {
23368 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
23369 		    DK_DEVLEN - 1);
23370 	}
23371 
23372 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23373 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23374 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23375 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23376 
23377 	/* Unit Information */
23378 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23379 	info->dki_slave = ((tgt << 3) | lun);
23380 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23381 	    DK_DEVLEN - 1);
23382 	info->dki_flags = DKI_FMTVOL;
23383 	info->dki_partition = SDPART(dev);
23384 
23385 	/* Max Transfer size of this device in blocks */
23386 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23387 	info->dki_addr = 0;
23388 	info->dki_space = 0;
23389 	info->dki_prio = 0;
23390 	info->dki_vec = 0;
23391 
23392 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23393 		kmem_free(info, sizeof (struct dk_cinfo));
23394 		return (EFAULT);
23395 	} else {
23396 		kmem_free(info, sizeof (struct dk_cinfo));
23397 		return (0);
23398 	}
23399 }
23400 
23401 /*
23402  *    Function: sd_get_media_info_com
23403  *
23404  * Description: This routine returns the information required to populate
23405  *		the fields for the dk_minfo/dk_minfo_ext structures.
23406  *
23407  *   Arguments: dev		- the device number
23408  *		dki_media_type	- media_type
23409  *		dki_lbsize	- logical block size
23410  *		dki_capacity	- capacity in blocks
23411  *		dki_pbsize	- physical block size (if requested)
23412  *
23413  * Return Code: 0
23414  *		EACCESS
23415  *		EFAULT
23416  *		ENXIO
23417  *		EIO
23418  */
23419 static int
23420 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23421 	diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23422 {
23423 	struct sd_lun		*un = NULL;
23424 	struct uscsi_cmd	com;
23425 	struct scsi_inquiry	*sinq;
23426 	u_longlong_t		media_capacity;
23427 	uint64_t		capacity;
23428 	uint_t			lbasize;
23429 	uint_t			pbsize;
23430 	uchar_t			*out_data;
23431 	uchar_t			*rqbuf;
23432 	int			rval = 0;
23433 	int			rtn;
23434 	sd_ssc_t		*ssc;
23435 
23436 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23437 	    (un->un_state == SD_STATE_OFFLINE)) {
23438 		return (ENXIO);
23439 	}
23440 
23441 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
23442 
23443 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23444 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23445 	ssc = sd_ssc_init(un);
23446 
23447 	/* Issue a TUR to determine if the drive is ready with media present */
23448 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23449 	if (rval == ENXIO) {
23450 		goto done;
23451 	} else if (rval != 0) {
23452 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23453 	}
23454 
23455 	/* Now get configuration data */
23456 	if (ISCD(un)) {
23457 		*dki_media_type = DK_CDROM;
23458 
23459 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23460 		if (un->un_f_mmc_cap == TRUE) {
23461 			rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23462 			    SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23463 			    SD_PATH_STANDARD);
23464 
23465 			if (rtn) {
23466 				/*
23467 				 * We ignore all failures for CD and need to
23468 				 * put the assessment before processing code
23469 				 * to avoid missing assessment for FMA.
23470 				 */
23471 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23472 				/*
23473 				 * Failed for other than an illegal request
23474 				 * or command not supported
23475 				 */
23476 				if ((com.uscsi_status == STATUS_CHECK) &&
23477 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
23478 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23479 					    (rqbuf[12] != 0x20)) {
23480 						rval = EIO;
23481 						goto no_assessment;
23482 					}
23483 				}
23484 			} else {
23485 				/*
23486 				 * The GET CONFIGURATION command succeeded
23487 				 * so set the media type according to the
23488 				 * returned data
23489 				 */
23490 				*dki_media_type = out_data[6];
23491 				*dki_media_type <<= 8;
23492 				*dki_media_type |= out_data[7];
23493 			}
23494 		}
23495 	} else {
23496 		/*
23497 		 * The profile list is not available, so we attempt to identify
23498 		 * the media type based on the inquiry data
23499 		 */
23500 		sinq = un->un_sd->sd_inq;
23501 		if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23502 		    (sinq->inq_dtype == DTYPE_OPTICAL)) {
23503 			/* This is a direct access device  or optical disk */
23504 			*dki_media_type = DK_FIXED_DISK;
23505 
23506 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23507 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23508 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23509 					*dki_media_type = DK_ZIP;
23510 				} else if (
23511 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23512 					*dki_media_type = DK_JAZ;
23513 				}
23514 			}
23515 		} else {
23516 			/*
23517 			 * Not a CD, direct access or optical disk so return
23518 			 * unknown media
23519 			 */
23520 			*dki_media_type = DK_UNKNOWN;
23521 		}
23522 	}
23523 
23524 	/*
23525 	 * Now read the capacity so we can provide the lbasize,
23526 	 * pbsize and capacity.
23527 	 */
23528 	if (dki_pbsize && un->un_f_descr_format_supported) {
23529 		rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23530 		    &pbsize, SD_PATH_DIRECT);
23531 
23532 		/*
23533 		 * Override the physical blocksize if the instance already
23534 		 * has a larger value.
23535 		 */
23536 		pbsize = MAX(pbsize, un->un_phy_blocksize);
23537 	}
23538 
23539 	if (dki_pbsize == NULL || rval != 0 ||
23540 	    !un->un_f_descr_format_supported) {
23541 		rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23542 		    SD_PATH_DIRECT);
23543 
23544 		switch (rval) {
23545 		case 0:
23546 			if (un->un_f_enable_rmw &&
23547 			    un->un_phy_blocksize != 0) {
23548 				pbsize = un->un_phy_blocksize;
23549 			} else {
23550 				pbsize = lbasize;
23551 			}
23552 			media_capacity = capacity;
23553 
23554 			/*
23555 			 * sd_send_scsi_READ_CAPACITY() reports capacity in
23556 			 * un->un_sys_blocksize chunks. So we need to convert
23557 			 * it into cap.lbsize chunks.
23558 			 */
23559 			if (un->un_f_has_removable_media) {
23560 				media_capacity *= un->un_sys_blocksize;
23561 				media_capacity /= lbasize;
23562 			}
23563 			break;
23564 		case EACCES:
23565 			rval = EACCES;
23566 			goto done;
23567 		default:
23568 			rval = EIO;
23569 			goto done;
23570 		}
23571 	} else {
23572 		if (un->un_f_enable_rmw &&
23573 		    !ISP2(pbsize % DEV_BSIZE)) {
23574 			pbsize = SSD_SECSIZE;
23575 		} else if (!ISP2(lbasize % DEV_BSIZE) ||
23576 		    !ISP2(pbsize % DEV_BSIZE)) {
23577 			pbsize = lbasize = DEV_BSIZE;
23578 		}
23579 		media_capacity = capacity;
23580 	}
23581 
23582 	/*
23583 	 * If lun is expanded dynamically, update the un structure.
23584 	 */
23585 	mutex_enter(SD_MUTEX(un));
23586 	if ((un->un_f_blockcount_is_valid == TRUE) &&
23587 	    (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23588 	    (capacity > un->un_blockcount)) {
23589 		un->un_f_expnevent = B_FALSE;
23590 		sd_update_block_info(un, lbasize, capacity);
23591 	}
23592 	mutex_exit(SD_MUTEX(un));
23593 
23594 	*dki_lbsize = lbasize;
23595 	*dki_capacity = media_capacity;
23596 	if (dki_pbsize)
23597 		*dki_pbsize = pbsize;
23598 
23599 done:
23600 	if (rval != 0) {
23601 		if (rval == EIO)
23602 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23603 		else
23604 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23605 	}
23606 no_assessment:
23607 	sd_ssc_fini(ssc);
23608 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23609 	kmem_free(rqbuf, SENSE_LENGTH);
23610 	return (rval);
23611 }
23612 
23613 /*
23614  *    Function: sd_get_media_info
23615  *
23616  * Description: This routine is the driver entry point for handling ioctl
23617  *		requests for the media type or command set profile used by the
23618  *		drive to operate on the media (DKIOCGMEDIAINFO).
23619  *
23620  *   Arguments: dev	- the device number
23621  *		arg	- pointer to user provided dk_minfo structure
23622  *			  specifying the media type, logical block size and
23623  *			  drive capacity.
23624  *		flag	- this argument is a pass through to ddi_copyxxx()
23625  *			  directly from the mode argument of ioctl().
23626  *
23627  * Return Code: returns the value from sd_get_media_info_com
23628  */
23629 static int
23630 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23631 {
23632 	struct dk_minfo		mi;
23633 	int			rval;
23634 
23635 	rval = sd_get_media_info_com(dev, &mi.dki_media_type,
23636 	    &mi.dki_lbsize, &mi.dki_capacity, NULL);
23637 
23638 	if (rval)
23639 		return (rval);
23640 	if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
23641 		rval = EFAULT;
23642 	return (rval);
23643 }
23644 
23645 /*
23646  *    Function: sd_get_media_info_ext
23647  *
23648  * Description: This routine is the driver entry point for handling ioctl
23649  *		requests for the media type or command set profile used by the
23650  *		drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23651  *		difference this ioctl and DKIOCGMEDIAINFO is the return value
23652  *		of this ioctl contains both logical block size and physical
23653  *		block size.
23654  *
23655  *
23656  *   Arguments: dev	- the device number
23657  *		arg	- pointer to user provided dk_minfo_ext structure
23658  *			  specifying the media type, logical block size,
23659  *			  physical block size and disk capacity.
23660  *		flag	- this argument is a pass through to ddi_copyxxx()
23661  *			  directly from the mode argument of ioctl().
23662  *
23663  * Return Code: returns the value from sd_get_media_info_com
23664  */
23665 static int
23666 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
23667 {
23668 	struct dk_minfo_ext	mie;
23669 	int			rval = 0;
23670 
23671 	rval = sd_get_media_info_com(dev, &mie.dki_media_type,
23672 	    &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize);
23673 
23674 	if (rval)
23675 		return (rval);
23676 	if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag))
23677 		rval = EFAULT;
23678 	return (rval);
23679 
23680 }
23681 
23682 /*
23683  *    Function: sd_watch_request_submit
23684  *
23685  * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
23686  *		depending on which is supported by device.
23687  */
23688 static opaque_t
23689 sd_watch_request_submit(struct sd_lun *un)
23690 {
23691 	dev_t			dev;
23692 
23693 	/* All submissions are unified to use same device number */
23694 	dev = sd_make_device(SD_DEVINFO(un));
23695 
23696 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23697 		return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
23698 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23699 		    (caddr_t)dev));
23700 	} else {
23701 		return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
23702 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23703 		    (caddr_t)dev));
23704 	}
23705 }
23706 
23707 
23708 /*
23709  *    Function: sd_check_media
23710  *
23711  * Description: This utility routine implements the functionality for the
23712  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23713  *		driver state changes from that specified by the user
23714  *		(inserted or ejected). For example, if the user specifies
23715  *		DKIO_EJECTED and the current media state is inserted this
23716  *		routine will immediately return DKIO_INSERTED. However, if the
23717  *		current media state is not inserted the user thread will be
23718  *		blocked until the drive state changes. If DKIO_NONE is specified
23719  *		the user thread will block until a drive state change occurs.
23720  *
23721  *   Arguments: dev  - the device number
23722  *		state  - user pointer to a dkio_state, updated with the current
23723  *			drive state at return.
23724  *
23725  * Return Code: ENXIO
23726  *		EIO
23727  *		EAGAIN
23728  *		EINTR
23729  */
23730 
23731 static int
23732 sd_check_media(dev_t dev, enum dkio_state state)
23733 {
23734 	struct sd_lun		*un = NULL;
23735 	enum dkio_state		prev_state;
23736 	opaque_t		token = NULL;
23737 	int			rval = 0;
23738 	sd_ssc_t		*ssc;
23739 
23740 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23741 		return (ENXIO);
23742 	}
23743 
23744 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23745 
23746 	ssc = sd_ssc_init(un);
23747 
23748 	mutex_enter(SD_MUTEX(un));
23749 
23750 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23751 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
23752 
23753 	prev_state = un->un_mediastate;
23754 
23755 	/* is there anything to do? */
23756 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23757 		/*
23758 		 * submit the request to the scsi_watch service;
23759 		 * scsi_media_watch_cb() does the real work
23760 		 */
23761 		mutex_exit(SD_MUTEX(un));
23762 
23763 		/*
23764 		 * This change handles the case where a scsi watch request is
23765 		 * added to a device that is powered down. To accomplish this
23766 		 * we power up the device before adding the scsi watch request,
23767 		 * since the scsi watch sends a TUR directly to the device
23768 		 * which the device cannot handle if it is powered down.
23769 		 */
23770 		if (sd_pm_entry(un) != DDI_SUCCESS) {
23771 			mutex_enter(SD_MUTEX(un));
23772 			goto done;
23773 		}
23774 
23775 		token = sd_watch_request_submit(un);
23776 
23777 		sd_pm_exit(un);
23778 
23779 		mutex_enter(SD_MUTEX(un));
23780 		if (token == NULL) {
23781 			rval = EAGAIN;
23782 			goto done;
23783 		}
23784 
23785 		/*
23786 		 * This is a special case IOCTL that doesn't return
23787 		 * until the media state changes. Routine sdpower
23788 		 * knows about and handles this so don't count it
23789 		 * as an active cmd in the driver, which would
23790 		 * keep the device busy to the pm framework.
23791 		 * If the count isn't decremented the device can't
23792 		 * be powered down.
23793 		 */
23794 		un->un_ncmds_in_driver--;
23795 		ASSERT(un->un_ncmds_in_driver >= 0);
23796 
23797 		/*
23798 		 * if a prior request had been made, this will be the same
23799 		 * token, as scsi_watch was designed that way.
23800 		 */
23801 		un->un_swr_token = token;
23802 		un->un_specified_mediastate = state;
23803 
23804 		/*
23805 		 * now wait for media change
23806 		 * we will not be signalled unless mediastate == state but it is
23807 		 * still better to test for this condition, since there is a
23808 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23809 		 */
23810 		SD_TRACE(SD_LOG_COMMON, un,
23811 		    "sd_check_media: waiting for media state change\n");
23812 		while (un->un_mediastate == state) {
23813 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23814 				SD_TRACE(SD_LOG_COMMON, un,
23815 				    "sd_check_media: waiting for media state "
23816 				    "was interrupted\n");
23817 				un->un_ncmds_in_driver++;
23818 				rval = EINTR;
23819 				goto done;
23820 			}
23821 			SD_TRACE(SD_LOG_COMMON, un,
23822 			    "sd_check_media: received signal, state=%x\n",
23823 			    un->un_mediastate);
23824 		}
23825 		/*
23826 		 * Inc the counter to indicate the device once again
23827 		 * has an active outstanding cmd.
23828 		 */
23829 		un->un_ncmds_in_driver++;
23830 	}
23831 
23832 	/* invalidate geometry */
23833 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23834 		sr_ejected(un);
23835 	}
23836 
23837 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23838 		uint64_t	capacity;
23839 		uint_t		lbasize;
23840 
23841 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23842 		mutex_exit(SD_MUTEX(un));
23843 		/*
23844 		 * Since the following routines use SD_PATH_DIRECT, we must
23845 		 * call PM directly before the upcoming disk accesses. This
23846 		 * may cause the disk to be power/spin up.
23847 		 */
23848 
23849 		if (sd_pm_entry(un) == DDI_SUCCESS) {
23850 			rval = sd_send_scsi_READ_CAPACITY(ssc,
23851 			    &capacity, &lbasize, SD_PATH_DIRECT);
23852 			if (rval != 0) {
23853 				sd_pm_exit(un);
23854 				if (rval == EIO)
23855 					sd_ssc_assessment(ssc,
23856 					    SD_FMT_STATUS_CHECK);
23857 				else
23858 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23859 				mutex_enter(SD_MUTEX(un));
23860 				goto done;
23861 			}
23862 		} else {
23863 			rval = EIO;
23864 			mutex_enter(SD_MUTEX(un));
23865 			goto done;
23866 		}
23867 		mutex_enter(SD_MUTEX(un));
23868 
23869 		sd_update_block_info(un, lbasize, capacity);
23870 
23871 		/*
23872 		 *  Check if the media in the device is writable or not
23873 		 */
23874 		if (ISCD(un)) {
23875 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
23876 		}
23877 
23878 		mutex_exit(SD_MUTEX(un));
23879 		cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
23880 		if ((cmlb_validate(un->un_cmlbhandle, 0,
23881 		    (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
23882 			sd_set_pstats(un);
23883 			SD_TRACE(SD_LOG_IO_PARTITION, un,
23884 			    "sd_check_media: un:0x%p pstats created and "
23885 			    "set\n", un);
23886 		}
23887 
23888 		rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
23889 		    SD_PATH_DIRECT);
23890 
23891 		sd_pm_exit(un);
23892 
23893 		if (rval != 0) {
23894 			if (rval == EIO)
23895 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23896 			else
23897 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23898 		}
23899 
23900 		mutex_enter(SD_MUTEX(un));
23901 	}
23902 done:
23903 	sd_ssc_fini(ssc);
23904 	un->un_f_watcht_stopped = FALSE;
23905 	if (token != NULL && un->un_swr_token != NULL) {
23906 		/*
23907 		 * Use of this local token and the mutex ensures that we avoid
23908 		 * some race conditions associated with terminating the
23909 		 * scsi watch.
23910 		 */
23911 		token = un->un_swr_token;
23912 		mutex_exit(SD_MUTEX(un));
23913 		(void) scsi_watch_request_terminate(token,
23914 		    SCSI_WATCH_TERMINATE_WAIT);
23915 		if (scsi_watch_get_ref_count(token) == 0) {
23916 			mutex_enter(SD_MUTEX(un));
23917 			un->un_swr_token = (opaque_t)NULL;
23918 		} else {
23919 			mutex_enter(SD_MUTEX(un));
23920 		}
23921 	}
23922 
23923 	/*
23924 	 * Update the capacity kstat value, if no media previously
23925 	 * (capacity kstat is 0) and a media has been inserted
23926 	 * (un_f_blockcount_is_valid == TRUE)
23927 	 */
23928 	if (un->un_errstats) {
23929 		struct sd_errstats	*stp = NULL;
23930 
23931 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
23932 		if ((stp->sd_capacity.value.ui64 == 0) &&
23933 		    (un->un_f_blockcount_is_valid == TRUE)) {
23934 			stp->sd_capacity.value.ui64 =
23935 			    (uint64_t)((uint64_t)un->un_blockcount *
23936 			    un->un_sys_blocksize);
23937 		}
23938 	}
23939 	mutex_exit(SD_MUTEX(un));
23940 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23941 	return (rval);
23942 }
23943 
23944 
23945 /*
23946  *    Function: sd_delayed_cv_broadcast
23947  *
23948  * Description: Delayed cv_broadcast to allow for target to recover from media
23949  *		insertion.
23950  *
23951  *   Arguments: arg - driver soft state (unit) structure
23952  */
23953 
23954 static void
23955 sd_delayed_cv_broadcast(void *arg)
23956 {
23957 	struct sd_lun *un = arg;
23958 
23959 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23960 
23961 	mutex_enter(SD_MUTEX(un));
23962 	un->un_dcvb_timeid = NULL;
23963 	cv_broadcast(&un->un_state_cv);
23964 	mutex_exit(SD_MUTEX(un));
23965 }
23966 
23967 
23968 /*
23969  *    Function: sd_media_watch_cb
23970  *
23971  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23972  *		routine processes the TUR sense data and updates the driver
23973  *		state if a transition has occurred. The user thread
23974  *		(sd_check_media) is then signalled.
23975  *
23976  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
23977  *			among multiple watches that share this callback function
23978  *		resultp - scsi watch facility result packet containing scsi
23979  *			  packet, status byte and sense data
23980  *
23981  * Return Code: 0 for success, -1 for failure
23982  */
23983 
23984 static int
23985 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23986 {
23987 	struct sd_lun			*un;
23988 	struct scsi_status		*statusp = resultp->statusp;
23989 	uint8_t				*sensep = (uint8_t *)resultp->sensep;
23990 	enum dkio_state			state = DKIO_NONE;
23991 	dev_t				dev = (dev_t)arg;
23992 	uchar_t				actual_sense_length;
23993 	uint8_t				skey, asc, ascq;
23994 
23995 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23996 		return (-1);
23997 	}
23998 	actual_sense_length = resultp->actual_sense_length;
23999 
24000 	mutex_enter(SD_MUTEX(un));
24001 	SD_TRACE(SD_LOG_COMMON, un,
24002 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
24003 	    *((char *)statusp), (void *)sensep, actual_sense_length);
24004 
24005 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
24006 		un->un_mediastate = DKIO_DEV_GONE;
24007 		cv_broadcast(&un->un_state_cv);
24008 		mutex_exit(SD_MUTEX(un));
24009 
24010 		return (0);
24011 	}
24012 
24013 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
24014 		if (sd_gesn_media_data_valid(resultp->mmc_data)) {
24015 			if ((resultp->mmc_data[5] &
24016 			    SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
24017 				state = DKIO_INSERTED;
24018 			} else {
24019 				state = DKIO_EJECTED;
24020 			}
24021 			if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
24022 			    SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
24023 				sd_log_eject_request_event(un, KM_NOSLEEP);
24024 			}
24025 		}
24026 	} else if (sensep != NULL) {
24027 		/*
24028 		 * If there was a check condition then sensep points to valid
24029 		 * sense data. If status was not a check condition but a
24030 		 * reservation or busy status then the new state is DKIO_NONE.
24031 		 */
24032 		skey = scsi_sense_key(sensep);
24033 		asc = scsi_sense_asc(sensep);
24034 		ascq = scsi_sense_ascq(sensep);
24035 
24036 		SD_INFO(SD_LOG_COMMON, un,
24037 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
24038 		    skey, asc, ascq);
24039 		/* This routine only uses up to 13 bytes of sense data. */
24040 		if (actual_sense_length >= 13) {
24041 			if (skey == KEY_UNIT_ATTENTION) {
24042 				if (asc == 0x28) {
24043 					state = DKIO_INSERTED;
24044 				}
24045 			} else if (skey == KEY_NOT_READY) {
24046 				/*
24047 				 * Sense data of 02/06/00 means that the
24048 				 * drive could not read the media (No
24049 				 * reference position found). In this case
24050 				 * to prevent a hang on the DKIOCSTATE IOCTL
24051 				 * we set the media state to DKIO_INSERTED.
24052 				 */
24053 				if (asc == 0x06 && ascq == 0x00)
24054 					state = DKIO_INSERTED;
24055 
24056 				/*
24057 				 * if 02/04/02  means that the host
24058 				 * should send start command. Explicitly
24059 				 * leave the media state as is
24060 				 * (inserted) as the media is inserted
24061 				 * and host has stopped device for PM
24062 				 * reasons. Upon next true read/write
24063 				 * to this media will bring the
24064 				 * device to the right state good for
24065 				 * media access.
24066 				 */
24067 				if (asc == 0x3a) {
24068 					state = DKIO_EJECTED;
24069 				} else {
24070 					/*
24071 					 * If the drive is busy with an
24072 					 * operation or long write, keep the
24073 					 * media in an inserted state.
24074 					 */
24075 
24076 					if ((asc == 0x04) &&
24077 					    ((ascq == 0x02) ||
24078 					    (ascq == 0x07) ||
24079 					    (ascq == 0x08))) {
24080 						state = DKIO_INSERTED;
24081 					}
24082 				}
24083 			} else if (skey == KEY_NO_SENSE) {
24084 				if ((asc == 0x00) && (ascq == 0x00)) {
24085 					/*
24086 					 * Sense Data 00/00/00 does not provide
24087 					 * any information about the state of
24088 					 * the media. Ignore it.
24089 					 */
24090 					mutex_exit(SD_MUTEX(un));
24091 					return (0);
24092 				}
24093 			}
24094 		}
24095 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
24096 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24097 		state = DKIO_INSERTED;
24098 	}
24099 
24100 	SD_TRACE(SD_LOG_COMMON, un,
24101 	    "sd_media_watch_cb: state=%x, specified=%x\n",
24102 	    state, un->un_specified_mediastate);
24103 
24104 	/*
24105 	 * now signal the waiting thread if this is *not* the specified state;
24106 	 * delay the signal if the state is DKIO_INSERTED to allow the target
24107 	 * to recover
24108 	 */
24109 	if (state != un->un_specified_mediastate) {
24110 		un->un_mediastate = state;
24111 		if (state == DKIO_INSERTED) {
24112 			/*
24113 			 * delay the signal to give the drive a chance
24114 			 * to do what it apparently needs to do
24115 			 */
24116 			SD_TRACE(SD_LOG_COMMON, un,
24117 			    "sd_media_watch_cb: delayed cv_broadcast\n");
24118 			if (un->un_dcvb_timeid == NULL) {
24119 				un->un_dcvb_timeid =
24120 				    timeout(sd_delayed_cv_broadcast, un,
24121 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24122 			}
24123 		} else {
24124 			SD_TRACE(SD_LOG_COMMON, un,
24125 			    "sd_media_watch_cb: immediate cv_broadcast\n");
24126 			cv_broadcast(&un->un_state_cv);
24127 		}
24128 	}
24129 	mutex_exit(SD_MUTEX(un));
24130 	return (0);
24131 }
24132 
24133 
24134 /*
24135  *    Function: sd_dkio_get_temp
24136  *
24137  * Description: This routine is the driver entry point for handling ioctl
24138  *		requests to get the disk temperature.
24139  *
24140  *   Arguments: dev  - the device number
24141  *		arg  - pointer to user provided dk_temperature structure.
24142  *		flag - this argument is a pass through to ddi_copyxxx()
24143  *		       directly from the mode argument of ioctl().
24144  *
24145  * Return Code: 0
24146  *		EFAULT
24147  *		ENXIO
24148  *		EAGAIN
24149  */
24150 
24151 static int
24152 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24153 {
24154 	struct sd_lun		*un = NULL;
24155 	struct dk_temperature	*dktemp = NULL;
24156 	uchar_t			*temperature_page;
24157 	int			rval = 0;
24158 	int			path_flag = SD_PATH_STANDARD;
24159 	sd_ssc_t		*ssc;
24160 
24161 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24162 		return (ENXIO);
24163 	}
24164 
24165 	ssc = sd_ssc_init(un);
24166 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24167 
24168 	/* copyin the disk temp argument to get the user flags */
24169 	if (ddi_copyin((void *)arg, dktemp,
24170 	    sizeof (struct dk_temperature), flag) != 0) {
24171 		rval = EFAULT;
24172 		goto done;
24173 	}
24174 
24175 	/* Initialize the temperature to invalid. */
24176 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24177 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24178 
24179 	/*
24180 	 * Note: Investigate removing the "bypass pm" semantic.
24181 	 * Can we just bypass PM always?
24182 	 */
24183 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24184 		path_flag = SD_PATH_DIRECT;
24185 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24186 		mutex_enter(&un->un_pm_mutex);
24187 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24188 			/*
24189 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24190 			 * in low power mode, we can not wake it up, Need to
24191 			 * return EAGAIN.
24192 			 */
24193 			mutex_exit(&un->un_pm_mutex);
24194 			rval = EAGAIN;
24195 			goto done;
24196 		} else {
24197 			/*
24198 			 * Indicate to PM the device is busy. This is required
24199 			 * to avoid a race - i.e. the ioctl is issuing a
24200 			 * command and the pm framework brings down the device
24201 			 * to low power mode (possible power cut-off on some
24202 			 * platforms).
24203 			 */
24204 			mutex_exit(&un->un_pm_mutex);
24205 			if (sd_pm_entry(un) != DDI_SUCCESS) {
24206 				rval = EAGAIN;
24207 				goto done;
24208 			}
24209 		}
24210 	}
24211 
24212 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24213 
24214 	rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24215 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24216 	if (rval != 0)
24217 		goto done2;
24218 
24219 	/*
24220 	 * For the current temperature verify that the parameter length is 0x02
24221 	 * and the parameter code is 0x00
24222 	 */
24223 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24224 	    (temperature_page[5] == 0x00)) {
24225 		if (temperature_page[9] == 0xFF) {
24226 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24227 		} else {
24228 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24229 		}
24230 	}
24231 
24232 	/*
24233 	 * For the reference temperature verify that the parameter
24234 	 * length is 0x02 and the parameter code is 0x01
24235 	 */
24236 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24237 	    (temperature_page[11] == 0x01)) {
24238 		if (temperature_page[15] == 0xFF) {
24239 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24240 		} else {
24241 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24242 		}
24243 	}
24244 
24245 	/* Do the copyout regardless of the temperature commands status. */
24246 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24247 	    flag) != 0) {
24248 		rval = EFAULT;
24249 		goto done1;
24250 	}
24251 
24252 done2:
24253 	if (rval != 0) {
24254 		if (rval == EIO)
24255 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24256 		else
24257 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24258 	}
24259 done1:
24260 	if (path_flag == SD_PATH_DIRECT) {
24261 		sd_pm_exit(un);
24262 	}
24263 
24264 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24265 done:
24266 	sd_ssc_fini(ssc);
24267 	if (dktemp != NULL) {
24268 		kmem_free(dktemp, sizeof (struct dk_temperature));
24269 	}
24270 
24271 	return (rval);
24272 }
24273 
24274 
24275 /*
24276  *    Function: sd_log_page_supported
24277  *
24278  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24279  *		supported log pages.
24280  *
24281  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
24282  *                      structure for this target.
24283  *		log_page -
24284  *
24285  * Return Code: -1 - on error (log sense is optional and may not be supported).
24286  *		0  - log page not found.
24287  *  		1  - log page found.
24288  */
24289 
24290 static int
24291 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24292 {
24293 	uchar_t *log_page_data;
24294 	int	i;
24295 	int	match = 0;
24296 	int	log_size;
24297 	int	status = 0;
24298 	struct sd_lun	*un;
24299 
24300 	ASSERT(ssc != NULL);
24301 	un = ssc->ssc_un;
24302 	ASSERT(un != NULL);
24303 
24304 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24305 
24306 	status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24307 	    SD_PATH_DIRECT);
24308 
24309 	if (status != 0) {
24310 		if (status == EIO) {
24311 			/*
24312 			 * Some disks do not support log sense, we
24313 			 * should ignore this kind of error(sense key is
24314 			 * 0x5 - illegal request).
24315 			 */
24316 			uint8_t *sensep;
24317 			int senlen;
24318 
24319 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24320 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24321 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
24322 
24323 			if (senlen > 0 &&
24324 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24325 				sd_ssc_assessment(ssc,
24326 				    SD_FMT_IGNORE_COMPROMISE);
24327 			} else {
24328 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24329 			}
24330 		} else {
24331 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24332 		}
24333 
24334 		SD_ERROR(SD_LOG_COMMON, un,
24335 		    "sd_log_page_supported: failed log page retrieval\n");
24336 		kmem_free(log_page_data, 0xFF);
24337 		return (-1);
24338 	}
24339 
24340 	log_size = log_page_data[3];
24341 
24342 	/*
24343 	 * The list of supported log pages start from the fourth byte. Check
24344 	 * until we run out of log pages or a match is found.
24345 	 */
24346 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
24347 		if (log_page_data[i] == log_page) {
24348 			match++;
24349 		}
24350 	}
24351 	kmem_free(log_page_data, 0xFF);
24352 	return (match);
24353 }
24354 
24355 
24356 /*
24357  *    Function: sd_mhdioc_failfast
24358  *
24359  * Description: This routine is the driver entry point for handling ioctl
24360  *		requests to enable/disable the multihost failfast option.
24361  *		(MHIOCENFAILFAST)
24362  *
24363  *   Arguments: dev	- the device number
24364  *		arg	- user specified probing interval.
24365  *		flag	- this argument is a pass through to ddi_copyxxx()
24366  *			  directly from the mode argument of ioctl().
24367  *
24368  * Return Code: 0
24369  *		EFAULT
24370  *		ENXIO
24371  */
24372 
24373 static int
24374 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24375 {
24376 	struct sd_lun	*un = NULL;
24377 	int		mh_time;
24378 	int		rval = 0;
24379 
24380 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24381 		return (ENXIO);
24382 	}
24383 
24384 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24385 		return (EFAULT);
24386 
24387 	if (mh_time) {
24388 		mutex_enter(SD_MUTEX(un));
24389 		un->un_resvd_status |= SD_FAILFAST;
24390 		mutex_exit(SD_MUTEX(un));
24391 		/*
24392 		 * If mh_time is INT_MAX, then this ioctl is being used for
24393 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24394 		 */
24395 		if (mh_time != INT_MAX) {
24396 			rval = sd_check_mhd(dev, mh_time);
24397 		}
24398 	} else {
24399 		(void) sd_check_mhd(dev, 0);
24400 		mutex_enter(SD_MUTEX(un));
24401 		un->un_resvd_status &= ~SD_FAILFAST;
24402 		mutex_exit(SD_MUTEX(un));
24403 	}
24404 	return (rval);
24405 }
24406 
24407 
24408 /*
24409  *    Function: sd_mhdioc_takeown
24410  *
24411  * Description: This routine is the driver entry point for handling ioctl
24412  *		requests to forcefully acquire exclusive access rights to the
24413  *		multihost disk (MHIOCTKOWN).
24414  *
24415  *   Arguments: dev	- the device number
24416  *		arg	- user provided structure specifying the delay
24417  *			  parameters in milliseconds
24418  *		flag	- this argument is a pass through to ddi_copyxxx()
24419  *			  directly from the mode argument of ioctl().
24420  *
24421  * Return Code: 0
24422  *		EFAULT
24423  *		ENXIO
24424  */
24425 
24426 static int
24427 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24428 {
24429 	struct sd_lun		*un = NULL;
24430 	struct mhioctkown	*tkown = NULL;
24431 	int			rval = 0;
24432 
24433 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24434 		return (ENXIO);
24435 	}
24436 
24437 	if (arg != NULL) {
24438 		tkown = (struct mhioctkown *)
24439 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24440 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24441 		if (rval != 0) {
24442 			rval = EFAULT;
24443 			goto error;
24444 		}
24445 	}
24446 
24447 	rval = sd_take_ownership(dev, tkown);
24448 	mutex_enter(SD_MUTEX(un));
24449 	if (rval == 0) {
24450 		un->un_resvd_status |= SD_RESERVE;
24451 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24452 			sd_reinstate_resv_delay =
24453 			    tkown->reinstate_resv_delay * 1000;
24454 		} else {
24455 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24456 		}
24457 		/*
24458 		 * Give the scsi_watch routine interval set by
24459 		 * the MHIOCENFAILFAST ioctl precedence here.
24460 		 */
24461 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24462 			mutex_exit(SD_MUTEX(un));
24463 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24464 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24465 			    "sd_mhdioc_takeown : %d\n",
24466 			    sd_reinstate_resv_delay);
24467 		} else {
24468 			mutex_exit(SD_MUTEX(un));
24469 		}
24470 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24471 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24472 	} else {
24473 		un->un_resvd_status &= ~SD_RESERVE;
24474 		mutex_exit(SD_MUTEX(un));
24475 	}
24476 
24477 error:
24478 	if (tkown != NULL) {
24479 		kmem_free(tkown, sizeof (struct mhioctkown));
24480 	}
24481 	return (rval);
24482 }
24483 
24484 
24485 /*
24486  *    Function: sd_mhdioc_release
24487  *
24488  * Description: This routine is the driver entry point for handling ioctl
24489  *		requests to release exclusive access rights to the multihost
24490  *		disk (MHIOCRELEASE).
24491  *
24492  *   Arguments: dev	- the device number
24493  *
24494  * Return Code: 0
24495  *		ENXIO
24496  */
24497 
24498 static int
24499 sd_mhdioc_release(dev_t dev)
24500 {
24501 	struct sd_lun		*un = NULL;
24502 	timeout_id_t		resvd_timeid_save;
24503 	int			resvd_status_save;
24504 	int			rval = 0;
24505 
24506 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24507 		return (ENXIO);
24508 	}
24509 
24510 	mutex_enter(SD_MUTEX(un));
24511 	resvd_status_save = un->un_resvd_status;
24512 	un->un_resvd_status &=
24513 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24514 	if (un->un_resvd_timeid) {
24515 		resvd_timeid_save = un->un_resvd_timeid;
24516 		un->un_resvd_timeid = NULL;
24517 		mutex_exit(SD_MUTEX(un));
24518 		(void) untimeout(resvd_timeid_save);
24519 	} else {
24520 		mutex_exit(SD_MUTEX(un));
24521 	}
24522 
24523 	/*
24524 	 * destroy any pending timeout thread that may be attempting to
24525 	 * reinstate reservation on this device.
24526 	 */
24527 	sd_rmv_resv_reclaim_req(dev);
24528 
24529 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24530 		mutex_enter(SD_MUTEX(un));
24531 		if ((un->un_mhd_token) &&
24532 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24533 			mutex_exit(SD_MUTEX(un));
24534 			(void) sd_check_mhd(dev, 0);
24535 		} else {
24536 			mutex_exit(SD_MUTEX(un));
24537 		}
24538 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24539 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24540 	} else {
24541 		/*
24542 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24543 		 */
24544 		mutex_enter(SD_MUTEX(un));
24545 		un->un_resvd_status = resvd_status_save;
24546 		mutex_exit(SD_MUTEX(un));
24547 	}
24548 	return (rval);
24549 }
24550 
24551 
24552 /*
24553  *    Function: sd_mhdioc_register_devid
24554  *
24555  * Description: This routine is the driver entry point for handling ioctl
24556  *		requests to register the device id (MHIOCREREGISTERDEVID).
24557  *
24558  *		Note: The implementation for this ioctl has been updated to
24559  *		be consistent with the original PSARC case (1999/357)
24560  *		(4375899, 4241671, 4220005)
24561  *
24562  *   Arguments: dev	- the device number
24563  *
24564  * Return Code: 0
24565  *		ENXIO
24566  */
24567 
24568 static int
24569 sd_mhdioc_register_devid(dev_t dev)
24570 {
24571 	struct sd_lun	*un = NULL;
24572 	int		rval = 0;
24573 	sd_ssc_t	*ssc;
24574 
24575 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24576 		return (ENXIO);
24577 	}
24578 
24579 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24580 
24581 	mutex_enter(SD_MUTEX(un));
24582 
24583 	/* If a devid already exists, de-register it */
24584 	if (un->un_devid != NULL) {
24585 		ddi_devid_unregister(SD_DEVINFO(un));
24586 		/*
24587 		 * After unregister devid, needs to free devid memory
24588 		 */
24589 		ddi_devid_free(un->un_devid);
24590 		un->un_devid = NULL;
24591 	}
24592 
24593 	/* Check for reservation conflict */
24594 	mutex_exit(SD_MUTEX(un));
24595 	ssc = sd_ssc_init(un);
24596 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24597 	mutex_enter(SD_MUTEX(un));
24598 
24599 	switch (rval) {
24600 	case 0:
24601 		sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24602 		break;
24603 	case EACCES:
24604 		break;
24605 	default:
24606 		rval = EIO;
24607 	}
24608 
24609 	mutex_exit(SD_MUTEX(un));
24610 	if (rval != 0) {
24611 		if (rval == EIO)
24612 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24613 		else
24614 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24615 	}
24616 	sd_ssc_fini(ssc);
24617 	return (rval);
24618 }
24619 
24620 
24621 /*
24622  *    Function: sd_mhdioc_inkeys
24623  *
24624  * Description: This routine is the driver entry point for handling ioctl
24625  *		requests to issue the SCSI-3 Persistent In Read Keys command
24626  *		to the device (MHIOCGRP_INKEYS).
24627  *
24628  *   Arguments: dev	- the device number
24629  *		arg	- user provided in_keys structure
24630  *		flag	- this argument is a pass through to ddi_copyxxx()
24631  *			  directly from the mode argument of ioctl().
24632  *
24633  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24634  *		ENXIO
24635  *		EFAULT
24636  */
24637 
24638 static int
24639 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24640 {
24641 	struct sd_lun		*un;
24642 	mhioc_inkeys_t		inkeys;
24643 	int			rval = 0;
24644 
24645 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24646 		return (ENXIO);
24647 	}
24648 
24649 #ifdef _MULTI_DATAMODEL
24650 	switch (ddi_model_convert_from(flag & FMODELS)) {
24651 	case DDI_MODEL_ILP32: {
24652 		struct mhioc_inkeys32	inkeys32;
24653 
24654 		if (ddi_copyin(arg, &inkeys32,
24655 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
24656 			return (EFAULT);
24657 		}
24658 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24659 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24660 		    &inkeys, flag)) != 0) {
24661 			return (rval);
24662 		}
24663 		inkeys32.generation = inkeys.generation;
24664 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24665 		    flag) != 0) {
24666 			return (EFAULT);
24667 		}
24668 		break;
24669 	}
24670 	case DDI_MODEL_NONE:
24671 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24672 		    flag) != 0) {
24673 			return (EFAULT);
24674 		}
24675 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24676 		    &inkeys, flag)) != 0) {
24677 			return (rval);
24678 		}
24679 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24680 		    flag) != 0) {
24681 			return (EFAULT);
24682 		}
24683 		break;
24684 	}
24685 
24686 #else /* ! _MULTI_DATAMODEL */
24687 
24688 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24689 		return (EFAULT);
24690 	}
24691 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24692 	if (rval != 0) {
24693 		return (rval);
24694 	}
24695 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24696 		return (EFAULT);
24697 	}
24698 
24699 #endif /* _MULTI_DATAMODEL */
24700 
24701 	return (rval);
24702 }
24703 
24704 
24705 /*
24706  *    Function: sd_mhdioc_inresv
24707  *
24708  * Description: This routine is the driver entry point for handling ioctl
24709  *		requests to issue the SCSI-3 Persistent In Read Reservations
24710  *		command to the device (MHIOCGRP_INKEYS).
24711  *
24712  *   Arguments: dev	- the device number
24713  *		arg	- user provided in_resv structure
24714  *		flag	- this argument is a pass through to ddi_copyxxx()
24715  *			  directly from the mode argument of ioctl().
24716  *
24717  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24718  *		ENXIO
24719  *		EFAULT
24720  */
24721 
24722 static int
24723 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24724 {
24725 	struct sd_lun		*un;
24726 	mhioc_inresvs_t		inresvs;
24727 	int			rval = 0;
24728 
24729 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24730 		return (ENXIO);
24731 	}
24732 
24733 #ifdef _MULTI_DATAMODEL
24734 
24735 	switch (ddi_model_convert_from(flag & FMODELS)) {
24736 	case DDI_MODEL_ILP32: {
24737 		struct mhioc_inresvs32	inresvs32;
24738 
24739 		if (ddi_copyin(arg, &inresvs32,
24740 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24741 			return (EFAULT);
24742 		}
24743 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24744 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24745 		    &inresvs, flag)) != 0) {
24746 			return (rval);
24747 		}
24748 		inresvs32.generation = inresvs.generation;
24749 		if (ddi_copyout(&inresvs32, arg,
24750 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24751 			return (EFAULT);
24752 		}
24753 		break;
24754 	}
24755 	case DDI_MODEL_NONE:
24756 		if (ddi_copyin(arg, &inresvs,
24757 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24758 			return (EFAULT);
24759 		}
24760 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24761 		    &inresvs, flag)) != 0) {
24762 			return (rval);
24763 		}
24764 		if (ddi_copyout(&inresvs, arg,
24765 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24766 			return (EFAULT);
24767 		}
24768 		break;
24769 	}
24770 
24771 #else /* ! _MULTI_DATAMODEL */
24772 
24773 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24774 		return (EFAULT);
24775 	}
24776 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24777 	if (rval != 0) {
24778 		return (rval);
24779 	}
24780 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24781 		return (EFAULT);
24782 	}
24783 
24784 #endif /* ! _MULTI_DATAMODEL */
24785 
24786 	return (rval);
24787 }
24788 
24789 
24790 /*
24791  * The following routines support the clustering functionality described below
24792  * and implement lost reservation reclaim functionality.
24793  *
24794  * Clustering
24795  * ----------
24796  * The clustering code uses two different, independent forms of SCSI
24797  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24798  * Persistent Group Reservations. For any particular disk, it will use either
24799  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24800  *
24801  * SCSI-2
24802  * The cluster software takes ownership of a multi-hosted disk by issuing the
24803  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24804  * MHIOCRELEASE ioctl.  Closely related is the MHIOCENFAILFAST ioctl -- a
24805  * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
24806  * then issues the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the
24807  * driver. The meaning of failfast is that if the driver (on this host) ever
24808  * encounters the scsi error return code RESERVATION_CONFLICT from the device,
24809  * it should immediately panic the host. The motivation for this ioctl is that
24810  * if this host does encounter reservation conflict, the underlying cause is
24811  * that some other host of the cluster has decided that this host is no longer
24812  * in the cluster and has seized control of the disks for itself. Since this
24813  * host is no longer in the cluster, it ought to panic itself. The
24814  * MHIOCENFAILFAST ioctl does two things:
24815  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24816  *      error to panic the host
24817  *      (b) it sets up a periodic timer to test whether this host still has
24818  *      "access" (in that no other host has reserved the device):  if the
24819  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24820  *      purpose of that periodic timer is to handle scenarios where the host is
24821  *      otherwise temporarily quiescent, temporarily doing no real i/o.
24822  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24823  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
24824  * the device itself.
24825  *
24826  * SCSI-3 PGR
24827  * A direct semantic implementation of the SCSI-3 Persistent Reservation
24828  * facility is supported through the shared multihost disk ioctls
24829  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24830  * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR)
24831  *
24832  * Reservation Reclaim:
24833  * --------------------
24834  * To support the lost reservation reclaim operations this driver creates a
24835  * single thread to handle reinstating reservations on all devices that have
24836  * lost reservations sd_resv_reclaim_requests are logged for all devices that
24837  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24838  * and the reservation reclaim thread loops through the requests to regain the
24839  * lost reservations.
24840  */
24841 
24842 /*
24843  *    Function: sd_check_mhd()
24844  *
24845  * Description: This function sets up and submits a scsi watch request or
24846  *		terminates an existing watch request. This routine is used in
24847  *		support of reservation reclaim.
24848  *
24849  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
24850  *			 among multiple watches that share the callback function
24851  *		interval - the number of microseconds specifying the watch
24852  *			   interval for issuing TEST UNIT READY commands. If
24853  *			   set to 0 the watch should be terminated. If the
24854  *			   interval is set to 0 and if the device is required
24855  *			   to hold reservation while disabling failfast, the
24856  *			   watch is restarted with an interval of
24857  *			   reinstate_resv_delay.
24858  *
24859  * Return Code: 0	   - Successful submit/terminate of scsi watch request
24860  *		ENXIO      - Indicates an invalid device was specified
24861  *		EAGAIN     - Unable to submit the scsi watch request
24862  */
24863 
24864 static int
24865 sd_check_mhd(dev_t dev, int interval)
24866 {
24867 	struct sd_lun	*un;
24868 	opaque_t	token;
24869 
24870 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24871 		return (ENXIO);
24872 	}
24873 
24874 	/* is this a watch termination request? */
24875 	if (interval == 0) {
24876 		mutex_enter(SD_MUTEX(un));
24877 		/* if there is an existing watch task then terminate it */
24878 		if (un->un_mhd_token) {
24879 			token = un->un_mhd_token;
24880 			un->un_mhd_token = NULL;
24881 			mutex_exit(SD_MUTEX(un));
24882 			(void) scsi_watch_request_terminate(token,
24883 			    SCSI_WATCH_TERMINATE_ALL_WAIT);
24884 			mutex_enter(SD_MUTEX(un));
24885 		} else {
24886 			mutex_exit(SD_MUTEX(un));
24887 			/*
24888 			 * Note: If we return here we don't check for the
24889 			 * failfast case. This is the original legacy
24890 			 * implementation but perhaps we should be checking
24891 			 * the failfast case.
24892 			 */
24893 			return (0);
24894 		}
24895 		/*
24896 		 * If the device is required to hold reservation while
24897 		 * disabling failfast, we need to restart the scsi_watch
24898 		 * routine with an interval of reinstate_resv_delay.
24899 		 */
24900 		if (un->un_resvd_status & SD_RESERVE) {
24901 			interval = sd_reinstate_resv_delay/1000;
24902 		} else {
24903 			/* no failfast so bail */
24904 			mutex_exit(SD_MUTEX(un));
24905 			return (0);
24906 		}
24907 		mutex_exit(SD_MUTEX(un));
24908 	}
24909 
24910 	/*
24911 	 * adjust minimum time interval to 1 second,
24912 	 * and convert from msecs to usecs
24913 	 */
24914 	if (interval > 0 && interval < 1000) {
24915 		interval = 1000;
24916 	}
24917 	interval *= 1000;
24918 
24919 	/*
24920 	 * submit the request to the scsi_watch service
24921 	 */
24922 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24923 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24924 	if (token == NULL) {
24925 		return (EAGAIN);
24926 	}
24927 
24928 	/*
24929 	 * save token for termination later on
24930 	 */
24931 	mutex_enter(SD_MUTEX(un));
24932 	un->un_mhd_token = token;
24933 	mutex_exit(SD_MUTEX(un));
24934 	return (0);
24935 }
24936 
24937 
24938 /*
24939  *    Function: sd_mhd_watch_cb()
24940  *
24941  * Description: This function is the call back function used by the scsi watch
24942  *		facility. The scsi watch facility sends the "Test Unit Ready"
24943  *		and processes the status. If applicable (i.e. a "Unit Attention"
24944  *		status and automatic "Request Sense" not used) the scsi watch
24945  *		facility will send a "Request Sense" and retrieve the sense data
24946  *		to be passed to this callback function. In either case the
24947  *		automatic "Request Sense" or the facility submitting one, this
24948  *		callback is passed the status and sense data.
24949  *
24950  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24951  *			among multiple watches that share this callback function
24952  *		resultp - scsi watch facility result packet containing scsi
24953  *			  packet, status byte and sense data
24954  *
24955  * Return Code: 0 - continue the watch task
24956  *		non-zero - terminate the watch task
24957  */
24958 
24959 static int
24960 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24961 {
24962 	struct sd_lun			*un;
24963 	struct scsi_status		*statusp;
24964 	uint8_t				*sensep;
24965 	struct scsi_pkt			*pkt;
24966 	uchar_t				actual_sense_length;
24967 	dev_t  				dev = (dev_t)arg;
24968 
24969 	ASSERT(resultp != NULL);
24970 	statusp			= resultp->statusp;
24971 	sensep			= (uint8_t *)resultp->sensep;
24972 	pkt			= resultp->pkt;
24973 	actual_sense_length	= resultp->actual_sense_length;
24974 
24975 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24976 		return (ENXIO);
24977 	}
24978 
24979 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
24980 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24981 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24982 
24983 	/* Begin processing of the status and/or sense data */
24984 	if (pkt->pkt_reason != CMD_CMPLT) {
24985 		/* Handle the incomplete packet */
24986 		sd_mhd_watch_incomplete(un, pkt);
24987 		return (0);
24988 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24989 		if (*((unsigned char *)statusp)
24990 		    == STATUS_RESERVATION_CONFLICT) {
24991 			/*
24992 			 * Handle a reservation conflict by panicking if
24993 			 * configured for failfast or by logging the conflict
24994 			 * and updating the reservation status
24995 			 */
24996 			mutex_enter(SD_MUTEX(un));
24997 			if ((un->un_resvd_status & SD_FAILFAST) &&
24998 			    (sd_failfast_enable)) {
24999 				sd_panic_for_res_conflict(un);
25000 				/*NOTREACHED*/
25001 			}
25002 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25003 			    "sd_mhd_watch_cb: Reservation Conflict\n");
25004 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
25005 			mutex_exit(SD_MUTEX(un));
25006 		}
25007 	}
25008 
25009 	if (sensep != NULL) {
25010 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
25011 			mutex_enter(SD_MUTEX(un));
25012 			if ((scsi_sense_asc(sensep) ==
25013 			    SD_SCSI_RESET_SENSE_CODE) &&
25014 			    (un->un_resvd_status & SD_RESERVE)) {
25015 				/*
25016 				 * The additional sense code indicates a power
25017 				 * on or bus device reset has occurred; update
25018 				 * the reservation status.
25019 				 */
25020 				un->un_resvd_status |=
25021 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25022 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25023 				    "sd_mhd_watch_cb: Lost Reservation\n");
25024 			}
25025 		} else {
25026 			return (0);
25027 		}
25028 	} else {
25029 		mutex_enter(SD_MUTEX(un));
25030 	}
25031 
25032 	if ((un->un_resvd_status & SD_RESERVE) &&
25033 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
25034 		if (un->un_resvd_status & SD_WANT_RESERVE) {
25035 			/*
25036 			 * A reset occurred in between the last probe and this
25037 			 * one so if a timeout is pending cancel it.
25038 			 */
25039 			if (un->un_resvd_timeid) {
25040 				timeout_id_t temp_id = un->un_resvd_timeid;
25041 				un->un_resvd_timeid = NULL;
25042 				mutex_exit(SD_MUTEX(un));
25043 				(void) untimeout(temp_id);
25044 				mutex_enter(SD_MUTEX(un));
25045 			}
25046 			un->un_resvd_status &= ~SD_WANT_RESERVE;
25047 		}
25048 		if (un->un_resvd_timeid == 0) {
25049 			/* Schedule a timeout to handle the lost reservation */
25050 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25051 			    (void *)dev,
25052 			    drv_usectohz(sd_reinstate_resv_delay));
25053 		}
25054 	}
25055 	mutex_exit(SD_MUTEX(un));
25056 	return (0);
25057 }
25058 
25059 
25060 /*
25061  *    Function: sd_mhd_watch_incomplete()
25062  *
25063  * Description: This function is used to find out why a scsi pkt sent by the
25064  *		scsi watch facility was not completed. Under some scenarios this
25065  *		routine will return. Otherwise it will send a bus reset to see
25066  *		if the drive is still online.
25067  *
25068  *   Arguments: un  - driver soft state (unit) structure
25069  *		pkt - incomplete scsi pkt
25070  */
25071 
25072 static void
25073 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25074 {
25075 	int	be_chatty;
25076 	int	perr;
25077 
25078 	ASSERT(pkt != NULL);
25079 	ASSERT(un != NULL);
25080 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
25081 	perr		= (pkt->pkt_statistics & STAT_PERR);
25082 
25083 	mutex_enter(SD_MUTEX(un));
25084 	if (un->un_state == SD_STATE_DUMPING) {
25085 		mutex_exit(SD_MUTEX(un));
25086 		return;
25087 	}
25088 
25089 	switch (pkt->pkt_reason) {
25090 	case CMD_UNX_BUS_FREE:
25091 		/*
25092 		 * If we had a parity error that caused the target to drop BSY*,
25093 		 * don't be chatty about it.
25094 		 */
25095 		if (perr && be_chatty) {
25096 			be_chatty = 0;
25097 		}
25098 		break;
25099 	case CMD_TAG_REJECT:
25100 		/*
25101 		 * The SCSI-2 spec states that a tag reject will be sent by the
25102 		 * target if tagged queuing is not supported. A tag reject may
25103 		 * also be sent during certain initialization periods or to
25104 		 * control internal resources. For the latter case the target
25105 		 * may also return Queue Full.
25106 		 *
25107 		 * If this driver receives a tag reject from a target that is
25108 		 * going through an init period or controlling internal
25109 		 * resources tagged queuing will be disabled. This is a less
25110 		 * than optimal behavior but the driver is unable to determine
25111 		 * the target state and assumes tagged queueing is not supported
25112 		 */
25113 		pkt->pkt_flags = 0;
25114 		un->un_tagflags = 0;
25115 
25116 		if (un->un_f_opt_queueing == TRUE) {
25117 			un->un_throttle = min(un->un_throttle, 3);
25118 		} else {
25119 			un->un_throttle = 1;
25120 		}
25121 		mutex_exit(SD_MUTEX(un));
25122 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25123 		mutex_enter(SD_MUTEX(un));
25124 		break;
25125 	case CMD_INCOMPLETE:
25126 		/*
25127 		 * The transport stopped with an abnormal state, fallthrough and
25128 		 * reset the target and/or bus unless selection did not complete
25129 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
25130 		 * go through a target/bus reset
25131 		 */
25132 		if (pkt->pkt_state == STATE_GOT_BUS) {
25133 			break;
25134 		}
25135 		/*FALLTHROUGH*/
25136 
25137 	case CMD_TIMEOUT:
25138 	default:
25139 		/*
25140 		 * The lun may still be running the command, so a lun reset
25141 		 * should be attempted. If the lun reset fails or cannot be
25142 		 * issued, than try a target reset. Lastly try a bus reset.
25143 		 */
25144 		if ((pkt->pkt_statistics &
25145 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25146 			int reset_retval = 0;
25147 			mutex_exit(SD_MUTEX(un));
25148 			if (un->un_f_allow_bus_device_reset == TRUE) {
25149 				if (un->un_f_lun_reset_enabled == TRUE) {
25150 					reset_retval =
25151 					    scsi_reset(SD_ADDRESS(un),
25152 					    RESET_LUN);
25153 				}
25154 				if (reset_retval == 0) {
25155 					reset_retval =
25156 					    scsi_reset(SD_ADDRESS(un),
25157 					    RESET_TARGET);
25158 				}
25159 			}
25160 			if (reset_retval == 0) {
25161 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25162 			}
25163 			mutex_enter(SD_MUTEX(un));
25164 		}
25165 		break;
25166 	}
25167 
25168 	/* A device/bus reset has occurred; update the reservation status. */
25169 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25170 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25171 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25172 			un->un_resvd_status |=
25173 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25174 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25175 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25176 		}
25177 	}
25178 
25179 	/*
25180 	 * The disk has been turned off; Update the device state.
25181 	 *
25182 	 * Note: Should we be offlining the disk here?
25183 	 */
25184 	if (pkt->pkt_state == STATE_GOT_BUS) {
25185 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25186 		    "Disk not responding to selection\n");
25187 		if (un->un_state != SD_STATE_OFFLINE) {
25188 			New_state(un, SD_STATE_OFFLINE);
25189 		}
25190 	} else if (be_chatty) {
25191 		/*
25192 		 * suppress messages if they are all the same pkt reason;
25193 		 * with TQ, many (up to 256) are returned with the same
25194 		 * pkt_reason
25195 		 */
25196 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25197 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25198 			    "sd_mhd_watch_incomplete: "
25199 			    "SCSI transport failed: reason '%s'\n",
25200 			    scsi_rname(pkt->pkt_reason));
25201 		}
25202 	}
25203 	un->un_last_pkt_reason = pkt->pkt_reason;
25204 	mutex_exit(SD_MUTEX(un));
25205 }
25206 
25207 
25208 /*
25209  *    Function: sd_sname()
25210  *
25211  * Description: This is a simple little routine to return a string containing
25212  *		a printable description of command status byte for use in
25213  *		logging.
25214  *
25215  *   Arguments: status - pointer to a status byte
25216  *
25217  * Return Code: char * - string containing status description.
25218  */
25219 
25220 static char *
25221 sd_sname(uchar_t status)
25222 {
25223 	switch (status & STATUS_MASK) {
25224 	case STATUS_GOOD:
25225 		return ("good status");
25226 	case STATUS_CHECK:
25227 		return ("check condition");
25228 	case STATUS_MET:
25229 		return ("condition met");
25230 	case STATUS_BUSY:
25231 		return ("busy");
25232 	case STATUS_INTERMEDIATE:
25233 		return ("intermediate");
25234 	case STATUS_INTERMEDIATE_MET:
25235 		return ("intermediate - condition met");
25236 	case STATUS_RESERVATION_CONFLICT:
25237 		return ("reservation_conflict");
25238 	case STATUS_TERMINATED:
25239 		return ("command terminated");
25240 	case STATUS_QFULL:
25241 		return ("queue full");
25242 	default:
25243 		return ("<unknown status>");
25244 	}
25245 }
25246 
25247 
25248 /*
25249  *    Function: sd_mhd_resvd_recover()
25250  *
25251  * Description: This function adds a reservation entry to the
25252  *		sd_resv_reclaim_request list and signals the reservation
25253  *		reclaim thread that there is work pending. If the reservation
25254  *		reclaim thread has not been previously created this function
25255  *		will kick it off.
25256  *
25257  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25258  *			among multiple watches that share this callback function
25259  *
25260  *     Context: This routine is called by timeout() and is run in interrupt
25261  *		context. It must not sleep or call other functions which may
25262  *		sleep.
25263  */
25264 
25265 static void
25266 sd_mhd_resvd_recover(void *arg)
25267 {
25268 	dev_t			dev = (dev_t)arg;
25269 	struct sd_lun		*un;
25270 	struct sd_thr_request	*sd_treq = NULL;
25271 	struct sd_thr_request	*sd_cur = NULL;
25272 	struct sd_thr_request	*sd_prev = NULL;
25273 	int			already_there = 0;
25274 
25275 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25276 		return;
25277 	}
25278 
25279 	mutex_enter(SD_MUTEX(un));
25280 	un->un_resvd_timeid = NULL;
25281 	if (un->un_resvd_status & SD_WANT_RESERVE) {
25282 		/*
25283 		 * There was a reset so don't issue the reserve, allow the
25284 		 * sd_mhd_watch_cb callback function to notice this and
25285 		 * reschedule the timeout for reservation.
25286 		 */
25287 		mutex_exit(SD_MUTEX(un));
25288 		return;
25289 	}
25290 	mutex_exit(SD_MUTEX(un));
25291 
25292 	/*
25293 	 * Add this device to the sd_resv_reclaim_request list and the
25294 	 * sd_resv_reclaim_thread should take care of the rest.
25295 	 *
25296 	 * Note: We can't sleep in this context so if the memory allocation
25297 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
25298 	 * reschedule the timeout for reservation.  (4378460)
25299 	 */
25300 	sd_treq = (struct sd_thr_request *)
25301 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25302 	if (sd_treq == NULL) {
25303 		return;
25304 	}
25305 
25306 	sd_treq->sd_thr_req_next = NULL;
25307 	sd_treq->dev = dev;
25308 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25309 	if (sd_tr.srq_thr_req_head == NULL) {
25310 		sd_tr.srq_thr_req_head = sd_treq;
25311 	} else {
25312 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25313 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25314 			if (sd_cur->dev == dev) {
25315 				/*
25316 				 * already in Queue so don't log
25317 				 * another request for the device
25318 				 */
25319 				already_there = 1;
25320 				break;
25321 			}
25322 			sd_prev = sd_cur;
25323 		}
25324 		if (!already_there) {
25325 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25326 			    "logging request for %lx\n", dev);
25327 			sd_prev->sd_thr_req_next = sd_treq;
25328 		} else {
25329 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
25330 		}
25331 	}
25332 
25333 	/*
25334 	 * Create a kernel thread to do the reservation reclaim and free up this
25335 	 * thread. We cannot block this thread while we go away to do the
25336 	 * reservation reclaim
25337 	 */
25338 	if (sd_tr.srq_resv_reclaim_thread == NULL)
25339 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25340 		    sd_resv_reclaim_thread, NULL,
25341 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
25342 
25343 	/* Tell the reservation reclaim thread that it has work to do */
25344 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
25345 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25346 }
25347 
25348 /*
25349  *    Function: sd_resv_reclaim_thread()
25350  *
25351  * Description: This function implements the reservation reclaim operations
25352  *
25353  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25354  *		      among multiple watches that share this callback function
25355  */
25356 
25357 static void
25358 sd_resv_reclaim_thread()
25359 {
25360 	struct sd_lun		*un;
25361 	struct sd_thr_request	*sd_mhreq;
25362 
25363 	/* Wait for work */
25364 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25365 	if (sd_tr.srq_thr_req_head == NULL) {
25366 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
25367 		    &sd_tr.srq_resv_reclaim_mutex);
25368 	}
25369 
25370 	/* Loop while we have work */
25371 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25372 		un = ddi_get_soft_state(sd_state,
25373 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
25374 		if (un == NULL) {
25375 			/*
25376 			 * softstate structure is NULL so just
25377 			 * dequeue the request and continue
25378 			 */
25379 			sd_tr.srq_thr_req_head =
25380 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25381 			kmem_free(sd_tr.srq_thr_cur_req,
25382 			    sizeof (struct sd_thr_request));
25383 			continue;
25384 		}
25385 
25386 		/* dequeue the request */
25387 		sd_mhreq = sd_tr.srq_thr_cur_req;
25388 		sd_tr.srq_thr_req_head =
25389 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25390 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25391 
25392 		/*
25393 		 * Reclaim reservation only if SD_RESERVE is still set. There
25394 		 * may have been a call to MHIOCRELEASE before we got here.
25395 		 */
25396 		mutex_enter(SD_MUTEX(un));
25397 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25398 			/*
25399 			 * Note: The SD_LOST_RESERVE flag is cleared before
25400 			 * reclaiming the reservation. If this is done after the
25401 			 * call to sd_reserve_release a reservation loss in the
25402 			 * window between pkt completion of reserve cmd and
25403 			 * mutex_enter below may not be recognized
25404 			 */
25405 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25406 			mutex_exit(SD_MUTEX(un));
25407 
25408 			if (sd_reserve_release(sd_mhreq->dev,
25409 			    SD_RESERVE) == 0) {
25410 				mutex_enter(SD_MUTEX(un));
25411 				un->un_resvd_status |= SD_RESERVE;
25412 				mutex_exit(SD_MUTEX(un));
25413 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25414 				    "sd_resv_reclaim_thread: "
25415 				    "Reservation Recovered\n");
25416 			} else {
25417 				mutex_enter(SD_MUTEX(un));
25418 				un->un_resvd_status |= SD_LOST_RESERVE;
25419 				mutex_exit(SD_MUTEX(un));
25420 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25421 				    "sd_resv_reclaim_thread: Failed "
25422 				    "Reservation Recovery\n");
25423 			}
25424 		} else {
25425 			mutex_exit(SD_MUTEX(un));
25426 		}
25427 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25428 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25429 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25430 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25431 		/*
25432 		 * wakeup the destroy thread if anyone is waiting on
25433 		 * us to complete.
25434 		 */
25435 		cv_signal(&sd_tr.srq_inprocess_cv);
25436 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25437 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25438 	}
25439 
25440 	/*
25441 	 * cleanup the sd_tr structure now that this thread will not exist
25442 	 */
25443 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25444 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25445 	sd_tr.srq_resv_reclaim_thread = NULL;
25446 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25447 	thread_exit();
25448 }
25449 
25450 
25451 /*
25452  *    Function: sd_rmv_resv_reclaim_req()
25453  *
25454  * Description: This function removes any pending reservation reclaim requests
25455  *		for the specified device.
25456  *
25457  *   Arguments: dev - the device 'dev_t'
25458  */
25459 
25460 static void
25461 sd_rmv_resv_reclaim_req(dev_t dev)
25462 {
25463 	struct sd_thr_request *sd_mhreq;
25464 	struct sd_thr_request *sd_prev;
25465 
25466 	/* Remove a reservation reclaim request from the list */
25467 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25468 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25469 		/*
25470 		 * We are attempting to reinstate reservation for
25471 		 * this device. We wait for sd_reserve_release()
25472 		 * to return before we return.
25473 		 */
25474 		cv_wait(&sd_tr.srq_inprocess_cv,
25475 		    &sd_tr.srq_resv_reclaim_mutex);
25476 	} else {
25477 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25478 		if (sd_mhreq && sd_mhreq->dev == dev) {
25479 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25480 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25481 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25482 			return;
25483 		}
25484 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25485 			if (sd_mhreq && sd_mhreq->dev == dev) {
25486 				break;
25487 			}
25488 			sd_prev = sd_mhreq;
25489 		}
25490 		if (sd_mhreq != NULL) {
25491 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25492 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25493 		}
25494 	}
25495 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25496 }
25497 
25498 
25499 /*
25500  *    Function: sd_mhd_reset_notify_cb()
25501  *
25502  * Description: This is a call back function for scsi_reset_notify. This
25503  *		function updates the softstate reserved status and logs the
25504  *		reset. The driver scsi watch facility callback function
25505  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25506  *		will reclaim the reservation.
25507  *
25508  *   Arguments: arg  - driver soft state (unit) structure
25509  */
25510 
25511 static void
25512 sd_mhd_reset_notify_cb(caddr_t arg)
25513 {
25514 	struct sd_lun *un = (struct sd_lun *)arg;
25515 
25516 	mutex_enter(SD_MUTEX(un));
25517 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25518 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25519 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25520 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25521 	}
25522 	mutex_exit(SD_MUTEX(un));
25523 }
25524 
25525 
25526 /*
25527  *    Function: sd_take_ownership()
25528  *
25529  * Description: This routine implements an algorithm to achieve a stable
25530  *		reservation on disks which don't implement priority reserve,
25531  *		and makes sure that other host lose re-reservation attempts.
25532  *		This algorithm contains of a loop that keeps issuing the RESERVE
25533  *		for some period of time (min_ownership_delay, default 6 seconds)
25534  *		During that loop, it looks to see if there has been a bus device
25535  *		reset or bus reset (both of which cause an existing reservation
25536  *		to be lost). If the reservation is lost issue RESERVE until a
25537  *		period of min_ownership_delay with no resets has gone by, or
25538  *		until max_ownership_delay has expired. This loop ensures that
25539  *		the host really did manage to reserve the device, in spite of
25540  *		resets. The looping for min_ownership_delay (default six
25541  *		seconds) is important to early generation clustering products,
25542  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25543  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25544  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25545  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25546  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25547  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25548  *		no longer "owns" the disk and will have panicked itself.  Thus,
25549  *		the host issuing the MHIOCTKOWN is assured (with timing
25550  *		dependencies) that by the time it actually starts to use the
25551  *		disk for real work, the old owner is no longer accessing it.
25552  *
25553  *		min_ownership_delay is the minimum amount of time for which the
25554  *		disk must be reserved continuously devoid of resets before the
25555  *		MHIOCTKOWN ioctl will return success.
25556  *
25557  *		max_ownership_delay indicates the amount of time by which the
25558  *		take ownership should succeed or timeout with an error.
25559  *
25560  *   Arguments: dev - the device 'dev_t'
25561  *		*p  - struct containing timing info.
25562  *
25563  * Return Code: 0 for success or error code
25564  */
25565 
25566 static int
25567 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25568 {
25569 	struct sd_lun	*un;
25570 	int		rval;
25571 	int		err;
25572 	int		reservation_count   = 0;
25573 	int		min_ownership_delay =  6000000; /* in usec */
25574 	int		max_ownership_delay = 30000000; /* in usec */
25575 	clock_t		start_time;	/* starting time of this algorithm */
25576 	clock_t		end_time;	/* time limit for giving up */
25577 	clock_t		ownership_time;	/* time limit for stable ownership */
25578 	clock_t		current_time;
25579 	clock_t		previous_current_time;
25580 
25581 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25582 		return (ENXIO);
25583 	}
25584 
25585 	/*
25586 	 * Attempt a device reservation. A priority reservation is requested.
25587 	 */
25588 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25589 	    != SD_SUCCESS) {
25590 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25591 		    "sd_take_ownership: return(1)=%d\n", rval);
25592 		return (rval);
25593 	}
25594 
25595 	/* Update the softstate reserved status to indicate the reservation */
25596 	mutex_enter(SD_MUTEX(un));
25597 	un->un_resvd_status |= SD_RESERVE;
25598 	un->un_resvd_status &=
25599 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25600 	mutex_exit(SD_MUTEX(un));
25601 
25602 	if (p != NULL) {
25603 		if (p->min_ownership_delay != 0) {
25604 			min_ownership_delay = p->min_ownership_delay * 1000;
25605 		}
25606 		if (p->max_ownership_delay != 0) {
25607 			max_ownership_delay = p->max_ownership_delay * 1000;
25608 		}
25609 	}
25610 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25611 	    "sd_take_ownership: min, max delays: %d, %d\n",
25612 	    min_ownership_delay, max_ownership_delay);
25613 
25614 	start_time = ddi_get_lbolt();
25615 	current_time	= start_time;
25616 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
25617 	end_time	= start_time + drv_usectohz(max_ownership_delay);
25618 
25619 	while (current_time - end_time < 0) {
25620 		delay(drv_usectohz(500000));
25621 
25622 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25623 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25624 				mutex_enter(SD_MUTEX(un));
25625 				rval = (un->un_resvd_status &
25626 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25627 				mutex_exit(SD_MUTEX(un));
25628 				break;
25629 			}
25630 		}
25631 		previous_current_time = current_time;
25632 		current_time = ddi_get_lbolt();
25633 		mutex_enter(SD_MUTEX(un));
25634 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25635 			ownership_time = ddi_get_lbolt() +
25636 			    drv_usectohz(min_ownership_delay);
25637 			reservation_count = 0;
25638 		} else {
25639 			reservation_count++;
25640 		}
25641 		un->un_resvd_status |= SD_RESERVE;
25642 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25643 		mutex_exit(SD_MUTEX(un));
25644 
25645 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25646 		    "sd_take_ownership: ticks for loop iteration=%ld, "
25647 		    "reservation=%s\n", (current_time - previous_current_time),
25648 		    reservation_count ? "ok" : "reclaimed");
25649 
25650 		if (current_time - ownership_time >= 0 &&
25651 		    reservation_count >= 4) {
25652 			rval = 0; /* Achieved a stable ownership */
25653 			break;
25654 		}
25655 		if (current_time - end_time >= 0) {
25656 			rval = EACCES; /* No ownership in max possible time */
25657 			break;
25658 		}
25659 	}
25660 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25661 	    "sd_take_ownership: return(2)=%d\n", rval);
25662 	return (rval);
25663 }
25664 
25665 
25666 /*
25667  *    Function: sd_reserve_release()
25668  *
25669  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25670  *		PRIORITY RESERVE commands based on a user specified command type
25671  *
25672  *   Arguments: dev - the device 'dev_t'
25673  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25674  *		      SD_RESERVE, SD_RELEASE
25675  *
25676  * Return Code: 0 or Error Code
25677  */
25678 
25679 static int
25680 sd_reserve_release(dev_t dev, int cmd)
25681 {
25682 	struct uscsi_cmd	*com = NULL;
25683 	struct sd_lun		*un = NULL;
25684 	char			cdb[CDB_GROUP0];
25685 	int			rval;
25686 
25687 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25688 	    (cmd == SD_PRIORITY_RESERVE));
25689 
25690 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25691 		return (ENXIO);
25692 	}
25693 
25694 	/* instantiate and initialize the command and cdb */
25695 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25696 	bzero(cdb, CDB_GROUP0);
25697 	com->uscsi_flags   = USCSI_SILENT;
25698 	com->uscsi_timeout = un->un_reserve_release_time;
25699 	com->uscsi_cdblen  = CDB_GROUP0;
25700 	com->uscsi_cdb	   = cdb;
25701 	if (cmd == SD_RELEASE) {
25702 		cdb[0] = SCMD_RELEASE;
25703 	} else {
25704 		cdb[0] = SCMD_RESERVE;
25705 	}
25706 
25707 	/* Send the command. */
25708 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25709 	    SD_PATH_STANDARD);
25710 
25711 	/*
25712 	 * "break" a reservation that is held by another host, by issuing a
25713 	 * reset if priority reserve is desired, and we could not get the
25714 	 * device.
25715 	 */
25716 	if ((cmd == SD_PRIORITY_RESERVE) &&
25717 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25718 		/*
25719 		 * First try to reset the LUN. If we cannot, then try a target
25720 		 * reset, followed by a bus reset if the target reset fails.
25721 		 */
25722 		int reset_retval = 0;
25723 		if (un->un_f_lun_reset_enabled == TRUE) {
25724 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25725 		}
25726 		if (reset_retval == 0) {
25727 			/* The LUN reset either failed or was not issued */
25728 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25729 		}
25730 		if ((reset_retval == 0) &&
25731 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25732 			rval = EIO;
25733 			kmem_free(com, sizeof (*com));
25734 			return (rval);
25735 		}
25736 
25737 		bzero(com, sizeof (struct uscsi_cmd));
25738 		com->uscsi_flags   = USCSI_SILENT;
25739 		com->uscsi_cdb	   = cdb;
25740 		com->uscsi_cdblen  = CDB_GROUP0;
25741 		com->uscsi_timeout = 5;
25742 
25743 		/*
25744 		 * Reissue the last reserve command, this time without request
25745 		 * sense.  Assume that it is just a regular reserve command.
25746 		 */
25747 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25748 		    SD_PATH_STANDARD);
25749 	}
25750 
25751 	/* Return an error if still getting a reservation conflict. */
25752 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25753 		rval = EACCES;
25754 	}
25755 
25756 	kmem_free(com, sizeof (*com));
25757 	return (rval);
25758 }
25759 
25760 
25761 #define	SD_NDUMP_RETRIES	12
25762 /*
25763  *	System Crash Dump routine
25764  */
25765 
25766 static int
25767 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25768 {
25769 	int		instance;
25770 	int		partition;
25771 	int		i;
25772 	int		err;
25773 	struct sd_lun	*un;
25774 	struct scsi_pkt *wr_pktp;
25775 	struct buf	*wr_bp;
25776 	struct buf	wr_buf;
25777 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
25778 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
25779 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
25780 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
25781 	size_t		io_start_offset;
25782 	int		doing_rmw = FALSE;
25783 	int		rval;
25784 	ssize_t		dma_resid;
25785 	daddr_t		oblkno;
25786 	diskaddr_t	nblks = 0;
25787 	diskaddr_t	start_block;
25788 
25789 	instance = SDUNIT(dev);
25790 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25791 	    !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25792 		return (ENXIO);
25793 	}
25794 
25795 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25796 
25797 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25798 
25799 	partition = SDPART(dev);
25800 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25801 
25802 	if (!(NOT_DEVBSIZE(un))) {
25803 		int secmask = 0;
25804 		int blknomask = 0;
25805 
25806 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25807 		secmask = un->un_tgt_blocksize - 1;
25808 
25809 		if (blkno & blknomask) {
25810 			SD_TRACE(SD_LOG_DUMP, un,
25811 			    "sddump: dump start block not modulo %d\n",
25812 			    un->un_tgt_blocksize);
25813 			return (EINVAL);
25814 		}
25815 
25816 		if ((nblk * DEV_BSIZE) & secmask) {
25817 			SD_TRACE(SD_LOG_DUMP, un,
25818 			    "sddump: dump length not modulo %d\n",
25819 			    un->un_tgt_blocksize);
25820 			return (EINVAL);
25821 		}
25822 
25823 	}
25824 
25825 	/* Validate blocks to dump at against partition size. */
25826 
25827 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
25828 	    &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
25829 
25830 	if (NOT_DEVBSIZE(un)) {
25831 		if ((blkno + nblk) > nblks) {
25832 			SD_TRACE(SD_LOG_DUMP, un,
25833 			    "sddump: dump range larger than partition: "
25834 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25835 			    blkno, nblk, nblks);
25836 			return (EINVAL);
25837 		}
25838 	} else {
25839 		if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
25840 		    (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
25841 			SD_TRACE(SD_LOG_DUMP, un,
25842 			    "sddump: dump range larger than partition: "
25843 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25844 			    blkno, nblk, nblks);
25845 			return (EINVAL);
25846 		}
25847 	}
25848 
25849 	mutex_enter(&un->un_pm_mutex);
25850 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25851 		struct scsi_pkt *start_pktp;
25852 
25853 		mutex_exit(&un->un_pm_mutex);
25854 
25855 		/*
25856 		 * use pm framework to power on HBA 1st
25857 		 */
25858 		(void) pm_raise_power(SD_DEVINFO(un), 0,
25859 		    SD_PM_STATE_ACTIVE(un));
25860 
25861 		/*
25862 		 * Dump no long uses sdpower to power on a device, it's
25863 		 * in-line here so it can be done in polled mode.
25864 		 */
25865 
25866 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25867 
25868 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25869 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25870 
25871 		if (start_pktp == NULL) {
25872 			/* We were not given a SCSI packet, fail. */
25873 			return (EIO);
25874 		}
25875 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25876 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25877 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25878 		start_pktp->pkt_flags = FLAG_NOINTR;
25879 
25880 		mutex_enter(SD_MUTEX(un));
25881 		SD_FILL_SCSI1_LUN(un, start_pktp);
25882 		mutex_exit(SD_MUTEX(un));
25883 		/*
25884 		 * Scsi_poll returns 0 (success) if the command completes and
25885 		 * the status block is STATUS_GOOD.
25886 		 */
25887 		if (sd_scsi_poll(un, start_pktp) != 0) {
25888 			scsi_destroy_pkt(start_pktp);
25889 			return (EIO);
25890 		}
25891 		scsi_destroy_pkt(start_pktp);
25892 		(void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
25893 		    SD_PM_STATE_CHANGE);
25894 	} else {
25895 		mutex_exit(&un->un_pm_mutex);
25896 	}
25897 
25898 	mutex_enter(SD_MUTEX(un));
25899 	un->un_throttle = 0;
25900 
25901 	/*
25902 	 * The first time through, reset the specific target device.
25903 	 * However, when cpr calls sddump we know that sd is in a
25904 	 * a good state so no bus reset is required.
25905 	 * Clear sense data via Request Sense cmd.
25906 	 * In sddump we don't care about allow_bus_device_reset anymore
25907 	 */
25908 
25909 	if ((un->un_state != SD_STATE_SUSPENDED) &&
25910 	    (un->un_state != SD_STATE_DUMPING)) {
25911 
25912 		New_state(un, SD_STATE_DUMPING);
25913 
25914 		if (un->un_f_is_fibre == FALSE) {
25915 			mutex_exit(SD_MUTEX(un));
25916 			/*
25917 			 * Attempt a bus reset for parallel scsi.
25918 			 *
25919 			 * Note: A bus reset is required because on some host
25920 			 * systems (i.e. E420R) a bus device reset is
25921 			 * insufficient to reset the state of the target.
25922 			 *
25923 			 * Note: Don't issue the reset for fibre-channel,
25924 			 * because this tends to hang the bus (loop) for
25925 			 * too long while everyone is logging out and in
25926 			 * and the deadman timer for dumping will fire
25927 			 * before the dump is complete.
25928 			 */
25929 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25930 				mutex_enter(SD_MUTEX(un));
25931 				Restore_state(un);
25932 				mutex_exit(SD_MUTEX(un));
25933 				return (EIO);
25934 			}
25935 
25936 			/* Delay to give the device some recovery time. */
25937 			drv_usecwait(10000);
25938 
25939 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
25940 				SD_INFO(SD_LOG_DUMP, un,
25941 				    "sddump: sd_send_polled_RQS failed\n");
25942 			}
25943 			mutex_enter(SD_MUTEX(un));
25944 		}
25945 	}
25946 
25947 	/*
25948 	 * Convert the partition-relative block number to a
25949 	 * disk physical block number.
25950 	 */
25951 	if (NOT_DEVBSIZE(un)) {
25952 		blkno += start_block;
25953 	} else {
25954 		blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
25955 		blkno += start_block;
25956 	}
25957 
25958 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25959 
25960 
25961 	/*
25962 	 * Check if the device has a non-512 block size.
25963 	 */
25964 	wr_bp = NULL;
25965 	if (NOT_DEVBSIZE(un)) {
25966 		tgt_byte_offset = blkno * un->un_sys_blocksize;
25967 		tgt_byte_count = nblk * un->un_sys_blocksize;
25968 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25969 		    (tgt_byte_count % un->un_tgt_blocksize)) {
25970 			doing_rmw = TRUE;
25971 			/*
25972 			 * Calculate the block number and number of block
25973 			 * in terms of the media block size.
25974 			 */
25975 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25976 			tgt_nblk =
25977 			    ((tgt_byte_offset + tgt_byte_count +
25978 			    (un->un_tgt_blocksize - 1)) /
25979 			    un->un_tgt_blocksize) - tgt_blkno;
25980 
25981 			/*
25982 			 * Invoke the routine which is going to do read part
25983 			 * of read-modify-write.
25984 			 * Note that this routine returns a pointer to
25985 			 * a valid bp in wr_bp.
25986 			 */
25987 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25988 			    &wr_bp);
25989 			if (err) {
25990 				mutex_exit(SD_MUTEX(un));
25991 				return (err);
25992 			}
25993 			/*
25994 			 * Offset is being calculated as -
25995 			 * (original block # * system block size) -
25996 			 * (new block # * target block size)
25997 			 */
25998 			io_start_offset =
25999 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
26000 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
26001 
26002 			ASSERT((io_start_offset >= 0) &&
26003 			    (io_start_offset < un->un_tgt_blocksize));
26004 			/*
26005 			 * Do the modify portion of read modify write.
26006 			 */
26007 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
26008 			    (size_t)nblk * un->un_sys_blocksize);
26009 		} else {
26010 			doing_rmw = FALSE;
26011 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26012 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
26013 		}
26014 
26015 		/* Convert blkno and nblk to target blocks */
26016 		blkno = tgt_blkno;
26017 		nblk = tgt_nblk;
26018 	} else {
26019 		wr_bp = &wr_buf;
26020 		bzero(wr_bp, sizeof (struct buf));
26021 		wr_bp->b_flags		= B_BUSY;
26022 		wr_bp->b_un.b_addr	= addr;
26023 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
26024 		wr_bp->b_resid		= 0;
26025 	}
26026 
26027 	mutex_exit(SD_MUTEX(un));
26028 
26029 	/*
26030 	 * Obtain a SCSI packet for the write command.
26031 	 * It should be safe to call the allocator here without
26032 	 * worrying about being locked for DVMA mapping because
26033 	 * the address we're passed is already a DVMA mapping
26034 	 *
26035 	 * We are also not going to worry about semaphore ownership
26036 	 * in the dump buffer. Dumping is single threaded at present.
26037 	 */
26038 
26039 	wr_pktp = NULL;
26040 
26041 	dma_resid = wr_bp->b_bcount;
26042 	oblkno = blkno;
26043 
26044 	if (!(NOT_DEVBSIZE(un))) {
26045 		nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
26046 	}
26047 
26048 	while (dma_resid != 0) {
26049 
26050 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26051 		wr_bp->b_flags &= ~B_ERROR;
26052 
26053 		if (un->un_partial_dma_supported == 1) {
26054 			blkno = oblkno +
26055 			    ((wr_bp->b_bcount - dma_resid) /
26056 			    un->un_tgt_blocksize);
26057 			nblk = dma_resid / un->un_tgt_blocksize;
26058 
26059 			if (wr_pktp) {
26060 				/*
26061 				 * Partial DMA transfers after initial transfer
26062 				 */
26063 				rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26064 				    blkno, nblk);
26065 			} else {
26066 				/* Initial transfer */
26067 				rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26068 				    un->un_pkt_flags, NULL_FUNC, NULL,
26069 				    blkno, nblk);
26070 			}
26071 		} else {
26072 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26073 			    0, NULL_FUNC, NULL, blkno, nblk);
26074 		}
26075 
26076 		if (rval == 0) {
26077 			/* We were given a SCSI packet, continue. */
26078 			break;
26079 		}
26080 
26081 		if (i == 0) {
26082 			if (wr_bp->b_flags & B_ERROR) {
26083 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26084 				    "no resources for dumping; "
26085 				    "error code: 0x%x, retrying",
26086 				    geterror(wr_bp));
26087 			} else {
26088 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26089 				    "no resources for dumping; retrying");
26090 			}
26091 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
26092 			if (wr_bp->b_flags & B_ERROR) {
26093 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26094 				    "no resources for dumping; error code: "
26095 				    "0x%x, retrying\n", geterror(wr_bp));
26096 			}
26097 		} else {
26098 			if (wr_bp->b_flags & B_ERROR) {
26099 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26100 				    "no resources for dumping; "
26101 				    "error code: 0x%x, retries failed, "
26102 				    "giving up.\n", geterror(wr_bp));
26103 			} else {
26104 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26105 				    "no resources for dumping; "
26106 				    "retries failed, giving up.\n");
26107 			}
26108 			mutex_enter(SD_MUTEX(un));
26109 			Restore_state(un);
26110 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26111 				mutex_exit(SD_MUTEX(un));
26112 				scsi_free_consistent_buf(wr_bp);
26113 			} else {
26114 				mutex_exit(SD_MUTEX(un));
26115 			}
26116 			return (EIO);
26117 		}
26118 		drv_usecwait(10000);
26119 	}
26120 
26121 	if (un->un_partial_dma_supported == 1) {
26122 		/*
26123 		 * save the resid from PARTIAL_DMA
26124 		 */
26125 		dma_resid = wr_pktp->pkt_resid;
26126 		if (dma_resid != 0)
26127 			nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26128 		wr_pktp->pkt_resid = 0;
26129 	} else {
26130 		dma_resid = 0;
26131 	}
26132 
26133 	/* SunBug 1222170 */
26134 	wr_pktp->pkt_flags = FLAG_NOINTR;
26135 
26136 	err = EIO;
26137 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26138 
26139 		/*
26140 		 * Scsi_poll returns 0 (success) if the command completes and
26141 		 * the status block is STATUS_GOOD.  We should only check
26142 		 * errors if this condition is not true.  Even then we should
26143 		 * send our own request sense packet only if we have a check
26144 		 * condition and auto request sense has not been performed by
26145 		 * the hba.
26146 		 */
26147 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26148 
26149 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26150 		    (wr_pktp->pkt_resid == 0)) {
26151 			err = SD_SUCCESS;
26152 			break;
26153 		}
26154 
26155 		/*
26156 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
26157 		 */
26158 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26159 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26160 			    "Error while dumping state...Device is gone\n");
26161 			break;
26162 		}
26163 
26164 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26165 			SD_INFO(SD_LOG_DUMP, un,
26166 			    "sddump: write failed with CHECK, try # %d\n", i);
26167 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26168 				(void) sd_send_polled_RQS(un);
26169 			}
26170 
26171 			continue;
26172 		}
26173 
26174 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26175 			int reset_retval = 0;
26176 
26177 			SD_INFO(SD_LOG_DUMP, un,
26178 			    "sddump: write failed with BUSY, try # %d\n", i);
26179 
26180 			if (un->un_f_lun_reset_enabled == TRUE) {
26181 				reset_retval = scsi_reset(SD_ADDRESS(un),
26182 				    RESET_LUN);
26183 			}
26184 			if (reset_retval == 0) {
26185 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26186 			}
26187 			(void) sd_send_polled_RQS(un);
26188 
26189 		} else {
26190 			SD_INFO(SD_LOG_DUMP, un,
26191 			    "sddump: write failed with 0x%x, try # %d\n",
26192 			    SD_GET_PKT_STATUS(wr_pktp), i);
26193 			mutex_enter(SD_MUTEX(un));
26194 			sd_reset_target(un, wr_pktp);
26195 			mutex_exit(SD_MUTEX(un));
26196 		}
26197 
26198 		/*
26199 		 * If we are not getting anywhere with lun/target resets,
26200 		 * let's reset the bus.
26201 		 */
26202 		if (i == SD_NDUMP_RETRIES/2) {
26203 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26204 			(void) sd_send_polled_RQS(un);
26205 		}
26206 	}
26207 	}
26208 
26209 	scsi_destroy_pkt(wr_pktp);
26210 	mutex_enter(SD_MUTEX(un));
26211 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26212 		mutex_exit(SD_MUTEX(un));
26213 		scsi_free_consistent_buf(wr_bp);
26214 	} else {
26215 		mutex_exit(SD_MUTEX(un));
26216 	}
26217 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26218 	return (err);
26219 }
26220 
26221 /*
26222  *    Function: sd_scsi_poll()
26223  *
26224  * Description: This is a wrapper for the scsi_poll call.
26225  *
26226  *   Arguments: sd_lun - The unit structure
26227  *              scsi_pkt - The scsi packet being sent to the device.
26228  *
26229  * Return Code: 0 - Command completed successfully with good status
26230  *             -1 - Command failed.  This could indicate a check condition
26231  *                  or other status value requiring recovery action.
26232  *
26233  * NOTE: This code is only called off sddump().
26234  */
26235 
26236 static int
26237 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26238 {
26239 	int status;
26240 
26241 	ASSERT(un != NULL);
26242 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26243 	ASSERT(pktp != NULL);
26244 
26245 	status = SD_SUCCESS;
26246 
26247 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26248 		pktp->pkt_flags |= un->un_tagflags;
26249 		pktp->pkt_flags &= ~FLAG_NODISCON;
26250 	}
26251 
26252 	status = sd_ddi_scsi_poll(pktp);
26253 	/*
26254 	 * Scsi_poll returns 0 (success) if the command completes and the
26255 	 * status block is STATUS_GOOD.  We should only check errors if this
26256 	 * condition is not true.  Even then we should send our own request
26257 	 * sense packet only if we have a check condition and auto
26258 	 * request sense has not been performed by the hba.
26259 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26260 	 */
26261 	if ((status != SD_SUCCESS) &&
26262 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26263 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26264 	    (pktp->pkt_reason != CMD_DEV_GONE))
26265 		(void) sd_send_polled_RQS(un);
26266 
26267 	return (status);
26268 }
26269 
26270 /*
26271  *    Function: sd_send_polled_RQS()
26272  *
26273  * Description: This sends the request sense command to a device.
26274  *
26275  *   Arguments: sd_lun - The unit structure
26276  *
26277  * Return Code: 0 - Command completed successfully with good status
26278  *             -1 - Command failed.
26279  *
26280  */
26281 
26282 static int
26283 sd_send_polled_RQS(struct sd_lun *un)
26284 {
26285 	int	ret_val;
26286 	struct	scsi_pkt	*rqs_pktp;
26287 	struct	buf		*rqs_bp;
26288 
26289 	ASSERT(un != NULL);
26290 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26291 
26292 	ret_val = SD_SUCCESS;
26293 
26294 	rqs_pktp = un->un_rqs_pktp;
26295 	rqs_bp	 = un->un_rqs_bp;
26296 
26297 	mutex_enter(SD_MUTEX(un));
26298 
26299 	if (un->un_sense_isbusy) {
26300 		ret_val = SD_FAILURE;
26301 		mutex_exit(SD_MUTEX(un));
26302 		return (ret_val);
26303 	}
26304 
26305 	/*
26306 	 * If the request sense buffer (and packet) is not in use,
26307 	 * let's set the un_sense_isbusy and send our packet
26308 	 */
26309 	un->un_sense_isbusy 	= 1;
26310 	rqs_pktp->pkt_resid  	= 0;
26311 	rqs_pktp->pkt_reason 	= 0;
26312 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
26313 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26314 
26315 	mutex_exit(SD_MUTEX(un));
26316 
26317 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26318 	    " 0x%p\n", rqs_bp->b_un.b_addr);
26319 
26320 	/*
26321 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26322 	 * axle - it has a call into us!
26323 	 */
26324 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26325 		SD_INFO(SD_LOG_COMMON, un,
26326 		    "sd_send_polled_RQS: RQS failed\n");
26327 	}
26328 
26329 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26330 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26331 
26332 	mutex_enter(SD_MUTEX(un));
26333 	un->un_sense_isbusy = 0;
26334 	mutex_exit(SD_MUTEX(un));
26335 
26336 	return (ret_val);
26337 }
26338 
26339 /*
26340  * Defines needed for localized version of the scsi_poll routine.
26341  */
26342 #define	CSEC		10000			/* usecs */
26343 #define	SEC_TO_CSEC	(1000000/CSEC)
26344 
26345 /*
26346  *    Function: sd_ddi_scsi_poll()
26347  *
26348  * Description: Localized version of the scsi_poll routine.  The purpose is to
26349  *		send a scsi_pkt to a device as a polled command.  This version
26350  *		is to ensure more robust handling of transport errors.
26351  *		Specifically this routine cures not ready, coming ready
26352  *		transition for power up and reset of sonoma's.  This can take
26353  *		up to 45 seconds for power-on and 20 seconds for reset of a
26354  * 		sonoma lun.
26355  *
26356  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26357  *
26358  * Return Code: 0 - Command completed successfully with good status
26359  *             -1 - Command failed.
26360  *
26361  * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26362  * be fixed (removing this code), we need to determine how to handle the
26363  * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26364  *
26365  * NOTE: This code is only called off sddump().
26366  */
26367 static int
26368 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26369 {
26370 	int			rval = -1;
26371 	int			savef;
26372 	long			savet;
26373 	void			(*savec)();
26374 	int			timeout;
26375 	int			busy_count;
26376 	int			poll_delay;
26377 	int			rc;
26378 	uint8_t			*sensep;
26379 	struct scsi_arq_status	*arqstat;
26380 	extern int		do_polled_io;
26381 
26382 	ASSERT(pkt->pkt_scbp);
26383 
26384 	/*
26385 	 * save old flags..
26386 	 */
26387 	savef = pkt->pkt_flags;
26388 	savec = pkt->pkt_comp;
26389 	savet = pkt->pkt_time;
26390 
26391 	pkt->pkt_flags |= FLAG_NOINTR;
26392 
26393 	/*
26394 	 * XXX there is nothing in the SCSA spec that states that we should not
26395 	 * do a callback for polled cmds; however, removing this will break sd
26396 	 * and probably other target drivers
26397 	 */
26398 	pkt->pkt_comp = NULL;
26399 
26400 	/*
26401 	 * we don't like a polled command without timeout.
26402 	 * 60 seconds seems long enough.
26403 	 */
26404 	if (pkt->pkt_time == 0)
26405 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
26406 
26407 	/*
26408 	 * Send polled cmd.
26409 	 *
26410 	 * We do some error recovery for various errors.  Tran_busy,
26411 	 * queue full, and non-dispatched commands are retried every 10 msec.
26412 	 * as they are typically transient failures.  Busy status and Not
26413 	 * Ready are retried every second as this status takes a while to
26414 	 * change.
26415 	 */
26416 	timeout = pkt->pkt_time * SEC_TO_CSEC;
26417 
26418 	for (busy_count = 0; busy_count < timeout; busy_count++) {
26419 		/*
26420 		 * Initialize pkt status variables.
26421 		 */
26422 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26423 
26424 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26425 			if (rc != TRAN_BUSY) {
26426 				/* Transport failed - give up. */
26427 				break;
26428 			} else {
26429 				/* Transport busy - try again. */
26430 				poll_delay = 1 * CSEC;		/* 10 msec. */
26431 			}
26432 		} else {
26433 			/*
26434 			 * Transport accepted - check pkt status.
26435 			 */
26436 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
26437 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26438 			    (rc == STATUS_CHECK) &&
26439 			    (pkt->pkt_state & STATE_ARQ_DONE)) {
26440 				arqstat =
26441 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26442 				sensep = (uint8_t *)&arqstat->sts_sensedata;
26443 			} else {
26444 				sensep = NULL;
26445 			}
26446 
26447 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26448 			    (rc == STATUS_GOOD)) {
26449 				/* No error - we're done */
26450 				rval = 0;
26451 				break;
26452 
26453 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26454 				/* Lost connection - give up */
26455 				break;
26456 
26457 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26458 			    (pkt->pkt_state == 0)) {
26459 				/* Pkt not dispatched - try again. */
26460 				poll_delay = 1 * CSEC;		/* 10 msec. */
26461 
26462 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26463 			    (rc == STATUS_QFULL)) {
26464 				/* Queue full - try again. */
26465 				poll_delay = 1 * CSEC;		/* 10 msec. */
26466 
26467 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26468 			    (rc == STATUS_BUSY)) {
26469 				/* Busy - try again. */
26470 				poll_delay = 100 * CSEC;	/* 1 sec. */
26471 				busy_count += (SEC_TO_CSEC - 1);
26472 
26473 			} else if ((sensep != NULL) &&
26474 			    (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26475 				/*
26476 				 * Unit Attention - try again.
26477 				 * Pretend it took 1 sec.
26478 				 * NOTE: 'continue' avoids poll_delay
26479 				 */
26480 				busy_count += (SEC_TO_CSEC - 1);
26481 				continue;
26482 
26483 			} else if ((sensep != NULL) &&
26484 			    (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26485 			    (scsi_sense_asc(sensep) == 0x04) &&
26486 			    (scsi_sense_ascq(sensep) == 0x01)) {
26487 				/*
26488 				 * Not ready -> ready - try again.
26489 				 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26490 				 * ...same as STATUS_BUSY
26491 				 */
26492 				poll_delay = 100 * CSEC;	/* 1 sec. */
26493 				busy_count += (SEC_TO_CSEC - 1);
26494 
26495 			} else {
26496 				/* BAD status - give up. */
26497 				break;
26498 			}
26499 		}
26500 
26501 		if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26502 		    !do_polled_io) {
26503 			delay(drv_usectohz(poll_delay));
26504 		} else {
26505 			/* we busy wait during cpr_dump or interrupt threads */
26506 			drv_usecwait(poll_delay);
26507 		}
26508 	}
26509 
26510 	pkt->pkt_flags = savef;
26511 	pkt->pkt_comp = savec;
26512 	pkt->pkt_time = savet;
26513 
26514 	/* return on error */
26515 	if (rval)
26516 		return (rval);
26517 
26518 	/*
26519 	 * This is not a performance critical code path.
26520 	 *
26521 	 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26522 	 * issues associated with looking at DMA memory prior to
26523 	 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26524 	 */
26525 	scsi_sync_pkt(pkt);
26526 	return (0);
26527 }
26528 
26529 
26530 
26531 /*
26532  *    Function: sd_persistent_reservation_in_read_keys
26533  *
26534  * Description: This routine is the driver entry point for handling CD-ROM
26535  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26536  *		by sending the SCSI-3 PRIN commands to the device.
26537  *		Processes the read keys command response by copying the
26538  *		reservation key information into the user provided buffer.
26539  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26540  *
26541  *   Arguments: un   -  Pointer to soft state struct for the target.
26542  *		usrp -	user provided pointer to multihost Persistent In Read
26543  *			Keys structure (mhioc_inkeys_t)
26544  *		flag -	this argument is a pass through to ddi_copyxxx()
26545  *			directly from the mode argument of ioctl().
26546  *
26547  * Return Code: 0   - Success
26548  *		EACCES
26549  *		ENOTSUP
26550  *		errno return code from sd_send_scsi_cmd()
26551  *
26552  *     Context: Can sleep. Does not return until command is completed.
26553  */
26554 
26555 static int
26556 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26557     mhioc_inkeys_t *usrp, int flag)
26558 {
26559 #ifdef _MULTI_DATAMODEL
26560 	struct mhioc_key_list32	li32;
26561 #endif
26562 	sd_prin_readkeys_t	*in;
26563 	mhioc_inkeys_t		*ptr;
26564 	mhioc_key_list_t	li;
26565 	uchar_t			*data_bufp;
26566 	int 			data_len;
26567 	int			rval = 0;
26568 	size_t			copysz;
26569 	sd_ssc_t		*ssc;
26570 
26571 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26572 		return (EINVAL);
26573 	}
26574 	bzero(&li, sizeof (mhioc_key_list_t));
26575 
26576 	ssc = sd_ssc_init(un);
26577 
26578 	/*
26579 	 * Get the listsize from user
26580 	 */
26581 #ifdef _MULTI_DATAMODEL
26582 
26583 	switch (ddi_model_convert_from(flag & FMODELS)) {
26584 	case DDI_MODEL_ILP32:
26585 		copysz = sizeof (struct mhioc_key_list32);
26586 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26587 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26588 			    "sd_persistent_reservation_in_read_keys: "
26589 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26590 			rval = EFAULT;
26591 			goto done;
26592 		}
26593 		li.listsize = li32.listsize;
26594 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26595 		break;
26596 
26597 	case DDI_MODEL_NONE:
26598 		copysz = sizeof (mhioc_key_list_t);
26599 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26600 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26601 			    "sd_persistent_reservation_in_read_keys: "
26602 			    "failed ddi_copyin: mhioc_key_list_t\n");
26603 			rval = EFAULT;
26604 			goto done;
26605 		}
26606 		break;
26607 	}
26608 
26609 #else /* ! _MULTI_DATAMODEL */
26610 	copysz = sizeof (mhioc_key_list_t);
26611 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26612 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26613 		    "sd_persistent_reservation_in_read_keys: "
26614 		    "failed ddi_copyin: mhioc_key_list_t\n");
26615 		rval = EFAULT;
26616 		goto done;
26617 	}
26618 #endif
26619 
26620 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26621 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26622 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26623 
26624 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26625 	    data_len, data_bufp);
26626 	if (rval != 0) {
26627 		if (rval == EIO)
26628 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26629 		else
26630 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26631 		goto done;
26632 	}
26633 	in = (sd_prin_readkeys_t *)data_bufp;
26634 	ptr->generation = BE_32(in->generation);
26635 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26636 
26637 	/*
26638 	 * Return the min(listsize, listlen) keys
26639 	 */
26640 #ifdef _MULTI_DATAMODEL
26641 
26642 	switch (ddi_model_convert_from(flag & FMODELS)) {
26643 	case DDI_MODEL_ILP32:
26644 		li32.listlen = li.listlen;
26645 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26646 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26647 			    "sd_persistent_reservation_in_read_keys: "
26648 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26649 			rval = EFAULT;
26650 			goto done;
26651 		}
26652 		break;
26653 
26654 	case DDI_MODEL_NONE:
26655 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26656 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26657 			    "sd_persistent_reservation_in_read_keys: "
26658 			    "failed ddi_copyout: mhioc_key_list_t\n");
26659 			rval = EFAULT;
26660 			goto done;
26661 		}
26662 		break;
26663 	}
26664 
26665 #else /* ! _MULTI_DATAMODEL */
26666 
26667 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26668 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26669 		    "sd_persistent_reservation_in_read_keys: "
26670 		    "failed ddi_copyout: mhioc_key_list_t\n");
26671 		rval = EFAULT;
26672 		goto done;
26673 	}
26674 
26675 #endif /* _MULTI_DATAMODEL */
26676 
26677 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26678 	    li.listsize * MHIOC_RESV_KEY_SIZE);
26679 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26680 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26681 		    "sd_persistent_reservation_in_read_keys: "
26682 		    "failed ddi_copyout: keylist\n");
26683 		rval = EFAULT;
26684 	}
26685 done:
26686 	sd_ssc_fini(ssc);
26687 	kmem_free(data_bufp, data_len);
26688 	return (rval);
26689 }
26690 
26691 
26692 /*
26693  *    Function: sd_persistent_reservation_in_read_resv
26694  *
26695  * Description: This routine is the driver entry point for handling CD-ROM
26696  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
26697  *		by sending the SCSI-3 PRIN commands to the device.
26698  *		Process the read persistent reservations command response by
26699  *		copying the reservation information into the user provided
26700  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26701  *
26702  *   Arguments: un   -  Pointer to soft state struct for the target.
26703  *		usrp -	user provided pointer to multihost Persistent In Read
26704  *			Keys structure (mhioc_inkeys_t)
26705  *		flag -	this argument is a pass through to ddi_copyxxx()
26706  *			directly from the mode argument of ioctl().
26707  *
26708  * Return Code: 0   - Success
26709  *		EACCES
26710  *		ENOTSUP
26711  *		errno return code from sd_send_scsi_cmd()
26712  *
26713  *     Context: Can sleep. Does not return until command is completed.
26714  */
26715 
26716 static int
26717 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26718     mhioc_inresvs_t *usrp, int flag)
26719 {
26720 #ifdef _MULTI_DATAMODEL
26721 	struct mhioc_resv_desc_list32 resvlist32;
26722 #endif
26723 	sd_prin_readresv_t	*in;
26724 	mhioc_inresvs_t		*ptr;
26725 	sd_readresv_desc_t	*readresv_ptr;
26726 	mhioc_resv_desc_list_t	resvlist;
26727 	mhioc_resv_desc_t 	resvdesc;
26728 	uchar_t			*data_bufp = NULL;
26729 	int 			data_len;
26730 	int			rval = 0;
26731 	int			i;
26732 	size_t			copysz;
26733 	mhioc_resv_desc_t	*bufp;
26734 	sd_ssc_t		*ssc;
26735 
26736 	if ((ptr = usrp) == NULL) {
26737 		return (EINVAL);
26738 	}
26739 
26740 	ssc = sd_ssc_init(un);
26741 
26742 	/*
26743 	 * Get the listsize from user
26744 	 */
26745 #ifdef _MULTI_DATAMODEL
26746 	switch (ddi_model_convert_from(flag & FMODELS)) {
26747 	case DDI_MODEL_ILP32:
26748 		copysz = sizeof (struct mhioc_resv_desc_list32);
26749 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26750 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26751 			    "sd_persistent_reservation_in_read_resv: "
26752 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26753 			rval = EFAULT;
26754 			goto done;
26755 		}
26756 		resvlist.listsize = resvlist32.listsize;
26757 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26758 		break;
26759 
26760 	case DDI_MODEL_NONE:
26761 		copysz = sizeof (mhioc_resv_desc_list_t);
26762 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26763 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26764 			    "sd_persistent_reservation_in_read_resv: "
26765 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26766 			rval = EFAULT;
26767 			goto done;
26768 		}
26769 		break;
26770 	}
26771 #else /* ! _MULTI_DATAMODEL */
26772 	copysz = sizeof (mhioc_resv_desc_list_t);
26773 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26774 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26775 		    "sd_persistent_reservation_in_read_resv: "
26776 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26777 		rval = EFAULT;
26778 		goto done;
26779 	}
26780 #endif /* ! _MULTI_DATAMODEL */
26781 
26782 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26783 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26784 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26785 
26786 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
26787 	    data_len, data_bufp);
26788 	if (rval != 0) {
26789 		if (rval == EIO)
26790 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26791 		else
26792 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26793 		goto done;
26794 	}
26795 	in = (sd_prin_readresv_t *)data_bufp;
26796 	ptr->generation = BE_32(in->generation);
26797 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26798 
26799 	/*
26800 	 * Return the min(listsize, listlen( keys
26801 	 */
26802 #ifdef _MULTI_DATAMODEL
26803 
26804 	switch (ddi_model_convert_from(flag & FMODELS)) {
26805 	case DDI_MODEL_ILP32:
26806 		resvlist32.listlen = resvlist.listlen;
26807 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26808 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26809 			    "sd_persistent_reservation_in_read_resv: "
26810 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26811 			rval = EFAULT;
26812 			goto done;
26813 		}
26814 		break;
26815 
26816 	case DDI_MODEL_NONE:
26817 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26818 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26819 			    "sd_persistent_reservation_in_read_resv: "
26820 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26821 			rval = EFAULT;
26822 			goto done;
26823 		}
26824 		break;
26825 	}
26826 
26827 #else /* ! _MULTI_DATAMODEL */
26828 
26829 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26830 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26831 		    "sd_persistent_reservation_in_read_resv: "
26832 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26833 		rval = EFAULT;
26834 		goto done;
26835 	}
26836 
26837 #endif /* ! _MULTI_DATAMODEL */
26838 
26839 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26840 	bufp = resvlist.list;
26841 	copysz = sizeof (mhioc_resv_desc_t);
26842 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26843 	    i++, readresv_ptr++, bufp++) {
26844 
26845 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26846 		    MHIOC_RESV_KEY_SIZE);
26847 		resvdesc.type  = readresv_ptr->type;
26848 		resvdesc.scope = readresv_ptr->scope;
26849 		resvdesc.scope_specific_addr =
26850 		    BE_32(readresv_ptr->scope_specific_addr);
26851 
26852 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26853 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26854 			    "sd_persistent_reservation_in_read_resv: "
26855 			    "failed ddi_copyout: resvlist\n");
26856 			rval = EFAULT;
26857 			goto done;
26858 		}
26859 	}
26860 done:
26861 	sd_ssc_fini(ssc);
26862 	/* only if data_bufp is allocated, we need to free it */
26863 	if (data_bufp) {
26864 		kmem_free(data_bufp, data_len);
26865 	}
26866 	return (rval);
26867 }
26868 
26869 
26870 /*
26871  *    Function: sr_change_blkmode()
26872  *
26873  * Description: This routine is the driver entry point for handling CD-ROM
26874  *		block mode ioctl requests. Support for returning and changing
26875  *		the current block size in use by the device is implemented. The
26876  *		LBA size is changed via a MODE SELECT Block Descriptor.
26877  *
26878  *		This routine issues a mode sense with an allocation length of
26879  *		12 bytes for the mode page header and a single block descriptor.
26880  *
26881  *   Arguments: dev - the device 'dev_t'
26882  *		cmd - the request type; one of CDROMGBLKMODE (get) or
26883  *		      CDROMSBLKMODE (set)
26884  *		data - current block size or requested block size
26885  *		flag - this argument is a pass through to ddi_copyxxx() directly
26886  *		       from the mode argument of ioctl().
26887  *
26888  * Return Code: the code returned by sd_send_scsi_cmd()
26889  *		EINVAL if invalid arguments are provided
26890  *		EFAULT if ddi_copyxxx() fails
26891  *		ENXIO if fail ddi_get_soft_state
26892  *		EIO if invalid mode sense block descriptor length
26893  *
26894  */
26895 
26896 static int
26897 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26898 {
26899 	struct sd_lun			*un = NULL;
26900 	struct mode_header		*sense_mhp, *select_mhp;
26901 	struct block_descriptor		*sense_desc, *select_desc;
26902 	int				current_bsize;
26903 	int				rval = EINVAL;
26904 	uchar_t				*sense = NULL;
26905 	uchar_t				*select = NULL;
26906 	sd_ssc_t			*ssc;
26907 
26908 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26909 
26910 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26911 		return (ENXIO);
26912 	}
26913 
26914 	/*
26915 	 * The block length is changed via the Mode Select block descriptor, the
26916 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26917 	 * required as part of this routine. Therefore the mode sense allocation
26918 	 * length is specified to be the length of a mode page header and a
26919 	 * block descriptor.
26920 	 */
26921 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26922 
26923 	ssc = sd_ssc_init(un);
26924 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26925 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
26926 	sd_ssc_fini(ssc);
26927 	if (rval != 0) {
26928 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26929 		    "sr_change_blkmode: Mode Sense Failed\n");
26930 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26931 		return (rval);
26932 	}
26933 
26934 	/* Check the block descriptor len to handle only 1 block descriptor */
26935 	sense_mhp = (struct mode_header *)sense;
26936 	if ((sense_mhp->bdesc_length == 0) ||
26937 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26938 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26939 		    "sr_change_blkmode: Mode Sense returned invalid block"
26940 		    " descriptor length\n");
26941 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26942 		return (EIO);
26943 	}
26944 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26945 	current_bsize = ((sense_desc->blksize_hi << 16) |
26946 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26947 
26948 	/* Process command */
26949 	switch (cmd) {
26950 	case CDROMGBLKMODE:
26951 		/* Return the block size obtained during the mode sense */
26952 		if (ddi_copyout(&current_bsize, (void *)data,
26953 		    sizeof (int), flag) != 0)
26954 			rval = EFAULT;
26955 		break;
26956 	case CDROMSBLKMODE:
26957 		/* Validate the requested block size */
26958 		switch (data) {
26959 		case CDROM_BLK_512:
26960 		case CDROM_BLK_1024:
26961 		case CDROM_BLK_2048:
26962 		case CDROM_BLK_2056:
26963 		case CDROM_BLK_2336:
26964 		case CDROM_BLK_2340:
26965 		case CDROM_BLK_2352:
26966 		case CDROM_BLK_2368:
26967 		case CDROM_BLK_2448:
26968 		case CDROM_BLK_2646:
26969 		case CDROM_BLK_2647:
26970 			break;
26971 		default:
26972 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26973 			    "sr_change_blkmode: "
26974 			    "Block Size '%ld' Not Supported\n", data);
26975 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26976 			return (EINVAL);
26977 		}
26978 
26979 		/*
26980 		 * The current block size matches the requested block size so
26981 		 * there is no need to send the mode select to change the size
26982 		 */
26983 		if (current_bsize == data) {
26984 			break;
26985 		}
26986 
26987 		/* Build the select data for the requested block size */
26988 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26989 		select_mhp = (struct mode_header *)select;
26990 		select_desc =
26991 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26992 		/*
26993 		 * The LBA size is changed via the block descriptor, so the
26994 		 * descriptor is built according to the user data
26995 		 */
26996 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26997 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
26998 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
26999 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
27000 
27001 		/* Send the mode select for the requested block size */
27002 		ssc = sd_ssc_init(un);
27003 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
27004 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27005 		    SD_PATH_STANDARD);
27006 		sd_ssc_fini(ssc);
27007 		if (rval != 0) {
27008 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27009 			    "sr_change_blkmode: Mode Select Failed\n");
27010 			/*
27011 			 * The mode select failed for the requested block size,
27012 			 * so reset the data for the original block size and
27013 			 * send it to the target. The error is indicated by the
27014 			 * return value for the failed mode select.
27015 			 */
27016 			select_desc->blksize_hi  = sense_desc->blksize_hi;
27017 			select_desc->blksize_mid = sense_desc->blksize_mid;
27018 			select_desc->blksize_lo  = sense_desc->blksize_lo;
27019 			ssc = sd_ssc_init(un);
27020 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
27021 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27022 			    SD_PATH_STANDARD);
27023 			sd_ssc_fini(ssc);
27024 		} else {
27025 			ASSERT(!mutex_owned(SD_MUTEX(un)));
27026 			mutex_enter(SD_MUTEX(un));
27027 			sd_update_block_info(un, (uint32_t)data, 0);
27028 			mutex_exit(SD_MUTEX(un));
27029 		}
27030 		break;
27031 	default:
27032 		/* should not reach here, but check anyway */
27033 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27034 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
27035 		rval = EINVAL;
27036 		break;
27037 	}
27038 
27039 	if (select) {
27040 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
27041 	}
27042 	if (sense) {
27043 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27044 	}
27045 	return (rval);
27046 }
27047 
27048 
27049 /*
27050  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27051  * implement driver support for getting and setting the CD speed. The command
27052  * set used will be based on the device type. If the device has not been
27053  * identified as MMC the Toshiba vendor specific mode page will be used. If
27054  * the device is MMC but does not support the Real Time Streaming feature
27055  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27056  * be used to read the speed.
27057  */
27058 
27059 /*
27060  *    Function: sr_change_speed()
27061  *
27062  * Description: This routine is the driver entry point for handling CD-ROM
27063  *		drive speed ioctl requests for devices supporting the Toshiba
27064  *		vendor specific drive speed mode page. Support for returning
27065  *		and changing the current drive speed in use by the device is
27066  *		implemented.
27067  *
27068  *   Arguments: dev - the device 'dev_t'
27069  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
27070  *		      CDROMSDRVSPEED (set)
27071  *		data - current drive speed or requested drive speed
27072  *		flag - this argument is a pass through to ddi_copyxxx() directly
27073  *		       from the mode argument of ioctl().
27074  *
27075  * Return Code: the code returned by sd_send_scsi_cmd()
27076  *		EINVAL if invalid arguments are provided
27077  *		EFAULT if ddi_copyxxx() fails
27078  *		ENXIO if fail ddi_get_soft_state
27079  *		EIO if invalid mode sense block descriptor length
27080  */
27081 
27082 static int
27083 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27084 {
27085 	struct sd_lun			*un = NULL;
27086 	struct mode_header		*sense_mhp, *select_mhp;
27087 	struct mode_speed		*sense_page, *select_page;
27088 	int				current_speed;
27089 	int				rval = EINVAL;
27090 	int				bd_len;
27091 	uchar_t				*sense = NULL;
27092 	uchar_t				*select = NULL;
27093 	sd_ssc_t			*ssc;
27094 
27095 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27096 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27097 		return (ENXIO);
27098 	}
27099 
27100 	/*
27101 	 * Note: The drive speed is being modified here according to a Toshiba
27102 	 * vendor specific mode page (0x31).
27103 	 */
27104 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27105 
27106 	ssc = sd_ssc_init(un);
27107 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
27108 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27109 	    SD_PATH_STANDARD);
27110 	sd_ssc_fini(ssc);
27111 	if (rval != 0) {
27112 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27113 		    "sr_change_speed: Mode Sense Failed\n");
27114 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27115 		return (rval);
27116 	}
27117 	sense_mhp  = (struct mode_header *)sense;
27118 
27119 	/* Check the block descriptor len to handle only 1 block descriptor */
27120 	bd_len = sense_mhp->bdesc_length;
27121 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27122 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27123 		    "sr_change_speed: Mode Sense returned invalid block "
27124 		    "descriptor length\n");
27125 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27126 		return (EIO);
27127 	}
27128 
27129 	sense_page = (struct mode_speed *)
27130 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27131 	current_speed = sense_page->speed;
27132 
27133 	/* Process command */
27134 	switch (cmd) {
27135 	case CDROMGDRVSPEED:
27136 		/* Return the drive speed obtained during the mode sense */
27137 		if (current_speed == 0x2) {
27138 			current_speed = CDROM_TWELVE_SPEED;
27139 		}
27140 		if (ddi_copyout(&current_speed, (void *)data,
27141 		    sizeof (int), flag) != 0) {
27142 			rval = EFAULT;
27143 		}
27144 		break;
27145 	case CDROMSDRVSPEED:
27146 		/* Validate the requested drive speed */
27147 		switch ((uchar_t)data) {
27148 		case CDROM_TWELVE_SPEED:
27149 			data = 0x2;
27150 			/*FALLTHROUGH*/
27151 		case CDROM_NORMAL_SPEED:
27152 		case CDROM_DOUBLE_SPEED:
27153 		case CDROM_QUAD_SPEED:
27154 		case CDROM_MAXIMUM_SPEED:
27155 			break;
27156 		default:
27157 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27158 			    "sr_change_speed: "
27159 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27160 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27161 			return (EINVAL);
27162 		}
27163 
27164 		/*
27165 		 * The current drive speed matches the requested drive speed so
27166 		 * there is no need to send the mode select to change the speed
27167 		 */
27168 		if (current_speed == data) {
27169 			break;
27170 		}
27171 
27172 		/* Build the select data for the requested drive speed */
27173 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27174 		select_mhp = (struct mode_header *)select;
27175 		select_mhp->bdesc_length = 0;
27176 		select_page =
27177 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27178 		select_page =
27179 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27180 		select_page->mode_page.code = CDROM_MODE_SPEED;
27181 		select_page->mode_page.length = 2;
27182 		select_page->speed = (uchar_t)data;
27183 
27184 		/* Send the mode select for the requested block size */
27185 		ssc = sd_ssc_init(un);
27186 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27187 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27188 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27189 		sd_ssc_fini(ssc);
27190 		if (rval != 0) {
27191 			/*
27192 			 * The mode select failed for the requested drive speed,
27193 			 * so reset the data for the original drive speed and
27194 			 * send it to the target. The error is indicated by the
27195 			 * return value for the failed mode select.
27196 			 */
27197 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27198 			    "sr_drive_speed: Mode Select Failed\n");
27199 			select_page->speed = sense_page->speed;
27200 			ssc = sd_ssc_init(un);
27201 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27202 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27203 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27204 			sd_ssc_fini(ssc);
27205 		}
27206 		break;
27207 	default:
27208 		/* should not reach here, but check anyway */
27209 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27210 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27211 		rval = EINVAL;
27212 		break;
27213 	}
27214 
27215 	if (select) {
27216 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27217 	}
27218 	if (sense) {
27219 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27220 	}
27221 
27222 	return (rval);
27223 }
27224 
27225 
27226 /*
27227  *    Function: sr_atapi_change_speed()
27228  *
27229  * Description: This routine is the driver entry point for handling CD-ROM
27230  *		drive speed ioctl requests for MMC devices that do not support
27231  *		the Real Time Streaming feature (0x107).
27232  *
27233  *		Note: This routine will use the SET SPEED command which may not
27234  *		be supported by all devices.
27235  *
27236  *   Arguments: dev- the device 'dev_t'
27237  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27238  *		     CDROMSDRVSPEED (set)
27239  *		data- current drive speed or requested drive speed
27240  *		flag- this argument is a pass through to ddi_copyxxx() directly
27241  *		      from the mode argument of ioctl().
27242  *
27243  * Return Code: the code returned by sd_send_scsi_cmd()
27244  *		EINVAL if invalid arguments are provided
27245  *		EFAULT if ddi_copyxxx() fails
27246  *		ENXIO if fail ddi_get_soft_state
27247  *		EIO if invalid mode sense block descriptor length
27248  */
27249 
27250 static int
27251 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27252 {
27253 	struct sd_lun			*un;
27254 	struct uscsi_cmd		*com = NULL;
27255 	struct mode_header_grp2		*sense_mhp;
27256 	uchar_t				*sense_page;
27257 	uchar_t				*sense = NULL;
27258 	char				cdb[CDB_GROUP5];
27259 	int				bd_len;
27260 	int				current_speed = 0;
27261 	int				max_speed = 0;
27262 	int				rval;
27263 	sd_ssc_t			*ssc;
27264 
27265 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27266 
27267 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27268 		return (ENXIO);
27269 	}
27270 
27271 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27272 
27273 	ssc = sd_ssc_init(un);
27274 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27275 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27276 	    SD_PATH_STANDARD);
27277 	sd_ssc_fini(ssc);
27278 	if (rval != 0) {
27279 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27280 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27281 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27282 		return (rval);
27283 	}
27284 
27285 	/* Check the block descriptor len to handle only 1 block descriptor */
27286 	sense_mhp = (struct mode_header_grp2 *)sense;
27287 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27288 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27289 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27290 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27291 		    "block descriptor length\n");
27292 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27293 		return (EIO);
27294 	}
27295 
27296 	/* Calculate the current and maximum drive speeds */
27297 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27298 	current_speed = (sense_page[14] << 8) | sense_page[15];
27299 	max_speed = (sense_page[8] << 8) | sense_page[9];
27300 
27301 	/* Process the command */
27302 	switch (cmd) {
27303 	case CDROMGDRVSPEED:
27304 		current_speed /= SD_SPEED_1X;
27305 		if (ddi_copyout(&current_speed, (void *)data,
27306 		    sizeof (int), flag) != 0)
27307 			rval = EFAULT;
27308 		break;
27309 	case CDROMSDRVSPEED:
27310 		/* Convert the speed code to KB/sec */
27311 		switch ((uchar_t)data) {
27312 		case CDROM_NORMAL_SPEED:
27313 			current_speed = SD_SPEED_1X;
27314 			break;
27315 		case CDROM_DOUBLE_SPEED:
27316 			current_speed = 2 * SD_SPEED_1X;
27317 			break;
27318 		case CDROM_QUAD_SPEED:
27319 			current_speed = 4 * SD_SPEED_1X;
27320 			break;
27321 		case CDROM_TWELVE_SPEED:
27322 			current_speed = 12 * SD_SPEED_1X;
27323 			break;
27324 		case CDROM_MAXIMUM_SPEED:
27325 			current_speed = 0xffff;
27326 			break;
27327 		default:
27328 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27329 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27330 			    (uchar_t)data);
27331 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27332 			return (EINVAL);
27333 		}
27334 
27335 		/* Check the request against the drive's max speed. */
27336 		if (current_speed != 0xffff) {
27337 			if (current_speed > max_speed) {
27338 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27339 				return (EINVAL);
27340 			}
27341 		}
27342 
27343 		/*
27344 		 * Build and send the SET SPEED command
27345 		 *
27346 		 * Note: The SET SPEED (0xBB) command used in this routine is
27347 		 * obsolete per the SCSI MMC spec but still supported in the
27348 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27349 		 * therefore the command is still implemented in this routine.
27350 		 */
27351 		bzero(cdb, sizeof (cdb));
27352 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27353 		cdb[2] = (uchar_t)(current_speed >> 8);
27354 		cdb[3] = (uchar_t)current_speed;
27355 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27356 		com->uscsi_cdb	   = (caddr_t)cdb;
27357 		com->uscsi_cdblen  = CDB_GROUP5;
27358 		com->uscsi_bufaddr = NULL;
27359 		com->uscsi_buflen  = 0;
27360 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27361 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27362 		break;
27363 	default:
27364 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27365 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27366 		rval = EINVAL;
27367 	}
27368 
27369 	if (sense) {
27370 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27371 	}
27372 	if (com) {
27373 		kmem_free(com, sizeof (*com));
27374 	}
27375 	return (rval);
27376 }
27377 
27378 
27379 /*
27380  *    Function: sr_pause_resume()
27381  *
27382  * Description: This routine is the driver entry point for handling CD-ROM
27383  *		pause/resume ioctl requests. This only affects the audio play
27384  *		operation.
27385  *
27386  *   Arguments: dev - the device 'dev_t'
27387  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27388  *		      for setting the resume bit of the cdb.
27389  *
27390  * Return Code: the code returned by sd_send_scsi_cmd()
27391  *		EINVAL if invalid mode specified
27392  *
27393  */
27394 
27395 static int
27396 sr_pause_resume(dev_t dev, int cmd)
27397 {
27398 	struct sd_lun		*un;
27399 	struct uscsi_cmd	*com;
27400 	char			cdb[CDB_GROUP1];
27401 	int			rval;
27402 
27403 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27404 		return (ENXIO);
27405 	}
27406 
27407 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27408 	bzero(cdb, CDB_GROUP1);
27409 	cdb[0] = SCMD_PAUSE_RESUME;
27410 	switch (cmd) {
27411 	case CDROMRESUME:
27412 		cdb[8] = 1;
27413 		break;
27414 	case CDROMPAUSE:
27415 		cdb[8] = 0;
27416 		break;
27417 	default:
27418 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27419 		    " Command '%x' Not Supported\n", cmd);
27420 		rval = EINVAL;
27421 		goto done;
27422 	}
27423 
27424 	com->uscsi_cdb    = cdb;
27425 	com->uscsi_cdblen = CDB_GROUP1;
27426 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27427 
27428 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27429 	    SD_PATH_STANDARD);
27430 
27431 done:
27432 	kmem_free(com, sizeof (*com));
27433 	return (rval);
27434 }
27435 
27436 
27437 /*
27438  *    Function: sr_play_msf()
27439  *
27440  * Description: This routine is the driver entry point for handling CD-ROM
27441  *		ioctl requests to output the audio signals at the specified
27442  *		starting address and continue the audio play until the specified
27443  *		ending address (CDROMPLAYMSF) The address is in Minute Second
27444  *		Frame (MSF) format.
27445  *
27446  *   Arguments: dev	- the device 'dev_t'
27447  *		data	- pointer to user provided audio msf structure,
27448  *		          specifying start/end addresses.
27449  *		flag	- this argument is a pass through to ddi_copyxxx()
27450  *		          directly from the mode argument of ioctl().
27451  *
27452  * Return Code: the code returned by sd_send_scsi_cmd()
27453  *		EFAULT if ddi_copyxxx() fails
27454  *		ENXIO if fail ddi_get_soft_state
27455  *		EINVAL if data pointer is NULL
27456  */
27457 
27458 static int
27459 sr_play_msf(dev_t dev, caddr_t data, int flag)
27460 {
27461 	struct sd_lun		*un;
27462 	struct uscsi_cmd	*com;
27463 	struct cdrom_msf	msf_struct;
27464 	struct cdrom_msf	*msf = &msf_struct;
27465 	char			cdb[CDB_GROUP1];
27466 	int			rval;
27467 
27468 	if (data == NULL) {
27469 		return (EINVAL);
27470 	}
27471 
27472 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27473 		return (ENXIO);
27474 	}
27475 
27476 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27477 		return (EFAULT);
27478 	}
27479 
27480 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27481 	bzero(cdb, CDB_GROUP1);
27482 	cdb[0] = SCMD_PLAYAUDIO_MSF;
27483 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
27484 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27485 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27486 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27487 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27488 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27489 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27490 	} else {
27491 		cdb[3] = msf->cdmsf_min0;
27492 		cdb[4] = msf->cdmsf_sec0;
27493 		cdb[5] = msf->cdmsf_frame0;
27494 		cdb[6] = msf->cdmsf_min1;
27495 		cdb[7] = msf->cdmsf_sec1;
27496 		cdb[8] = msf->cdmsf_frame1;
27497 	}
27498 	com->uscsi_cdb    = cdb;
27499 	com->uscsi_cdblen = CDB_GROUP1;
27500 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27501 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27502 	    SD_PATH_STANDARD);
27503 	kmem_free(com, sizeof (*com));
27504 	return (rval);
27505 }
27506 
27507 
27508 /*
27509  *    Function: sr_play_trkind()
27510  *
27511  * Description: This routine is the driver entry point for handling CD-ROM
27512  *		ioctl requests to output the audio signals at the specified
27513  *		starting address and continue the audio play until the specified
27514  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27515  *		format.
27516  *
27517  *   Arguments: dev	- the device 'dev_t'
27518  *		data	- pointer to user provided audio track/index structure,
27519  *		          specifying start/end addresses.
27520  *		flag	- this argument is a pass through to ddi_copyxxx()
27521  *		          directly from the mode argument of ioctl().
27522  *
27523  * Return Code: the code returned by sd_send_scsi_cmd()
27524  *		EFAULT if ddi_copyxxx() fails
27525  *		ENXIO if fail ddi_get_soft_state
27526  *		EINVAL if data pointer is NULL
27527  */
27528 
27529 static int
27530 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27531 {
27532 	struct cdrom_ti		ti_struct;
27533 	struct cdrom_ti		*ti = &ti_struct;
27534 	struct uscsi_cmd	*com = NULL;
27535 	char			cdb[CDB_GROUP1];
27536 	int			rval;
27537 
27538 	if (data == NULL) {
27539 		return (EINVAL);
27540 	}
27541 
27542 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27543 		return (EFAULT);
27544 	}
27545 
27546 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27547 	bzero(cdb, CDB_GROUP1);
27548 	cdb[0] = SCMD_PLAYAUDIO_TI;
27549 	cdb[4] = ti->cdti_trk0;
27550 	cdb[5] = ti->cdti_ind0;
27551 	cdb[7] = ti->cdti_trk1;
27552 	cdb[8] = ti->cdti_ind1;
27553 	com->uscsi_cdb    = cdb;
27554 	com->uscsi_cdblen = CDB_GROUP1;
27555 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27556 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27557 	    SD_PATH_STANDARD);
27558 	kmem_free(com, sizeof (*com));
27559 	return (rval);
27560 }
27561 
27562 
27563 /*
27564  *    Function: sr_read_all_subcodes()
27565  *
27566  * Description: This routine is the driver entry point for handling CD-ROM
27567  *		ioctl requests to return raw subcode data while the target is
27568  *		playing audio (CDROMSUBCODE).
27569  *
27570  *   Arguments: dev	- the device 'dev_t'
27571  *		data	- pointer to user provided cdrom subcode structure,
27572  *		          specifying the transfer length and address.
27573  *		flag	- this argument is a pass through to ddi_copyxxx()
27574  *		          directly from the mode argument of ioctl().
27575  *
27576  * Return Code: the code returned by sd_send_scsi_cmd()
27577  *		EFAULT if ddi_copyxxx() fails
27578  *		ENXIO if fail ddi_get_soft_state
27579  *		EINVAL if data pointer is NULL
27580  */
27581 
27582 static int
27583 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27584 {
27585 	struct sd_lun		*un = NULL;
27586 	struct uscsi_cmd	*com = NULL;
27587 	struct cdrom_subcode	*subcode = NULL;
27588 	int			rval;
27589 	size_t			buflen;
27590 	char			cdb[CDB_GROUP5];
27591 
27592 #ifdef _MULTI_DATAMODEL
27593 	/* To support ILP32 applications in an LP64 world */
27594 	struct cdrom_subcode32		cdrom_subcode32;
27595 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27596 #endif
27597 	if (data == NULL) {
27598 		return (EINVAL);
27599 	}
27600 
27601 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27602 		return (ENXIO);
27603 	}
27604 
27605 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27606 
27607 #ifdef _MULTI_DATAMODEL
27608 	switch (ddi_model_convert_from(flag & FMODELS)) {
27609 	case DDI_MODEL_ILP32:
27610 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27611 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27612 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27613 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27614 			return (EFAULT);
27615 		}
27616 		/* Convert the ILP32 uscsi data from the application to LP64 */
27617 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27618 		break;
27619 	case DDI_MODEL_NONE:
27620 		if (ddi_copyin(data, subcode,
27621 		    sizeof (struct cdrom_subcode), flag)) {
27622 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27623 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27624 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27625 			return (EFAULT);
27626 		}
27627 		break;
27628 	}
27629 #else /* ! _MULTI_DATAMODEL */
27630 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27631 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27632 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27633 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27634 		return (EFAULT);
27635 	}
27636 #endif /* _MULTI_DATAMODEL */
27637 
27638 	/*
27639 	 * Since MMC-2 expects max 3 bytes for length, check if the
27640 	 * length input is greater than 3 bytes
27641 	 */
27642 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27643 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27644 		    "sr_read_all_subcodes: "
27645 		    "cdrom transfer length too large: %d (limit %d)\n",
27646 		    subcode->cdsc_length, 0xFFFFFF);
27647 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27648 		return (EINVAL);
27649 	}
27650 
27651 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27652 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27653 	bzero(cdb, CDB_GROUP5);
27654 
27655 	if (un->un_f_mmc_cap == TRUE) {
27656 		cdb[0] = (char)SCMD_READ_CD;
27657 		cdb[2] = (char)0xff;
27658 		cdb[3] = (char)0xff;
27659 		cdb[4] = (char)0xff;
27660 		cdb[5] = (char)0xff;
27661 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27662 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27663 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27664 		cdb[10] = 1;
27665 	} else {
27666 		/*
27667 		 * Note: A vendor specific command (0xDF) is being used here to
27668 		 * request a read of all subcodes.
27669 		 */
27670 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27671 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27672 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27673 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27674 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27675 	}
27676 	com->uscsi_cdb	   = cdb;
27677 	com->uscsi_cdblen  = CDB_GROUP5;
27678 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27679 	com->uscsi_buflen  = buflen;
27680 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27681 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27682 	    SD_PATH_STANDARD);
27683 	kmem_free(subcode, sizeof (struct cdrom_subcode));
27684 	kmem_free(com, sizeof (*com));
27685 	return (rval);
27686 }
27687 
27688 
27689 /*
27690  *    Function: sr_read_subchannel()
27691  *
27692  * Description: This routine is the driver entry point for handling CD-ROM
27693  *		ioctl requests to return the Q sub-channel data of the CD
27694  *		current position block. (CDROMSUBCHNL) The data includes the
27695  *		track number, index number, absolute CD-ROM address (LBA or MSF
27696  *		format per the user) , track relative CD-ROM address (LBA or MSF
27697  *		format per the user), control data and audio status.
27698  *
27699  *   Arguments: dev	- the device 'dev_t'
27700  *		data	- pointer to user provided cdrom sub-channel structure
27701  *		flag	- this argument is a pass through to ddi_copyxxx()
27702  *		          directly from the mode argument of ioctl().
27703  *
27704  * Return Code: the code returned by sd_send_scsi_cmd()
27705  *		EFAULT if ddi_copyxxx() fails
27706  *		ENXIO if fail ddi_get_soft_state
27707  *		EINVAL if data pointer is NULL
27708  */
27709 
27710 static int
27711 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27712 {
27713 	struct sd_lun		*un;
27714 	struct uscsi_cmd	*com;
27715 	struct cdrom_subchnl	subchanel;
27716 	struct cdrom_subchnl	*subchnl = &subchanel;
27717 	char			cdb[CDB_GROUP1];
27718 	caddr_t			buffer;
27719 	int			rval;
27720 
27721 	if (data == NULL) {
27722 		return (EINVAL);
27723 	}
27724 
27725 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27726 	    (un->un_state == SD_STATE_OFFLINE)) {
27727 		return (ENXIO);
27728 	}
27729 
27730 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27731 		return (EFAULT);
27732 	}
27733 
27734 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27735 	bzero(cdb, CDB_GROUP1);
27736 	cdb[0] = SCMD_READ_SUBCHANNEL;
27737 	/* Set the MSF bit based on the user requested address format */
27738 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27739 	/*
27740 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27741 	 * returned
27742 	 */
27743 	cdb[2] = 0x40;
27744 	/*
27745 	 * Set byte 3 to specify the return data format. A value of 0x01
27746 	 * indicates that the CD-ROM current position should be returned.
27747 	 */
27748 	cdb[3] = 0x01;
27749 	cdb[8] = 0x10;
27750 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27751 	com->uscsi_cdb	   = cdb;
27752 	com->uscsi_cdblen  = CDB_GROUP1;
27753 	com->uscsi_bufaddr = buffer;
27754 	com->uscsi_buflen  = 16;
27755 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27756 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27757 	    SD_PATH_STANDARD);
27758 	if (rval != 0) {
27759 		kmem_free(buffer, 16);
27760 		kmem_free(com, sizeof (*com));
27761 		return (rval);
27762 	}
27763 
27764 	/* Process the returned Q sub-channel data */
27765 	subchnl->cdsc_audiostatus = buffer[1];
27766 	subchnl->cdsc_adr	= (buffer[5] & 0xF0) >> 4;
27767 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
27768 	subchnl->cdsc_trk	= buffer[6];
27769 	subchnl->cdsc_ind	= buffer[7];
27770 	if (subchnl->cdsc_format & CDROM_LBA) {
27771 		subchnl->cdsc_absaddr.lba =
27772 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27773 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27774 		subchnl->cdsc_reladdr.lba =
27775 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27776 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27777 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
27778 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27779 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27780 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27781 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27782 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27783 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27784 	} else {
27785 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
27786 		subchnl->cdsc_absaddr.msf.second = buffer[10];
27787 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27788 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
27789 		subchnl->cdsc_reladdr.msf.second = buffer[14];
27790 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27791 	}
27792 	kmem_free(buffer, 16);
27793 	kmem_free(com, sizeof (*com));
27794 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27795 	    != 0) {
27796 		return (EFAULT);
27797 	}
27798 	return (rval);
27799 }
27800 
27801 
27802 /*
27803  *    Function: sr_read_tocentry()
27804  *
27805  * Description: This routine is the driver entry point for handling CD-ROM
27806  *		ioctl requests to read from the Table of Contents (TOC)
27807  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27808  *		fields, the starting address (LBA or MSF format per the user)
27809  *		and the data mode if the user specified track is a data track.
27810  *
27811  *		Note: The READ HEADER (0x44) command used in this routine is
27812  *		obsolete per the SCSI MMC spec but still supported in the
27813  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27814  *		therefore the command is still implemented in this routine.
27815  *
27816  *   Arguments: dev	- the device 'dev_t'
27817  *		data	- pointer to user provided toc entry structure,
27818  *			  specifying the track # and the address format
27819  *			  (LBA or MSF).
27820  *		flag	- this argument is a pass through to ddi_copyxxx()
27821  *		          directly from the mode argument of ioctl().
27822  *
27823  * Return Code: the code returned by sd_send_scsi_cmd()
27824  *		EFAULT if ddi_copyxxx() fails
27825  *		ENXIO if fail ddi_get_soft_state
27826  *		EINVAL if data pointer is NULL
27827  */
27828 
27829 static int
27830 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27831 {
27832 	struct sd_lun		*un = NULL;
27833 	struct uscsi_cmd	*com;
27834 	struct cdrom_tocentry	toc_entry;
27835 	struct cdrom_tocentry	*entry = &toc_entry;
27836 	caddr_t			buffer;
27837 	int			rval;
27838 	char			cdb[CDB_GROUP1];
27839 
27840 	if (data == NULL) {
27841 		return (EINVAL);
27842 	}
27843 
27844 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27845 	    (un->un_state == SD_STATE_OFFLINE)) {
27846 		return (ENXIO);
27847 	}
27848 
27849 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27850 		return (EFAULT);
27851 	}
27852 
27853 	/* Validate the requested track and address format */
27854 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27855 		return (EINVAL);
27856 	}
27857 
27858 	if (entry->cdte_track == 0) {
27859 		return (EINVAL);
27860 	}
27861 
27862 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27863 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27864 	bzero(cdb, CDB_GROUP1);
27865 
27866 	cdb[0] = SCMD_READ_TOC;
27867 	/* Set the MSF bit based on the user requested address format  */
27868 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27869 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27870 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27871 	} else {
27872 		cdb[6] = entry->cdte_track;
27873 	}
27874 
27875 	/*
27876 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27877 	 * (4 byte TOC response header + 8 byte track descriptor)
27878 	 */
27879 	cdb[8] = 12;
27880 	com->uscsi_cdb	   = cdb;
27881 	com->uscsi_cdblen  = CDB_GROUP1;
27882 	com->uscsi_bufaddr = buffer;
27883 	com->uscsi_buflen  = 0x0C;
27884 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27885 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27886 	    SD_PATH_STANDARD);
27887 	if (rval != 0) {
27888 		kmem_free(buffer, 12);
27889 		kmem_free(com, sizeof (*com));
27890 		return (rval);
27891 	}
27892 
27893 	/* Process the toc entry */
27894 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
27895 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
27896 	if (entry->cdte_format & CDROM_LBA) {
27897 		entry->cdte_addr.lba =
27898 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27899 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27900 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27901 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
27902 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
27903 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
27904 		/*
27905 		 * Send a READ TOC command using the LBA address format to get
27906 		 * the LBA for the track requested so it can be used in the
27907 		 * READ HEADER request
27908 		 *
27909 		 * Note: The MSF bit of the READ HEADER command specifies the
27910 		 * output format. The block address specified in that command
27911 		 * must be in LBA format.
27912 		 */
27913 		cdb[1] = 0;
27914 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27915 		    SD_PATH_STANDARD);
27916 		if (rval != 0) {
27917 			kmem_free(buffer, 12);
27918 			kmem_free(com, sizeof (*com));
27919 			return (rval);
27920 		}
27921 	} else {
27922 		entry->cdte_addr.msf.minute	= buffer[9];
27923 		entry->cdte_addr.msf.second	= buffer[10];
27924 		entry->cdte_addr.msf.frame	= buffer[11];
27925 		/*
27926 		 * Send a READ TOC command using the LBA address format to get
27927 		 * the LBA for the track requested so it can be used in the
27928 		 * READ HEADER request
27929 		 *
27930 		 * Note: The MSF bit of the READ HEADER command specifies the
27931 		 * output format. The block address specified in that command
27932 		 * must be in LBA format.
27933 		 */
27934 		cdb[1] = 0;
27935 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27936 		    SD_PATH_STANDARD);
27937 		if (rval != 0) {
27938 			kmem_free(buffer, 12);
27939 			kmem_free(com, sizeof (*com));
27940 			return (rval);
27941 		}
27942 	}
27943 
27944 	/*
27945 	 * Build and send the READ HEADER command to determine the data mode of
27946 	 * the user specified track.
27947 	 */
27948 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27949 	    (entry->cdte_track != CDROM_LEADOUT)) {
27950 		bzero(cdb, CDB_GROUP1);
27951 		cdb[0] = SCMD_READ_HEADER;
27952 		cdb[2] = buffer[8];
27953 		cdb[3] = buffer[9];
27954 		cdb[4] = buffer[10];
27955 		cdb[5] = buffer[11];
27956 		cdb[8] = 0x08;
27957 		com->uscsi_buflen = 0x08;
27958 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27959 		    SD_PATH_STANDARD);
27960 		if (rval == 0) {
27961 			entry->cdte_datamode = buffer[0];
27962 		} else {
27963 			/*
27964 			 * READ HEADER command failed, since this is
27965 			 * obsoleted in one spec, its better to return
27966 			 * -1 for an invlid track so that we can still
27967 			 * receive the rest of the TOC data.
27968 			 */
27969 			entry->cdte_datamode = (uchar_t)-1;
27970 		}
27971 	} else {
27972 		entry->cdte_datamode = (uchar_t)-1;
27973 	}
27974 
27975 	kmem_free(buffer, 12);
27976 	kmem_free(com, sizeof (*com));
27977 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27978 		return (EFAULT);
27979 
27980 	return (rval);
27981 }
27982 
27983 
27984 /*
27985  *    Function: sr_read_tochdr()
27986  *
27987  * Description: This routine is the driver entry point for handling CD-ROM
27988  * 		ioctl requests to read the Table of Contents (TOC) header
27989  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
27990  *		and ending track numbers
27991  *
27992  *   Arguments: dev	- the device 'dev_t'
27993  *		data	- pointer to user provided toc header structure,
27994  *			  specifying the starting and ending track numbers.
27995  *		flag	- this argument is a pass through to ddi_copyxxx()
27996  *			  directly from the mode argument of ioctl().
27997  *
27998  * Return Code: the code returned by sd_send_scsi_cmd()
27999  *		EFAULT if ddi_copyxxx() fails
28000  *		ENXIO if fail ddi_get_soft_state
28001  *		EINVAL if data pointer is NULL
28002  */
28003 
28004 static int
28005 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
28006 {
28007 	struct sd_lun		*un;
28008 	struct uscsi_cmd	*com;
28009 	struct cdrom_tochdr	toc_header;
28010 	struct cdrom_tochdr	*hdr = &toc_header;
28011 	char			cdb[CDB_GROUP1];
28012 	int			rval;
28013 	caddr_t			buffer;
28014 
28015 	if (data == NULL) {
28016 		return (EINVAL);
28017 	}
28018 
28019 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28020 	    (un->un_state == SD_STATE_OFFLINE)) {
28021 		return (ENXIO);
28022 	}
28023 
28024 	buffer = kmem_zalloc(4, KM_SLEEP);
28025 	bzero(cdb, CDB_GROUP1);
28026 	cdb[0] = SCMD_READ_TOC;
28027 	/*
28028 	 * Specifying a track number of 0x00 in the READ TOC command indicates
28029 	 * that the TOC header should be returned
28030 	 */
28031 	cdb[6] = 0x00;
28032 	/*
28033 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28034 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28035 	 */
28036 	cdb[8] = 0x04;
28037 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28038 	com->uscsi_cdb	   = cdb;
28039 	com->uscsi_cdblen  = CDB_GROUP1;
28040 	com->uscsi_bufaddr = buffer;
28041 	com->uscsi_buflen  = 0x04;
28042 	com->uscsi_timeout = 300;
28043 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28044 
28045 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28046 	    SD_PATH_STANDARD);
28047 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28048 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28049 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28050 	} else {
28051 		hdr->cdth_trk0 = buffer[2];
28052 		hdr->cdth_trk1 = buffer[3];
28053 	}
28054 	kmem_free(buffer, 4);
28055 	kmem_free(com, sizeof (*com));
28056 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28057 		return (EFAULT);
28058 	}
28059 	return (rval);
28060 }
28061 
28062 
28063 /*
28064  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28065  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28066  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28067  * digital audio and extended architecture digital audio. These modes are
28068  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28069  * MMC specs.
28070  *
28071  * In addition to support for the various data formats these routines also
28072  * include support for devices that implement only the direct access READ
28073  * commands (0x08, 0x28), devices that implement the READ_CD commands
28074  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28075  * READ CDXA commands (0xD8, 0xDB)
28076  */
28077 
28078 /*
28079  *    Function: sr_read_mode1()
28080  *
28081  * Description: This routine is the driver entry point for handling CD-ROM
28082  *		ioctl read mode1 requests (CDROMREADMODE1).
28083  *
28084  *   Arguments: dev	- the device 'dev_t'
28085  *		data	- pointer to user provided cd read structure specifying
28086  *			  the lba buffer address and length.
28087  *		flag	- this argument is a pass through to ddi_copyxxx()
28088  *			  directly from the mode argument of ioctl().
28089  *
28090  * Return Code: the code returned by sd_send_scsi_cmd()
28091  *		EFAULT if ddi_copyxxx() fails
28092  *		ENXIO if fail ddi_get_soft_state
28093  *		EINVAL if data pointer is NULL
28094  */
28095 
28096 static int
28097 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28098 {
28099 	struct sd_lun		*un;
28100 	struct cdrom_read	mode1_struct;
28101 	struct cdrom_read	*mode1 = &mode1_struct;
28102 	int			rval;
28103 	sd_ssc_t		*ssc;
28104 
28105 #ifdef _MULTI_DATAMODEL
28106 	/* To support ILP32 applications in an LP64 world */
28107 	struct cdrom_read32	cdrom_read32;
28108 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28109 #endif /* _MULTI_DATAMODEL */
28110 
28111 	if (data == NULL) {
28112 		return (EINVAL);
28113 	}
28114 
28115 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28116 	    (un->un_state == SD_STATE_OFFLINE)) {
28117 		return (ENXIO);
28118 	}
28119 
28120 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28121 	    "sd_read_mode1: entry: un:0x%p\n", un);
28122 
28123 #ifdef _MULTI_DATAMODEL
28124 	switch (ddi_model_convert_from(flag & FMODELS)) {
28125 	case DDI_MODEL_ILP32:
28126 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28127 			return (EFAULT);
28128 		}
28129 		/* Convert the ILP32 uscsi data from the application to LP64 */
28130 		cdrom_read32tocdrom_read(cdrd32, mode1);
28131 		break;
28132 	case DDI_MODEL_NONE:
28133 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28134 			return (EFAULT);
28135 		}
28136 	}
28137 #else /* ! _MULTI_DATAMODEL */
28138 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28139 		return (EFAULT);
28140 	}
28141 #endif /* _MULTI_DATAMODEL */
28142 
28143 	ssc = sd_ssc_init(un);
28144 	rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
28145 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28146 	sd_ssc_fini(ssc);
28147 
28148 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28149 	    "sd_read_mode1: exit: un:0x%p\n", un);
28150 
28151 	return (rval);
28152 }
28153 
28154 
28155 /*
28156  *    Function: sr_read_cd_mode2()
28157  *
28158  * Description: This routine is the driver entry point for handling CD-ROM
28159  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28160  *		support the READ CD (0xBE) command or the 1st generation
28161  *		READ CD (0xD4) command.
28162  *
28163  *   Arguments: dev	- the device 'dev_t'
28164  *		data	- pointer to user provided cd read structure specifying
28165  *			  the lba buffer address and length.
28166  *		flag	- this argument is a pass through to ddi_copyxxx()
28167  *			  directly from the mode argument of ioctl().
28168  *
28169  * Return Code: the code returned by sd_send_scsi_cmd()
28170  *		EFAULT if ddi_copyxxx() fails
28171  *		ENXIO if fail ddi_get_soft_state
28172  *		EINVAL if data pointer is NULL
28173  */
28174 
28175 static int
28176 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28177 {
28178 	struct sd_lun		*un;
28179 	struct uscsi_cmd	*com;
28180 	struct cdrom_read	mode2_struct;
28181 	struct cdrom_read	*mode2 = &mode2_struct;
28182 	uchar_t			cdb[CDB_GROUP5];
28183 	int			nblocks;
28184 	int			rval;
28185 #ifdef _MULTI_DATAMODEL
28186 	/*  To support ILP32 applications in an LP64 world */
28187 	struct cdrom_read32	cdrom_read32;
28188 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28189 #endif /* _MULTI_DATAMODEL */
28190 
28191 	if (data == NULL) {
28192 		return (EINVAL);
28193 	}
28194 
28195 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28196 	    (un->un_state == SD_STATE_OFFLINE)) {
28197 		return (ENXIO);
28198 	}
28199 
28200 #ifdef _MULTI_DATAMODEL
28201 	switch (ddi_model_convert_from(flag & FMODELS)) {
28202 	case DDI_MODEL_ILP32:
28203 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28204 			return (EFAULT);
28205 		}
28206 		/* Convert the ILP32 uscsi data from the application to LP64 */
28207 		cdrom_read32tocdrom_read(cdrd32, mode2);
28208 		break;
28209 	case DDI_MODEL_NONE:
28210 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28211 			return (EFAULT);
28212 		}
28213 		break;
28214 	}
28215 
28216 #else /* ! _MULTI_DATAMODEL */
28217 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28218 		return (EFAULT);
28219 	}
28220 #endif /* _MULTI_DATAMODEL */
28221 
28222 	bzero(cdb, sizeof (cdb));
28223 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28224 		/* Read command supported by 1st generation atapi drives */
28225 		cdb[0] = SCMD_READ_CDD4;
28226 	} else {
28227 		/* Universal CD Access Command */
28228 		cdb[0] = SCMD_READ_CD;
28229 	}
28230 
28231 	/*
28232 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28233 	 */
28234 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28235 
28236 	/* set the start address */
28237 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28238 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28239 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28240 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28241 
28242 	/* set the transfer length */
28243 	nblocks = mode2->cdread_buflen / 2336;
28244 	cdb[6] = (uchar_t)(nblocks >> 16);
28245 	cdb[7] = (uchar_t)(nblocks >> 8);
28246 	cdb[8] = (uchar_t)nblocks;
28247 
28248 	/* set the filter bits */
28249 	cdb[9] = CDROM_READ_CD_USERDATA;
28250 
28251 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28252 	com->uscsi_cdb = (caddr_t)cdb;
28253 	com->uscsi_cdblen = sizeof (cdb);
28254 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28255 	com->uscsi_buflen = mode2->cdread_buflen;
28256 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28257 
28258 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28259 	    SD_PATH_STANDARD);
28260 	kmem_free(com, sizeof (*com));
28261 	return (rval);
28262 }
28263 
28264 
28265 /*
28266  *    Function: sr_read_mode2()
28267  *
28268  * Description: This routine is the driver entry point for handling CD-ROM
28269  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28270  *		do not support the READ CD (0xBE) command.
28271  *
28272  *   Arguments: dev	- the device 'dev_t'
28273  *		data	- pointer to user provided cd read structure specifying
28274  *			  the lba buffer address and length.
28275  *		flag	- this argument is a pass through to ddi_copyxxx()
28276  *			  directly from the mode argument of ioctl().
28277  *
28278  * Return Code: the code returned by sd_send_scsi_cmd()
28279  *		EFAULT if ddi_copyxxx() fails
28280  *		ENXIO if fail ddi_get_soft_state
28281  *		EINVAL if data pointer is NULL
28282  *		EIO if fail to reset block size
28283  *		EAGAIN if commands are in progress in the driver
28284  */
28285 
28286 static int
28287 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28288 {
28289 	struct sd_lun		*un;
28290 	struct cdrom_read	mode2_struct;
28291 	struct cdrom_read	*mode2 = &mode2_struct;
28292 	int			rval;
28293 	uint32_t		restore_blksize;
28294 	struct uscsi_cmd	*com;
28295 	uchar_t			cdb[CDB_GROUP0];
28296 	int			nblocks;
28297 
28298 #ifdef _MULTI_DATAMODEL
28299 	/* To support ILP32 applications in an LP64 world */
28300 	struct cdrom_read32	cdrom_read32;
28301 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28302 #endif /* _MULTI_DATAMODEL */
28303 
28304 	if (data == NULL) {
28305 		return (EINVAL);
28306 	}
28307 
28308 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28309 	    (un->un_state == SD_STATE_OFFLINE)) {
28310 		return (ENXIO);
28311 	}
28312 
28313 	/*
28314 	 * Because this routine will update the device and driver block size
28315 	 * being used we want to make sure there are no commands in progress.
28316 	 * If commands are in progress the user will have to try again.
28317 	 *
28318 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28319 	 * in sdioctl to protect commands from sdioctl through to the top of
28320 	 * sd_uscsi_strategy. See sdioctl for details.
28321 	 */
28322 	mutex_enter(SD_MUTEX(un));
28323 	if (un->un_ncmds_in_driver != 1) {
28324 		mutex_exit(SD_MUTEX(un));
28325 		return (EAGAIN);
28326 	}
28327 	mutex_exit(SD_MUTEX(un));
28328 
28329 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28330 	    "sd_read_mode2: entry: un:0x%p\n", un);
28331 
28332 #ifdef _MULTI_DATAMODEL
28333 	switch (ddi_model_convert_from(flag & FMODELS)) {
28334 	case DDI_MODEL_ILP32:
28335 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28336 			return (EFAULT);
28337 		}
28338 		/* Convert the ILP32 uscsi data from the application to LP64 */
28339 		cdrom_read32tocdrom_read(cdrd32, mode2);
28340 		break;
28341 	case DDI_MODEL_NONE:
28342 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28343 			return (EFAULT);
28344 		}
28345 		break;
28346 	}
28347 #else /* ! _MULTI_DATAMODEL */
28348 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28349 		return (EFAULT);
28350 	}
28351 #endif /* _MULTI_DATAMODEL */
28352 
28353 	/* Store the current target block size for restoration later */
28354 	restore_blksize = un->un_tgt_blocksize;
28355 
28356 	/* Change the device and soft state target block size to 2336 */
28357 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28358 		rval = EIO;
28359 		goto done;
28360 	}
28361 
28362 
28363 	bzero(cdb, sizeof (cdb));
28364 
28365 	/* set READ operation */
28366 	cdb[0] = SCMD_READ;
28367 
28368 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
28369 	mode2->cdread_lba >>= 2;
28370 
28371 	/* set the start address */
28372 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28373 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28374 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28375 
28376 	/* set the transfer length */
28377 	nblocks = mode2->cdread_buflen / 2336;
28378 	cdb[4] = (uchar_t)nblocks & 0xFF;
28379 
28380 	/* build command */
28381 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28382 	com->uscsi_cdb = (caddr_t)cdb;
28383 	com->uscsi_cdblen = sizeof (cdb);
28384 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28385 	com->uscsi_buflen = mode2->cdread_buflen;
28386 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28387 
28388 	/*
28389 	 * Issue SCSI command with user space address for read buffer.
28390 	 *
28391 	 * This sends the command through main channel in the driver.
28392 	 *
28393 	 * Since this is accessed via an IOCTL call, we go through the
28394 	 * standard path, so that if the device was powered down, then
28395 	 * it would be 'awakened' to handle the command.
28396 	 */
28397 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28398 	    SD_PATH_STANDARD);
28399 
28400 	kmem_free(com, sizeof (*com));
28401 
28402 	/* Restore the device and soft state target block size */
28403 	if (sr_sector_mode(dev, restore_blksize) != 0) {
28404 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28405 		    "can't do switch back to mode 1\n");
28406 		/*
28407 		 * If sd_send_scsi_READ succeeded we still need to report
28408 		 * an error because we failed to reset the block size
28409 		 */
28410 		if (rval == 0) {
28411 			rval = EIO;
28412 		}
28413 	}
28414 
28415 done:
28416 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28417 	    "sd_read_mode2: exit: un:0x%p\n", un);
28418 
28419 	return (rval);
28420 }
28421 
28422 
28423 /*
28424  *    Function: sr_sector_mode()
28425  *
28426  * Description: This utility function is used by sr_read_mode2 to set the target
28427  *		block size based on the user specified size. This is a legacy
28428  *		implementation based upon a vendor specific mode page
28429  *
28430  *   Arguments: dev	- the device 'dev_t'
28431  *		data	- flag indicating if block size is being set to 2336 or
28432  *			  512.
28433  *
28434  * Return Code: the code returned by sd_send_scsi_cmd()
28435  *		EFAULT if ddi_copyxxx() fails
28436  *		ENXIO if fail ddi_get_soft_state
28437  *		EINVAL if data pointer is NULL
28438  */
28439 
28440 static int
28441 sr_sector_mode(dev_t dev, uint32_t blksize)
28442 {
28443 	struct sd_lun	*un;
28444 	uchar_t		*sense;
28445 	uchar_t		*select;
28446 	int		rval;
28447 	sd_ssc_t	*ssc;
28448 
28449 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28450 	    (un->un_state == SD_STATE_OFFLINE)) {
28451 		return (ENXIO);
28452 	}
28453 
28454 	sense = kmem_zalloc(20, KM_SLEEP);
28455 
28456 	/* Note: This is a vendor specific mode page (0x81) */
28457 	ssc = sd_ssc_init(un);
28458 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28459 	    SD_PATH_STANDARD);
28460 	sd_ssc_fini(ssc);
28461 	if (rval != 0) {
28462 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28463 		    "sr_sector_mode: Mode Sense failed\n");
28464 		kmem_free(sense, 20);
28465 		return (rval);
28466 	}
28467 	select = kmem_zalloc(20, KM_SLEEP);
28468 	select[3] = 0x08;
28469 	select[10] = ((blksize >> 8) & 0xff);
28470 	select[11] = (blksize & 0xff);
28471 	select[12] = 0x01;
28472 	select[13] = 0x06;
28473 	select[14] = sense[14];
28474 	select[15] = sense[15];
28475 	if (blksize == SD_MODE2_BLKSIZE) {
28476 		select[14] |= 0x01;
28477 	}
28478 
28479 	ssc = sd_ssc_init(un);
28480 	rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28481 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28482 	sd_ssc_fini(ssc);
28483 	if (rval != 0) {
28484 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28485 		    "sr_sector_mode: Mode Select failed\n");
28486 	} else {
28487 		/*
28488 		 * Only update the softstate block size if we successfully
28489 		 * changed the device block mode.
28490 		 */
28491 		mutex_enter(SD_MUTEX(un));
28492 		sd_update_block_info(un, blksize, 0);
28493 		mutex_exit(SD_MUTEX(un));
28494 	}
28495 	kmem_free(sense, 20);
28496 	kmem_free(select, 20);
28497 	return (rval);
28498 }
28499 
28500 
28501 /*
28502  *    Function: sr_read_cdda()
28503  *
28504  * Description: This routine is the driver entry point for handling CD-ROM
28505  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28506  *		the target supports CDDA these requests are handled via a vendor
28507  *		specific command (0xD8) If the target does not support CDDA
28508  *		these requests are handled via the READ CD command (0xBE).
28509  *
28510  *   Arguments: dev	- the device 'dev_t'
28511  *		data	- pointer to user provided CD-DA structure specifying
28512  *			  the track starting address, transfer length, and
28513  *			  subcode options.
28514  *		flag	- this argument is a pass through to ddi_copyxxx()
28515  *			  directly from the mode argument of ioctl().
28516  *
28517  * Return Code: the code returned by sd_send_scsi_cmd()
28518  *		EFAULT if ddi_copyxxx() fails
28519  *		ENXIO if fail ddi_get_soft_state
28520  *		EINVAL if invalid arguments are provided
28521  *		ENOTTY
28522  */
28523 
28524 static int
28525 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28526 {
28527 	struct sd_lun			*un;
28528 	struct uscsi_cmd		*com;
28529 	struct cdrom_cdda		*cdda;
28530 	int				rval;
28531 	size_t				buflen;
28532 	char				cdb[CDB_GROUP5];
28533 
28534 #ifdef _MULTI_DATAMODEL
28535 	/* To support ILP32 applications in an LP64 world */
28536 	struct cdrom_cdda32	cdrom_cdda32;
28537 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28538 #endif /* _MULTI_DATAMODEL */
28539 
28540 	if (data == NULL) {
28541 		return (EINVAL);
28542 	}
28543 
28544 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28545 		return (ENXIO);
28546 	}
28547 
28548 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28549 
28550 #ifdef _MULTI_DATAMODEL
28551 	switch (ddi_model_convert_from(flag & FMODELS)) {
28552 	case DDI_MODEL_ILP32:
28553 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28554 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28555 			    "sr_read_cdda: ddi_copyin Failed\n");
28556 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28557 			return (EFAULT);
28558 		}
28559 		/* Convert the ILP32 uscsi data from the application to LP64 */
28560 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28561 		break;
28562 	case DDI_MODEL_NONE:
28563 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28564 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28565 			    "sr_read_cdda: ddi_copyin Failed\n");
28566 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28567 			return (EFAULT);
28568 		}
28569 		break;
28570 	}
28571 #else /* ! _MULTI_DATAMODEL */
28572 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28573 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28574 		    "sr_read_cdda: ddi_copyin Failed\n");
28575 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28576 		return (EFAULT);
28577 	}
28578 #endif /* _MULTI_DATAMODEL */
28579 
28580 	/*
28581 	 * Since MMC-2 expects max 3 bytes for length, check if the
28582 	 * length input is greater than 3 bytes
28583 	 */
28584 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28585 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28586 		    "cdrom transfer length too large: %d (limit %d)\n",
28587 		    cdda->cdda_length, 0xFFFFFF);
28588 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28589 		return (EINVAL);
28590 	}
28591 
28592 	switch (cdda->cdda_subcode) {
28593 	case CDROM_DA_NO_SUBCODE:
28594 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28595 		break;
28596 	case CDROM_DA_SUBQ:
28597 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28598 		break;
28599 	case CDROM_DA_ALL_SUBCODE:
28600 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28601 		break;
28602 	case CDROM_DA_SUBCODE_ONLY:
28603 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28604 		break;
28605 	default:
28606 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28607 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28608 		    cdda->cdda_subcode);
28609 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28610 		return (EINVAL);
28611 	}
28612 
28613 	/* Build and send the command */
28614 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28615 	bzero(cdb, CDB_GROUP5);
28616 
28617 	if (un->un_f_cfg_cdda == TRUE) {
28618 		cdb[0] = (char)SCMD_READ_CD;
28619 		cdb[1] = 0x04;
28620 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28621 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28622 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28623 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28624 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28625 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28626 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28627 		cdb[9] = 0x10;
28628 		switch (cdda->cdda_subcode) {
28629 		case CDROM_DA_NO_SUBCODE :
28630 			cdb[10] = 0x0;
28631 			break;
28632 		case CDROM_DA_SUBQ :
28633 			cdb[10] = 0x2;
28634 			break;
28635 		case CDROM_DA_ALL_SUBCODE :
28636 			cdb[10] = 0x1;
28637 			break;
28638 		case CDROM_DA_SUBCODE_ONLY :
28639 			/* FALLTHROUGH */
28640 		default :
28641 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28642 			kmem_free(com, sizeof (*com));
28643 			return (ENOTTY);
28644 		}
28645 	} else {
28646 		cdb[0] = (char)SCMD_READ_CDDA;
28647 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28648 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28649 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28650 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28651 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28652 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28653 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28654 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28655 		cdb[10] = cdda->cdda_subcode;
28656 	}
28657 
28658 	com->uscsi_cdb = cdb;
28659 	com->uscsi_cdblen = CDB_GROUP5;
28660 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28661 	com->uscsi_buflen = buflen;
28662 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28663 
28664 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28665 	    SD_PATH_STANDARD);
28666 
28667 	kmem_free(cdda, sizeof (struct cdrom_cdda));
28668 	kmem_free(com, sizeof (*com));
28669 	return (rval);
28670 }
28671 
28672 
28673 /*
28674  *    Function: sr_read_cdxa()
28675  *
28676  * Description: This routine is the driver entry point for handling CD-ROM
28677  *		ioctl requests to return CD-XA (Extended Architecture) data.
28678  *		(CDROMCDXA).
28679  *
28680  *   Arguments: dev	- the device 'dev_t'
28681  *		data	- pointer to user provided CD-XA structure specifying
28682  *			  the data starting address, transfer length, and format
28683  *		flag	- this argument is a pass through to ddi_copyxxx()
28684  *			  directly from the mode argument of ioctl().
28685  *
28686  * Return Code: the code returned by sd_send_scsi_cmd()
28687  *		EFAULT if ddi_copyxxx() fails
28688  *		ENXIO if fail ddi_get_soft_state
28689  *		EINVAL if data pointer is NULL
28690  */
28691 
28692 static int
28693 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28694 {
28695 	struct sd_lun		*un;
28696 	struct uscsi_cmd	*com;
28697 	struct cdrom_cdxa	*cdxa;
28698 	int			rval;
28699 	size_t			buflen;
28700 	char			cdb[CDB_GROUP5];
28701 	uchar_t			read_flags;
28702 
28703 #ifdef _MULTI_DATAMODEL
28704 	/* To support ILP32 applications in an LP64 world */
28705 	struct cdrom_cdxa32		cdrom_cdxa32;
28706 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
28707 #endif /* _MULTI_DATAMODEL */
28708 
28709 	if (data == NULL) {
28710 		return (EINVAL);
28711 	}
28712 
28713 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28714 		return (ENXIO);
28715 	}
28716 
28717 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28718 
28719 #ifdef _MULTI_DATAMODEL
28720 	switch (ddi_model_convert_from(flag & FMODELS)) {
28721 	case DDI_MODEL_ILP32:
28722 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28723 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28724 			return (EFAULT);
28725 		}
28726 		/*
28727 		 * Convert the ILP32 uscsi data from the
28728 		 * application to LP64 for internal use.
28729 		 */
28730 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28731 		break;
28732 	case DDI_MODEL_NONE:
28733 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28734 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28735 			return (EFAULT);
28736 		}
28737 		break;
28738 	}
28739 #else /* ! _MULTI_DATAMODEL */
28740 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28741 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28742 		return (EFAULT);
28743 	}
28744 #endif /* _MULTI_DATAMODEL */
28745 
28746 	/*
28747 	 * Since MMC-2 expects max 3 bytes for length, check if the
28748 	 * length input is greater than 3 bytes
28749 	 */
28750 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28751 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28752 		    "cdrom transfer length too large: %d (limit %d)\n",
28753 		    cdxa->cdxa_length, 0xFFFFFF);
28754 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28755 		return (EINVAL);
28756 	}
28757 
28758 	switch (cdxa->cdxa_format) {
28759 	case CDROM_XA_DATA:
28760 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28761 		read_flags = 0x10;
28762 		break;
28763 	case CDROM_XA_SECTOR_DATA:
28764 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28765 		read_flags = 0xf8;
28766 		break;
28767 	case CDROM_XA_DATA_W_ERROR:
28768 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28769 		read_flags = 0xfc;
28770 		break;
28771 	default:
28772 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28773 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
28774 		    cdxa->cdxa_format);
28775 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28776 		return (EINVAL);
28777 	}
28778 
28779 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28780 	bzero(cdb, CDB_GROUP5);
28781 	if (un->un_f_mmc_cap == TRUE) {
28782 		cdb[0] = (char)SCMD_READ_CD;
28783 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28784 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28785 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28786 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28787 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28788 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28789 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28790 		cdb[9] = (char)read_flags;
28791 	} else {
28792 		/*
28793 		 * Note: A vendor specific command (0xDB) is being used her to
28794 		 * request a read of all subcodes.
28795 		 */
28796 		cdb[0] = (char)SCMD_READ_CDXA;
28797 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28798 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28799 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28800 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28801 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28802 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28803 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28804 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28805 		cdb[10] = cdxa->cdxa_format;
28806 	}
28807 	com->uscsi_cdb	   = cdb;
28808 	com->uscsi_cdblen  = CDB_GROUP5;
28809 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28810 	com->uscsi_buflen  = buflen;
28811 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28812 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28813 	    SD_PATH_STANDARD);
28814 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28815 	kmem_free(com, sizeof (*com));
28816 	return (rval);
28817 }
28818 
28819 
28820 /*
28821  *    Function: sr_eject()
28822  *
28823  * Description: This routine is the driver entry point for handling CD-ROM
28824  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28825  *
28826  *   Arguments: dev	- the device 'dev_t'
28827  *
28828  * Return Code: the code returned by sd_send_scsi_cmd()
28829  */
28830 
28831 static int
28832 sr_eject(dev_t dev)
28833 {
28834 	struct sd_lun	*un;
28835 	int		rval;
28836 	sd_ssc_t	*ssc;
28837 
28838 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28839 	    (un->un_state == SD_STATE_OFFLINE)) {
28840 		return (ENXIO);
28841 	}
28842 
28843 	/*
28844 	 * To prevent race conditions with the eject
28845 	 * command, keep track of an eject command as
28846 	 * it progresses. If we are already handling
28847 	 * an eject command in the driver for the given
28848 	 * unit and another request to eject is received
28849 	 * immediately return EAGAIN so we don't lose
28850 	 * the command if the current eject command fails.
28851 	 */
28852 	mutex_enter(SD_MUTEX(un));
28853 	if (un->un_f_ejecting == TRUE) {
28854 		mutex_exit(SD_MUTEX(un));
28855 		return (EAGAIN);
28856 	}
28857 	un->un_f_ejecting = TRUE;
28858 	mutex_exit(SD_MUTEX(un));
28859 
28860 	ssc = sd_ssc_init(un);
28861 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
28862 	    SD_PATH_STANDARD);
28863 	sd_ssc_fini(ssc);
28864 
28865 	if (rval != 0) {
28866 		mutex_enter(SD_MUTEX(un));
28867 		un->un_f_ejecting = FALSE;
28868 		mutex_exit(SD_MUTEX(un));
28869 		return (rval);
28870 	}
28871 
28872 	ssc = sd_ssc_init(un);
28873 	rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
28874 	    SD_TARGET_EJECT, SD_PATH_STANDARD);
28875 	sd_ssc_fini(ssc);
28876 
28877 	if (rval == 0) {
28878 		mutex_enter(SD_MUTEX(un));
28879 		sr_ejected(un);
28880 		un->un_mediastate = DKIO_EJECTED;
28881 		un->un_f_ejecting = FALSE;
28882 		cv_broadcast(&un->un_state_cv);
28883 		mutex_exit(SD_MUTEX(un));
28884 	} else {
28885 		mutex_enter(SD_MUTEX(un));
28886 		un->un_f_ejecting = FALSE;
28887 		mutex_exit(SD_MUTEX(un));
28888 	}
28889 	return (rval);
28890 }
28891 
28892 
28893 /*
28894  *    Function: sr_ejected()
28895  *
28896  * Description: This routine updates the soft state structure to invalidate the
28897  *		geometry information after the media has been ejected or a
28898  *		media eject has been detected.
28899  *
28900  *   Arguments: un - driver soft state (unit) structure
28901  */
28902 
28903 static void
28904 sr_ejected(struct sd_lun *un)
28905 {
28906 	struct sd_errstats *stp;
28907 
28908 	ASSERT(un != NULL);
28909 	ASSERT(mutex_owned(SD_MUTEX(un)));
28910 
28911 	un->un_f_blockcount_is_valid	= FALSE;
28912 	un->un_f_tgt_blocksize_is_valid	= FALSE;
28913 	mutex_exit(SD_MUTEX(un));
28914 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
28915 	mutex_enter(SD_MUTEX(un));
28916 
28917 	if (un->un_errstats != NULL) {
28918 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
28919 		stp->sd_capacity.value.ui64 = 0;
28920 	}
28921 }
28922 
28923 
28924 /*
28925  *    Function: sr_check_wp()
28926  *
28927  * Description: This routine checks the write protection of a removable
28928  *      media disk and hotpluggable devices via the write protect bit of
28929  *      the Mode Page Header device specific field. Some devices choke
28930  *      on unsupported mode page. In order to workaround this issue,
28931  *      this routine has been implemented to use 0x3f mode page(request
28932  *      for all pages) for all device types.
28933  *
28934  *   Arguments: dev             - the device 'dev_t'
28935  *
28936  * Return Code: int indicating if the device is write protected (1) or not (0)
28937  *
28938  *     Context: Kernel thread.
28939  *
28940  */
28941 
28942 static int
28943 sr_check_wp(dev_t dev)
28944 {
28945 	struct sd_lun	*un;
28946 	uchar_t		device_specific;
28947 	uchar_t		*sense;
28948 	int		hdrlen;
28949 	int		rval = FALSE;
28950 	int		status;
28951 	sd_ssc_t	*ssc;
28952 
28953 	/*
28954 	 * Note: The return codes for this routine should be reworked to
28955 	 * properly handle the case of a NULL softstate.
28956 	 */
28957 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28958 		return (FALSE);
28959 	}
28960 
28961 	if (un->un_f_cfg_is_atapi == TRUE) {
28962 		/*
28963 		 * The mode page contents are not required; set the allocation
28964 		 * length for the mode page header only
28965 		 */
28966 		hdrlen = MODE_HEADER_LENGTH_GRP2;
28967 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28968 		ssc = sd_ssc_init(un);
28969 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
28970 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28971 		sd_ssc_fini(ssc);
28972 		if (status != 0)
28973 			goto err_exit;
28974 		device_specific =
28975 		    ((struct mode_header_grp2 *)sense)->device_specific;
28976 	} else {
28977 		hdrlen = MODE_HEADER_LENGTH;
28978 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28979 		ssc = sd_ssc_init(un);
28980 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
28981 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28982 		sd_ssc_fini(ssc);
28983 		if (status != 0)
28984 			goto err_exit;
28985 		device_specific =
28986 		    ((struct mode_header *)sense)->device_specific;
28987 	}
28988 
28989 
28990 	/*
28991 	 * Write protect mode sense failed; not all disks
28992 	 * understand this query. Return FALSE assuming that
28993 	 * these devices are not writable.
28994 	 */
28995 	if (device_specific & WRITE_PROTECT) {
28996 		rval = TRUE;
28997 	}
28998 
28999 err_exit:
29000 	kmem_free(sense, hdrlen);
29001 	return (rval);
29002 }
29003 
29004 /*
29005  *    Function: sr_volume_ctrl()
29006  *
29007  * Description: This routine is the driver entry point for handling CD-ROM
29008  *		audio output volume ioctl requests. (CDROMVOLCTRL)
29009  *
29010  *   Arguments: dev	- the device 'dev_t'
29011  *		data	- pointer to user audio volume control structure
29012  *		flag	- this argument is a pass through to ddi_copyxxx()
29013  *			  directly from the mode argument of ioctl().
29014  *
29015  * Return Code: the code returned by sd_send_scsi_cmd()
29016  *		EFAULT if ddi_copyxxx() fails
29017  *		ENXIO if fail ddi_get_soft_state
29018  *		EINVAL if data pointer is NULL
29019  *
29020  */
29021 
29022 static int
29023 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
29024 {
29025 	struct sd_lun		*un;
29026 	struct cdrom_volctrl    volume;
29027 	struct cdrom_volctrl    *vol = &volume;
29028 	uchar_t			*sense_page;
29029 	uchar_t			*select_page;
29030 	uchar_t			*sense;
29031 	uchar_t			*select;
29032 	int			sense_buflen;
29033 	int			select_buflen;
29034 	int			rval;
29035 	sd_ssc_t		*ssc;
29036 
29037 	if (data == NULL) {
29038 		return (EINVAL);
29039 	}
29040 
29041 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29042 	    (un->un_state == SD_STATE_OFFLINE)) {
29043 		return (ENXIO);
29044 	}
29045 
29046 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
29047 		return (EFAULT);
29048 	}
29049 
29050 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29051 		struct mode_header_grp2		*sense_mhp;
29052 		struct mode_header_grp2		*select_mhp;
29053 		int				bd_len;
29054 
29055 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29056 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
29057 		    MODEPAGE_AUDIO_CTRL_LEN;
29058 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29059 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29060 		ssc = sd_ssc_init(un);
29061 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
29062 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29063 		    SD_PATH_STANDARD);
29064 		sd_ssc_fini(ssc);
29065 
29066 		if (rval != 0) {
29067 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29068 			    "sr_volume_ctrl: Mode Sense Failed\n");
29069 			kmem_free(sense, sense_buflen);
29070 			kmem_free(select, select_buflen);
29071 			return (rval);
29072 		}
29073 		sense_mhp = (struct mode_header_grp2 *)sense;
29074 		select_mhp = (struct mode_header_grp2 *)select;
29075 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
29076 		    sense_mhp->bdesc_length_lo;
29077 		if (bd_len > MODE_BLK_DESC_LENGTH) {
29078 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29079 			    "sr_volume_ctrl: Mode Sense returned invalid "
29080 			    "block descriptor length\n");
29081 			kmem_free(sense, sense_buflen);
29082 			kmem_free(select, select_buflen);
29083 			return (EIO);
29084 		}
29085 		sense_page = (uchar_t *)
29086 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29087 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29088 		select_mhp->length_msb = 0;
29089 		select_mhp->length_lsb = 0;
29090 		select_mhp->bdesc_length_hi = 0;
29091 		select_mhp->bdesc_length_lo = 0;
29092 	} else {
29093 		struct mode_header		*sense_mhp, *select_mhp;
29094 
29095 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29096 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29097 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29098 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29099 		ssc = sd_ssc_init(un);
29100 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
29101 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29102 		    SD_PATH_STANDARD);
29103 		sd_ssc_fini(ssc);
29104 
29105 		if (rval != 0) {
29106 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29107 			    "sr_volume_ctrl: Mode Sense Failed\n");
29108 			kmem_free(sense, sense_buflen);
29109 			kmem_free(select, select_buflen);
29110 			return (rval);
29111 		}
29112 		sense_mhp  = (struct mode_header *)sense;
29113 		select_mhp = (struct mode_header *)select;
29114 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29115 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29116 			    "sr_volume_ctrl: Mode Sense returned invalid "
29117 			    "block descriptor length\n");
29118 			kmem_free(sense, sense_buflen);
29119 			kmem_free(select, select_buflen);
29120 			return (EIO);
29121 		}
29122 		sense_page = (uchar_t *)
29123 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29124 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29125 		select_mhp->length = 0;
29126 		select_mhp->bdesc_length = 0;
29127 	}
29128 	/*
29129 	 * Note: An audio control data structure could be created and overlayed
29130 	 * on the following in place of the array indexing method implemented.
29131 	 */
29132 
29133 	/* Build the select data for the user volume data */
29134 	select_page[0] = MODEPAGE_AUDIO_CTRL;
29135 	select_page[1] = 0xE;
29136 	/* Set the immediate bit */
29137 	select_page[2] = 0x04;
29138 	/* Zero out reserved fields */
29139 	select_page[3] = 0x00;
29140 	select_page[4] = 0x00;
29141 	/* Return sense data for fields not to be modified */
29142 	select_page[5] = sense_page[5];
29143 	select_page[6] = sense_page[6];
29144 	select_page[7] = sense_page[7];
29145 	/* Set the user specified volume levels for channel 0 and 1 */
29146 	select_page[8] = 0x01;
29147 	select_page[9] = vol->channel0;
29148 	select_page[10] = 0x02;
29149 	select_page[11] = vol->channel1;
29150 	/* Channel 2 and 3 are currently unsupported so return the sense data */
29151 	select_page[12] = sense_page[12];
29152 	select_page[13] = sense_page[13];
29153 	select_page[14] = sense_page[14];
29154 	select_page[15] = sense_page[15];
29155 
29156 	ssc = sd_ssc_init(un);
29157 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29158 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
29159 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29160 	} else {
29161 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
29162 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29163 	}
29164 	sd_ssc_fini(ssc);
29165 
29166 	kmem_free(sense, sense_buflen);
29167 	kmem_free(select, select_buflen);
29168 	return (rval);
29169 }
29170 
29171 
29172 /*
29173  *    Function: sr_read_sony_session_offset()
29174  *
29175  * Description: This routine is the driver entry point for handling CD-ROM
29176  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29177  *		The address of the first track in the last session of a
29178  *		multi-session CD-ROM is returned
29179  *
29180  *		Note: This routine uses a vendor specific key value in the
29181  *		command control field without implementing any vendor check here
29182  *		or in the ioctl routine.
29183  *
29184  *   Arguments: dev	- the device 'dev_t'
29185  *		data	- pointer to an int to hold the requested address
29186  *		flag	- this argument is a pass through to ddi_copyxxx()
29187  *			  directly from the mode argument of ioctl().
29188  *
29189  * Return Code: the code returned by sd_send_scsi_cmd()
29190  *		EFAULT if ddi_copyxxx() fails
29191  *		ENXIO if fail ddi_get_soft_state
29192  *		EINVAL if data pointer is NULL
29193  */
29194 
29195 static int
29196 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29197 {
29198 	struct sd_lun		*un;
29199 	struct uscsi_cmd	*com;
29200 	caddr_t			buffer;
29201 	char			cdb[CDB_GROUP1];
29202 	int			session_offset = 0;
29203 	int			rval;
29204 
29205 	if (data == NULL) {
29206 		return (EINVAL);
29207 	}
29208 
29209 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29210 	    (un->un_state == SD_STATE_OFFLINE)) {
29211 		return (ENXIO);
29212 	}
29213 
29214 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29215 	bzero(cdb, CDB_GROUP1);
29216 	cdb[0] = SCMD_READ_TOC;
29217 	/*
29218 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29219 	 * (4 byte TOC response header + 8 byte response data)
29220 	 */
29221 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29222 	/* Byte 9 is the control byte. A vendor specific value is used */
29223 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29224 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29225 	com->uscsi_cdb = cdb;
29226 	com->uscsi_cdblen = CDB_GROUP1;
29227 	com->uscsi_bufaddr = buffer;
29228 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29229 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29230 
29231 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29232 	    SD_PATH_STANDARD);
29233 	if (rval != 0) {
29234 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29235 		kmem_free(com, sizeof (*com));
29236 		return (rval);
29237 	}
29238 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29239 		session_offset =
29240 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29241 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29242 		/*
29243 		 * Offset returned offset in current lbasize block's. Convert to
29244 		 * 2k block's to return to the user
29245 		 */
29246 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29247 			session_offset >>= 2;
29248 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29249 			session_offset >>= 1;
29250 		}
29251 	}
29252 
29253 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29254 		rval = EFAULT;
29255 	}
29256 
29257 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29258 	kmem_free(com, sizeof (*com));
29259 	return (rval);
29260 }
29261 
29262 
29263 /*
29264  *    Function: sd_wm_cache_constructor()
29265  *
29266  * Description: Cache Constructor for the wmap cache for the read/modify/write
29267  * 		devices.
29268  *
29269  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29270  *		un	- sd_lun structure for the device.
29271  *		flag	- the km flags passed to constructor
29272  *
29273  * Return Code: 0 on success.
29274  *		-1 on failure.
29275  */
29276 
29277 /*ARGSUSED*/
29278 static int
29279 sd_wm_cache_constructor(void *wm, void *un, int flags)
29280 {
29281 	bzero(wm, sizeof (struct sd_w_map));
29282 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29283 	return (0);
29284 }
29285 
29286 
29287 /*
29288  *    Function: sd_wm_cache_destructor()
29289  *
29290  * Description: Cache destructor for the wmap cache for the read/modify/write
29291  * 		devices.
29292  *
29293  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29294  *		un	- sd_lun structure for the device.
29295  */
29296 /*ARGSUSED*/
29297 static void
29298 sd_wm_cache_destructor(void *wm, void *un)
29299 {
29300 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29301 }
29302 
29303 
29304 /*
29305  *    Function: sd_range_lock()
29306  *
29307  * Description: Lock the range of blocks specified as parameter to ensure
29308  *		that read, modify write is atomic and no other i/o writes
29309  *		to the same location. The range is specified in terms
29310  *		of start and end blocks. Block numbers are the actual
29311  *		media block numbers and not system.
29312  *
29313  *   Arguments: un	- sd_lun structure for the device.
29314  *		startb - The starting block number
29315  *		endb - The end block number
29316  *		typ - type of i/o - simple/read_modify_write
29317  *
29318  * Return Code: wm  - pointer to the wmap structure.
29319  *
29320  *     Context: This routine can sleep.
29321  */
29322 
29323 static struct sd_w_map *
29324 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29325 {
29326 	struct sd_w_map *wmp = NULL;
29327 	struct sd_w_map *sl_wmp = NULL;
29328 	struct sd_w_map *tmp_wmp;
29329 	wm_state state = SD_WM_CHK_LIST;
29330 
29331 
29332 	ASSERT(un != NULL);
29333 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29334 
29335 	mutex_enter(SD_MUTEX(un));
29336 
29337 	while (state != SD_WM_DONE) {
29338 
29339 		switch (state) {
29340 		case SD_WM_CHK_LIST:
29341 			/*
29342 			 * This is the starting state. Check the wmap list
29343 			 * to see if the range is currently available.
29344 			 */
29345 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29346 				/*
29347 				 * If this is a simple write and no rmw
29348 				 * i/o is pending then try to lock the
29349 				 * range as the range should be available.
29350 				 */
29351 				state = SD_WM_LOCK_RANGE;
29352 			} else {
29353 				tmp_wmp = sd_get_range(un, startb, endb);
29354 				if (tmp_wmp != NULL) {
29355 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29356 						/*
29357 						 * Should not keep onlist wmps
29358 						 * while waiting this macro
29359 						 * will also do wmp = NULL;
29360 						 */
29361 						FREE_ONLIST_WMAP(un, wmp);
29362 					}
29363 					/*
29364 					 * sl_wmp is the wmap on which wait
29365 					 * is done, since the tmp_wmp points
29366 					 * to the inuse wmap, set sl_wmp to
29367 					 * tmp_wmp and change the state to sleep
29368 					 */
29369 					sl_wmp = tmp_wmp;
29370 					state = SD_WM_WAIT_MAP;
29371 				} else {
29372 					state = SD_WM_LOCK_RANGE;
29373 				}
29374 
29375 			}
29376 			break;
29377 
29378 		case SD_WM_LOCK_RANGE:
29379 			ASSERT(un->un_wm_cache);
29380 			/*
29381 			 * The range need to be locked, try to get a wmap.
29382 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29383 			 * if possible as we will have to release the sd mutex
29384 			 * if we have to sleep.
29385 			 */
29386 			if (wmp == NULL)
29387 				wmp = kmem_cache_alloc(un->un_wm_cache,
29388 				    KM_NOSLEEP);
29389 			if (wmp == NULL) {
29390 				mutex_exit(SD_MUTEX(un));
29391 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29392 				    (sd_lun::un_wm_cache))
29393 				wmp = kmem_cache_alloc(un->un_wm_cache,
29394 				    KM_SLEEP);
29395 				mutex_enter(SD_MUTEX(un));
29396 				/*
29397 				 * we released the mutex so recheck and go to
29398 				 * check list state.
29399 				 */
29400 				state = SD_WM_CHK_LIST;
29401 			} else {
29402 				/*
29403 				 * We exit out of state machine since we
29404 				 * have the wmap. Do the housekeeping first.
29405 				 * place the wmap on the wmap list if it is not
29406 				 * on it already and then set the state to done.
29407 				 */
29408 				wmp->wm_start = startb;
29409 				wmp->wm_end = endb;
29410 				wmp->wm_flags = typ | SD_WM_BUSY;
29411 				if (typ & SD_WTYPE_RMW) {
29412 					un->un_rmw_count++;
29413 				}
29414 				/*
29415 				 * If not already on the list then link
29416 				 */
29417 				if (!ONLIST(un, wmp)) {
29418 					wmp->wm_next = un->un_wm;
29419 					wmp->wm_prev = NULL;
29420 					if (wmp->wm_next)
29421 						wmp->wm_next->wm_prev = wmp;
29422 					un->un_wm = wmp;
29423 				}
29424 				state = SD_WM_DONE;
29425 			}
29426 			break;
29427 
29428 		case SD_WM_WAIT_MAP:
29429 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29430 			/*
29431 			 * Wait is done on sl_wmp, which is set in the
29432 			 * check_list state.
29433 			 */
29434 			sl_wmp->wm_wanted_count++;
29435 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29436 			sl_wmp->wm_wanted_count--;
29437 			/*
29438 			 * We can reuse the memory from the completed sl_wmp
29439 			 * lock range for our new lock, but only if noone is
29440 			 * waiting for it.
29441 			 */
29442 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29443 			if (sl_wmp->wm_wanted_count == 0) {
29444 				if (wmp != NULL)
29445 					CHK_N_FREEWMP(un, wmp);
29446 				wmp = sl_wmp;
29447 			}
29448 			sl_wmp = NULL;
29449 			/*
29450 			 * After waking up, need to recheck for availability of
29451 			 * range.
29452 			 */
29453 			state = SD_WM_CHK_LIST;
29454 			break;
29455 
29456 		default:
29457 			panic("sd_range_lock: "
29458 			    "Unknown state %d in sd_range_lock", state);
29459 			/*NOTREACHED*/
29460 		} /* switch(state) */
29461 
29462 	} /* while(state != SD_WM_DONE) */
29463 
29464 	mutex_exit(SD_MUTEX(un));
29465 
29466 	ASSERT(wmp != NULL);
29467 
29468 	return (wmp);
29469 }
29470 
29471 
29472 /*
29473  *    Function: sd_get_range()
29474  *
29475  * Description: Find if there any overlapping I/O to this one
29476  *		Returns the write-map of 1st such I/O, NULL otherwise.
29477  *
29478  *   Arguments: un	- sd_lun structure for the device.
29479  *		startb - The starting block number
29480  *		endb - The end block number
29481  *
29482  * Return Code: wm  - pointer to the wmap structure.
29483  */
29484 
29485 static struct sd_w_map *
29486 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29487 {
29488 	struct sd_w_map *wmp;
29489 
29490 	ASSERT(un != NULL);
29491 
29492 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29493 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
29494 			continue;
29495 		}
29496 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29497 			break;
29498 		}
29499 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29500 			break;
29501 		}
29502 	}
29503 
29504 	return (wmp);
29505 }
29506 
29507 
29508 /*
29509  *    Function: sd_free_inlist_wmap()
29510  *
29511  * Description: Unlink and free a write map struct.
29512  *
29513  *   Arguments: un      - sd_lun structure for the device.
29514  *		wmp	- sd_w_map which needs to be unlinked.
29515  */
29516 
29517 static void
29518 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29519 {
29520 	ASSERT(un != NULL);
29521 
29522 	if (un->un_wm == wmp) {
29523 		un->un_wm = wmp->wm_next;
29524 	} else {
29525 		wmp->wm_prev->wm_next = wmp->wm_next;
29526 	}
29527 
29528 	if (wmp->wm_next) {
29529 		wmp->wm_next->wm_prev = wmp->wm_prev;
29530 	}
29531 
29532 	wmp->wm_next = wmp->wm_prev = NULL;
29533 
29534 	kmem_cache_free(un->un_wm_cache, wmp);
29535 }
29536 
29537 
29538 /*
29539  *    Function: sd_range_unlock()
29540  *
29541  * Description: Unlock the range locked by wm.
29542  *		Free write map if nobody else is waiting on it.
29543  *
29544  *   Arguments: un      - sd_lun structure for the device.
29545  *              wmp     - sd_w_map which needs to be unlinked.
29546  */
29547 
29548 static void
29549 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29550 {
29551 	ASSERT(un != NULL);
29552 	ASSERT(wm != NULL);
29553 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29554 
29555 	mutex_enter(SD_MUTEX(un));
29556 
29557 	if (wm->wm_flags & SD_WTYPE_RMW) {
29558 		un->un_rmw_count--;
29559 	}
29560 
29561 	if (wm->wm_wanted_count) {
29562 		wm->wm_flags = 0;
29563 		/*
29564 		 * Broadcast that the wmap is available now.
29565 		 */
29566 		cv_broadcast(&wm->wm_avail);
29567 	} else {
29568 		/*
29569 		 * If no one is waiting on the map, it should be free'ed.
29570 		 */
29571 		sd_free_inlist_wmap(un, wm);
29572 	}
29573 
29574 	mutex_exit(SD_MUTEX(un));
29575 }
29576 
29577 
29578 /*
29579  *    Function: sd_read_modify_write_task
29580  *
29581  * Description: Called from a taskq thread to initiate the write phase of
29582  *		a read-modify-write request.  This is used for targets where
29583  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29584  *
29585  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29586  *
29587  *     Context: Called under taskq thread context.
29588  */
29589 
29590 static void
29591 sd_read_modify_write_task(void *arg)
29592 {
29593 	struct sd_mapblocksize_info	*bsp;
29594 	struct buf	*bp;
29595 	struct sd_xbuf	*xp;
29596 	struct sd_lun	*un;
29597 
29598 	bp = arg;	/* The bp is given in arg */
29599 	ASSERT(bp != NULL);
29600 
29601 	/* Get the pointer to the layer-private data struct */
29602 	xp = SD_GET_XBUF(bp);
29603 	ASSERT(xp != NULL);
29604 	bsp = xp->xb_private;
29605 	ASSERT(bsp != NULL);
29606 
29607 	un = SD_GET_UN(bp);
29608 	ASSERT(un != NULL);
29609 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29610 
29611 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29612 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29613 
29614 	/*
29615 	 * This is the write phase of a read-modify-write request, called
29616 	 * under the context of a taskq thread in response to the completion
29617 	 * of the read portion of the rmw request completing under interrupt
29618 	 * context. The write request must be sent from here down the iostart
29619 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29620 	 * we use the layer index saved in the layer-private data area.
29621 	 */
29622 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29623 
29624 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29625 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29626 }
29627 
29628 
29629 /*
29630  *    Function: sddump_do_read_of_rmw()
29631  *
29632  * Description: This routine will be called from sddump, If sddump is called
29633  *		with an I/O which not aligned on device blocksize boundary
29634  *		then the write has to be converted to read-modify-write.
29635  *		Do the read part here in order to keep sddump simple.
29636  *		Note - That the sd_mutex is held across the call to this
29637  *		routine.
29638  *
29639  *   Arguments: un	- sd_lun
29640  *		blkno	- block number in terms of media block size.
29641  *		nblk	- number of blocks.
29642  *		bpp	- pointer to pointer to the buf structure. On return
29643  *			from this function, *bpp points to the valid buffer
29644  *			to which the write has to be done.
29645  *
29646  * Return Code: 0 for success or errno-type return code
29647  */
29648 
29649 static int
29650 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29651 	struct buf **bpp)
29652 {
29653 	int err;
29654 	int i;
29655 	int rval;
29656 	struct buf *bp;
29657 	struct scsi_pkt *pkt = NULL;
29658 	uint32_t target_blocksize;
29659 
29660 	ASSERT(un != NULL);
29661 	ASSERT(mutex_owned(SD_MUTEX(un)));
29662 
29663 	target_blocksize = un->un_tgt_blocksize;
29664 
29665 	mutex_exit(SD_MUTEX(un));
29666 
29667 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29668 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29669 	if (bp == NULL) {
29670 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29671 		    "no resources for dumping; giving up");
29672 		err = ENOMEM;
29673 		goto done;
29674 	}
29675 
29676 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29677 	    blkno, nblk);
29678 	if (rval != 0) {
29679 		scsi_free_consistent_buf(bp);
29680 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29681 		    "no resources for dumping; giving up");
29682 		err = ENOMEM;
29683 		goto done;
29684 	}
29685 
29686 	pkt->pkt_flags |= FLAG_NOINTR;
29687 
29688 	err = EIO;
29689 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29690 
29691 		/*
29692 		 * Scsi_poll returns 0 (success) if the command completes and
29693 		 * the status block is STATUS_GOOD.  We should only check
29694 		 * errors if this condition is not true.  Even then we should
29695 		 * send our own request sense packet only if we have a check
29696 		 * condition and auto request sense has not been performed by
29697 		 * the hba.
29698 		 */
29699 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29700 
29701 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29702 			err = 0;
29703 			break;
29704 		}
29705 
29706 		/*
29707 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
29708 		 * no need to read RQS data.
29709 		 */
29710 		if (pkt->pkt_reason == CMD_DEV_GONE) {
29711 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29712 			    "Error while dumping state with rmw..."
29713 			    "Device is gone\n");
29714 			break;
29715 		}
29716 
29717 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29718 			SD_INFO(SD_LOG_DUMP, un,
29719 			    "sddump: read failed with CHECK, try # %d\n", i);
29720 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29721 				(void) sd_send_polled_RQS(un);
29722 			}
29723 
29724 			continue;
29725 		}
29726 
29727 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29728 			int reset_retval = 0;
29729 
29730 			SD_INFO(SD_LOG_DUMP, un,
29731 			    "sddump: read failed with BUSY, try # %d\n", i);
29732 
29733 			if (un->un_f_lun_reset_enabled == TRUE) {
29734 				reset_retval = scsi_reset(SD_ADDRESS(un),
29735 				    RESET_LUN);
29736 			}
29737 			if (reset_retval == 0) {
29738 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29739 			}
29740 			(void) sd_send_polled_RQS(un);
29741 
29742 		} else {
29743 			SD_INFO(SD_LOG_DUMP, un,
29744 			    "sddump: read failed with 0x%x, try # %d\n",
29745 			    SD_GET_PKT_STATUS(pkt), i);
29746 			mutex_enter(SD_MUTEX(un));
29747 			sd_reset_target(un, pkt);
29748 			mutex_exit(SD_MUTEX(un));
29749 		}
29750 
29751 		/*
29752 		 * If we are not getting anywhere with lun/target resets,
29753 		 * let's reset the bus.
29754 		 */
29755 		if (i > SD_NDUMP_RETRIES/2) {
29756 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29757 			(void) sd_send_polled_RQS(un);
29758 		}
29759 
29760 	}
29761 	scsi_destroy_pkt(pkt);
29762 
29763 	if (err != 0) {
29764 		scsi_free_consistent_buf(bp);
29765 		*bpp = NULL;
29766 	} else {
29767 		*bpp = bp;
29768 	}
29769 
29770 done:
29771 	mutex_enter(SD_MUTEX(un));
29772 	return (err);
29773 }
29774 
29775 
29776 /*
29777  *    Function: sd_failfast_flushq
29778  *
29779  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29780  *		in b_flags and move them onto the failfast queue, then kick
29781  *		off a thread to return all bp's on the failfast queue to
29782  *		their owners with an error set.
29783  *
29784  *   Arguments: un - pointer to the soft state struct for the instance.
29785  *
29786  *     Context: may execute in interrupt context.
29787  */
29788 
29789 static void
29790 sd_failfast_flushq(struct sd_lun *un)
29791 {
29792 	struct buf *bp;
29793 	struct buf *next_waitq_bp;
29794 	struct buf *prev_waitq_bp = NULL;
29795 
29796 	ASSERT(un != NULL);
29797 	ASSERT(mutex_owned(SD_MUTEX(un)));
29798 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29799 	ASSERT(un->un_failfast_bp == NULL);
29800 
29801 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29802 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
29803 
29804 	/*
29805 	 * Check if we should flush all bufs when entering failfast state, or
29806 	 * just those with B_FAILFAST set.
29807 	 */
29808 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29809 		/*
29810 		 * Move *all* bp's on the wait queue to the failfast flush
29811 		 * queue, including those that do NOT have B_FAILFAST set.
29812 		 */
29813 		if (un->un_failfast_headp == NULL) {
29814 			ASSERT(un->un_failfast_tailp == NULL);
29815 			un->un_failfast_headp = un->un_waitq_headp;
29816 		} else {
29817 			ASSERT(un->un_failfast_tailp != NULL);
29818 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29819 		}
29820 
29821 		un->un_failfast_tailp = un->un_waitq_tailp;
29822 
29823 		/* update kstat for each bp moved out of the waitq */
29824 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29825 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29826 		}
29827 
29828 		/* empty the waitq */
29829 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
29830 
29831 	} else {
29832 		/*
29833 		 * Go thru the wait queue, pick off all entries with
29834 		 * B_FAILFAST set, and move these onto the failfast queue.
29835 		 */
29836 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29837 			/*
29838 			 * Save the pointer to the next bp on the wait queue,
29839 			 * so we get to it on the next iteration of this loop.
29840 			 */
29841 			next_waitq_bp = bp->av_forw;
29842 
29843 			/*
29844 			 * If this bp from the wait queue does NOT have
29845 			 * B_FAILFAST set, just move on to the next element
29846 			 * in the wait queue. Note, this is the only place
29847 			 * where it is correct to set prev_waitq_bp.
29848 			 */
29849 			if ((bp->b_flags & B_FAILFAST) == 0) {
29850 				prev_waitq_bp = bp;
29851 				continue;
29852 			}
29853 
29854 			/*
29855 			 * Remove the bp from the wait queue.
29856 			 */
29857 			if (bp == un->un_waitq_headp) {
29858 				/* The bp is the first element of the waitq. */
29859 				un->un_waitq_headp = next_waitq_bp;
29860 				if (un->un_waitq_headp == NULL) {
29861 					/* The wait queue is now empty */
29862 					un->un_waitq_tailp = NULL;
29863 				}
29864 			} else {
29865 				/*
29866 				 * The bp is either somewhere in the middle
29867 				 * or at the end of the wait queue.
29868 				 */
29869 				ASSERT(un->un_waitq_headp != NULL);
29870 				ASSERT(prev_waitq_bp != NULL);
29871 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29872 				    == 0);
29873 				if (bp == un->un_waitq_tailp) {
29874 					/* bp is the last entry on the waitq. */
29875 					ASSERT(next_waitq_bp == NULL);
29876 					un->un_waitq_tailp = prev_waitq_bp;
29877 				}
29878 				prev_waitq_bp->av_forw = next_waitq_bp;
29879 			}
29880 			bp->av_forw = NULL;
29881 
29882 			/*
29883 			 * update kstat since the bp is moved out of
29884 			 * the waitq
29885 			 */
29886 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29887 
29888 			/*
29889 			 * Now put the bp onto the failfast queue.
29890 			 */
29891 			if (un->un_failfast_headp == NULL) {
29892 				/* failfast queue is currently empty */
29893 				ASSERT(un->un_failfast_tailp == NULL);
29894 				un->un_failfast_headp =
29895 				    un->un_failfast_tailp = bp;
29896 			} else {
29897 				/* Add the bp to the end of the failfast q */
29898 				ASSERT(un->un_failfast_tailp != NULL);
29899 				ASSERT(un->un_failfast_tailp->b_flags &
29900 				    B_FAILFAST);
29901 				un->un_failfast_tailp->av_forw = bp;
29902 				un->un_failfast_tailp = bp;
29903 			}
29904 		}
29905 	}
29906 
29907 	/*
29908 	 * Now return all bp's on the failfast queue to their owners.
29909 	 */
29910 	while ((bp = un->un_failfast_headp) != NULL) {
29911 
29912 		un->un_failfast_headp = bp->av_forw;
29913 		if (un->un_failfast_headp == NULL) {
29914 			un->un_failfast_tailp = NULL;
29915 		}
29916 
29917 		/*
29918 		 * We want to return the bp with a failure error code, but
29919 		 * we do not want a call to sd_start_cmds() to occur here,
29920 		 * so use sd_return_failed_command_no_restart() instead of
29921 		 * sd_return_failed_command().
29922 		 */
29923 		sd_return_failed_command_no_restart(un, bp, EIO);
29924 	}
29925 
29926 	/* Flush the xbuf queues if required. */
29927 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29928 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29929 	}
29930 
29931 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29932 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
29933 }
29934 
29935 
29936 /*
29937  *    Function: sd_failfast_flushq_callback
29938  *
29939  * Description: Return TRUE if the given bp meets the criteria for failfast
29940  *		flushing. Used with ddi_xbuf_flushq(9F).
29941  *
29942  *   Arguments: bp - ptr to buf struct to be examined.
29943  *
29944  *     Context: Any
29945  */
29946 
29947 static int
29948 sd_failfast_flushq_callback(struct buf *bp)
29949 {
29950 	/*
29951 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
29952 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
29953 	 */
29954 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29955 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29956 }
29957 
29958 
29959 
29960 /*
29961  * Function: sd_setup_next_xfer
29962  *
29963  * Description: Prepare next I/O operation using DMA_PARTIAL
29964  *
29965  */
29966 
29967 static int
29968 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29969     struct scsi_pkt *pkt, struct sd_xbuf *xp)
29970 {
29971 	ssize_t	num_blks_not_xfered;
29972 	daddr_t	strt_blk_num;
29973 	ssize_t	bytes_not_xfered;
29974 	int	rval;
29975 
29976 	ASSERT(pkt->pkt_resid == 0);
29977 
29978 	/*
29979 	 * Calculate next block number and amount to be transferred.
29980 	 *
29981 	 * How much data NOT transfered to the HBA yet.
29982 	 */
29983 	bytes_not_xfered = xp->xb_dma_resid;
29984 
29985 	/*
29986 	 * figure how many blocks NOT transfered to the HBA yet.
29987 	 */
29988 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29989 
29990 	/*
29991 	 * set starting block number to the end of what WAS transfered.
29992 	 */
29993 	strt_blk_num = xp->xb_blkno +
29994 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29995 
29996 	/*
29997 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
29998 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
29999 	 * the disk mutex here.
30000 	 */
30001 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
30002 	    strt_blk_num, num_blks_not_xfered);
30003 
30004 	if (rval == 0) {
30005 
30006 		/*
30007 		 * Success.
30008 		 *
30009 		 * Adjust things if there are still more blocks to be
30010 		 * transfered.
30011 		 */
30012 		xp->xb_dma_resid = pkt->pkt_resid;
30013 		pkt->pkt_resid = 0;
30014 
30015 		return (1);
30016 	}
30017 
30018 	/*
30019 	 * There's really only one possible return value from
30020 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
30021 	 * returns NULL.
30022 	 */
30023 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
30024 
30025 	bp->b_resid = bp->b_bcount;
30026 	bp->b_flags |= B_ERROR;
30027 
30028 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30029 	    "Error setting up next portion of DMA transfer\n");
30030 
30031 	return (0);
30032 }
30033 
30034 /*
30035  *    Function: sd_panic_for_res_conflict
30036  *
30037  * Description: Call panic with a string formatted with "Reservation Conflict"
30038  *		and a human readable identifier indicating the SD instance
30039  *		that experienced the reservation conflict.
30040  *
30041  *   Arguments: un - pointer to the soft state struct for the instance.
30042  *
30043  *     Context: may execute in interrupt context.
30044  */
30045 
30046 #define	SD_RESV_CONFLICT_FMT_LEN 40
30047 void
30048 sd_panic_for_res_conflict(struct sd_lun *un)
30049 {
30050 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30051 	char path_str[MAXPATHLEN];
30052 
30053 	(void) snprintf(panic_str, sizeof (panic_str),
30054 	    "Reservation Conflict\nDisk: %s",
30055 	    ddi_pathname(SD_DEVINFO(un), path_str));
30056 
30057 	panic(panic_str);
30058 }
30059 
30060 /*
30061  * Note: The following sd_faultinjection_ioctl( ) routines implement
30062  * driver support for handling fault injection for error analysis
30063  * causing faults in multiple layers of the driver.
30064  *
30065  */
30066 
30067 #ifdef SD_FAULT_INJECTION
30068 static uint_t   sd_fault_injection_on = 0;
30069 
30070 /*
30071  *    Function: sd_faultinjection_ioctl()
30072  *
30073  * Description: This routine is the driver entry point for handling
30074  *              faultinjection ioctls to inject errors into the
30075  *              layer model
30076  *
30077  *   Arguments: cmd	- the ioctl cmd received
30078  *		arg	- the arguments from user and returns
30079  */
30080 
30081 static void
30082 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
30083 
30084 	uint_t i = 0;
30085 	uint_t rval;
30086 
30087 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30088 
30089 	mutex_enter(SD_MUTEX(un));
30090 
30091 	switch (cmd) {
30092 	case SDIOCRUN:
30093 		/* Allow pushed faults to be injected */
30094 		SD_INFO(SD_LOG_SDTEST, un,
30095 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
30096 
30097 		sd_fault_injection_on = 1;
30098 
30099 		SD_INFO(SD_LOG_IOERR, un,
30100 		    "sd_faultinjection_ioctl: run finished\n");
30101 		break;
30102 
30103 	case SDIOCSTART:
30104 		/* Start Injection Session */
30105 		SD_INFO(SD_LOG_SDTEST, un,
30106 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
30107 
30108 		sd_fault_injection_on = 0;
30109 		un->sd_injection_mask = 0xFFFFFFFF;
30110 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30111 			un->sd_fi_fifo_pkt[i] = NULL;
30112 			un->sd_fi_fifo_xb[i] = NULL;
30113 			un->sd_fi_fifo_un[i] = NULL;
30114 			un->sd_fi_fifo_arq[i] = NULL;
30115 		}
30116 		un->sd_fi_fifo_start = 0;
30117 		un->sd_fi_fifo_end = 0;
30118 
30119 		mutex_enter(&(un->un_fi_mutex));
30120 		un->sd_fi_log[0] = '\0';
30121 		un->sd_fi_buf_len = 0;
30122 		mutex_exit(&(un->un_fi_mutex));
30123 
30124 		SD_INFO(SD_LOG_IOERR, un,
30125 		    "sd_faultinjection_ioctl: start finished\n");
30126 		break;
30127 
30128 	case SDIOCSTOP:
30129 		/* Stop Injection Session */
30130 		SD_INFO(SD_LOG_SDTEST, un,
30131 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30132 		sd_fault_injection_on = 0;
30133 		un->sd_injection_mask = 0x0;
30134 
30135 		/* Empty stray or unuseds structs from fifo */
30136 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30137 			if (un->sd_fi_fifo_pkt[i] != NULL) {
30138 				kmem_free(un->sd_fi_fifo_pkt[i],
30139 				    sizeof (struct sd_fi_pkt));
30140 			}
30141 			if (un->sd_fi_fifo_xb[i] != NULL) {
30142 				kmem_free(un->sd_fi_fifo_xb[i],
30143 				    sizeof (struct sd_fi_xb));
30144 			}
30145 			if (un->sd_fi_fifo_un[i] != NULL) {
30146 				kmem_free(un->sd_fi_fifo_un[i],
30147 				    sizeof (struct sd_fi_un));
30148 			}
30149 			if (un->sd_fi_fifo_arq[i] != NULL) {
30150 				kmem_free(un->sd_fi_fifo_arq[i],
30151 				    sizeof (struct sd_fi_arq));
30152 			}
30153 			un->sd_fi_fifo_pkt[i] = NULL;
30154 			un->sd_fi_fifo_un[i] = NULL;
30155 			un->sd_fi_fifo_xb[i] = NULL;
30156 			un->sd_fi_fifo_arq[i] = NULL;
30157 		}
30158 		un->sd_fi_fifo_start = 0;
30159 		un->sd_fi_fifo_end = 0;
30160 
30161 		SD_INFO(SD_LOG_IOERR, un,
30162 		    "sd_faultinjection_ioctl: stop finished\n");
30163 		break;
30164 
30165 	case SDIOCINSERTPKT:
30166 		/* Store a packet struct to be pushed onto fifo */
30167 		SD_INFO(SD_LOG_SDTEST, un,
30168 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30169 
30170 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30171 
30172 		sd_fault_injection_on = 0;
30173 
30174 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30175 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30176 			kmem_free(un->sd_fi_fifo_pkt[i],
30177 			    sizeof (struct sd_fi_pkt));
30178 		}
30179 		if (arg != NULL) {
30180 			un->sd_fi_fifo_pkt[i] =
30181 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30182 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30183 				/* Alloc failed don't store anything */
30184 				break;
30185 			}
30186 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30187 			    sizeof (struct sd_fi_pkt), 0);
30188 			if (rval == -1) {
30189 				kmem_free(un->sd_fi_fifo_pkt[i],
30190 				    sizeof (struct sd_fi_pkt));
30191 				un->sd_fi_fifo_pkt[i] = NULL;
30192 			}
30193 		} else {
30194 			SD_INFO(SD_LOG_IOERR, un,
30195 			    "sd_faultinjection_ioctl: pkt null\n");
30196 		}
30197 		break;
30198 
30199 	case SDIOCINSERTXB:
30200 		/* Store a xb struct to be pushed onto fifo */
30201 		SD_INFO(SD_LOG_SDTEST, un,
30202 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30203 
30204 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30205 
30206 		sd_fault_injection_on = 0;
30207 
30208 		if (un->sd_fi_fifo_xb[i] != NULL) {
30209 			kmem_free(un->sd_fi_fifo_xb[i],
30210 			    sizeof (struct sd_fi_xb));
30211 			un->sd_fi_fifo_xb[i] = NULL;
30212 		}
30213 		if (arg != NULL) {
30214 			un->sd_fi_fifo_xb[i] =
30215 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30216 			if (un->sd_fi_fifo_xb[i] == NULL) {
30217 				/* Alloc failed don't store anything */
30218 				break;
30219 			}
30220 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30221 			    sizeof (struct sd_fi_xb), 0);
30222 
30223 			if (rval == -1) {
30224 				kmem_free(un->sd_fi_fifo_xb[i],
30225 				    sizeof (struct sd_fi_xb));
30226 				un->sd_fi_fifo_xb[i] = NULL;
30227 			}
30228 		} else {
30229 			SD_INFO(SD_LOG_IOERR, un,
30230 			    "sd_faultinjection_ioctl: xb null\n");
30231 		}
30232 		break;
30233 
30234 	case SDIOCINSERTUN:
30235 		/* Store a un struct to be pushed onto fifo */
30236 		SD_INFO(SD_LOG_SDTEST, un,
30237 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30238 
30239 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30240 
30241 		sd_fault_injection_on = 0;
30242 
30243 		if (un->sd_fi_fifo_un[i] != NULL) {
30244 			kmem_free(un->sd_fi_fifo_un[i],
30245 			    sizeof (struct sd_fi_un));
30246 			un->sd_fi_fifo_un[i] = NULL;
30247 		}
30248 		if (arg != NULL) {
30249 			un->sd_fi_fifo_un[i] =
30250 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30251 			if (un->sd_fi_fifo_un[i] == NULL) {
30252 				/* Alloc failed don't store anything */
30253 				break;
30254 			}
30255 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30256 			    sizeof (struct sd_fi_un), 0);
30257 			if (rval == -1) {
30258 				kmem_free(un->sd_fi_fifo_un[i],
30259 				    sizeof (struct sd_fi_un));
30260 				un->sd_fi_fifo_un[i] = NULL;
30261 			}
30262 
30263 		} else {
30264 			SD_INFO(SD_LOG_IOERR, un,
30265 			    "sd_faultinjection_ioctl: un null\n");
30266 		}
30267 
30268 		break;
30269 
30270 	case SDIOCINSERTARQ:
30271 		/* Store a arq struct to be pushed onto fifo */
30272 		SD_INFO(SD_LOG_SDTEST, un,
30273 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30274 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30275 
30276 		sd_fault_injection_on = 0;
30277 
30278 		if (un->sd_fi_fifo_arq[i] != NULL) {
30279 			kmem_free(un->sd_fi_fifo_arq[i],
30280 			    sizeof (struct sd_fi_arq));
30281 			un->sd_fi_fifo_arq[i] = NULL;
30282 		}
30283 		if (arg != NULL) {
30284 			un->sd_fi_fifo_arq[i] =
30285 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30286 			if (un->sd_fi_fifo_arq[i] == NULL) {
30287 				/* Alloc failed don't store anything */
30288 				break;
30289 			}
30290 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30291 			    sizeof (struct sd_fi_arq), 0);
30292 			if (rval == -1) {
30293 				kmem_free(un->sd_fi_fifo_arq[i],
30294 				    sizeof (struct sd_fi_arq));
30295 				un->sd_fi_fifo_arq[i] = NULL;
30296 			}
30297 
30298 		} else {
30299 			SD_INFO(SD_LOG_IOERR, un,
30300 			    "sd_faultinjection_ioctl: arq null\n");
30301 		}
30302 
30303 		break;
30304 
30305 	case SDIOCPUSH:
30306 		/* Push stored xb, pkt, un, and arq onto fifo */
30307 		sd_fault_injection_on = 0;
30308 
30309 		if (arg != NULL) {
30310 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30311 			if (rval != -1 &&
30312 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30313 				un->sd_fi_fifo_end += i;
30314 			}
30315 		} else {
30316 			SD_INFO(SD_LOG_IOERR, un,
30317 			    "sd_faultinjection_ioctl: push arg null\n");
30318 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30319 				un->sd_fi_fifo_end++;
30320 			}
30321 		}
30322 		SD_INFO(SD_LOG_IOERR, un,
30323 		    "sd_faultinjection_ioctl: push to end=%d\n",
30324 		    un->sd_fi_fifo_end);
30325 		break;
30326 
30327 	case SDIOCRETRIEVE:
30328 		/* Return buffer of log from Injection session */
30329 		SD_INFO(SD_LOG_SDTEST, un,
30330 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30331 
30332 		sd_fault_injection_on = 0;
30333 
30334 		mutex_enter(&(un->un_fi_mutex));
30335 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30336 		    un->sd_fi_buf_len+1, 0);
30337 		mutex_exit(&(un->un_fi_mutex));
30338 
30339 		if (rval == -1) {
30340 			/*
30341 			 * arg is possibly invalid setting
30342 			 * it to NULL for return
30343 			 */
30344 			arg = NULL;
30345 		}
30346 		break;
30347 	}
30348 
30349 	mutex_exit(SD_MUTEX(un));
30350 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30351 			    " exit\n");
30352 }
30353 
30354 
30355 /*
30356  *    Function: sd_injection_log()
30357  *
30358  * Description: This routine adds buff to the already existing injection log
30359  *              for retrieval via faultinjection_ioctl for use in fault
30360  *              detection and recovery
30361  *
30362  *   Arguments: buf - the string to add to the log
30363  */
30364 
30365 static void
30366 sd_injection_log(char *buf, struct sd_lun *un)
30367 {
30368 	uint_t len;
30369 
30370 	ASSERT(un != NULL);
30371 	ASSERT(buf != NULL);
30372 
30373 	mutex_enter(&(un->un_fi_mutex));
30374 
30375 	len = min(strlen(buf), 255);
30376 	/* Add logged value to Injection log to be returned later */
30377 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30378 		uint_t	offset = strlen((char *)un->sd_fi_log);
30379 		char *destp = (char *)un->sd_fi_log + offset;
30380 		int i;
30381 		for (i = 0; i < len; i++) {
30382 			*destp++ = *buf++;
30383 		}
30384 		un->sd_fi_buf_len += len;
30385 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30386 	}
30387 
30388 	mutex_exit(&(un->un_fi_mutex));
30389 }
30390 
30391 
30392 /*
30393  *    Function: sd_faultinjection()
30394  *
30395  * Description: This routine takes the pkt and changes its
30396  *		content based on error injection scenerio.
30397  *
30398  *   Arguments: pktp	- packet to be changed
30399  */
30400 
30401 static void
30402 sd_faultinjection(struct scsi_pkt *pktp)
30403 {
30404 	uint_t i;
30405 	struct sd_fi_pkt *fi_pkt;
30406 	struct sd_fi_xb *fi_xb;
30407 	struct sd_fi_un *fi_un;
30408 	struct sd_fi_arq *fi_arq;
30409 	struct buf *bp;
30410 	struct sd_xbuf *xb;
30411 	struct sd_lun *un;
30412 
30413 	ASSERT(pktp != NULL);
30414 
30415 	/* pull bp xb and un from pktp */
30416 	bp = (struct buf *)pktp->pkt_private;
30417 	xb = SD_GET_XBUF(bp);
30418 	un = SD_GET_UN(bp);
30419 
30420 	ASSERT(un != NULL);
30421 
30422 	mutex_enter(SD_MUTEX(un));
30423 
30424 	SD_TRACE(SD_LOG_SDTEST, un,
30425 	    "sd_faultinjection: entry Injection from sdintr\n");
30426 
30427 	/* if injection is off return */
30428 	if (sd_fault_injection_on == 0 ||
30429 	    un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30430 		mutex_exit(SD_MUTEX(un));
30431 		return;
30432 	}
30433 
30434 	SD_INFO(SD_LOG_SDTEST, un,
30435 	    "sd_faultinjection: is working for copying\n");
30436 
30437 	/* take next set off fifo */
30438 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30439 
30440 	fi_pkt = un->sd_fi_fifo_pkt[i];
30441 	fi_xb = un->sd_fi_fifo_xb[i];
30442 	fi_un = un->sd_fi_fifo_un[i];
30443 	fi_arq = un->sd_fi_fifo_arq[i];
30444 
30445 
30446 	/* set variables accordingly */
30447 	/* set pkt if it was on fifo */
30448 	if (fi_pkt != NULL) {
30449 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30450 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30451 		if (fi_pkt->pkt_cdbp != 0xff)
30452 			SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30453 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30454 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30455 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30456 
30457 	}
30458 	/* set xb if it was on fifo */
30459 	if (fi_xb != NULL) {
30460 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30461 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30462 		if (fi_xb->xb_retry_count != 0)
30463 			SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30464 		SD_CONDSET(xb, xb, xb_victim_retry_count,
30465 		    "xb_victim_retry_count");
30466 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30467 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30468 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30469 
30470 		/* copy in block data from sense */
30471 		/*
30472 		 * if (fi_xb->xb_sense_data[0] != -1) {
30473 		 *	bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30474 		 *	SENSE_LENGTH);
30475 		 * }
30476 		 */
30477 		bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30478 
30479 		/* copy in extended sense codes */
30480 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30481 		    xb, es_code, "es_code");
30482 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30483 		    xb, es_key, "es_key");
30484 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30485 		    xb, es_add_code, "es_add_code");
30486 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30487 		    xb, es_qual_code, "es_qual_code");
30488 		struct scsi_extended_sense *esp;
30489 		esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30490 		esp->es_class = CLASS_EXTENDED_SENSE;
30491 	}
30492 
30493 	/* set un if it was on fifo */
30494 	if (fi_un != NULL) {
30495 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30496 		SD_CONDSET(un, un, un_ctype, "un_ctype");
30497 		SD_CONDSET(un, un, un_reset_retry_count,
30498 		    "un_reset_retry_count");
30499 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30500 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30501 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30502 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30503 		    "un_f_allow_bus_device_reset");
30504 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30505 
30506 	}
30507 
30508 	/* copy in auto request sense if it was on fifo */
30509 	if (fi_arq != NULL) {
30510 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30511 	}
30512 
30513 	/* free structs */
30514 	if (un->sd_fi_fifo_pkt[i] != NULL) {
30515 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30516 	}
30517 	if (un->sd_fi_fifo_xb[i] != NULL) {
30518 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30519 	}
30520 	if (un->sd_fi_fifo_un[i] != NULL) {
30521 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30522 	}
30523 	if (un->sd_fi_fifo_arq[i] != NULL) {
30524 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30525 	}
30526 
30527 	/*
30528 	 * kmem_free does not gurantee to set to NULL
30529 	 * since we uses these to determine if we set
30530 	 * values or not lets confirm they are always
30531 	 * NULL after free
30532 	 */
30533 	un->sd_fi_fifo_pkt[i] = NULL;
30534 	un->sd_fi_fifo_un[i] = NULL;
30535 	un->sd_fi_fifo_xb[i] = NULL;
30536 	un->sd_fi_fifo_arq[i] = NULL;
30537 
30538 	un->sd_fi_fifo_start++;
30539 
30540 	mutex_exit(SD_MUTEX(un));
30541 
30542 	SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30543 }
30544 
30545 #endif /* SD_FAULT_INJECTION */
30546 
30547 /*
30548  * This routine is invoked in sd_unit_attach(). Before calling it, the
30549  * properties in conf file should be processed already, and "hotpluggable"
30550  * property was processed also.
30551  *
30552  * The sd driver distinguishes 3 different type of devices: removable media,
30553  * non-removable media, and hotpluggable. Below the differences are defined:
30554  *
30555  * 1. Device ID
30556  *
30557  *     The device ID of a device is used to identify this device. Refer to
30558  *     ddi_devid_register(9F).
30559  *
30560  *     For a non-removable media disk device which can provide 0x80 or 0x83
30561  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30562  *     device ID is created to identify this device. For other non-removable
30563  *     media devices, a default device ID is created only if this device has
30564  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30565  *
30566  *     -------------------------------------------------------
30567  *     removable media   hotpluggable  | Can Have Device ID
30568  *     -------------------------------------------------------
30569  *         false             false     |     Yes
30570  *         false             true      |     Yes
30571  *         true                x       |     No
30572  *     ------------------------------------------------------
30573  *
30574  *
30575  * 2. SCSI group 4 commands
30576  *
30577  *     In SCSI specs, only some commands in group 4 command set can use
30578  *     8-byte addresses that can be used to access >2TB storage spaces.
30579  *     Other commands have no such capability. Without supporting group4,
30580  *     it is impossible to make full use of storage spaces of a disk with
30581  *     capacity larger than 2TB.
30582  *
30583  *     -----------------------------------------------
30584  *     removable media   hotpluggable   LP64  |  Group
30585  *     -----------------------------------------------
30586  *           false          false       false |   1
30587  *           false          false       true  |   4
30588  *           false          true        false |   1
30589  *           false          true        true  |   4
30590  *           true             x           x   |   5
30591  *     -----------------------------------------------
30592  *
30593  *
30594  * 3. Check for VTOC Label
30595  *
30596  *     If a direct-access disk has no EFI label, sd will check if it has a
30597  *     valid VTOC label. Now, sd also does that check for removable media
30598  *     and hotpluggable devices.
30599  *
30600  *     --------------------------------------------------------------
30601  *     Direct-Access   removable media    hotpluggable |  Check Label
30602  *     -------------------------------------------------------------
30603  *         false          false           false        |   No
30604  *         false          false           true         |   No
30605  *         false          true            false        |   Yes
30606  *         false          true            true         |   Yes
30607  *         true            x                x          |   Yes
30608  *     --------------------------------------------------------------
30609  *
30610  *
30611  * 4. Building default VTOC label
30612  *
30613  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30614  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30615  *     create default VTOC for them. Currently sd creates default VTOC label
30616  *     for all devices on x86 platform (VTOC_16), but only for removable
30617  *     media devices on SPARC (VTOC_8).
30618  *
30619  *     -----------------------------------------------------------
30620  *       removable media hotpluggable platform   |   Default Label
30621  *     -----------------------------------------------------------
30622  *             false          false    sparc     |     No
30623  *             false          true      x86      |     Yes
30624  *             false          true     sparc     |     Yes
30625  *             true             x        x       |     Yes
30626  *     ----------------------------------------------------------
30627  *
30628  *
30629  * 5. Supported blocksizes of target devices
30630  *
30631  *     Sd supports non-512-byte blocksize for removable media devices only.
30632  *     For other devices, only 512-byte blocksize is supported. This may be
30633  *     changed in near future because some RAID devices require non-512-byte
30634  *     blocksize
30635  *
30636  *     -----------------------------------------------------------
30637  *     removable media    hotpluggable    | non-512-byte blocksize
30638  *     -----------------------------------------------------------
30639  *           false          false         |   No
30640  *           false          true          |   No
30641  *           true             x           |   Yes
30642  *     -----------------------------------------------------------
30643  *
30644  *
30645  * 6. Automatic mount & unmount
30646  *
30647  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30648  *     if a device is removable media device. It return 1 for removable media
30649  *     devices, and 0 for others.
30650  *
30651  *     The automatic mounting subsystem should distinguish between the types
30652  *     of devices and apply automounting policies to each.
30653  *
30654  *
30655  * 7. fdisk partition management
30656  *
30657  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30658  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
30659  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30660  *     fdisk partitions on both x86 and SPARC platform.
30661  *
30662  *     -----------------------------------------------------------
30663  *       platform   removable media  USB/1394  |  fdisk supported
30664  *     -----------------------------------------------------------
30665  *        x86         X               X        |       true
30666  *     ------------------------------------------------------------
30667  *        sparc       X               X        |       false
30668  *     ------------------------------------------------------------
30669  *
30670  *
30671  * 8. MBOOT/MBR
30672  *
30673  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30674  *     read/write mboot for removable media devices on sparc platform.
30675  *
30676  *     -----------------------------------------------------------
30677  *       platform   removable media  USB/1394  |  mboot supported
30678  *     -----------------------------------------------------------
30679  *        x86         X               X        |       true
30680  *     ------------------------------------------------------------
30681  *        sparc      false           false     |       false
30682  *        sparc      false           true      |       true
30683  *        sparc      true            false     |       true
30684  *        sparc      true            true      |       true
30685  *     ------------------------------------------------------------
30686  *
30687  *
30688  * 9.  error handling during opening device
30689  *
30690  *     If failed to open a disk device, an errno is returned. For some kinds
30691  *     of errors, different errno is returned depending on if this device is
30692  *     a removable media device. This brings USB/1394 hard disks in line with
30693  *     expected hard disk behavior. It is not expected that this breaks any
30694  *     application.
30695  *
30696  *     ------------------------------------------------------
30697  *       removable media    hotpluggable   |  errno
30698  *     ------------------------------------------------------
30699  *             false          false        |   EIO
30700  *             false          true         |   EIO
30701  *             true             x          |   ENXIO
30702  *     ------------------------------------------------------
30703  *
30704  *
30705  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30706  *
30707  *     These IOCTLs are applicable only to removable media devices.
30708  *
30709  *     -----------------------------------------------------------
30710  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30711  *     -----------------------------------------------------------
30712  *             false          false        |     No
30713  *             false          true         |     No
30714  *             true            x           |     Yes
30715  *     -----------------------------------------------------------
30716  *
30717  *
30718  * 12. Kstats for partitions
30719  *
30720  *     sd creates partition kstat for non-removable media devices. USB and
30721  *     Firewire hard disks now have partition kstats
30722  *
30723  *      ------------------------------------------------------
30724  *       removable media    hotpluggable   |   kstat
30725  *      ------------------------------------------------------
30726  *             false          false        |    Yes
30727  *             false          true         |    Yes
30728  *             true             x          |    No
30729  *       ------------------------------------------------------
30730  *
30731  *
30732  * 13. Removable media & hotpluggable properties
30733  *
30734  *     Sd driver creates a "removable-media" property for removable media
30735  *     devices. Parent nexus drivers create a "hotpluggable" property if
30736  *     it supports hotplugging.
30737  *
30738  *     ---------------------------------------------------------------------
30739  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30740  *     ---------------------------------------------------------------------
30741  *       false            false       |    No                   No
30742  *       false            true        |    No                   Yes
30743  *       true             false       |    Yes                  No
30744  *       true             true        |    Yes                  Yes
30745  *     ---------------------------------------------------------------------
30746  *
30747  *
30748  * 14. Power Management
30749  *
30750  *     sd only power manages removable media devices or devices that support
30751  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30752  *
30753  *     A parent nexus that supports hotplugging can also set "pm-capable"
30754  *     if the disk can be power managed.
30755  *
30756  *     ------------------------------------------------------------
30757  *       removable media hotpluggable pm-capable  |   power manage
30758  *     ------------------------------------------------------------
30759  *             false          false     false     |     No
30760  *             false          false     true      |     Yes
30761  *             false          true      false     |     No
30762  *             false          true      true      |     Yes
30763  *             true             x        x        |     Yes
30764  *     ------------------------------------------------------------
30765  *
30766  *      USB and firewire hard disks can now be power managed independently
30767  *      of the framebuffer
30768  *
30769  *
30770  * 15. Support for USB disks with capacity larger than 1TB
30771  *
30772  *     Currently, sd doesn't permit a fixed disk device with capacity
30773  *     larger than 1TB to be used in a 32-bit operating system environment.
30774  *     However, sd doesn't do that for removable media devices. Instead, it
30775  *     assumes that removable media devices cannot have a capacity larger
30776  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30777  *     supported, which can cause some unexpected results.
30778  *
30779  *     ---------------------------------------------------------------------
30780  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30781  *     ---------------------------------------------------------------------
30782  *             false          false  |   true         |     no
30783  *             false          true   |   true         |     no
30784  *             true           false  |   true         |     Yes
30785  *             true           true   |   true         |     Yes
30786  *     ---------------------------------------------------------------------
30787  *
30788  *
30789  * 16. Check write-protection at open time
30790  *
30791  *     When a removable media device is being opened for writing without NDELAY
30792  *     flag, sd will check if this device is writable. If attempting to open
30793  *     without NDELAY flag a write-protected device, this operation will abort.
30794  *
30795  *     ------------------------------------------------------------
30796  *       removable media    USB/1394   |   WP Check
30797  *     ------------------------------------------------------------
30798  *             false          false    |     No
30799  *             false          true     |     No
30800  *             true           false    |     Yes
30801  *             true           true     |     Yes
30802  *     ------------------------------------------------------------
30803  *
30804  *
30805  * 17. syslog when corrupted VTOC is encountered
30806  *
30807  *      Currently, if an invalid VTOC is encountered, sd only print syslog
30808  *      for fixed SCSI disks.
30809  *     ------------------------------------------------------------
30810  *       removable media    USB/1394   |   print syslog
30811  *     ------------------------------------------------------------
30812  *             false          false    |     Yes
30813  *             false          true     |     No
30814  *             true           false    |     No
30815  *             true           true     |     No
30816  *     ------------------------------------------------------------
30817  */
30818 static void
30819 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30820 {
30821 	int	pm_cap;
30822 
30823 	ASSERT(un->un_sd);
30824 	ASSERT(un->un_sd->sd_inq);
30825 
30826 	/*
30827 	 * Enable SYNC CACHE support for all devices.
30828 	 */
30829 	un->un_f_sync_cache_supported = TRUE;
30830 
30831 	/*
30832 	 * Set the sync cache required flag to false.
30833 	 * This would ensure that there is no SYNC CACHE
30834 	 * sent when there are no writes
30835 	 */
30836 	un->un_f_sync_cache_required = FALSE;
30837 
30838 	if (un->un_sd->sd_inq->inq_rmb) {
30839 		/*
30840 		 * The media of this device is removable. And for this kind
30841 		 * of devices, it is possible to change medium after opening
30842 		 * devices. Thus we should support this operation.
30843 		 */
30844 		un->un_f_has_removable_media = TRUE;
30845 
30846 		/*
30847 		 * support non-512-byte blocksize of removable media devices
30848 		 */
30849 		un->un_f_non_devbsize_supported = TRUE;
30850 
30851 		/*
30852 		 * Assume that all removable media devices support DOOR_LOCK
30853 		 */
30854 		un->un_f_doorlock_supported = TRUE;
30855 
30856 		/*
30857 		 * For a removable media device, it is possible to be opened
30858 		 * with NDELAY flag when there is no media in drive, in this
30859 		 * case we don't care if device is writable. But if without
30860 		 * NDELAY flag, we need to check if media is write-protected.
30861 		 */
30862 		un->un_f_chk_wp_open = TRUE;
30863 
30864 		/*
30865 		 * need to start a SCSI watch thread to monitor media state,
30866 		 * when media is being inserted or ejected, notify syseventd.
30867 		 */
30868 		un->un_f_monitor_media_state = TRUE;
30869 
30870 		/*
30871 		 * Some devices don't support START_STOP_UNIT command.
30872 		 * Therefore, we'd better check if a device supports it
30873 		 * before sending it.
30874 		 */
30875 		un->un_f_check_start_stop = TRUE;
30876 
30877 		/*
30878 		 * support eject media ioctl:
30879 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
30880 		 */
30881 		un->un_f_eject_media_supported = TRUE;
30882 
30883 		/*
30884 		 * Because many removable-media devices don't support
30885 		 * LOG_SENSE, we couldn't use this command to check if
30886 		 * a removable media device support power-management.
30887 		 * We assume that they support power-management via
30888 		 * START_STOP_UNIT command and can be spun up and down
30889 		 * without limitations.
30890 		 */
30891 		un->un_f_pm_supported = TRUE;
30892 
30893 		/*
30894 		 * Need to create a zero length (Boolean) property
30895 		 * removable-media for the removable media devices.
30896 		 * Note that the return value of the property is not being
30897 		 * checked, since if unable to create the property
30898 		 * then do not want the attach to fail altogether. Consistent
30899 		 * with other property creation in attach.
30900 		 */
30901 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
30902 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
30903 
30904 	} else {
30905 		/*
30906 		 * create device ID for device
30907 		 */
30908 		un->un_f_devid_supported = TRUE;
30909 
30910 		/*
30911 		 * Spin up non-removable-media devices once it is attached
30912 		 */
30913 		un->un_f_attach_spinup = TRUE;
30914 
30915 		/*
30916 		 * According to SCSI specification, Sense data has two kinds of
30917 		 * format: fixed format, and descriptor format. At present, we
30918 		 * don't support descriptor format sense data for removable
30919 		 * media.
30920 		 */
30921 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
30922 			un->un_f_descr_format_supported = TRUE;
30923 		}
30924 
30925 		/*
30926 		 * kstats are created only for non-removable media devices.
30927 		 *
30928 		 * Set this in sd.conf to 0 in order to disable kstats.  The
30929 		 * default is 1, so they are enabled by default.
30930 		 */
30931 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
30932 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
30933 		    "enable-partition-kstats", 1));
30934 
30935 		/*
30936 		 * Check if HBA has set the "pm-capable" property.
30937 		 * If "pm-capable" exists and is non-zero then we can
30938 		 * power manage the device without checking the start/stop
30939 		 * cycle count log sense page.
30940 		 *
30941 		 * If "pm-capable" exists and is set to be false (0),
30942 		 * then we should not power manage the device.
30943 		 *
30944 		 * If "pm-capable" doesn't exist then pm_cap will
30945 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
30946 		 * sd will check the start/stop cycle count log sense page
30947 		 * and power manage the device if the cycle count limit has
30948 		 * not been exceeded.
30949 		 */
30950 		pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
30951 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
30952 		if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
30953 			un->un_f_log_sense_supported = TRUE;
30954 			if (!un->un_f_power_condition_disabled &&
30955 			    SD_INQUIRY(un)->inq_ansi == 6) {
30956 				un->un_f_power_condition_supported = TRUE;
30957 			}
30958 		} else {
30959 			/*
30960 			 * pm-capable property exists.
30961 			 *
30962 			 * Convert "TRUE" values for pm_cap to
30963 			 * SD_PM_CAPABLE_IS_TRUE to make it easier to check
30964 			 * later. "TRUE" values are any values defined in
30965 			 * inquiry.h.
30966 			 */
30967 			if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
30968 				un->un_f_log_sense_supported = FALSE;
30969 			} else {
30970 				/* SD_PM_CAPABLE_IS_TRUE case */
30971 				un->un_f_pm_supported = TRUE;
30972 				if (!un->un_f_power_condition_disabled &&
30973 				    SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
30974 					un->un_f_power_condition_supported =
30975 					    TRUE;
30976 				}
30977 				if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
30978 					un->un_f_log_sense_supported = TRUE;
30979 					un->un_f_pm_log_sense_smart =
30980 					    SD_PM_CAP_SMART_LOG(pm_cap);
30981 				}
30982 			}
30983 
30984 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
30985 			    "sd_unit_attach: un:0x%p pm-capable "
30986 			    "property set to %d.\n", un, un->un_f_pm_supported);
30987 		}
30988 	}
30989 
30990 	if (un->un_f_is_hotpluggable) {
30991 
30992 		/*
30993 		 * Have to watch hotpluggable devices as well, since
30994 		 * that's the only way for userland applications to
30995 		 * detect hot removal while device is busy/mounted.
30996 		 */
30997 		un->un_f_monitor_media_state = TRUE;
30998 
30999 		un->un_f_check_start_stop = TRUE;
31000 
31001 	}
31002 }
31003 
31004 /*
31005  * sd_tg_rdwr:
31006  * Provides rdwr access for cmlb via sd_tgops. The start_block is
31007  * in sys block size, req_length in bytes.
31008  *
31009  */
31010 static int
31011 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
31012     diskaddr_t start_block, size_t reqlength, void *tg_cookie)
31013 {
31014 	struct sd_lun *un;
31015 	int path_flag = (int)(uintptr_t)tg_cookie;
31016 	char *dkl = NULL;
31017 	diskaddr_t real_addr = start_block;
31018 	diskaddr_t first_byte, end_block;
31019 
31020 	size_t	buffer_size = reqlength;
31021 	int rval = 0;
31022 	diskaddr_t	cap;
31023 	uint32_t	lbasize;
31024 	sd_ssc_t	*ssc;
31025 
31026 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31027 	if (un == NULL)
31028 		return (ENXIO);
31029 
31030 	if (cmd != TG_READ && cmd != TG_WRITE)
31031 		return (EINVAL);
31032 
31033 	ssc = sd_ssc_init(un);
31034 	mutex_enter(SD_MUTEX(un));
31035 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
31036 		mutex_exit(SD_MUTEX(un));
31037 		rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31038 		    &lbasize, path_flag);
31039 		if (rval != 0)
31040 			goto done1;
31041 		mutex_enter(SD_MUTEX(un));
31042 		sd_update_block_info(un, lbasize, cap);
31043 		if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
31044 			mutex_exit(SD_MUTEX(un));
31045 			rval = EIO;
31046 			goto done;
31047 		}
31048 	}
31049 
31050 	if (NOT_DEVBSIZE(un)) {
31051 		/*
31052 		 * sys_blocksize != tgt_blocksize, need to re-adjust
31053 		 * blkno and save the index to beginning of dk_label
31054 		 */
31055 		first_byte  = SD_SYSBLOCKS2BYTES(start_block);
31056 		real_addr = first_byte / un->un_tgt_blocksize;
31057 
31058 		end_block = (first_byte + reqlength +
31059 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
31060 
31061 		/* round up buffer size to multiple of target block size */
31062 		buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
31063 
31064 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
31065 		    "label_addr: 0x%x allocation size: 0x%x\n",
31066 		    real_addr, buffer_size);
31067 
31068 		if (((first_byte % un->un_tgt_blocksize) != 0) ||
31069 		    (reqlength % un->un_tgt_blocksize) != 0)
31070 			/* the request is not aligned */
31071 			dkl = kmem_zalloc(buffer_size, KM_SLEEP);
31072 	}
31073 
31074 	/*
31075 	 * The MMC standard allows READ CAPACITY to be
31076 	 * inaccurate by a bounded amount (in the interest of
31077 	 * response latency).  As a result, failed READs are
31078 	 * commonplace (due to the reading of metadata and not
31079 	 * data). Depending on the per-Vendor/drive Sense data,
31080 	 * the failed READ can cause many (unnecessary) retries.
31081 	 */
31082 
31083 	if (ISCD(un) && (cmd == TG_READ) &&
31084 	    (un->un_f_blockcount_is_valid == TRUE) &&
31085 	    ((start_block == (un->un_blockcount - 1))||
31086 	    (start_block == (un->un_blockcount - 2)))) {
31087 			path_flag = SD_PATH_DIRECT_PRIORITY;
31088 	}
31089 
31090 	mutex_exit(SD_MUTEX(un));
31091 	if (cmd == TG_READ) {
31092 		rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
31093 		    buffer_size, real_addr, path_flag);
31094 		if (dkl != NULL)
31095 			bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
31096 			    real_addr), bufaddr, reqlength);
31097 	} else {
31098 		if (dkl) {
31099 			rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
31100 			    real_addr, path_flag);
31101 			if (rval) {
31102 				goto done1;
31103 			}
31104 			bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
31105 			    real_addr), reqlength);
31106 		}
31107 		rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
31108 		    buffer_size, real_addr, path_flag);
31109 	}
31110 
31111 done1:
31112 	if (dkl != NULL)
31113 		kmem_free(dkl, buffer_size);
31114 
31115 	if (rval != 0) {
31116 		if (rval == EIO)
31117 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
31118 		else
31119 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31120 	}
31121 done:
31122 	sd_ssc_fini(ssc);
31123 	return (rval);
31124 }
31125 
31126 
31127 static int
31128 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
31129 {
31130 
31131 	struct sd_lun *un;
31132 	diskaddr_t	cap;
31133 	uint32_t	lbasize;
31134 	int		path_flag = (int)(uintptr_t)tg_cookie;
31135 	int		ret = 0;
31136 
31137 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31138 	if (un == NULL)
31139 		return (ENXIO);
31140 
31141 	switch (cmd) {
31142 	case TG_GETPHYGEOM:
31143 	case TG_GETVIRTGEOM:
31144 	case TG_GETCAPACITY:
31145 	case TG_GETBLOCKSIZE:
31146 		mutex_enter(SD_MUTEX(un));
31147 
31148 		if ((un->un_f_blockcount_is_valid == TRUE) &&
31149 		    (un->un_f_tgt_blocksize_is_valid == TRUE)) {
31150 			cap = un->un_blockcount;
31151 			lbasize = un->un_tgt_blocksize;
31152 			mutex_exit(SD_MUTEX(un));
31153 		} else {
31154 			sd_ssc_t	*ssc;
31155 			mutex_exit(SD_MUTEX(un));
31156 			ssc = sd_ssc_init(un);
31157 			ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31158 			    &lbasize, path_flag);
31159 			if (ret != 0) {
31160 				if (ret == EIO)
31161 					sd_ssc_assessment(ssc,
31162 					    SD_FMT_STATUS_CHECK);
31163 				else
31164 					sd_ssc_assessment(ssc,
31165 					    SD_FMT_IGNORE);
31166 				sd_ssc_fini(ssc);
31167 				return (ret);
31168 			}
31169 			sd_ssc_fini(ssc);
31170 			mutex_enter(SD_MUTEX(un));
31171 			sd_update_block_info(un, lbasize, cap);
31172 			if ((un->un_f_blockcount_is_valid == FALSE) ||
31173 			    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
31174 				mutex_exit(SD_MUTEX(un));
31175 				return (EIO);
31176 			}
31177 			mutex_exit(SD_MUTEX(un));
31178 		}
31179 
31180 		if (cmd == TG_GETCAPACITY) {
31181 			*(diskaddr_t *)arg = cap;
31182 			return (0);
31183 		}
31184 
31185 		if (cmd == TG_GETBLOCKSIZE) {
31186 			*(uint32_t *)arg = lbasize;
31187 			return (0);
31188 		}
31189 
31190 		if (cmd == TG_GETPHYGEOM)
31191 			ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
31192 			    cap, lbasize, path_flag);
31193 		else
31194 			/* TG_GETVIRTGEOM */
31195 			ret = sd_get_virtual_geometry(un,
31196 			    (cmlb_geom_t *)arg, cap, lbasize);
31197 
31198 		return (ret);
31199 
31200 	case TG_GETATTR:
31201 		mutex_enter(SD_MUTEX(un));
31202 		((tg_attribute_t *)arg)->media_is_writable =
31203 		    un->un_f_mmc_writable_media;
31204 		((tg_attribute_t *)arg)->media_is_solid_state =
31205 		    un->un_f_is_solid_state;
31206 		mutex_exit(SD_MUTEX(un));
31207 		return (0);
31208 	default:
31209 		return (ENOTTY);
31210 
31211 	}
31212 }
31213 
31214 /*
31215  *    Function: sd_ssc_ereport_post
31216  *
31217  * Description: Will be called when SD driver need to post an ereport.
31218  *
31219  *    Context: Kernel thread or interrupt context.
31220  */
31221 
31222 #define	DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
31223 
31224 static void
31225 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31226 {
31227 	int uscsi_path_instance = 0;
31228 	uchar_t	uscsi_pkt_reason;
31229 	uint32_t uscsi_pkt_state;
31230 	uint32_t uscsi_pkt_statistics;
31231 	uint64_t uscsi_ena;
31232 	uchar_t op_code;
31233 	uint8_t *sensep;
31234 	union scsi_cdb *cdbp;
31235 	uint_t cdblen = 0;
31236 	uint_t senlen = 0;
31237 	struct sd_lun *un;
31238 	dev_info_t *dip;
31239 	char *devid;
31240 	int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31241 	    SSC_FLAGS_INVALID_STATUS |
31242 	    SSC_FLAGS_INVALID_SENSE |
31243 	    SSC_FLAGS_INVALID_DATA;
31244 	char assessment[16];
31245 
31246 	ASSERT(ssc != NULL);
31247 	ASSERT(ssc->ssc_uscsi_cmd != NULL);
31248 	ASSERT(ssc->ssc_uscsi_info != NULL);
31249 
31250 	un = ssc->ssc_un;
31251 	ASSERT(un != NULL);
31252 
31253 	dip = un->un_sd->sd_dev;
31254 
31255 	/*
31256 	 * Get the devid:
31257 	 *	devid will only be passed to non-transport error reports.
31258 	 */
31259 	devid = DEVI(dip)->devi_devid_str;
31260 
31261 	/*
31262 	 * If we are syncing or dumping, the command will not be executed
31263 	 * so we bypass this situation.
31264 	 */
31265 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31266 	    (un->un_state == SD_STATE_DUMPING))
31267 		return;
31268 
31269 	uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31270 	uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31271 	uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31272 	uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31273 	uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31274 
31275 	sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31276 	cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31277 
31278 	/* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31279 	if (cdbp == NULL) {
31280 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31281 		    "sd_ssc_ereport_post meet empty cdb\n");
31282 		return;
31283 	}
31284 
31285 	op_code = cdbp->scc_cmd;
31286 
31287 	cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31288 	senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31289 	    ssc->ssc_uscsi_cmd->uscsi_rqresid);
31290 
31291 	if (senlen > 0)
31292 		ASSERT(sensep != NULL);
31293 
31294 	/*
31295 	 * Initialize drv_assess to corresponding values.
31296 	 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31297 	 * on the sense-key returned back.
31298 	 */
31299 	switch (drv_assess) {
31300 		case SD_FM_DRV_RECOVERY:
31301 			(void) sprintf(assessment, "%s", "recovered");
31302 			break;
31303 		case SD_FM_DRV_RETRY:
31304 			(void) sprintf(assessment, "%s", "retry");
31305 			break;
31306 		case SD_FM_DRV_NOTICE:
31307 			(void) sprintf(assessment, "%s", "info");
31308 			break;
31309 		case SD_FM_DRV_FATAL:
31310 		default:
31311 			(void) sprintf(assessment, "%s", "unknown");
31312 	}
31313 	/*
31314 	 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31315 	 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31316 	 * driver-assessment will always be "recovered" here.
31317 	 */
31318 	if (drv_assess == SD_FM_DRV_RECOVERY) {
31319 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31320 		    "cmd.disk.recovered", uscsi_ena, devid, NULL,
31321 		    DDI_NOSLEEP, NULL,
31322 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31323 		    DEVID_IF_KNOWN(devid),
31324 		    "driver-assessment", DATA_TYPE_STRING, assessment,
31325 		    "op-code", DATA_TYPE_UINT8, op_code,
31326 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31327 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31328 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31329 		    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31330 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31331 		    NULL);
31332 		return;
31333 	}
31334 
31335 	/*
31336 	 * If there is un-expected/un-decodable data, we should post
31337 	 * ereport.io.scsi.cmd.disk.dev.uderr.
31338 	 * driver-assessment will be set based on parameter drv_assess.
31339 	 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31340 	 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31341 	 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31342 	 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31343 	 */
31344 	if (ssc->ssc_flags & ssc_invalid_flags) {
31345 		if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31346 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31347 			    NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31348 			    NULL, DDI_NOSLEEP, NULL,
31349 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31350 			    DEVID_IF_KNOWN(devid),
31351 			    "driver-assessment", DATA_TYPE_STRING,
31352 			    drv_assess == SD_FM_DRV_FATAL ?
31353 			    "fail" : assessment,
31354 			    "op-code", DATA_TYPE_UINT8, op_code,
31355 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31356 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31357 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31358 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31359 			    "pkt-stats", DATA_TYPE_UINT32,
31360 			    uscsi_pkt_statistics,
31361 			    "stat-code", DATA_TYPE_UINT8,
31362 			    ssc->ssc_uscsi_cmd->uscsi_status,
31363 			    "un-decode-info", DATA_TYPE_STRING,
31364 			    ssc->ssc_info,
31365 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31366 			    senlen, sensep,
31367 			    NULL);
31368 		} else {
31369 			/*
31370 			 * For other type of invalid data, the
31371 			 * un-decode-value field would be empty because the
31372 			 * un-decodable content could be seen from upper
31373 			 * level payload or inside un-decode-info.
31374 			 */
31375 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31376 			    NULL,
31377 			    "cmd.disk.dev.uderr", uscsi_ena, devid,
31378 			    NULL, DDI_NOSLEEP, NULL,
31379 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31380 			    DEVID_IF_KNOWN(devid),
31381 			    "driver-assessment", DATA_TYPE_STRING,
31382 			    drv_assess == SD_FM_DRV_FATAL ?
31383 			    "fail" : assessment,
31384 			    "op-code", DATA_TYPE_UINT8, op_code,
31385 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31386 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31387 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31388 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31389 			    "pkt-stats", DATA_TYPE_UINT32,
31390 			    uscsi_pkt_statistics,
31391 			    "stat-code", DATA_TYPE_UINT8,
31392 			    ssc->ssc_uscsi_cmd->uscsi_status,
31393 			    "un-decode-info", DATA_TYPE_STRING,
31394 			    ssc->ssc_info,
31395 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31396 			    0, NULL,
31397 			    NULL);
31398 		}
31399 		ssc->ssc_flags &= ~ssc_invalid_flags;
31400 		return;
31401 	}
31402 
31403 	if (uscsi_pkt_reason != CMD_CMPLT ||
31404 	    (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31405 		/*
31406 		 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31407 		 * set inside sd_start_cmds due to errors(bad packet or
31408 		 * fatal transport error), we should take it as a
31409 		 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31410 		 * driver-assessment will be set based on drv_assess.
31411 		 * We will set devid to NULL because it is a transport
31412 		 * error.
31413 		 */
31414 		if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31415 			ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31416 
31417 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31418 		    "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31419 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31420 		    DEVID_IF_KNOWN(devid),
31421 		    "driver-assessment", DATA_TYPE_STRING,
31422 		    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31423 		    "op-code", DATA_TYPE_UINT8, op_code,
31424 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31425 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31426 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31427 		    "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31428 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31429 		    NULL);
31430 	} else {
31431 		/*
31432 		 * If we got here, we have a completed command, and we need
31433 		 * to further investigate the sense data to see what kind
31434 		 * of ereport we should post.
31435 		 * No ereport is needed if sense-key is KEY_RECOVERABLE_ERROR
31436 		 * and asc/ascq is "ATA PASS-THROUGH INFORMATION AVAILABLE".
31437 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr if sense-key is
31438 		 * KEY_MEDIUM_ERROR.
31439 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31440 		 * driver-assessment will be set based on the parameter
31441 		 * drv_assess.
31442 		 */
31443 		if (senlen > 0) {
31444 			/*
31445 			 * Here we have sense data available.
31446 			 */
31447 			uint8_t sense_key = scsi_sense_key(sensep);
31448 			uint8_t sense_asc = scsi_sense_asc(sensep);
31449 			uint8_t sense_ascq = scsi_sense_ascq(sensep);
31450 
31451 			if (sense_key == KEY_RECOVERABLE_ERROR &&
31452 			    sense_asc == 0x00 && sense_ascq == 0x1d)
31453 				return;
31454 
31455 			if (sense_key == KEY_MEDIUM_ERROR) {
31456 				/*
31457 				 * driver-assessment should be "fatal" if
31458 				 * drv_assess is SD_FM_DRV_FATAL.
31459 				 */
31460 				scsi_fm_ereport_post(un->un_sd,
31461 				    uscsi_path_instance, NULL,
31462 				    "cmd.disk.dev.rqs.merr",
31463 				    uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31464 				    FM_VERSION, DATA_TYPE_UINT8,
31465 				    FM_EREPORT_VERS0,
31466 				    DEVID_IF_KNOWN(devid),
31467 				    "driver-assessment",
31468 				    DATA_TYPE_STRING,
31469 				    drv_assess == SD_FM_DRV_FATAL ?
31470 				    "fatal" : assessment,
31471 				    "op-code",
31472 				    DATA_TYPE_UINT8, op_code,
31473 				    "cdb",
31474 				    DATA_TYPE_UINT8_ARRAY, cdblen,
31475 				    ssc->ssc_uscsi_cmd->uscsi_cdb,
31476 				    "pkt-reason",
31477 				    DATA_TYPE_UINT8, uscsi_pkt_reason,
31478 				    "pkt-state",
31479 				    DATA_TYPE_UINT8, uscsi_pkt_state,
31480 				    "pkt-stats",
31481 				    DATA_TYPE_UINT32,
31482 				    uscsi_pkt_statistics,
31483 				    "stat-code",
31484 				    DATA_TYPE_UINT8,
31485 				    ssc->ssc_uscsi_cmd->uscsi_status,
31486 				    "key",
31487 				    DATA_TYPE_UINT8,
31488 				    scsi_sense_key(sensep),
31489 				    "asc",
31490 				    DATA_TYPE_UINT8,
31491 				    scsi_sense_asc(sensep),
31492 				    "ascq",
31493 				    DATA_TYPE_UINT8,
31494 				    scsi_sense_ascq(sensep),
31495 				    "sense-data",
31496 				    DATA_TYPE_UINT8_ARRAY,
31497 				    senlen, sensep,
31498 				    "lba",
31499 				    DATA_TYPE_UINT64,
31500 				    ssc->ssc_uscsi_info->ui_lba,
31501 				    NULL);
31502 			} else {
31503 				/*
31504 				 * if sense-key == 0x4(hardware
31505 				 * error), driver-assessment should
31506 				 * be "fatal" if drv_assess is
31507 				 * SD_FM_DRV_FATAL.
31508 				 */
31509 				scsi_fm_ereport_post(un->un_sd,
31510 				    uscsi_path_instance, NULL,
31511 				    "cmd.disk.dev.rqs.derr",
31512 				    uscsi_ena, devid,
31513 				    NULL, DDI_NOSLEEP, NULL,
31514 				    FM_VERSION,
31515 				    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31516 				    DEVID_IF_KNOWN(devid),
31517 				    "driver-assessment",
31518 				    DATA_TYPE_STRING,
31519 				    drv_assess == SD_FM_DRV_FATAL ?
31520 				    (sense_key == 0x4 ?
31521 				    "fatal" : "fail") : assessment,
31522 				    "op-code",
31523 				    DATA_TYPE_UINT8, op_code,
31524 				    "cdb",
31525 				    DATA_TYPE_UINT8_ARRAY, cdblen,
31526 				    ssc->ssc_uscsi_cmd->uscsi_cdb,
31527 				    "pkt-reason",
31528 				    DATA_TYPE_UINT8, uscsi_pkt_reason,
31529 				    "pkt-state",
31530 				    DATA_TYPE_UINT8, uscsi_pkt_state,
31531 				    "pkt-stats",
31532 				    DATA_TYPE_UINT32,
31533 				    uscsi_pkt_statistics,
31534 				    "stat-code",
31535 				    DATA_TYPE_UINT8,
31536 				    ssc->ssc_uscsi_cmd->uscsi_status,
31537 				    "key",
31538 				    DATA_TYPE_UINT8,
31539 				    scsi_sense_key(sensep),
31540 				    "asc",
31541 				    DATA_TYPE_UINT8,
31542 				    scsi_sense_asc(sensep),
31543 				    "ascq",
31544 				    DATA_TYPE_UINT8,
31545 				    scsi_sense_ascq(sensep),
31546 				    "sense-data",
31547 				    DATA_TYPE_UINT8_ARRAY,
31548 				    senlen, sensep,
31549 				    NULL);
31550 			}
31551 		} else {
31552 			/*
31553 			 * For stat_code == STATUS_GOOD, this is not a
31554 			 * hardware error.
31555 			 */
31556 			if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31557 				return;
31558 
31559 			/*
31560 			 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31561 			 * stat-code but with sense data unavailable.
31562 			 * driver-assessment will be set based on parameter
31563 			 * drv_assess.
31564 			 */
31565 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31566 			    NULL,
31567 			    "cmd.disk.dev.serr", uscsi_ena,
31568 			    devid, NULL, DDI_NOSLEEP, NULL,
31569 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31570 			    DEVID_IF_KNOWN(devid),
31571 			    "driver-assessment", DATA_TYPE_STRING,
31572 			    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31573 			    "op-code", DATA_TYPE_UINT8, op_code,
31574 			    "cdb",
31575 			    DATA_TYPE_UINT8_ARRAY,
31576 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31577 			    "pkt-reason",
31578 			    DATA_TYPE_UINT8, uscsi_pkt_reason,
31579 			    "pkt-state",
31580 			    DATA_TYPE_UINT8, uscsi_pkt_state,
31581 			    "pkt-stats",
31582 			    DATA_TYPE_UINT32, uscsi_pkt_statistics,
31583 			    "stat-code",
31584 			    DATA_TYPE_UINT8,
31585 			    ssc->ssc_uscsi_cmd->uscsi_status,
31586 			    NULL);
31587 		}
31588 	}
31589 }
31590 
31591 /*
31592  *     Function: sd_ssc_extract_info
31593  *
31594  * Description: Extract information available to help generate ereport.
31595  *
31596  *     Context: Kernel thread or interrupt context.
31597  */
31598 static void
31599 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31600     struct buf *bp, struct sd_xbuf *xp)
31601 {
31602 	size_t senlen = 0;
31603 	union scsi_cdb *cdbp;
31604 	int path_instance;
31605 	/*
31606 	 * Need scsi_cdb_size array to determine the cdb length.
31607 	 */
31608 	extern uchar_t	scsi_cdb_size[];
31609 
31610 	ASSERT(un != NULL);
31611 	ASSERT(pktp != NULL);
31612 	ASSERT(bp != NULL);
31613 	ASSERT(xp != NULL);
31614 	ASSERT(ssc != NULL);
31615 	ASSERT(mutex_owned(SD_MUTEX(un)));
31616 
31617 	/*
31618 	 * Transfer the cdb buffer pointer here.
31619 	 */
31620 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31621 
31622 	ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31623 	ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31624 
31625 	/*
31626 	 * Transfer the sense data buffer pointer if sense data is available,
31627 	 * calculate the sense data length first.
31628 	 */
31629 	if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31630 	    (xp->xb_sense_state & STATE_ARQ_DONE)) {
31631 		/*
31632 		 * For arq case, we will enter here.
31633 		 */
31634 		if (xp->xb_sense_state & STATE_XARQ_DONE) {
31635 			senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31636 		} else {
31637 			senlen = SENSE_LENGTH;
31638 		}
31639 	} else {
31640 		/*
31641 		 * For non-arq case, we will enter this branch.
31642 		 */
31643 		if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31644 		    (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31645 			senlen = SENSE_LENGTH - xp->xb_sense_resid;
31646 		}
31647 
31648 	}
31649 
31650 	ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31651 	ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31652 	ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
31653 
31654 	ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
31655 
31656 	/*
31657 	 * Only transfer path_instance when scsi_pkt was properly allocated.
31658 	 */
31659 	path_instance = pktp->pkt_path_instance;
31660 	if (scsi_pkt_allocated_correctly(pktp) && path_instance)
31661 		ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
31662 	else
31663 		ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
31664 
31665 	/*
31666 	 * Copy in the other fields we may need when posting ereport.
31667 	 */
31668 	ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
31669 	ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
31670 	ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
31671 	ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
31672 
31673 	/*
31674 	 * For partially read/write command, we will not create ena
31675 	 * in case of a successful command be reconized as recovered.
31676 	 */
31677 	if ((pktp->pkt_reason == CMD_CMPLT) &&
31678 	    (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
31679 	    (senlen == 0)) {
31680 		return;
31681 	}
31682 
31683 	/*
31684 	 * To associate ereports of a single command execution flow, we
31685 	 * need a shared ena for a specific command.
31686 	 */
31687 	if (xp->xb_ena == 0)
31688 		xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
31689 	ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
31690 }
31691 
31692 
31693 /*
31694  *     Function: sd_check_solid_state
31695  *
31696  * Description: Query the optional INQUIRY VPD page 0xb1. If the device
31697  *              supports VPD page 0xb1, sd examines the MEDIUM ROTATION
31698  *              RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the
31699  *              device is a solid state drive.
31700  *
31701  *     Context: Kernel thread or interrupt context.
31702  */
31703 
31704 static void
31705 sd_check_solid_state(sd_ssc_t *ssc)
31706 {
31707 	int		rval		= 0;
31708 	uchar_t		*inqb1		= NULL;
31709 	size_t		inqb1_len	= MAX_INQUIRY_SIZE;
31710 	size_t		inqb1_resid	= 0;
31711 	struct sd_lun	*un;
31712 
31713 	ASSERT(ssc != NULL);
31714 	un = ssc->ssc_un;
31715 	ASSERT(un != NULL);
31716 	ASSERT(!mutex_owned(SD_MUTEX(un)));
31717 
31718 	mutex_enter(SD_MUTEX(un));
31719 	un->un_f_is_solid_state = FALSE;
31720 
31721 	if (ISCD(un)) {
31722 		mutex_exit(SD_MUTEX(un));
31723 		return;
31724 	}
31725 
31726 	if (sd_check_vpd_page_support(ssc) == 0 &&
31727 	    un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
31728 		mutex_exit(SD_MUTEX(un));
31729 		/* collect page b1 data */
31730 		inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
31731 
31732 		rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
31733 		    0x01, 0xB1, &inqb1_resid);
31734 
31735 		if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
31736 			SD_TRACE(SD_LOG_COMMON, un,
31737 			    "sd_check_solid_state: \
31738 			    successfully get VPD page: %x \
31739 			    PAGE LENGTH: %x BYTE 4: %x \
31740 			    BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
31741 			    inqb1[5]);
31742 
31743 			mutex_enter(SD_MUTEX(un));
31744 			/*
31745 			 * Check the MEDIUM ROTATION RATE. If it is set
31746 			 * to 1, the device is a solid state drive.
31747 			 */
31748 			if (inqb1[4] == 0 && inqb1[5] == 1) {
31749 				un->un_f_is_solid_state = TRUE;
31750 				/* solid state drives don't need disksort */
31751 				un->un_f_disksort_disabled = TRUE;
31752 			}
31753 			mutex_exit(SD_MUTEX(un));
31754 		} else if (rval != 0) {
31755 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31756 		}
31757 
31758 		kmem_free(inqb1, inqb1_len);
31759 	} else {
31760 		mutex_exit(SD_MUTEX(un));
31761 	}
31762 }
31763 
31764 /*
31765  *	Function: sd_check_emulation_mode
31766  *
31767  *   Description: Check whether the SSD is at emulation mode
31768  *		  by issuing READ_CAPACITY_16 to see whether
31769  *		  we can get physical block size of the drive.
31770  *
31771  *	 Context: Kernel thread or interrupt context.
31772  */
31773 
31774 static void
31775 sd_check_emulation_mode(sd_ssc_t *ssc)
31776 {
31777 	int		rval = 0;
31778 	uint64_t	capacity;
31779 	uint_t		lbasize;
31780 	uint_t		pbsize;
31781 	int		i;
31782 	int		devid_len;
31783 	struct sd_lun	*un;
31784 
31785 	ASSERT(ssc != NULL);
31786 	un = ssc->ssc_un;
31787 	ASSERT(un != NULL);
31788 	ASSERT(!mutex_owned(SD_MUTEX(un)));
31789 
31790 	mutex_enter(SD_MUTEX(un));
31791 	if (ISCD(un)) {
31792 		mutex_exit(SD_MUTEX(un));
31793 		return;
31794 	}
31795 
31796 	if (un->un_f_descr_format_supported) {
31797 		mutex_exit(SD_MUTEX(un));
31798 		rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
31799 		    &pbsize, SD_PATH_DIRECT);
31800 		mutex_enter(SD_MUTEX(un));
31801 
31802 		if (rval != 0) {
31803 			un->un_phy_blocksize = DEV_BSIZE;
31804 		} else {
31805 			if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
31806 				un->un_phy_blocksize = DEV_BSIZE;
31807 			} else if (pbsize > un->un_phy_blocksize) {
31808 				/*
31809 				 * Don't reset the physical blocksize
31810 				 * unless we've detected a larger value.
31811 				 */
31812 				un->un_phy_blocksize = pbsize;
31813 			}
31814 		}
31815 	}
31816 
31817 	for (i = 0; i < sd_flash_dev_table_size; i++) {
31818 		devid_len = (int)strlen(sd_flash_dev_table[i]);
31819 		if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
31820 		    == SD_SUCCESS) {
31821 			un->un_phy_blocksize = SSD_SECSIZE;
31822 			if (un->un_f_is_solid_state &&
31823 			    un->un_phy_blocksize != un->un_tgt_blocksize)
31824 				un->un_f_enable_rmw = TRUE;
31825 		}
31826 	}
31827 
31828 	mutex_exit(SD_MUTEX(un));
31829 }
31830