xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision 047c81d3)
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 #include <sys/dkioc_free_util.h>
56 
57 #ifdef __lock_lint
58 #define	_LP64
59 #define	__amd64
60 #endif
61 
62 #if (defined(__fibre))
63 /* Note: is there a leadville version of the following? */
64 #include <sys/fc4/fcal_linkapp.h>
65 #endif
66 #include <sys/taskq.h>
67 #include <sys/uuid.h>
68 #include <sys/byteorder.h>
69 #include <sys/sdt.h>
70 
71 #include "sd_xbuf.h"
72 
73 #include <sys/scsi/targets/sddef.h>
74 #include <sys/cmlb.h>
75 #include <sys/sysevent/eventdefs.h>
76 #include <sys/sysevent/dev.h>
77 
78 #include <sys/fm/protocol.h>
79 
80 /*
81  * Loadable module info.
82  */
83 #if (defined(__fibre))
84 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver"
85 #else /* !__fibre */
86 #define	SD_MODULE_NAME	"SCSI Disk Driver"
87 #endif /* !__fibre */
88 
89 /*
90  * Define the interconnect type, to allow the driver to distinguish
91  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
92  *
93  * This is really for backward compatibility. In the future, the driver
94  * should actually check the "interconnect-type" property as reported by
95  * the HBA; however at present this property is not defined by all HBAs,
96  * so we will use this #define (1) to permit the driver to run in
97  * backward-compatibility mode; and (2) to print a notification message
98  * if an FC HBA does not support the "interconnect-type" property.  The
99  * behavior of the driver will be to assume parallel SCSI behaviors unless
100  * the "interconnect-type" property is defined by the HBA **AND** has a
101  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
102  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
103  * Channel behaviors (as per the old ssd).  (Note that the
104  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
105  * will result in the driver assuming parallel SCSI behaviors.)
106  *
107  * (see common/sys/scsi/impl/services.h)
108  *
109  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
110  * since some FC HBAs may already support that, and there is some code in
111  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
112  * default would confuse that code, and besides things should work fine
113  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
114  * "interconnect_type" property.
115  *
116  */
117 #if (defined(__fibre))
118 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
119 #else
120 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
121 #endif
122 
123 /*
124  * The name of the driver, established from the module name in _init.
125  */
126 static	char *sd_label			= NULL;
127 
128 /*
129  * Driver name is unfortunately prefixed on some driver.conf properties.
130  */
131 #if (defined(__fibre))
132 #define	sd_max_xfer_size		ssd_max_xfer_size
133 #define	sd_config_list			ssd_config_list
134 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
135 static	char *sd_config_list		= "ssd-config-list";
136 #else
137 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
138 static	char *sd_config_list		= "sd-config-list";
139 #endif
140 
141 /*
142  * Driver global variables
143  */
144 
145 #if (defined(__fibre))
146 /*
147  * These #defines are to avoid namespace collisions that occur because this
148  * code is currently used to compile two separate driver modules: sd and ssd.
149  * All global variables need to be treated this way (even if declared static)
150  * in order to allow the debugger to resolve the names properly.
151  * It is anticipated that in the near future the ssd module will be obsoleted,
152  * at which time this namespace issue should go away.
153  */
154 #define	sd_state			ssd_state
155 #define	sd_io_time			ssd_io_time
156 #define	sd_failfast_enable		ssd_failfast_enable
157 #define	sd_ua_retry_count		ssd_ua_retry_count
158 #define	sd_report_pfa			ssd_report_pfa
159 #define	sd_max_throttle			ssd_max_throttle
160 #define	sd_min_throttle			ssd_min_throttle
161 #define	sd_rot_delay			ssd_rot_delay
162 
163 #define	sd_retry_on_reservation_conflict	\
164 					ssd_retry_on_reservation_conflict
165 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
166 #define	sd_resv_conflict_name		ssd_resv_conflict_name
167 
168 #define	sd_component_mask		ssd_component_mask
169 #define	sd_level_mask			ssd_level_mask
170 #define	sd_debug_un			ssd_debug_un
171 #define	sd_error_level			ssd_error_level
172 
173 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
174 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
175 
176 #define	sd_tr				ssd_tr
177 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
178 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
179 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
180 #define	sd_check_media_time		ssd_check_media_time
181 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
182 #define	sd_label_mutex			ssd_label_mutex
183 #define	sd_detach_mutex			ssd_detach_mutex
184 #define	sd_log_buf			ssd_log_buf
185 #define	sd_log_mutex			ssd_log_mutex
186 
187 #define	sd_disk_table			ssd_disk_table
188 #define	sd_disk_table_size		ssd_disk_table_size
189 #define	sd_sense_mutex			ssd_sense_mutex
190 #define	sd_cdbtab			ssd_cdbtab
191 
192 #define	sd_cb_ops			ssd_cb_ops
193 #define	sd_ops				ssd_ops
194 #define	sd_additional_codes		ssd_additional_codes
195 #define	sd_tgops			ssd_tgops
196 
197 #define	sd_minor_data			ssd_minor_data
198 #define	sd_minor_data_efi		ssd_minor_data_efi
199 
200 #define	sd_tq				ssd_tq
201 #define	sd_wmr_tq			ssd_wmr_tq
202 #define	sd_taskq_name			ssd_taskq_name
203 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
204 #define	sd_taskq_minalloc		ssd_taskq_minalloc
205 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
206 
207 #define	sd_dump_format_string		ssd_dump_format_string
208 
209 #define	sd_iostart_chain		ssd_iostart_chain
210 #define	sd_iodone_chain			ssd_iodone_chain
211 
212 #define	sd_pm_idletime			ssd_pm_idletime
213 
214 #define	sd_force_pm_supported		ssd_force_pm_supported
215 
216 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
217 
218 #define	sd_ssc_init			ssd_ssc_init
219 #define	sd_ssc_send			ssd_ssc_send
220 #define	sd_ssc_fini			ssd_ssc_fini
221 #define	sd_ssc_assessment		ssd_ssc_assessment
222 #define	sd_ssc_post			ssd_ssc_post
223 #define	sd_ssc_print			ssd_ssc_print
224 #define	sd_ssc_ereport_post		ssd_ssc_ereport_post
225 #define	sd_ssc_set_info			ssd_ssc_set_info
226 #define	sd_ssc_extract_info		ssd_ssc_extract_info
227 
228 #endif
229 
230 #ifdef	SDDEBUG
231 int	sd_force_pm_supported		= 0;
232 #endif	/* SDDEBUG */
233 
234 void *sd_state				= NULL;
235 int sd_io_time				= SD_IO_TIME;
236 int sd_failfast_enable			= 1;
237 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
238 int sd_report_pfa			= 1;
239 int sd_max_throttle			= SD_MAX_THROTTLE;
240 int sd_min_throttle			= SD_MIN_THROTTLE;
241 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
242 int sd_qfull_throttle_enable		= TRUE;
243 
244 int sd_retry_on_reservation_conflict	= 1;
245 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
246 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
247 
248 static int sd_dtype_optical_bind	= -1;
249 
250 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
251 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
252 
253 /*
254  * Global data for debug logging. To enable debug printing, sd_component_mask
255  * and sd_level_mask should be set to the desired bit patterns as outlined in
256  * sddef.h.
257  */
258 uint_t	sd_component_mask		= 0x0;
259 uint_t	sd_level_mask			= 0x0;
260 struct	sd_lun *sd_debug_un		= NULL;
261 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
262 
263 /* Note: these may go away in the future... */
264 static uint32_t	sd_xbuf_active_limit	= 512;
265 static uint32_t sd_xbuf_reserve_limit	= 16;
266 
267 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
268 
269 /*
270  * Timer value used to reset the throttle after it has been reduced
271  * (typically in response to TRAN_BUSY or STATUS_QFULL)
272  */
273 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
274 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
275 
276 /*
277  * Interval value associated with the media change scsi watch.
278  */
279 static int sd_check_media_time		= 3000000;
280 
281 /*
282  * Wait value used for in progress operations during a DDI_SUSPEND
283  */
284 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
285 
286 /*
287  * sd_label_mutex protects a static buffer used in the disk label
288  * component of the driver
289  */
290 static kmutex_t sd_label_mutex;
291 
292 /*
293  * sd_detach_mutex protects un_layer_count, un_detach_count, and
294  * un_opens_in_progress in the sd_lun structure.
295  */
296 static kmutex_t sd_detach_mutex;
297 
298 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
299 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
300 
301 /*
302  * Global buffer and mutex for debug logging
303  */
304 static char	sd_log_buf[1024];
305 static kmutex_t	sd_log_mutex;
306 
307 /*
308  * Structs and globals for recording attached lun information.
309  * This maintains a chain. Each node in the chain represents a SCSI controller.
310  * The structure records the number of luns attached to each target connected
311  * with the controller.
312  * For parallel scsi device only.
313  */
314 struct sd_scsi_hba_tgt_lun {
315 	struct sd_scsi_hba_tgt_lun	*next;
316 	dev_info_t			*pdip;
317 	int				nlun[NTARGETS_WIDE];
318 };
319 
320 /*
321  * Flag to indicate the lun is attached or detached
322  */
323 #define	SD_SCSI_LUN_ATTACH	0
324 #define	SD_SCSI_LUN_DETACH	1
325 
326 static kmutex_t	sd_scsi_target_lun_mutex;
327 static struct sd_scsi_hba_tgt_lun	*sd_scsi_target_lun_head = NULL;
328 
329 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
330     sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
331 
332 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
333     sd_scsi_target_lun_head))
334 
335 /*
336  * "Smart" Probe Caching structs, globals, #defines, etc.
337  * For parallel scsi and non-self-identify device only.
338  */
339 
340 /*
341  * The following resources and routines are implemented to support
342  * "smart" probing, which caches the scsi_probe() results in an array,
343  * in order to help avoid long probe times.
344  */
345 struct sd_scsi_probe_cache {
346 	struct	sd_scsi_probe_cache	*next;
347 	dev_info_t	*pdip;
348 	int		cache[NTARGETS_WIDE];
349 };
350 
351 static kmutex_t	sd_scsi_probe_cache_mutex;
352 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
353 
354 /*
355  * Really we only need protection on the head of the linked list, but
356  * better safe than sorry.
357  */
358 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
359     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
360 
361 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
362     sd_scsi_probe_cache_head))
363 
364 /*
365  * Power attribute table
366  */
367 static sd_power_attr_ss sd_pwr_ss = {
368 	{ "NAME=spindle-motor", "0=off", "1=on", NULL },
369 	{0, 100},
370 	{30, 0},
371 	{20000, 0}
372 };
373 
374 static sd_power_attr_pc sd_pwr_pc = {
375 	{ "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
376 		"3=active", NULL },
377 	{0, 0, 0, 100},
378 	{90, 90, 20, 0},
379 	{15000, 15000, 1000, 0}
380 };
381 
382 /*
383  * Power level to power condition
384  */
385 static int sd_pl2pc[] = {
386 	SD_TARGET_START_VALID,
387 	SD_TARGET_STANDBY,
388 	SD_TARGET_IDLE,
389 	SD_TARGET_ACTIVE
390 };
391 
392 /*
393  * Vendor specific data name property declarations
394  */
395 
396 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
397 
398 static sd_tunables seagate_properties = {
399 	SEAGATE_THROTTLE_VALUE,
400 	0,
401 	0,
402 	0,
403 	0,
404 	0,
405 	0,
406 	0,
407 	0
408 };
409 
410 
411 static sd_tunables fujitsu_properties = {
412 	FUJITSU_THROTTLE_VALUE,
413 	0,
414 	0,
415 	0,
416 	0,
417 	0,
418 	0,
419 	0,
420 	0
421 };
422 
423 static sd_tunables ibm_properties = {
424 	IBM_THROTTLE_VALUE,
425 	0,
426 	0,
427 	0,
428 	0,
429 	0,
430 	0,
431 	0,
432 	0
433 };
434 
435 static sd_tunables purple_properties = {
436 	PURPLE_THROTTLE_VALUE,
437 	0,
438 	0,
439 	PURPLE_BUSY_RETRIES,
440 	PURPLE_RESET_RETRY_COUNT,
441 	PURPLE_RESERVE_RELEASE_TIME,
442 	0,
443 	0,
444 	0
445 };
446 
447 static sd_tunables sve_properties = {
448 	SVE_THROTTLE_VALUE,
449 	0,
450 	0,
451 	SVE_BUSY_RETRIES,
452 	SVE_RESET_RETRY_COUNT,
453 	SVE_RESERVE_RELEASE_TIME,
454 	SVE_MIN_THROTTLE_VALUE,
455 	SVE_DISKSORT_DISABLED_FLAG,
456 	0
457 };
458 
459 static sd_tunables maserati_properties = {
460 	0,
461 	0,
462 	0,
463 	0,
464 	0,
465 	0,
466 	0,
467 	MASERATI_DISKSORT_DISABLED_FLAG,
468 	MASERATI_LUN_RESET_ENABLED_FLAG
469 };
470 
471 static sd_tunables pirus_properties = {
472 	PIRUS_THROTTLE_VALUE,
473 	0,
474 	PIRUS_NRR_COUNT,
475 	PIRUS_BUSY_RETRIES,
476 	PIRUS_RESET_RETRY_COUNT,
477 	0,
478 	PIRUS_MIN_THROTTLE_VALUE,
479 	PIRUS_DISKSORT_DISABLED_FLAG,
480 	PIRUS_LUN_RESET_ENABLED_FLAG
481 };
482 
483 #endif
484 
485 #if (defined(__sparc) && !defined(__fibre)) || \
486 	(defined(__i386) || defined(__amd64))
487 
488 
489 static sd_tunables elite_properties = {
490 	ELITE_THROTTLE_VALUE,
491 	0,
492 	0,
493 	0,
494 	0,
495 	0,
496 	0,
497 	0,
498 	0
499 };
500 
501 static sd_tunables st31200n_properties = {
502 	ST31200N_THROTTLE_VALUE,
503 	0,
504 	0,
505 	0,
506 	0,
507 	0,
508 	0,
509 	0,
510 	0
511 };
512 
513 #endif /* Fibre or not */
514 
515 static sd_tunables lsi_properties_scsi = {
516 	LSI_THROTTLE_VALUE,
517 	0,
518 	LSI_NOTREADY_RETRIES,
519 	0,
520 	0,
521 	0,
522 	0,
523 	0,
524 	0
525 };
526 
527 static sd_tunables symbios_properties = {
528 	SYMBIOS_THROTTLE_VALUE,
529 	0,
530 	SYMBIOS_NOTREADY_RETRIES,
531 	0,
532 	0,
533 	0,
534 	0,
535 	0,
536 	0
537 };
538 
539 static sd_tunables lsi_properties = {
540 	0,
541 	0,
542 	LSI_NOTREADY_RETRIES,
543 	0,
544 	0,
545 	0,
546 	0,
547 	0,
548 	0
549 };
550 
551 static sd_tunables lsi_oem_properties = {
552 	0,
553 	0,
554 	LSI_OEM_NOTREADY_RETRIES,
555 	0,
556 	0,
557 	0,
558 	0,
559 	0,
560 	0,
561 	1
562 };
563 
564 
565 
566 #if (defined(SD_PROP_TST))
567 
568 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
569 #define	SD_TST_THROTTLE_VAL	16
570 #define	SD_TST_NOTREADY_VAL	12
571 #define	SD_TST_BUSY_VAL		60
572 #define	SD_TST_RST_RETRY_VAL	36
573 #define	SD_TST_RSV_REL_TIME	60
574 
575 static sd_tunables tst_properties = {
576 	SD_TST_THROTTLE_VAL,
577 	SD_TST_CTYPE_VAL,
578 	SD_TST_NOTREADY_VAL,
579 	SD_TST_BUSY_VAL,
580 	SD_TST_RST_RETRY_VAL,
581 	SD_TST_RSV_REL_TIME,
582 	0,
583 	0,
584 	0
585 };
586 #endif
587 
588 /* This is similar to the ANSI toupper implementation */
589 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
590 
591 /*
592  * Static Driver Configuration Table
593  *
594  * This is the table of disks which need throttle adjustment (or, perhaps
595  * something else as defined by the flags at a future time.)  device_id
596  * is a string consisting of concatenated vid (vendor), pid (product/model)
597  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
598  * the parts of the string are as defined by the sizes in the scsi_inquiry
599  * structure.  Device type is searched as far as the device_id string is
600  * defined.  Flags defines which values are to be set in the driver from the
601  * properties list.
602  *
603  * Entries below which begin and end with a "*" are a special case.
604  * These do not have a specific vendor, and the string which follows
605  * can appear anywhere in the 16 byte PID portion of the inquiry data.
606  *
607  * Entries below which begin and end with a " " (blank) are a special
608  * case. The comparison function will treat multiple consecutive blanks
609  * as equivalent to a single blank. For example, this causes a
610  * sd_disk_table entry of " NEC CDROM " to match a device's id string
611  * of  "NEC       CDROM".
612  *
613  * Note: The MD21 controller type has been obsoleted.
614  *	 ST318202F is a Legacy device
615  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
616  *	 made with an FC connection. The entries here are a legacy.
617  */
618 static sd_disk_config_t sd_disk_table[] = {
619 #if defined(__fibre) || defined(__i386) || defined(__amd64)
620 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
621 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
622 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
623 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
624 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
625 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
626 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
627 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
628 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
629 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
630 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
631 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
632 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
633 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
634 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
635 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
636 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
637 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
638 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
639 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
640 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
641 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
642 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
643 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
644 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
645 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
646 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
647 	{ "IBM     1724-100",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
648 	{ "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649 	{ "IBM     1726-22x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650 	{ "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651 	{ "IBM     1726-42x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652 	{ "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
653 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
654 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
655 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660 	{ "IBM     1814",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
661 	{ "IBM     1814-200",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
662 	{ "IBM     1818",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
663 	{ "DELL    MD3000",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
664 	{ "DELL    MD3000i",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
665 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
666 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
667 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
668 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
669 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT |
670 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
671 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT |
672 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
673 	{ "Fujitsu SX300",	SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
674 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
675 	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
676 			SD_CONF_BSET_BSY_RETRY_COUNT|
677 			SD_CONF_BSET_RST_RETRIES|
678 			SD_CONF_BSET_RSV_REL_TIME,
679 		&purple_properties },
680 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
681 		SD_CONF_BSET_BSY_RETRY_COUNT|
682 		SD_CONF_BSET_RST_RETRIES|
683 		SD_CONF_BSET_RSV_REL_TIME|
684 		SD_CONF_BSET_MIN_THROTTLE|
685 		SD_CONF_BSET_DISKSORT_DISABLED,
686 		&sve_properties },
687 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
688 			SD_CONF_BSET_BSY_RETRY_COUNT|
689 			SD_CONF_BSET_RST_RETRIES|
690 			SD_CONF_BSET_RSV_REL_TIME,
691 		&purple_properties },
692 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
693 		SD_CONF_BSET_LUN_RESET_ENABLED,
694 		&maserati_properties },
695 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
696 		SD_CONF_BSET_NRR_COUNT|
697 		SD_CONF_BSET_BSY_RETRY_COUNT|
698 		SD_CONF_BSET_RST_RETRIES|
699 		SD_CONF_BSET_MIN_THROTTLE|
700 		SD_CONF_BSET_DISKSORT_DISABLED|
701 		SD_CONF_BSET_LUN_RESET_ENABLED,
702 		&pirus_properties },
703 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
704 		SD_CONF_BSET_NRR_COUNT|
705 		SD_CONF_BSET_BSY_RETRY_COUNT|
706 		SD_CONF_BSET_RST_RETRIES|
707 		SD_CONF_BSET_MIN_THROTTLE|
708 		SD_CONF_BSET_DISKSORT_DISABLED|
709 		SD_CONF_BSET_LUN_RESET_ENABLED,
710 		&pirus_properties },
711 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
712 		SD_CONF_BSET_NRR_COUNT|
713 		SD_CONF_BSET_BSY_RETRY_COUNT|
714 		SD_CONF_BSET_RST_RETRIES|
715 		SD_CONF_BSET_MIN_THROTTLE|
716 		SD_CONF_BSET_DISKSORT_DISABLED|
717 		SD_CONF_BSET_LUN_RESET_ENABLED,
718 		&pirus_properties },
719 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
720 		SD_CONF_BSET_NRR_COUNT|
721 		SD_CONF_BSET_BSY_RETRY_COUNT|
722 		SD_CONF_BSET_RST_RETRIES|
723 		SD_CONF_BSET_MIN_THROTTLE|
724 		SD_CONF_BSET_DISKSORT_DISABLED|
725 		SD_CONF_BSET_LUN_RESET_ENABLED,
726 		&pirus_properties },
727 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
728 		SD_CONF_BSET_NRR_COUNT|
729 		SD_CONF_BSET_BSY_RETRY_COUNT|
730 		SD_CONF_BSET_RST_RETRIES|
731 		SD_CONF_BSET_MIN_THROTTLE|
732 		SD_CONF_BSET_DISKSORT_DISABLED|
733 		SD_CONF_BSET_LUN_RESET_ENABLED,
734 		&pirus_properties },
735 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
736 		SD_CONF_BSET_NRR_COUNT|
737 		SD_CONF_BSET_BSY_RETRY_COUNT|
738 		SD_CONF_BSET_RST_RETRIES|
739 		SD_CONF_BSET_MIN_THROTTLE|
740 		SD_CONF_BSET_DISKSORT_DISABLED|
741 		SD_CONF_BSET_LUN_RESET_ENABLED,
742 		&pirus_properties },
743 	{ "SUN     STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
744 	{ "SUN     SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
745 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
746 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
747 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
748 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
749 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
750 #endif /* fibre or NON-sparc platforms */
751 #if ((defined(__sparc) && !defined(__fibre)) ||\
752 	(defined(__i386) || defined(__amd64)))
753 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
754 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
755 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
756 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
757 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
758 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
759 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
760 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
761 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
762 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
763 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
764 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
765 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
766 	    &symbios_properties },
767 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
768 	    &lsi_properties_scsi },
769 #if defined(__i386) || defined(__amd64)
770 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
771 				    | SD_CONF_BSET_READSUB_BCD
772 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
773 				    | SD_CONF_BSET_NO_READ_HEADER
774 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
775 
776 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
777 				    | SD_CONF_BSET_READSUB_BCD
778 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
779 				    | SD_CONF_BSET_NO_READ_HEADER
780 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
781 #endif /* __i386 || __amd64 */
782 #endif /* sparc NON-fibre or NON-sparc platforms */
783 
784 #if (defined(SD_PROP_TST))
785 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
786 				| SD_CONF_BSET_CTYPE
787 				| SD_CONF_BSET_NRR_COUNT
788 				| SD_CONF_BSET_FAB_DEVID
789 				| SD_CONF_BSET_NOCACHE
790 				| SD_CONF_BSET_BSY_RETRY_COUNT
791 				| SD_CONF_BSET_PLAYMSF_BCD
792 				| SD_CONF_BSET_READSUB_BCD
793 				| SD_CONF_BSET_READ_TOC_TRK_BCD
794 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
795 				| SD_CONF_BSET_NO_READ_HEADER
796 				| SD_CONF_BSET_READ_CD_XD4
797 				| SD_CONF_BSET_RST_RETRIES
798 				| SD_CONF_BSET_RSV_REL_TIME
799 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
800 #endif
801 };
802 
803 static const int sd_disk_table_size =
804 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
805 
806 /*
807  * Emulation mode disk drive VID/PID table
808  */
809 static char sd_flash_dev_table[][25] = {
810 	"ATA     MARVELL SD88SA02",
811 	"MARVELL SD88SA02",
812 	"TOSHIBA THNSNV05",
813 };
814 
815 static const int sd_flash_dev_table_size =
816 	sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
817 
818 #define	SD_INTERCONNECT_PARALLEL	0
819 #define	SD_INTERCONNECT_FABRIC		1
820 #define	SD_INTERCONNECT_FIBRE		2
821 #define	SD_INTERCONNECT_SSA		3
822 #define	SD_INTERCONNECT_SATA		4
823 #define	SD_INTERCONNECT_SAS		5
824 
825 #define	SD_IS_PARALLEL_SCSI(un)		\
826 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
827 #define	SD_IS_SERIAL(un)		\
828 	(((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
829 	((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
830 
831 /*
832  * Definitions used by device id registration routines
833  */
834 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
835 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
836 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
837 
838 static kmutex_t sd_sense_mutex = {0};
839 
840 /*
841  * Macros for updates of the driver state
842  */
843 #define	New_state(un, s)        \
844 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
845 #define	Restore_state(un)	\
846 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
847 
848 static struct sd_cdbinfo sd_cdbtab[] = {
849 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
850 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
851 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
852 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
853 };
854 
855 /*
856  * Specifies the number of seconds that must have elapsed since the last
857  * cmd. has completed for a device to be declared idle to the PM framework.
858  */
859 static int sd_pm_idletime = 1;
860 
861 /*
862  * Internal function prototypes
863  */
864 
865 #if (defined(__fibre))
866 /*
867  * These #defines are to avoid namespace collisions that occur because this
868  * code is currently used to compile two separate driver modules: sd and ssd.
869  * All function names need to be treated this way (even if declared static)
870  * in order to allow the debugger to resolve the names properly.
871  * It is anticipated that in the near future the ssd module will be obsoleted,
872  * at which time this ugliness should go away.
873  */
874 #define	sd_log_trace			ssd_log_trace
875 #define	sd_log_info			ssd_log_info
876 #define	sd_log_err			ssd_log_err
877 #define	sdprobe				ssdprobe
878 #define	sdinfo				ssdinfo
879 #define	sd_prop_op			ssd_prop_op
880 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
881 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
882 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
883 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
884 #define	sd_scsi_target_lun_init		ssd_scsi_target_lun_init
885 #define	sd_scsi_target_lun_fini		ssd_scsi_target_lun_fini
886 #define	sd_scsi_get_target_lun_count	ssd_scsi_get_target_lun_count
887 #define	sd_scsi_update_lun_on_target	ssd_scsi_update_lun_on_target
888 #define	sd_spin_up_unit			ssd_spin_up_unit
889 #define	sd_enable_descr_sense		ssd_enable_descr_sense
890 #define	sd_reenable_dsense_task		ssd_reenable_dsense_task
891 #define	sd_set_mmc_caps			ssd_set_mmc_caps
892 #define	sd_read_unit_properties		ssd_read_unit_properties
893 #define	sd_process_sdconf_file		ssd_process_sdconf_file
894 #define	sd_process_sdconf_table		ssd_process_sdconf_table
895 #define	sd_sdconf_id_match		ssd_sdconf_id_match
896 #define	sd_blank_cmp			ssd_blank_cmp
897 #define	sd_chk_vers1_data		ssd_chk_vers1_data
898 #define	sd_set_vers1_properties		ssd_set_vers1_properties
899 #define	sd_check_bdc_vpd		ssd_check_bdc_vpd
900 #define	sd_check_emulation_mode		ssd_check_emulation_mode
901 
902 #define	sd_get_physical_geometry	ssd_get_physical_geometry
903 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
904 #define	sd_update_block_info		ssd_update_block_info
905 #define	sd_register_devid		ssd_register_devid
906 #define	sd_get_devid			ssd_get_devid
907 #define	sd_create_devid			ssd_create_devid
908 #define	sd_write_deviceid		ssd_write_deviceid
909 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
910 #define	sd_setup_pm			ssd_setup_pm
911 #define	sd_create_pm_components		ssd_create_pm_components
912 #define	sd_ddi_suspend			ssd_ddi_suspend
913 #define	sd_ddi_resume			ssd_ddi_resume
914 #define	sd_pm_state_change		ssd_pm_state_change
915 #define	sdpower				ssdpower
916 #define	sdattach			ssdattach
917 #define	sddetach			ssddetach
918 #define	sd_unit_attach			ssd_unit_attach
919 #define	sd_unit_detach			ssd_unit_detach
920 #define	sd_set_unit_attributes		ssd_set_unit_attributes
921 #define	sd_create_errstats		ssd_create_errstats
922 #define	sd_set_errstats			ssd_set_errstats
923 #define	sd_set_pstats			ssd_set_pstats
924 #define	sddump				ssddump
925 #define	sd_scsi_poll			ssd_scsi_poll
926 #define	sd_send_polled_RQS		ssd_send_polled_RQS
927 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
928 #define	sd_init_event_callbacks		ssd_init_event_callbacks
929 #define	sd_event_callback		ssd_event_callback
930 #define	sd_cache_control		ssd_cache_control
931 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
932 #define	sd_get_write_cache_changeable	ssd_get_write_cache_changeable
933 #define	sd_get_nv_sup			ssd_get_nv_sup
934 #define	sd_make_device			ssd_make_device
935 #define	sdopen				ssdopen
936 #define	sdclose				ssdclose
937 #define	sd_ready_and_valid		ssd_ready_and_valid
938 #define	sdmin				ssdmin
939 #define	sdread				ssdread
940 #define	sdwrite				ssdwrite
941 #define	sdaread				ssdaread
942 #define	sdawrite			ssdawrite
943 #define	sdstrategy			ssdstrategy
944 #define	sdioctl				ssdioctl
945 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
946 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
947 #define	sd_checksum_iostart		ssd_checksum_iostart
948 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
949 #define	sd_pm_iostart			ssd_pm_iostart
950 #define	sd_core_iostart			ssd_core_iostart
951 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
952 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
953 #define	sd_checksum_iodone		ssd_checksum_iodone
954 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
955 #define	sd_pm_iodone			ssd_pm_iodone
956 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
957 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
958 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
959 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
960 #define	sd_buf_iodone			ssd_buf_iodone
961 #define	sd_uscsi_strategy		ssd_uscsi_strategy
962 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
963 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
964 #define	sd_uscsi_iodone			ssd_uscsi_iodone
965 #define	sd_xbuf_strategy		ssd_xbuf_strategy
966 #define	sd_xbuf_init			ssd_xbuf_init
967 #define	sd_pm_entry			ssd_pm_entry
968 #define	sd_pm_exit			ssd_pm_exit
969 
970 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
971 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
972 
973 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
974 #define	sdintr				ssdintr
975 #define	sd_start_cmds			ssd_start_cmds
976 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
977 #define	sd_bioclone_alloc		ssd_bioclone_alloc
978 #define	sd_bioclone_free		ssd_bioclone_free
979 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
980 #define	sd_shadow_buf_free		ssd_shadow_buf_free
981 #define	sd_print_transport_rejected_message	\
982 					ssd_print_transport_rejected_message
983 #define	sd_retry_command		ssd_retry_command
984 #define	sd_set_retry_bp			ssd_set_retry_bp
985 #define	sd_send_request_sense_command	ssd_send_request_sense_command
986 #define	sd_start_retry_command		ssd_start_retry_command
987 #define	sd_start_direct_priority_command	\
988 					ssd_start_direct_priority_command
989 #define	sd_return_failed_command	ssd_return_failed_command
990 #define	sd_return_failed_command_no_restart	\
991 					ssd_return_failed_command_no_restart
992 #define	sd_return_command		ssd_return_command
993 #define	sd_sync_with_callback		ssd_sync_with_callback
994 #define	sdrunout			ssdrunout
995 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
996 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
997 #define	sd_reduce_throttle		ssd_reduce_throttle
998 #define	sd_restore_throttle		ssd_restore_throttle
999 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
1000 #define	sd_init_cdb_limits		ssd_init_cdb_limits
1001 #define	sd_pkt_status_good		ssd_pkt_status_good
1002 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
1003 #define	sd_pkt_status_busy		ssd_pkt_status_busy
1004 #define	sd_pkt_status_reservation_conflict	\
1005 					ssd_pkt_status_reservation_conflict
1006 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
1007 #define	sd_handle_request_sense		ssd_handle_request_sense
1008 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
1009 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
1010 #define	sd_validate_sense_data		ssd_validate_sense_data
1011 #define	sd_decode_sense			ssd_decode_sense
1012 #define	sd_print_sense_msg		ssd_print_sense_msg
1013 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
1014 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
1015 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
1016 #define	sd_sense_key_medium_or_hardware_error	\
1017 					ssd_sense_key_medium_or_hardware_error
1018 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
1019 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
1020 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
1021 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
1022 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
1023 #define	sd_sense_key_default		ssd_sense_key_default
1024 #define	sd_print_retry_msg		ssd_print_retry_msg
1025 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
1026 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
1027 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
1028 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
1029 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
1030 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
1031 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
1032 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
1033 #define	sd_pkt_reason_default		ssd_pkt_reason_default
1034 #define	sd_reset_target			ssd_reset_target
1035 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
1036 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
1037 #define	sd_taskq_create			ssd_taskq_create
1038 #define	sd_taskq_delete			ssd_taskq_delete
1039 #define	sd_target_change_task		ssd_target_change_task
1040 #define	sd_log_dev_status_event		ssd_log_dev_status_event
1041 #define	sd_log_lun_expansion_event	ssd_log_lun_expansion_event
1042 #define	sd_log_eject_request_event	ssd_log_eject_request_event
1043 #define	sd_media_change_task		ssd_media_change_task
1044 #define	sd_handle_mchange		ssd_handle_mchange
1045 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
1046 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
1047 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
1048 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
1049 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
1050 					sd_send_scsi_feature_GET_CONFIGURATION
1051 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
1052 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
1053 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
1054 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
1055 					ssd_send_scsi_PERSISTENT_RESERVE_IN
1056 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
1057 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
1058 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
1059 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
1060 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1061 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
1062 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
1063 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
1064 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
1065 #define	sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION	\
1066 				ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1067 #define	sd_gesn_media_data_valid	ssd_gesn_media_data_valid
1068 #define	sd_alloc_rqs			ssd_alloc_rqs
1069 #define	sd_free_rqs			ssd_free_rqs
1070 #define	sd_dump_memory			ssd_dump_memory
1071 #define	sd_get_media_info_com		ssd_get_media_info_com
1072 #define	sd_get_media_info		ssd_get_media_info
1073 #define	sd_get_media_info_ext		ssd_get_media_info_ext
1074 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
1075 #define	sd_nvpair_str_decode		ssd_nvpair_str_decode
1076 #define	sd_strtok_r			ssd_strtok_r
1077 #define	sd_set_properties		ssd_set_properties
1078 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
1079 #define	sd_setup_next_xfer		ssd_setup_next_xfer
1080 #define	sd_dkio_get_temp		ssd_dkio_get_temp
1081 #define	sd_check_mhd			ssd_check_mhd
1082 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1083 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1084 #define	sd_sname			ssd_sname
1085 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1086 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1087 #define	sd_take_ownership		ssd_take_ownership
1088 #define	sd_reserve_release		ssd_reserve_release
1089 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1090 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1091 #define	sd_persistent_reservation_in_read_keys	\
1092 					ssd_persistent_reservation_in_read_keys
1093 #define	sd_persistent_reservation_in_read_resv	\
1094 					ssd_persistent_reservation_in_read_resv
1095 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1096 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1097 #define	sd_mhdioc_release		ssd_mhdioc_release
1098 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1099 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1100 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1101 #define	sr_change_blkmode		ssr_change_blkmode
1102 #define	sr_change_speed			ssr_change_speed
1103 #define	sr_atapi_change_speed		ssr_atapi_change_speed
1104 #define	sr_pause_resume			ssr_pause_resume
1105 #define	sr_play_msf			ssr_play_msf
1106 #define	sr_play_trkind			ssr_play_trkind
1107 #define	sr_read_all_subcodes		ssr_read_all_subcodes
1108 #define	sr_read_subchannel		ssr_read_subchannel
1109 #define	sr_read_tocentry		ssr_read_tocentry
1110 #define	sr_read_tochdr			ssr_read_tochdr
1111 #define	sr_read_cdda			ssr_read_cdda
1112 #define	sr_read_cdxa			ssr_read_cdxa
1113 #define	sr_read_mode1			ssr_read_mode1
1114 #define	sr_read_mode2			ssr_read_mode2
1115 #define	sr_read_cd_mode2		ssr_read_cd_mode2
1116 #define	sr_sector_mode			ssr_sector_mode
1117 #define	sr_eject			ssr_eject
1118 #define	sr_ejected			ssr_ejected
1119 #define	sr_check_wp			ssr_check_wp
1120 #define	sd_watch_request_submit		ssd_watch_request_submit
1121 #define	sd_check_media			ssd_check_media
1122 #define	sd_media_watch_cb		ssd_media_watch_cb
1123 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1124 #define	sr_volume_ctrl			ssr_volume_ctrl
1125 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1126 #define	sd_log_page_supported		ssd_log_page_supported
1127 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1128 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1129 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1130 #define	sd_range_lock			ssd_range_lock
1131 #define	sd_get_range			ssd_get_range
1132 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1133 #define	sd_range_unlock			ssd_range_unlock
1134 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1135 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1136 
1137 #define	sd_iostart_chain		ssd_iostart_chain
1138 #define	sd_iodone_chain			ssd_iodone_chain
1139 #define	sd_initpkt_map			ssd_initpkt_map
1140 #define	sd_destroypkt_map		ssd_destroypkt_map
1141 #define	sd_chain_type_map		ssd_chain_type_map
1142 #define	sd_chain_index_map		ssd_chain_index_map
1143 
1144 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1145 #define	sd_failfast_flushq		ssd_failfast_flushq
1146 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1147 
1148 #define	sd_is_lsi			ssd_is_lsi
1149 #define	sd_tg_rdwr			ssd_tg_rdwr
1150 #define	sd_tg_getinfo			ssd_tg_getinfo
1151 #define	sd_rmw_msg_print_handler	ssd_rmw_msg_print_handler
1152 
1153 #endif	/* #if (defined(__fibre)) */
1154 
1155 typedef struct unmap_param_hdr_s {
1156 	uint16_t	uph_data_len;
1157 	uint16_t	uph_descr_data_len;
1158 	uint32_t	uph_reserved;
1159 } unmap_param_hdr_t;
1160 
1161 typedef struct unmap_blk_descr_s {
1162 	uint64_t	ubd_lba;
1163 	uint32_t	ubd_lba_cnt;
1164 	uint32_t	ubd_reserved;
1165 } unmap_blk_descr_t;
1166 
1167 /* Max number of block descriptors in UNMAP command */
1168 #define	SD_UNMAP_MAX_DESCR \
1169 	((UINT16_MAX - sizeof (unmap_param_hdr_t)) / sizeof (unmap_blk_descr_t))
1170 /* Max size of the UNMAP parameter list in bytes */
1171 #define	SD_UNMAP_PARAM_LIST_MAXSZ	(sizeof (unmap_param_hdr_t) + \
1172 	SD_UNMAP_MAX_DESCR * sizeof (unmap_blk_descr_t))
1173 
1174 int _init(void);
1175 int _fini(void);
1176 int _info(struct modinfo *modinfop);
1177 
1178 /*PRINTFLIKE3*/
1179 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1180 /*PRINTFLIKE3*/
1181 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1182 /*PRINTFLIKE3*/
1183 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1184 
1185 static int sdprobe(dev_info_t *devi);
1186 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1187     void **result);
1188 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1189     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1190 
1191 /*
1192  * Smart probe for parallel scsi
1193  */
1194 static void sd_scsi_probe_cache_init(void);
1195 static void sd_scsi_probe_cache_fini(void);
1196 static void sd_scsi_clear_probe_cache(void);
1197 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1198 
1199 /*
1200  * Attached luns on target for parallel scsi
1201  */
1202 static void sd_scsi_target_lun_init(void);
1203 static void sd_scsi_target_lun_fini(void);
1204 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1205 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1206 
1207 static int	sd_spin_up_unit(sd_ssc_t *ssc);
1208 
1209 /*
1210  * Using sd_ssc_init to establish sd_ssc_t struct
1211  * Using sd_ssc_send to send uscsi internal command
1212  * Using sd_ssc_fini to free sd_ssc_t struct
1213  */
1214 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1215 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1216     int flag, enum uio_seg dataspace, int path_flag);
1217 static void sd_ssc_fini(sd_ssc_t *ssc);
1218 
1219 /*
1220  * Using sd_ssc_assessment to set correct type-of-assessment
1221  * Using sd_ssc_post to post ereport & system log
1222  *       sd_ssc_post will call sd_ssc_print to print system log
1223  *       sd_ssc_post will call sd_ssd_ereport_post to post ereport
1224  */
1225 static void sd_ssc_assessment(sd_ssc_t *ssc,
1226     enum sd_type_assessment tp_assess);
1227 
1228 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1229 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1230 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1231     enum sd_driver_assessment drv_assess);
1232 
1233 /*
1234  * Using sd_ssc_set_info to mark an un-decodable-data error.
1235  * Using sd_ssc_extract_info to transfer information from internal
1236  *       data structures to sd_ssc_t.
1237  */
1238 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1239     const char *fmt, ...);
1240 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1241     struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1242 
1243 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1244     enum uio_seg dataspace, int path_flag);
1245 
1246 #ifdef _LP64
1247 static void	sd_enable_descr_sense(sd_ssc_t *ssc);
1248 static void	sd_reenable_dsense_task(void *arg);
1249 #endif /* _LP64 */
1250 
1251 static void	sd_set_mmc_caps(sd_ssc_t *ssc);
1252 
1253 static void sd_read_unit_properties(struct sd_lun *un);
1254 static int  sd_process_sdconf_file(struct sd_lun *un);
1255 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1256 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1257 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1258 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1259     int *data_list, sd_tunables *values);
1260 static void sd_process_sdconf_table(struct sd_lun *un);
1261 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1262 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1263 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1264 	int list_len, char *dataname_ptr);
1265 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1266     sd_tunables *prop_list);
1267 
1268 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1269     int reservation_flag);
1270 static int  sd_get_devid(sd_ssc_t *ssc);
1271 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1272 static int  sd_write_deviceid(sd_ssc_t *ssc);
1273 static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
1274 
1275 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1276 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1277 
1278 static int  sd_ddi_suspend(dev_info_t *devi);
1279 static int  sd_ddi_resume(dev_info_t *devi);
1280 static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
1281 static int  sdpower(dev_info_t *devi, int component, int level);
1282 
1283 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1284 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1285 static int  sd_unit_attach(dev_info_t *devi);
1286 static int  sd_unit_detach(dev_info_t *devi);
1287 
1288 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1289 static void sd_create_errstats(struct sd_lun *un, int instance);
1290 static void sd_set_errstats(struct sd_lun *un);
1291 static void sd_set_pstats(struct sd_lun *un);
1292 
1293 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1294 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1295 static int  sd_send_polled_RQS(struct sd_lun *un);
1296 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1297 
1298 #if (defined(__fibre))
1299 /*
1300  * Event callbacks (photon)
1301  */
1302 static void sd_init_event_callbacks(struct sd_lun *un);
1303 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1304 #endif
1305 
1306 /*
1307  * Defines for sd_cache_control
1308  */
1309 
1310 #define	SD_CACHE_ENABLE		1
1311 #define	SD_CACHE_DISABLE	0
1312 #define	SD_CACHE_NOCHANGE	-1
1313 
1314 static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1315 static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1316 static void  sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
1317 static void  sd_get_nv_sup(sd_ssc_t *ssc);
1318 static dev_t sd_make_device(dev_info_t *devi);
1319 static void  sd_check_bdc_vpd(sd_ssc_t *ssc);
1320 static void  sd_check_emulation_mode(sd_ssc_t *ssc);
1321 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1322 	uint64_t capacity);
1323 
1324 /*
1325  * Driver entry point functions.
1326  */
1327 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1328 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1329 static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
1330 
1331 static void sdmin(struct buf *bp);
1332 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1333 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1334 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1335 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1336 
1337 static int sdstrategy(struct buf *bp);
1338 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1339 
1340 /*
1341  * Function prototypes for layering functions in the iostart chain.
1342  */
1343 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1344 	struct buf *bp);
1345 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1346 	struct buf *bp);
1347 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1348 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1349 	struct buf *bp);
1350 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1351 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1352 
1353 /*
1354  * Function prototypes for layering functions in the iodone chain.
1355  */
1356 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1357 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1358 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1359 	struct buf *bp);
1360 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1361 	struct buf *bp);
1362 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1363 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1364 	struct buf *bp);
1365 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1366 
1367 /*
1368  * Prototypes for functions to support buf(9S) based IO.
1369  */
1370 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1371 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1372 static void sd_destroypkt_for_buf(struct buf *);
1373 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1374 	struct buf *bp, int flags,
1375 	int (*callback)(caddr_t), caddr_t callback_arg,
1376 	diskaddr_t lba, uint32_t blockcount);
1377 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1378 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1379 
1380 /*
1381  * Prototypes for functions to support USCSI IO.
1382  */
1383 static int sd_uscsi_strategy(struct buf *bp);
1384 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1385 static void sd_destroypkt_for_uscsi(struct buf *);
1386 
1387 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1388 	uchar_t chain_type, void *pktinfop);
1389 
1390 static int  sd_pm_entry(struct sd_lun *un);
1391 static void sd_pm_exit(struct sd_lun *un);
1392 
1393 static void sd_pm_idletimeout_handler(void *arg);
1394 
1395 /*
1396  * sd_core internal functions (used at the sd_core_io layer).
1397  */
1398 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1399 static void sdintr(struct scsi_pkt *pktp);
1400 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1401 
1402 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1403 	enum uio_seg dataspace, int path_flag);
1404 
1405 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1406 	daddr_t blkno, int (*func)(struct buf *));
1407 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1408 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1409 static void sd_bioclone_free(struct buf *bp);
1410 static void sd_shadow_buf_free(struct buf *bp);
1411 
1412 static void sd_print_transport_rejected_message(struct sd_lun *un,
1413 	struct sd_xbuf *xp, int code);
1414 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1415     void *arg, int code);
1416 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1417     void *arg, int code);
1418 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1419     void *arg, int code);
1420 
1421 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1422 	int retry_check_flag,
1423 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1424 		int c),
1425 	void *user_arg, int failure_code,  clock_t retry_delay,
1426 	void (*statp)(kstat_io_t *));
1427 
1428 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1429 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1430 
1431 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1432 	struct scsi_pkt *pktp);
1433 static void sd_start_retry_command(void *arg);
1434 static void sd_start_direct_priority_command(void *arg);
1435 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1436 	int errcode);
1437 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1438 	struct buf *bp, int errcode);
1439 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1440 static void sd_sync_with_callback(struct sd_lun *un);
1441 static int sdrunout(caddr_t arg);
1442 
1443 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1444 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1445 
1446 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1447 static void sd_restore_throttle(void *arg);
1448 
1449 static void sd_init_cdb_limits(struct sd_lun *un);
1450 
1451 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1452 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1453 
1454 /*
1455  * Error handling functions
1456  */
1457 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1458 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1459 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1460 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1461 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1462 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1463 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1464 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1465 
1466 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1467 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1468 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1469 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1470 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1471 	struct sd_xbuf *xp, size_t actual_len);
1472 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1473 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1474 
1475 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1476 	void *arg, int code);
1477 
1478 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1479 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1480 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1481 	uint8_t *sense_datap,
1482 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1483 static void sd_sense_key_not_ready(struct sd_lun *un,
1484 	uint8_t *sense_datap,
1485 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1486 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1487 	uint8_t *sense_datap,
1488 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1489 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1490 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1491 static void sd_sense_key_unit_attention(struct sd_lun *un,
1492 	uint8_t *sense_datap,
1493 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1494 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1495 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1496 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1497 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1498 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1499 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1500 static void sd_sense_key_default(struct sd_lun *un,
1501 	uint8_t *sense_datap,
1502 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1503 
1504 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1505 	void *arg, int flag);
1506 
1507 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1508 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1509 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1510 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1511 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1512 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1513 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1514 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1515 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1516 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1517 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1518 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1519 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1520 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1521 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1522 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1523 
1524 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1525 
1526 static void sd_start_stop_unit_callback(void *arg);
1527 static void sd_start_stop_unit_task(void *arg);
1528 
1529 static void sd_taskq_create(void);
1530 static void sd_taskq_delete(void);
1531 static void sd_target_change_task(void *arg);
1532 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1533 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1534 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1535 static void sd_media_change_task(void *arg);
1536 
1537 static int sd_handle_mchange(struct sd_lun *un);
1538 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1539 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1540 	uint32_t *lbap, int path_flag);
1541 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1542 	uint32_t *lbap, uint32_t *psp, int path_flag);
1543 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1544 	int flag, int path_flag);
1545 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1546 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1547 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1548 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1549 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1550 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1551 	uchar_t usr_cmd, uchar_t *usr_bufp);
1552 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1553 	struct dk_callback *dkc);
1554 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1555 static int sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl,
1556 	int flag);
1557 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1558 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1559 	uchar_t *bufaddr, uint_t buflen, int path_flag);
1560 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1561 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1562 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1563 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1564 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1565 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1566 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1567 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1568 	size_t buflen, daddr_t start_block, int path_flag);
1569 #define	sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag)	\
1570 	sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1571 	path_flag)
1572 #define	sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1573 	sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1574 	path_flag)
1575 
1576 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1577 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1578 	uint16_t param_ptr, int path_flag);
1579 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1580 	uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1581 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1582 
1583 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1584 static void sd_free_rqs(struct sd_lun *un);
1585 
1586 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1587 	uchar_t *data, int len, int fmt);
1588 static void sd_panic_for_res_conflict(struct sd_lun *un);
1589 
1590 /*
1591  * Disk Ioctl Function Prototypes
1592  */
1593 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1594 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1595 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1596 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1597 
1598 /*
1599  * Multi-host Ioctl Prototypes
1600  */
1601 static int sd_check_mhd(dev_t dev, int interval);
1602 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1603 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1604 static char *sd_sname(uchar_t status);
1605 static void sd_mhd_resvd_recover(void *arg);
1606 static void sd_resv_reclaim_thread();
1607 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1608 static int sd_reserve_release(dev_t dev, int cmd);
1609 static void sd_rmv_resv_reclaim_req(dev_t dev);
1610 static void sd_mhd_reset_notify_cb(caddr_t arg);
1611 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1612 	mhioc_inkeys_t *usrp, int flag);
1613 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1614 	mhioc_inresvs_t *usrp, int flag);
1615 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1616 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1617 static int sd_mhdioc_release(dev_t dev);
1618 static int sd_mhdioc_register_devid(dev_t dev);
1619 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1620 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1621 
1622 /*
1623  * SCSI removable prototypes
1624  */
1625 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1626 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1627 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1628 static int sr_pause_resume(dev_t dev, int mode);
1629 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1630 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1631 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1632 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1633 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1634 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1635 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1636 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1637 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1638 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1639 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1640 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1641 static int sr_eject(dev_t dev);
1642 static void sr_ejected(register struct sd_lun *un);
1643 static int sr_check_wp(dev_t dev);
1644 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1645 static int sd_check_media(dev_t dev, enum dkio_state state);
1646 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1647 static void sd_delayed_cv_broadcast(void *arg);
1648 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1649 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1650 
1651 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1652 
1653 /*
1654  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1655  */
1656 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1657 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1658 static void sd_wm_cache_destructor(void *wm, void *un);
1659 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1660     daddr_t endb, ushort_t typ);
1661 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1662     daddr_t endb);
1663 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1664 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1665 static void sd_read_modify_write_task(void * arg);
1666 static int
1667 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1668     struct buf **bpp);
1669 
1670 
1671 /*
1672  * Function prototypes for failfast support.
1673  */
1674 static void sd_failfast_flushq(struct sd_lun *un);
1675 static int sd_failfast_flushq_callback(struct buf *bp);
1676 
1677 /*
1678  * Function prototypes to check for lsi devices
1679  */
1680 static void sd_is_lsi(struct sd_lun *un);
1681 
1682 /*
1683  * Function prototypes for partial DMA support
1684  */
1685 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1686 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1687 
1688 
1689 /* Function prototypes for cmlb */
1690 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1691     diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1692 
1693 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1694 
1695 /*
1696  * For printing RMW warning message timely
1697  */
1698 static void sd_rmw_msg_print_handler(void *arg);
1699 
1700 /*
1701  * Constants for failfast support:
1702  *
1703  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1704  * failfast processing being performed.
1705  *
1706  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1707  * failfast processing on all bufs with B_FAILFAST set.
1708  */
1709 
1710 #define	SD_FAILFAST_INACTIVE		0
1711 #define	SD_FAILFAST_ACTIVE		1
1712 
1713 /*
1714  * Bitmask to control behavior of buf(9S) flushes when a transition to
1715  * the failfast state occurs. Optional bits include:
1716  *
1717  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1718  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1719  * be flushed.
1720  *
1721  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1722  * driver, in addition to the regular wait queue. This includes the xbuf
1723  * queues. When clear, only the driver's wait queue will be flushed.
1724  */
1725 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1726 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1727 
1728 /*
1729  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1730  * to flush all queues within the driver.
1731  */
1732 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1733 
1734 
1735 /*
1736  * SD Testing Fault Injection
1737  */
1738 #ifdef SD_FAULT_INJECTION
1739 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1740 static void sd_faultinjection(struct scsi_pkt *pktp);
1741 static void sd_injection_log(char *buf, struct sd_lun *un);
1742 #endif
1743 
1744 /*
1745  * Device driver ops vector
1746  */
1747 static struct cb_ops sd_cb_ops = {
1748 	sdopen,			/* open */
1749 	sdclose,		/* close */
1750 	sdstrategy,		/* strategy */
1751 	nodev,			/* print */
1752 	sddump,			/* dump */
1753 	sdread,			/* read */
1754 	sdwrite,		/* write */
1755 	sdioctl,		/* ioctl */
1756 	nodev,			/* devmap */
1757 	nodev,			/* mmap */
1758 	nodev,			/* segmap */
1759 	nochpoll,		/* poll */
1760 	sd_prop_op,		/* cb_prop_op */
1761 	0,			/* streamtab  */
1762 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1763 	CB_REV,			/* cb_rev */
1764 	sdaread, 		/* async I/O read entry point */
1765 	sdawrite		/* async I/O write entry point */
1766 };
1767 
1768 struct dev_ops sd_ops = {
1769 	DEVO_REV,		/* devo_rev, */
1770 	0,			/* refcnt  */
1771 	sdinfo,			/* info */
1772 	nulldev,		/* identify */
1773 	sdprobe,		/* probe */
1774 	sdattach,		/* attach */
1775 	sddetach,		/* detach */
1776 	nodev,			/* reset */
1777 	&sd_cb_ops,		/* driver operations */
1778 	NULL,			/* bus operations */
1779 	sdpower,		/* power */
1780 	ddi_quiesce_not_needed,		/* quiesce */
1781 };
1782 
1783 /*
1784  * This is the loadable module wrapper.
1785  */
1786 #include <sys/modctl.h>
1787 
1788 static struct modldrv modldrv = {
1789 	&mod_driverops,		/* Type of module. This one is a driver */
1790 	SD_MODULE_NAME,		/* Module name. */
1791 	&sd_ops			/* driver ops */
1792 };
1793 
1794 static struct modlinkage modlinkage = {
1795 	MODREV_1, &modldrv, NULL
1796 };
1797 
1798 static cmlb_tg_ops_t sd_tgops = {
1799 	TG_DK_OPS_VERSION_1,
1800 	sd_tg_rdwr,
1801 	sd_tg_getinfo
1802 };
1803 
1804 static struct scsi_asq_key_strings sd_additional_codes[] = {
1805 	0x81, 0, "Logical Unit is Reserved",
1806 	0x85, 0, "Audio Address Not Valid",
1807 	0xb6, 0, "Media Load Mechanism Failed",
1808 	0xB9, 0, "Audio Play Operation Aborted",
1809 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1810 	0x53, 2, "Medium removal prevented",
1811 	0x6f, 0, "Authentication failed during key exchange",
1812 	0x6f, 1, "Key not present",
1813 	0x6f, 2, "Key not established",
1814 	0x6f, 3, "Read without proper authentication",
1815 	0x6f, 4, "Mismatched region to this logical unit",
1816 	0x6f, 5, "Region reset count error",
1817 	0xffff, 0x0, NULL
1818 };
1819 
1820 
1821 /*
1822  * Struct for passing printing information for sense data messages
1823  */
1824 struct sd_sense_info {
1825 	int	ssi_severity;
1826 	int	ssi_pfa_flag;
1827 };
1828 
1829 /*
1830  * Table of function pointers for iostart-side routines. Separate "chains"
1831  * of layered function calls are formed by placing the function pointers
1832  * sequentially in the desired order. Functions are called according to an
1833  * incrementing table index ordering. The last function in each chain must
1834  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1835  * in the sd_iodone_chain[] array.
1836  *
1837  * Note: It may seem more natural to organize both the iostart and iodone
1838  * functions together, into an array of structures (or some similar
1839  * organization) with a common index, rather than two separate arrays which
1840  * must be maintained in synchronization. The purpose of this division is
1841  * to achieve improved performance: individual arrays allows for more
1842  * effective cache line utilization on certain platforms.
1843  */
1844 
1845 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1846 
1847 
1848 static sd_chain_t sd_iostart_chain[] = {
1849 
1850 	/* Chain for buf IO for disk drive targets (PM enabled) */
1851 	sd_mapblockaddr_iostart,	/* Index: 0 */
1852 	sd_pm_iostart,			/* Index: 1 */
1853 	sd_core_iostart,		/* Index: 2 */
1854 
1855 	/* Chain for buf IO for disk drive targets (PM disabled) */
1856 	sd_mapblockaddr_iostart,	/* Index: 3 */
1857 	sd_core_iostart,		/* Index: 4 */
1858 
1859 	/*
1860 	 * Chain for buf IO for removable-media or large sector size
1861 	 * disk drive targets with RMW needed (PM enabled)
1862 	 */
1863 	sd_mapblockaddr_iostart,	/* Index: 5 */
1864 	sd_mapblocksize_iostart,	/* Index: 6 */
1865 	sd_pm_iostart,			/* Index: 7 */
1866 	sd_core_iostart,		/* Index: 8 */
1867 
1868 	/*
1869 	 * Chain for buf IO for removable-media or large sector size
1870 	 * disk drive targets with RMW needed (PM disabled)
1871 	 */
1872 	sd_mapblockaddr_iostart,	/* Index: 9 */
1873 	sd_mapblocksize_iostart,	/* Index: 10 */
1874 	sd_core_iostart,		/* Index: 11 */
1875 
1876 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1877 	sd_mapblockaddr_iostart,	/* Index: 12 */
1878 	sd_checksum_iostart,		/* Index: 13 */
1879 	sd_pm_iostart,			/* Index: 14 */
1880 	sd_core_iostart,		/* Index: 15 */
1881 
1882 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1883 	sd_mapblockaddr_iostart,	/* Index: 16 */
1884 	sd_checksum_iostart,		/* Index: 17 */
1885 	sd_core_iostart,		/* Index: 18 */
1886 
1887 	/* Chain for USCSI commands (all targets) */
1888 	sd_pm_iostart,			/* Index: 19 */
1889 	sd_core_iostart,		/* Index: 20 */
1890 
1891 	/* Chain for checksumming USCSI commands (all targets) */
1892 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1893 	sd_pm_iostart,			/* Index: 22 */
1894 	sd_core_iostart,		/* Index: 23 */
1895 
1896 	/* Chain for "direct" USCSI commands (all targets) */
1897 	sd_core_iostart,		/* Index: 24 */
1898 
1899 	/* Chain for "direct priority" USCSI commands (all targets) */
1900 	sd_core_iostart,		/* Index: 25 */
1901 
1902 	/*
1903 	 * Chain for buf IO for large sector size disk drive targets
1904 	 * with RMW needed with checksumming (PM enabled)
1905 	 */
1906 	sd_mapblockaddr_iostart,	/* Index: 26 */
1907 	sd_mapblocksize_iostart,	/* Index: 27 */
1908 	sd_checksum_iostart,		/* Index: 28 */
1909 	sd_pm_iostart,			/* Index: 29 */
1910 	sd_core_iostart,		/* Index: 30 */
1911 
1912 	/*
1913 	 * Chain for buf IO for large sector size disk drive targets
1914 	 * with RMW needed with checksumming (PM disabled)
1915 	 */
1916 	sd_mapblockaddr_iostart,	/* Index: 31 */
1917 	sd_mapblocksize_iostart,	/* Index: 32 */
1918 	sd_checksum_iostart,		/* Index: 33 */
1919 	sd_core_iostart,		/* Index: 34 */
1920 
1921 };
1922 
1923 /*
1924  * Macros to locate the first function of each iostart chain in the
1925  * sd_iostart_chain[] array. These are located by the index in the array.
1926  */
1927 #define	SD_CHAIN_DISK_IOSTART			0
1928 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1929 #define	SD_CHAIN_MSS_DISK_IOSTART		5
1930 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1931 #define	SD_CHAIN_MSS_DISK_IOSTART_NO_PM		9
1932 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1933 #define	SD_CHAIN_CHKSUM_IOSTART			12
1934 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1935 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1936 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1937 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1938 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1939 #define	SD_CHAIN_MSS_CHKSUM_IOSTART		26
1940 #define	SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM	31
1941 
1942 
1943 /*
1944  * Table of function pointers for the iodone-side routines for the driver-
1945  * internal layering mechanism.  The calling sequence for iodone routines
1946  * uses a decrementing table index, so the last routine called in a chain
1947  * must be at the lowest array index location for that chain.  The last
1948  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1949  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1950  * of the functions in an iodone side chain must correspond to the ordering
1951  * of the iostart routines for that chain.  Note that there is no iodone
1952  * side routine that corresponds to sd_core_iostart(), so there is no
1953  * entry in the table for this.
1954  */
1955 
1956 static sd_chain_t sd_iodone_chain[] = {
1957 
1958 	/* Chain for buf IO for disk drive targets (PM enabled) */
1959 	sd_buf_iodone,			/* Index: 0 */
1960 	sd_mapblockaddr_iodone,		/* Index: 1 */
1961 	sd_pm_iodone,			/* Index: 2 */
1962 
1963 	/* Chain for buf IO for disk drive targets (PM disabled) */
1964 	sd_buf_iodone,			/* Index: 3 */
1965 	sd_mapblockaddr_iodone,		/* Index: 4 */
1966 
1967 	/*
1968 	 * Chain for buf IO for removable-media or large sector size
1969 	 * disk drive targets with RMW needed (PM enabled)
1970 	 */
1971 	sd_buf_iodone,			/* Index: 5 */
1972 	sd_mapblockaddr_iodone,		/* Index: 6 */
1973 	sd_mapblocksize_iodone,		/* Index: 7 */
1974 	sd_pm_iodone,			/* Index: 8 */
1975 
1976 	/*
1977 	 * Chain for buf IO for removable-media or large sector size
1978 	 * disk drive targets with RMW needed (PM disabled)
1979 	 */
1980 	sd_buf_iodone,			/* Index: 9 */
1981 	sd_mapblockaddr_iodone,		/* Index: 10 */
1982 	sd_mapblocksize_iodone,		/* Index: 11 */
1983 
1984 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1985 	sd_buf_iodone,			/* Index: 12 */
1986 	sd_mapblockaddr_iodone,		/* Index: 13 */
1987 	sd_checksum_iodone,		/* Index: 14 */
1988 	sd_pm_iodone,			/* Index: 15 */
1989 
1990 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1991 	sd_buf_iodone,			/* Index: 16 */
1992 	sd_mapblockaddr_iodone,		/* Index: 17 */
1993 	sd_checksum_iodone,		/* Index: 18 */
1994 
1995 	/* Chain for USCSI commands (non-checksum targets) */
1996 	sd_uscsi_iodone,		/* Index: 19 */
1997 	sd_pm_iodone,			/* Index: 20 */
1998 
1999 	/* Chain for USCSI commands (checksum targets) */
2000 	sd_uscsi_iodone,		/* Index: 21 */
2001 	sd_checksum_uscsi_iodone,	/* Index: 22 */
2002 	sd_pm_iodone,			/* Index: 22 */
2003 
2004 	/* Chain for "direct" USCSI commands (all targets) */
2005 	sd_uscsi_iodone,		/* Index: 24 */
2006 
2007 	/* Chain for "direct priority" USCSI commands (all targets) */
2008 	sd_uscsi_iodone,		/* Index: 25 */
2009 
2010 	/*
2011 	 * Chain for buf IO for large sector size disk drive targets
2012 	 * with checksumming (PM enabled)
2013 	 */
2014 	sd_buf_iodone,			/* Index: 26 */
2015 	sd_mapblockaddr_iodone,		/* Index: 27 */
2016 	sd_mapblocksize_iodone,		/* Index: 28 */
2017 	sd_checksum_iodone,		/* Index: 29 */
2018 	sd_pm_iodone,			/* Index: 30 */
2019 
2020 	/*
2021 	 * Chain for buf IO for large sector size disk drive targets
2022 	 * with checksumming (PM disabled)
2023 	 */
2024 	sd_buf_iodone,			/* Index: 31 */
2025 	sd_mapblockaddr_iodone,		/* Index: 32 */
2026 	sd_mapblocksize_iodone,		/* Index: 33 */
2027 	sd_checksum_iodone,		/* Index: 34 */
2028 };
2029 
2030 
2031 /*
2032  * Macros to locate the "first" function in the sd_iodone_chain[] array for
2033  * each iodone-side chain. These are located by the array index, but as the
2034  * iodone side functions are called in a decrementing-index order, the
2035  * highest index number in each chain must be specified (as these correspond
2036  * to the first function in the iodone chain that will be called by the core
2037  * at IO completion time).
2038  */
2039 
2040 #define	SD_CHAIN_DISK_IODONE			2
2041 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
2042 #define	SD_CHAIN_RMMEDIA_IODONE			8
2043 #define	SD_CHAIN_MSS_DISK_IODONE		8
2044 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
2045 #define	SD_CHAIN_MSS_DISK_IODONE_NO_PM		11
2046 #define	SD_CHAIN_CHKSUM_IODONE			15
2047 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
2048 #define	SD_CHAIN_USCSI_CMD_IODONE		20
2049 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
2050 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
2051 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
2052 #define	SD_CHAIN_MSS_CHKSUM_IODONE		30
2053 #define	SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM	34
2054 
2055 
2056 
2057 /*
2058  * Array to map a layering chain index to the appropriate initpkt routine.
2059  * The redundant entries are present so that the index used for accessing
2060  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2061  * with this table as well.
2062  */
2063 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2064 
2065 static sd_initpkt_t	sd_initpkt_map[] = {
2066 
2067 	/* Chain for buf IO for disk drive targets (PM enabled) */
2068 	sd_initpkt_for_buf,		/* Index: 0 */
2069 	sd_initpkt_for_buf,		/* Index: 1 */
2070 	sd_initpkt_for_buf,		/* Index: 2 */
2071 
2072 	/* Chain for buf IO for disk drive targets (PM disabled) */
2073 	sd_initpkt_for_buf,		/* Index: 3 */
2074 	sd_initpkt_for_buf,		/* Index: 4 */
2075 
2076 	/*
2077 	 * Chain for buf IO for removable-media or large sector size
2078 	 * disk drive targets (PM enabled)
2079 	 */
2080 	sd_initpkt_for_buf,		/* Index: 5 */
2081 	sd_initpkt_for_buf,		/* Index: 6 */
2082 	sd_initpkt_for_buf,		/* Index: 7 */
2083 	sd_initpkt_for_buf,		/* Index: 8 */
2084 
2085 	/*
2086 	 * Chain for buf IO for removable-media or large sector size
2087 	 * disk drive targets (PM disabled)
2088 	 */
2089 	sd_initpkt_for_buf,		/* Index: 9 */
2090 	sd_initpkt_for_buf,		/* Index: 10 */
2091 	sd_initpkt_for_buf,		/* Index: 11 */
2092 
2093 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2094 	sd_initpkt_for_buf,		/* Index: 12 */
2095 	sd_initpkt_for_buf,		/* Index: 13 */
2096 	sd_initpkt_for_buf,		/* Index: 14 */
2097 	sd_initpkt_for_buf,		/* Index: 15 */
2098 
2099 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2100 	sd_initpkt_for_buf,		/* Index: 16 */
2101 	sd_initpkt_for_buf,		/* Index: 17 */
2102 	sd_initpkt_for_buf,		/* Index: 18 */
2103 
2104 	/* Chain for USCSI commands (non-checksum targets) */
2105 	sd_initpkt_for_uscsi,		/* Index: 19 */
2106 	sd_initpkt_for_uscsi,		/* Index: 20 */
2107 
2108 	/* Chain for USCSI commands (checksum targets) */
2109 	sd_initpkt_for_uscsi,		/* Index: 21 */
2110 	sd_initpkt_for_uscsi,		/* Index: 22 */
2111 	sd_initpkt_for_uscsi,		/* Index: 22 */
2112 
2113 	/* Chain for "direct" USCSI commands (all targets) */
2114 	sd_initpkt_for_uscsi,		/* Index: 24 */
2115 
2116 	/* Chain for "direct priority" USCSI commands (all targets) */
2117 	sd_initpkt_for_uscsi,		/* Index: 25 */
2118 
2119 	/*
2120 	 * Chain for buf IO for large sector size disk drive targets
2121 	 * with checksumming (PM enabled)
2122 	 */
2123 	sd_initpkt_for_buf,		/* Index: 26 */
2124 	sd_initpkt_for_buf,		/* Index: 27 */
2125 	sd_initpkt_for_buf,		/* Index: 28 */
2126 	sd_initpkt_for_buf,		/* Index: 29 */
2127 	sd_initpkt_for_buf,		/* Index: 30 */
2128 
2129 	/*
2130 	 * Chain for buf IO for large sector size disk drive targets
2131 	 * with checksumming (PM disabled)
2132 	 */
2133 	sd_initpkt_for_buf,		/* Index: 31 */
2134 	sd_initpkt_for_buf,		/* Index: 32 */
2135 	sd_initpkt_for_buf,		/* Index: 33 */
2136 	sd_initpkt_for_buf,		/* Index: 34 */
2137 };
2138 
2139 
2140 /*
2141  * Array to map a layering chain index to the appropriate destroypktpkt routine.
2142  * The redundant entries are present so that the index used for accessing
2143  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2144  * with this table as well.
2145  */
2146 typedef void (*sd_destroypkt_t)(struct buf *);
2147 
2148 static sd_destroypkt_t	sd_destroypkt_map[] = {
2149 
2150 	/* Chain for buf IO for disk drive targets (PM enabled) */
2151 	sd_destroypkt_for_buf,		/* Index: 0 */
2152 	sd_destroypkt_for_buf,		/* Index: 1 */
2153 	sd_destroypkt_for_buf,		/* Index: 2 */
2154 
2155 	/* Chain for buf IO for disk drive targets (PM disabled) */
2156 	sd_destroypkt_for_buf,		/* Index: 3 */
2157 	sd_destroypkt_for_buf,		/* Index: 4 */
2158 
2159 	/*
2160 	 * Chain for buf IO for removable-media or large sector size
2161 	 * disk drive targets (PM enabled)
2162 	 */
2163 	sd_destroypkt_for_buf,		/* Index: 5 */
2164 	sd_destroypkt_for_buf,		/* Index: 6 */
2165 	sd_destroypkt_for_buf,		/* Index: 7 */
2166 	sd_destroypkt_for_buf,		/* Index: 8 */
2167 
2168 	/*
2169 	 * Chain for buf IO for removable-media or large sector size
2170 	 * disk drive targets (PM disabled)
2171 	 */
2172 	sd_destroypkt_for_buf,		/* Index: 9 */
2173 	sd_destroypkt_for_buf,		/* Index: 10 */
2174 	sd_destroypkt_for_buf,		/* Index: 11 */
2175 
2176 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2177 	sd_destroypkt_for_buf,		/* Index: 12 */
2178 	sd_destroypkt_for_buf,		/* Index: 13 */
2179 	sd_destroypkt_for_buf,		/* Index: 14 */
2180 	sd_destroypkt_for_buf,		/* Index: 15 */
2181 
2182 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2183 	sd_destroypkt_for_buf,		/* Index: 16 */
2184 	sd_destroypkt_for_buf,		/* Index: 17 */
2185 	sd_destroypkt_for_buf,		/* Index: 18 */
2186 
2187 	/* Chain for USCSI commands (non-checksum targets) */
2188 	sd_destroypkt_for_uscsi,	/* Index: 19 */
2189 	sd_destroypkt_for_uscsi,	/* Index: 20 */
2190 
2191 	/* Chain for USCSI commands (checksum targets) */
2192 	sd_destroypkt_for_uscsi,	/* Index: 21 */
2193 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2194 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2195 
2196 	/* Chain for "direct" USCSI commands (all targets) */
2197 	sd_destroypkt_for_uscsi,	/* Index: 24 */
2198 
2199 	/* Chain for "direct priority" USCSI commands (all targets) */
2200 	sd_destroypkt_for_uscsi,	/* Index: 25 */
2201 
2202 	/*
2203 	 * Chain for buf IO for large sector size disk drive targets
2204 	 * with checksumming (PM disabled)
2205 	 */
2206 	sd_destroypkt_for_buf,		/* Index: 26 */
2207 	sd_destroypkt_for_buf,		/* Index: 27 */
2208 	sd_destroypkt_for_buf,		/* Index: 28 */
2209 	sd_destroypkt_for_buf,		/* Index: 29 */
2210 	sd_destroypkt_for_buf,		/* Index: 30 */
2211 
2212 	/*
2213 	 * Chain for buf IO for large sector size disk drive targets
2214 	 * with checksumming (PM enabled)
2215 	 */
2216 	sd_destroypkt_for_buf,		/* Index: 31 */
2217 	sd_destroypkt_for_buf,		/* Index: 32 */
2218 	sd_destroypkt_for_buf,		/* Index: 33 */
2219 	sd_destroypkt_for_buf,		/* Index: 34 */
2220 };
2221 
2222 
2223 
2224 /*
2225  * Array to map a layering chain index to the appropriate chain "type".
2226  * The chain type indicates a specific property/usage of the chain.
2227  * The redundant entries are present so that the index used for accessing
2228  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2229  * with this table as well.
2230  */
2231 
2232 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2233 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2234 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2235 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2236 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2237 						/* (for error recovery) */
2238 
2239 static int sd_chain_type_map[] = {
2240 
2241 	/* Chain for buf IO for disk drive targets (PM enabled) */
2242 	SD_CHAIN_BUFIO,			/* Index: 0 */
2243 	SD_CHAIN_BUFIO,			/* Index: 1 */
2244 	SD_CHAIN_BUFIO,			/* Index: 2 */
2245 
2246 	/* Chain for buf IO for disk drive targets (PM disabled) */
2247 	SD_CHAIN_BUFIO,			/* Index: 3 */
2248 	SD_CHAIN_BUFIO,			/* Index: 4 */
2249 
2250 	/*
2251 	 * Chain for buf IO for removable-media or large sector size
2252 	 * disk drive targets (PM enabled)
2253 	 */
2254 	SD_CHAIN_BUFIO,			/* Index: 5 */
2255 	SD_CHAIN_BUFIO,			/* Index: 6 */
2256 	SD_CHAIN_BUFIO,			/* Index: 7 */
2257 	SD_CHAIN_BUFIO,			/* Index: 8 */
2258 
2259 	/*
2260 	 * Chain for buf IO for removable-media or large sector size
2261 	 * disk drive targets (PM disabled)
2262 	 */
2263 	SD_CHAIN_BUFIO,			/* Index: 9 */
2264 	SD_CHAIN_BUFIO,			/* Index: 10 */
2265 	SD_CHAIN_BUFIO,			/* Index: 11 */
2266 
2267 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2268 	SD_CHAIN_BUFIO,			/* Index: 12 */
2269 	SD_CHAIN_BUFIO,			/* Index: 13 */
2270 	SD_CHAIN_BUFIO,			/* Index: 14 */
2271 	SD_CHAIN_BUFIO,			/* Index: 15 */
2272 
2273 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2274 	SD_CHAIN_BUFIO,			/* Index: 16 */
2275 	SD_CHAIN_BUFIO,			/* Index: 17 */
2276 	SD_CHAIN_BUFIO,			/* Index: 18 */
2277 
2278 	/* Chain for USCSI commands (non-checksum targets) */
2279 	SD_CHAIN_USCSI,			/* Index: 19 */
2280 	SD_CHAIN_USCSI,			/* Index: 20 */
2281 
2282 	/* Chain for USCSI commands (checksum targets) */
2283 	SD_CHAIN_USCSI,			/* Index: 21 */
2284 	SD_CHAIN_USCSI,			/* Index: 22 */
2285 	SD_CHAIN_USCSI,			/* Index: 23 */
2286 
2287 	/* Chain for "direct" USCSI commands (all targets) */
2288 	SD_CHAIN_DIRECT,		/* Index: 24 */
2289 
2290 	/* Chain for "direct priority" USCSI commands (all targets) */
2291 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2292 
2293 	/*
2294 	 * Chain for buf IO for large sector size disk drive targets
2295 	 * with checksumming (PM enabled)
2296 	 */
2297 	SD_CHAIN_BUFIO,			/* Index: 26 */
2298 	SD_CHAIN_BUFIO,			/* Index: 27 */
2299 	SD_CHAIN_BUFIO,			/* Index: 28 */
2300 	SD_CHAIN_BUFIO,			/* Index: 29 */
2301 	SD_CHAIN_BUFIO,			/* Index: 30 */
2302 
2303 	/*
2304 	 * Chain for buf IO for large sector size disk drive targets
2305 	 * with checksumming (PM disabled)
2306 	 */
2307 	SD_CHAIN_BUFIO,			/* Index: 31 */
2308 	SD_CHAIN_BUFIO,			/* Index: 32 */
2309 	SD_CHAIN_BUFIO,			/* Index: 33 */
2310 	SD_CHAIN_BUFIO,			/* Index: 34 */
2311 };
2312 
2313 
2314 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2315 #define	SD_IS_BUFIO(xp)			\
2316 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2317 
2318 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2319 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2320 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2321 
2322 
2323 
2324 /*
2325  * Struct, array, and macros to map a specific chain to the appropriate
2326  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2327  *
2328  * The sd_chain_index_map[] array is used at attach time to set the various
2329  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2330  * chain to be used with the instance. This allows different instances to use
2331  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2332  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2333  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2334  * dynamically & without the use of locking; and (2) a layer may update the
2335  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2336  * to allow for deferred processing of an IO within the same chain from a
2337  * different execution context.
2338  */
2339 
2340 struct sd_chain_index {
2341 	int	sci_iostart_index;
2342 	int	sci_iodone_index;
2343 };
2344 
2345 static struct sd_chain_index	sd_chain_index_map[] = {
2346 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2347 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2348 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2349 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2350 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2351 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2352 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2353 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2354 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2355 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2356 	{ SD_CHAIN_MSS_CHKSUM_IOSTART,		SD_CHAIN_MSS_CHKSUM_IODONE },
2357 	{ SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2358 
2359 };
2360 
2361 
2362 /*
2363  * The following are indexes into the sd_chain_index_map[] array.
2364  */
2365 
2366 /* un->un_buf_chain_type must be set to one of these */
2367 #define	SD_CHAIN_INFO_DISK		0
2368 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2369 #define	SD_CHAIN_INFO_RMMEDIA		2
2370 #define	SD_CHAIN_INFO_MSS_DISK		2
2371 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2372 #define	SD_CHAIN_INFO_MSS_DSK_NO_PM	3
2373 #define	SD_CHAIN_INFO_CHKSUM		4
2374 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2375 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM	10
2376 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM	11
2377 
2378 /* un->un_uscsi_chain_type must be set to one of these */
2379 #define	SD_CHAIN_INFO_USCSI_CMD		6
2380 /* USCSI with PM disabled is the same as DIRECT */
2381 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2382 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2383 
2384 /* un->un_direct_chain_type must be set to one of these */
2385 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2386 
2387 /* un->un_priority_chain_type must be set to one of these */
2388 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2389 
2390 /* size for devid inquiries */
2391 #define	MAX_INQUIRY_SIZE		0xF0
2392 
2393 /*
2394  * Macros used by functions to pass a given buf(9S) struct along to the
2395  * next function in the layering chain for further processing.
2396  *
2397  * In the following macros, passing more than three arguments to the called
2398  * routines causes the optimizer for the SPARC compiler to stop doing tail
2399  * call elimination which results in significant performance degradation.
2400  */
2401 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2402 	((*(sd_iostart_chain[index]))(index, un, bp))
2403 
2404 #define	SD_BEGIN_IODONE(index, un, bp)	\
2405 	((*(sd_iodone_chain[index]))(index, un, bp))
2406 
2407 #define	SD_NEXT_IOSTART(index, un, bp)				\
2408 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2409 
2410 #define	SD_NEXT_IODONE(index, un, bp)				\
2411 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2412 
2413 /*
2414  *    Function: _init
2415  *
2416  * Description: This is the driver _init(9E) entry point.
2417  *
2418  * Return Code: Returns the value from mod_install(9F) or
2419  *		ddi_soft_state_init(9F) as appropriate.
2420  *
2421  *     Context: Called when driver module loaded.
2422  */
2423 
2424 int
2425 _init(void)
2426 {
2427 	int	err;
2428 
2429 	/* establish driver name from module name */
2430 	sd_label = (char *)mod_modname(&modlinkage);
2431 
2432 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2433 	    SD_MAXUNIT);
2434 	if (err != 0) {
2435 		return (err);
2436 	}
2437 
2438 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2439 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2440 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2441 
2442 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2443 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2444 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2445 
2446 	/*
2447 	 * it's ok to init here even for fibre device
2448 	 */
2449 	sd_scsi_probe_cache_init();
2450 
2451 	sd_scsi_target_lun_init();
2452 
2453 	/*
2454 	 * Creating taskq before mod_install ensures that all callers (threads)
2455 	 * that enter the module after a successful mod_install encounter
2456 	 * a valid taskq.
2457 	 */
2458 	sd_taskq_create();
2459 
2460 	err = mod_install(&modlinkage);
2461 	if (err != 0) {
2462 		/* delete taskq if install fails */
2463 		sd_taskq_delete();
2464 
2465 		mutex_destroy(&sd_detach_mutex);
2466 		mutex_destroy(&sd_log_mutex);
2467 		mutex_destroy(&sd_label_mutex);
2468 
2469 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2470 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2471 		cv_destroy(&sd_tr.srq_inprocess_cv);
2472 
2473 		sd_scsi_probe_cache_fini();
2474 
2475 		sd_scsi_target_lun_fini();
2476 
2477 		ddi_soft_state_fini(&sd_state);
2478 
2479 		return (err);
2480 	}
2481 
2482 	return (err);
2483 }
2484 
2485 
2486 /*
2487  *    Function: _fini
2488  *
2489  * Description: This is the driver _fini(9E) entry point.
2490  *
2491  * Return Code: Returns the value from mod_remove(9F)
2492  *
2493  *     Context: Called when driver module is unloaded.
2494  */
2495 
2496 int
2497 _fini(void)
2498 {
2499 	int err;
2500 
2501 	if ((err = mod_remove(&modlinkage)) != 0) {
2502 		return (err);
2503 	}
2504 
2505 	sd_taskq_delete();
2506 
2507 	mutex_destroy(&sd_detach_mutex);
2508 	mutex_destroy(&sd_log_mutex);
2509 	mutex_destroy(&sd_label_mutex);
2510 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2511 
2512 	sd_scsi_probe_cache_fini();
2513 
2514 	sd_scsi_target_lun_fini();
2515 
2516 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2517 	cv_destroy(&sd_tr.srq_inprocess_cv);
2518 
2519 	ddi_soft_state_fini(&sd_state);
2520 
2521 	return (err);
2522 }
2523 
2524 
2525 /*
2526  *    Function: _info
2527  *
2528  * Description: This is the driver _info(9E) entry point.
2529  *
2530  *   Arguments: modinfop - pointer to the driver modinfo structure
2531  *
2532  * Return Code: Returns the value from mod_info(9F).
2533  *
2534  *     Context: Kernel thread context
2535  */
2536 
2537 int
2538 _info(struct modinfo *modinfop)
2539 {
2540 	return (mod_info(&modlinkage, modinfop));
2541 }
2542 
2543 
2544 /*
2545  * The following routines implement the driver message logging facility.
2546  * They provide component- and level- based debug output filtering.
2547  * Output may also be restricted to messages for a single instance by
2548  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2549  * to NULL, then messages for all instances are printed.
2550  *
2551  * These routines have been cloned from each other due to the language
2552  * constraints of macros and variable argument list processing.
2553  */
2554 
2555 
2556 /*
2557  *    Function: sd_log_err
2558  *
2559  * Description: This routine is called by the SD_ERROR macro for debug
2560  *		logging of error conditions.
2561  *
2562  *   Arguments: comp - driver component being logged
2563  *		dev  - pointer to driver info structure
2564  *		fmt  - error string and format to be logged
2565  */
2566 
2567 static void
2568 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2569 {
2570 	va_list		ap;
2571 	dev_info_t	*dev;
2572 
2573 	ASSERT(un != NULL);
2574 	dev = SD_DEVINFO(un);
2575 	ASSERT(dev != NULL);
2576 
2577 	/*
2578 	 * Filter messages based on the global component and level masks.
2579 	 * Also print if un matches the value of sd_debug_un, or if
2580 	 * sd_debug_un is set to NULL.
2581 	 */
2582 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2583 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2584 		mutex_enter(&sd_log_mutex);
2585 		va_start(ap, fmt);
2586 		(void) vsprintf(sd_log_buf, fmt, ap);
2587 		va_end(ap);
2588 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2589 		mutex_exit(&sd_log_mutex);
2590 	}
2591 #ifdef SD_FAULT_INJECTION
2592 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2593 	if (un->sd_injection_mask & comp) {
2594 		mutex_enter(&sd_log_mutex);
2595 		va_start(ap, fmt);
2596 		(void) vsprintf(sd_log_buf, fmt, ap);
2597 		va_end(ap);
2598 		sd_injection_log(sd_log_buf, un);
2599 		mutex_exit(&sd_log_mutex);
2600 	}
2601 #endif
2602 }
2603 
2604 
2605 /*
2606  *    Function: sd_log_info
2607  *
2608  * Description: This routine is called by the SD_INFO macro for debug
2609  *		logging of general purpose informational conditions.
2610  *
2611  *   Arguments: comp - driver component being logged
2612  *		dev  - pointer to driver info structure
2613  *		fmt  - info string and format to be logged
2614  */
2615 
2616 static void
2617 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2618 {
2619 	va_list		ap;
2620 	dev_info_t	*dev;
2621 
2622 	ASSERT(un != NULL);
2623 	dev = SD_DEVINFO(un);
2624 	ASSERT(dev != NULL);
2625 
2626 	/*
2627 	 * Filter messages based on the global component and level masks.
2628 	 * Also print if un matches the value of sd_debug_un, or if
2629 	 * sd_debug_un is set to NULL.
2630 	 */
2631 	if ((sd_component_mask & component) &&
2632 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2633 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2634 		mutex_enter(&sd_log_mutex);
2635 		va_start(ap, fmt);
2636 		(void) vsprintf(sd_log_buf, fmt, ap);
2637 		va_end(ap);
2638 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2639 		mutex_exit(&sd_log_mutex);
2640 	}
2641 #ifdef SD_FAULT_INJECTION
2642 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2643 	if (un->sd_injection_mask & component) {
2644 		mutex_enter(&sd_log_mutex);
2645 		va_start(ap, fmt);
2646 		(void) vsprintf(sd_log_buf, fmt, ap);
2647 		va_end(ap);
2648 		sd_injection_log(sd_log_buf, un);
2649 		mutex_exit(&sd_log_mutex);
2650 	}
2651 #endif
2652 }
2653 
2654 
2655 /*
2656  *    Function: sd_log_trace
2657  *
2658  * Description: This routine is called by the SD_TRACE macro for debug
2659  *		logging of trace conditions (i.e. function entry/exit).
2660  *
2661  *   Arguments: comp - driver component being logged
2662  *		dev  - pointer to driver info structure
2663  *		fmt  - trace string and format to be logged
2664  */
2665 
2666 static void
2667 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2668 {
2669 	va_list		ap;
2670 	dev_info_t	*dev;
2671 
2672 	ASSERT(un != NULL);
2673 	dev = SD_DEVINFO(un);
2674 	ASSERT(dev != NULL);
2675 
2676 	/*
2677 	 * Filter messages based on the global component and level masks.
2678 	 * Also print if un matches the value of sd_debug_un, or if
2679 	 * sd_debug_un is set to NULL.
2680 	 */
2681 	if ((sd_component_mask & component) &&
2682 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2683 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2684 		mutex_enter(&sd_log_mutex);
2685 		va_start(ap, fmt);
2686 		(void) vsprintf(sd_log_buf, fmt, ap);
2687 		va_end(ap);
2688 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2689 		mutex_exit(&sd_log_mutex);
2690 	}
2691 #ifdef SD_FAULT_INJECTION
2692 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2693 	if (un->sd_injection_mask & component) {
2694 		mutex_enter(&sd_log_mutex);
2695 		va_start(ap, fmt);
2696 		(void) vsprintf(sd_log_buf, fmt, ap);
2697 		va_end(ap);
2698 		sd_injection_log(sd_log_buf, un);
2699 		mutex_exit(&sd_log_mutex);
2700 	}
2701 #endif
2702 }
2703 
2704 
2705 /*
2706  *    Function: sdprobe
2707  *
2708  * Description: This is the driver probe(9e) entry point function.
2709  *
2710  *   Arguments: devi - opaque device info handle
2711  *
2712  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2713  *              DDI_PROBE_FAILURE: If the probe failed.
2714  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2715  *				   but may be present in the future.
2716  */
2717 
2718 static int
2719 sdprobe(dev_info_t *devi)
2720 {
2721 	struct scsi_device	*devp;
2722 	int			rval;
2723 	int			instance = ddi_get_instance(devi);
2724 
2725 	/*
2726 	 * if it wasn't for pln, sdprobe could actually be nulldev
2727 	 * in the "__fibre" case.
2728 	 */
2729 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2730 		return (DDI_PROBE_DONTCARE);
2731 	}
2732 
2733 	devp = ddi_get_driver_private(devi);
2734 
2735 	if (devp == NULL) {
2736 		/* Ooops... nexus driver is mis-configured... */
2737 		return (DDI_PROBE_FAILURE);
2738 	}
2739 
2740 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2741 		return (DDI_PROBE_PARTIAL);
2742 	}
2743 
2744 	/*
2745 	 * Call the SCSA utility probe routine to see if we actually
2746 	 * have a target at this SCSI nexus.
2747 	 */
2748 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2749 	case SCSIPROBE_EXISTS:
2750 		switch (devp->sd_inq->inq_dtype) {
2751 		case DTYPE_DIRECT:
2752 			rval = DDI_PROBE_SUCCESS;
2753 			break;
2754 		case DTYPE_RODIRECT:
2755 			/* CDs etc. Can be removable media */
2756 			rval = DDI_PROBE_SUCCESS;
2757 			break;
2758 		case DTYPE_OPTICAL:
2759 			/*
2760 			 * Rewritable optical driver HP115AA
2761 			 * Can also be removable media
2762 			 */
2763 
2764 			/*
2765 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2766 			 * pre solaris 9 sparc sd behavior is required
2767 			 *
2768 			 * If first time through and sd_dtype_optical_bind
2769 			 * has not been set in /etc/system check properties
2770 			 */
2771 
2772 			if (sd_dtype_optical_bind  < 0) {
2773 				sd_dtype_optical_bind = ddi_prop_get_int
2774 				    (DDI_DEV_T_ANY, devi, 0,
2775 				    "optical-device-bind", 1);
2776 			}
2777 
2778 			if (sd_dtype_optical_bind == 0) {
2779 				rval = DDI_PROBE_FAILURE;
2780 			} else {
2781 				rval = DDI_PROBE_SUCCESS;
2782 			}
2783 			break;
2784 
2785 		case DTYPE_NOTPRESENT:
2786 		default:
2787 			rval = DDI_PROBE_FAILURE;
2788 			break;
2789 		}
2790 		break;
2791 	default:
2792 		rval = DDI_PROBE_PARTIAL;
2793 		break;
2794 	}
2795 
2796 	/*
2797 	 * This routine checks for resource allocation prior to freeing,
2798 	 * so it will take care of the "smart probing" case where a
2799 	 * scsi_probe() may or may not have been issued and will *not*
2800 	 * free previously-freed resources.
2801 	 */
2802 	scsi_unprobe(devp);
2803 	return (rval);
2804 }
2805 
2806 
2807 /*
2808  *    Function: sdinfo
2809  *
2810  * Description: This is the driver getinfo(9e) entry point function.
2811  * 		Given the device number, return the devinfo pointer from
2812  *		the scsi_device structure or the instance number
2813  *		associated with the dev_t.
2814  *
2815  *   Arguments: dip     - pointer to device info structure
2816  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2817  *			  DDI_INFO_DEVT2INSTANCE)
2818  *		arg     - driver dev_t
2819  *		resultp - user buffer for request response
2820  *
2821  * Return Code: DDI_SUCCESS
2822  *              DDI_FAILURE
2823  */
2824 /* ARGSUSED */
2825 static int
2826 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2827 {
2828 	struct sd_lun	*un;
2829 	dev_t		dev;
2830 	int		instance;
2831 	int		error;
2832 
2833 	switch (infocmd) {
2834 	case DDI_INFO_DEVT2DEVINFO:
2835 		dev = (dev_t)arg;
2836 		instance = SDUNIT(dev);
2837 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2838 			return (DDI_FAILURE);
2839 		}
2840 		*result = (void *) SD_DEVINFO(un);
2841 		error = DDI_SUCCESS;
2842 		break;
2843 	case DDI_INFO_DEVT2INSTANCE:
2844 		dev = (dev_t)arg;
2845 		instance = SDUNIT(dev);
2846 		*result = (void *)(uintptr_t)instance;
2847 		error = DDI_SUCCESS;
2848 		break;
2849 	default:
2850 		error = DDI_FAILURE;
2851 	}
2852 	return (error);
2853 }
2854 
2855 /*
2856  *    Function: sd_prop_op
2857  *
2858  * Description: This is the driver prop_op(9e) entry point function.
2859  *		Return the number of blocks for the partition in question
2860  *		or forward the request to the property facilities.
2861  *
2862  *   Arguments: dev       - device number
2863  *		dip       - pointer to device info structure
2864  *		prop_op   - property operator
2865  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2866  *		name      - pointer to property name
2867  *		valuep    - pointer or address of the user buffer
2868  *		lengthp   - property length
2869  *
2870  * Return Code: DDI_PROP_SUCCESS
2871  *              DDI_PROP_NOT_FOUND
2872  *              DDI_PROP_UNDEFINED
2873  *              DDI_PROP_NO_MEMORY
2874  *              DDI_PROP_BUF_TOO_SMALL
2875  */
2876 
2877 static int
2878 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2879     char *name, caddr_t valuep, int *lengthp)
2880 {
2881 	struct sd_lun	*un;
2882 
2883 	if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2884 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2885 		    name, valuep, lengthp));
2886 
2887 	return (cmlb_prop_op(un->un_cmlbhandle,
2888 	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2889 	    SDPART(dev), (void *)SD_PATH_DIRECT));
2890 }
2891 
2892 /*
2893  * The following functions are for smart probing:
2894  * sd_scsi_probe_cache_init()
2895  * sd_scsi_probe_cache_fini()
2896  * sd_scsi_clear_probe_cache()
2897  * sd_scsi_probe_with_cache()
2898  */
2899 
2900 /*
2901  *    Function: sd_scsi_probe_cache_init
2902  *
2903  * Description: Initializes the probe response cache mutex and head pointer.
2904  *
2905  *     Context: Kernel thread context
2906  */
2907 
2908 static void
2909 sd_scsi_probe_cache_init(void)
2910 {
2911 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2912 	sd_scsi_probe_cache_head = NULL;
2913 }
2914 
2915 
2916 /*
2917  *    Function: sd_scsi_probe_cache_fini
2918  *
2919  * Description: Frees all resources associated with the probe response cache.
2920  *
2921  *     Context: Kernel thread context
2922  */
2923 
2924 static void
2925 sd_scsi_probe_cache_fini(void)
2926 {
2927 	struct sd_scsi_probe_cache *cp;
2928 	struct sd_scsi_probe_cache *ncp;
2929 
2930 	/* Clean up our smart probing linked list */
2931 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2932 		ncp = cp->next;
2933 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2934 	}
2935 	sd_scsi_probe_cache_head = NULL;
2936 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2937 }
2938 
2939 
2940 /*
2941  *    Function: sd_scsi_clear_probe_cache
2942  *
2943  * Description: This routine clears the probe response cache. This is
2944  *		done when open() returns ENXIO so that when deferred
2945  *		attach is attempted (possibly after a device has been
2946  *		turned on) we will retry the probe. Since we don't know
2947  *		which target we failed to open, we just clear the
2948  *		entire cache.
2949  *
2950  *     Context: Kernel thread context
2951  */
2952 
2953 static void
2954 sd_scsi_clear_probe_cache(void)
2955 {
2956 	struct sd_scsi_probe_cache	*cp;
2957 	int				i;
2958 
2959 	mutex_enter(&sd_scsi_probe_cache_mutex);
2960 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2961 		/*
2962 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2963 		 * force probing to be performed the next time
2964 		 * sd_scsi_probe_with_cache is called.
2965 		 */
2966 		for (i = 0; i < NTARGETS_WIDE; i++) {
2967 			cp->cache[i] = SCSIPROBE_EXISTS;
2968 		}
2969 	}
2970 	mutex_exit(&sd_scsi_probe_cache_mutex);
2971 }
2972 
2973 
2974 /*
2975  *    Function: sd_scsi_probe_with_cache
2976  *
2977  * Description: This routine implements support for a scsi device probe
2978  *		with cache. The driver maintains a cache of the target
2979  *		responses to scsi probes. If we get no response from a
2980  *		target during a probe inquiry, we remember that, and we
2981  *		avoid additional calls to scsi_probe on non-zero LUNs
2982  *		on the same target until the cache is cleared. By doing
2983  *		so we avoid the 1/4 sec selection timeout for nonzero
2984  *		LUNs. lun0 of a target is always probed.
2985  *
2986  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2987  *              waitfunc - indicates what the allocator routines should
2988  *			   do when resources are not available. This value
2989  *			   is passed on to scsi_probe() when that routine
2990  *			   is called.
2991  *
2992  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2993  *		otherwise the value returned by scsi_probe(9F).
2994  *
2995  *     Context: Kernel thread context
2996  */
2997 
2998 static int
2999 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
3000 {
3001 	struct sd_scsi_probe_cache	*cp;
3002 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
3003 	int		lun, tgt;
3004 
3005 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3006 	    SCSI_ADDR_PROP_LUN, 0);
3007 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3008 	    SCSI_ADDR_PROP_TARGET, -1);
3009 
3010 	/* Make sure caching enabled and target in range */
3011 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
3012 		/* do it the old way (no cache) */
3013 		return (scsi_probe(devp, waitfn));
3014 	}
3015 
3016 	mutex_enter(&sd_scsi_probe_cache_mutex);
3017 
3018 	/* Find the cache for this scsi bus instance */
3019 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
3020 		if (cp->pdip == pdip) {
3021 			break;
3022 		}
3023 	}
3024 
3025 	/* If we can't find a cache for this pdip, create one */
3026 	if (cp == NULL) {
3027 		int i;
3028 
3029 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3030 		    KM_SLEEP);
3031 		cp->pdip = pdip;
3032 		cp->next = sd_scsi_probe_cache_head;
3033 		sd_scsi_probe_cache_head = cp;
3034 		for (i = 0; i < NTARGETS_WIDE; i++) {
3035 			cp->cache[i] = SCSIPROBE_EXISTS;
3036 		}
3037 	}
3038 
3039 	mutex_exit(&sd_scsi_probe_cache_mutex);
3040 
3041 	/* Recompute the cache for this target if LUN zero */
3042 	if (lun == 0) {
3043 		cp->cache[tgt] = SCSIPROBE_EXISTS;
3044 	}
3045 
3046 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
3047 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3048 		return (SCSIPROBE_NORESP);
3049 	}
3050 
3051 	/* Do the actual probe; save & return the result */
3052 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3053 }
3054 
3055 
3056 /*
3057  *    Function: sd_scsi_target_lun_init
3058  *
3059  * Description: Initializes the attached lun chain mutex and head pointer.
3060  *
3061  *     Context: Kernel thread context
3062  */
3063 
3064 static void
3065 sd_scsi_target_lun_init(void)
3066 {
3067 	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3068 	sd_scsi_target_lun_head = NULL;
3069 }
3070 
3071 
3072 /*
3073  *    Function: sd_scsi_target_lun_fini
3074  *
3075  * Description: Frees all resources associated with the attached lun
3076  *              chain
3077  *
3078  *     Context: Kernel thread context
3079  */
3080 
3081 static void
3082 sd_scsi_target_lun_fini(void)
3083 {
3084 	struct sd_scsi_hba_tgt_lun	*cp;
3085 	struct sd_scsi_hba_tgt_lun	*ncp;
3086 
3087 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3088 		ncp = cp->next;
3089 		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3090 	}
3091 	sd_scsi_target_lun_head = NULL;
3092 	mutex_destroy(&sd_scsi_target_lun_mutex);
3093 }
3094 
3095 
3096 /*
3097  *    Function: sd_scsi_get_target_lun_count
3098  *
3099  * Description: This routine will check in the attached lun chain to see
3100  * 		how many luns are attached on the required SCSI controller
3101  * 		and target. Currently, some capabilities like tagged queue
3102  *		are supported per target based by HBA. So all luns in a
3103  *		target have the same capabilities. Based on this assumption,
3104  * 		sd should only set these capabilities once per target. This
3105  *		function is called when sd needs to decide how many luns
3106  *		already attached on a target.
3107  *
3108  *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
3109  *			  controller device.
3110  *              target	- The target ID on the controller's SCSI bus.
3111  *
3112  * Return Code: The number of luns attached on the required target and
3113  *		controller.
3114  *		-1 if target ID is not in parallel SCSI scope or the given
3115  * 		dip is not in the chain.
3116  *
3117  *     Context: Kernel thread context
3118  */
3119 
3120 static int
3121 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3122 {
3123 	struct sd_scsi_hba_tgt_lun	*cp;
3124 
3125 	if ((target < 0) || (target >= NTARGETS_WIDE)) {
3126 		return (-1);
3127 	}
3128 
3129 	mutex_enter(&sd_scsi_target_lun_mutex);
3130 
3131 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3132 		if (cp->pdip == dip) {
3133 			break;
3134 		}
3135 	}
3136 
3137 	mutex_exit(&sd_scsi_target_lun_mutex);
3138 
3139 	if (cp == NULL) {
3140 		return (-1);
3141 	}
3142 
3143 	return (cp->nlun[target]);
3144 }
3145 
3146 
3147 /*
3148  *    Function: sd_scsi_update_lun_on_target
3149  *
3150  * Description: This routine is used to update the attached lun chain when a
3151  *		lun is attached or detached on a target.
3152  *
3153  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
3154  *                        controller device.
3155  *              target  - The target ID on the controller's SCSI bus.
3156  *		flag	- Indicate the lun is attached or detached.
3157  *
3158  *     Context: Kernel thread context
3159  */
3160 
3161 static void
3162 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3163 {
3164 	struct sd_scsi_hba_tgt_lun	*cp;
3165 
3166 	mutex_enter(&sd_scsi_target_lun_mutex);
3167 
3168 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3169 		if (cp->pdip == dip) {
3170 			break;
3171 		}
3172 	}
3173 
3174 	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3175 		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3176 		    KM_SLEEP);
3177 		cp->pdip = dip;
3178 		cp->next = sd_scsi_target_lun_head;
3179 		sd_scsi_target_lun_head = cp;
3180 	}
3181 
3182 	mutex_exit(&sd_scsi_target_lun_mutex);
3183 
3184 	if (cp != NULL) {
3185 		if (flag == SD_SCSI_LUN_ATTACH) {
3186 			cp->nlun[target] ++;
3187 		} else {
3188 			cp->nlun[target] --;
3189 		}
3190 	}
3191 }
3192 
3193 
3194 /*
3195  *    Function: sd_spin_up_unit
3196  *
3197  * Description: Issues the following commands to spin-up the device:
3198  *		START STOP UNIT, and INQUIRY.
3199  *
3200  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3201  *                      structure for this target.
3202  *
3203  * Return Code: 0 - success
3204  *		EIO - failure
3205  *		EACCES - reservation conflict
3206  *
3207  *     Context: Kernel thread context
3208  */
3209 
3210 static int
3211 sd_spin_up_unit(sd_ssc_t *ssc)
3212 {
3213 	size_t	resid		= 0;
3214 	int	has_conflict	= FALSE;
3215 	uchar_t *bufaddr;
3216 	int 	status;
3217 	struct sd_lun	*un;
3218 
3219 	ASSERT(ssc != NULL);
3220 	un = ssc->ssc_un;
3221 	ASSERT(un != NULL);
3222 
3223 	/*
3224 	 * Send a throwaway START UNIT command.
3225 	 *
3226 	 * If we fail on this, we don't care presently what precisely
3227 	 * is wrong.  EMC's arrays will also fail this with a check
3228 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
3229 	 * we don't want to fail the attach because it may become
3230 	 * "active" later.
3231 	 * We don't know if power condition is supported or not at
3232 	 * this stage, use START STOP bit.
3233 	 */
3234 	status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3235 	    SD_TARGET_START, SD_PATH_DIRECT);
3236 
3237 	if (status != 0) {
3238 		if (status == EACCES)
3239 			has_conflict = TRUE;
3240 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3241 	}
3242 
3243 	/*
3244 	 * Send another INQUIRY command to the target. This is necessary for
3245 	 * non-removable media direct access devices because their INQUIRY data
3246 	 * may not be fully qualified until they are spun up (perhaps via the
3247 	 * START command above).  Note: This seems to be needed for some
3248 	 * legacy devices only.) The INQUIRY command should succeed even if a
3249 	 * Reservation Conflict is present.
3250 	 */
3251 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3252 
3253 	if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3254 	    != 0) {
3255 		kmem_free(bufaddr, SUN_INQSIZE);
3256 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3257 		return (EIO);
3258 	}
3259 
3260 	/*
3261 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3262 	 * Note that this routine does not return a failure here even if the
3263 	 * INQUIRY command did not return any data.  This is a legacy behavior.
3264 	 */
3265 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3266 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3267 	}
3268 
3269 	kmem_free(bufaddr, SUN_INQSIZE);
3270 
3271 	/* If we hit a reservation conflict above, tell the caller. */
3272 	if (has_conflict == TRUE) {
3273 		return (EACCES);
3274 	}
3275 
3276 	return (0);
3277 }
3278 
3279 #ifdef _LP64
3280 /*
3281  *    Function: sd_enable_descr_sense
3282  *
3283  * Description: This routine attempts to select descriptor sense format
3284  *		using the Control mode page.  Devices that support 64 bit
3285  *		LBAs (for >2TB luns) should also implement descriptor
3286  *		sense data so we will call this function whenever we see
3287  *		a lun larger than 2TB.  If for some reason the device
3288  *		supports 64 bit LBAs but doesn't support descriptor sense
3289  *		presumably the mode select will fail.  Everything will
3290  *		continue to work normally except that we will not get
3291  *		complete sense data for commands that fail with an LBA
3292  *		larger than 32 bits.
3293  *
3294  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3295  *                      structure for this target.
3296  *
3297  *     Context: Kernel thread context only
3298  */
3299 
3300 static void
3301 sd_enable_descr_sense(sd_ssc_t *ssc)
3302 {
3303 	uchar_t			*header;
3304 	struct mode_control_scsi3 *ctrl_bufp;
3305 	size_t			buflen;
3306 	size_t			bd_len;
3307 	int			status;
3308 	struct sd_lun		*un;
3309 
3310 	ASSERT(ssc != NULL);
3311 	un = ssc->ssc_un;
3312 	ASSERT(un != NULL);
3313 
3314 	/*
3315 	 * Read MODE SENSE page 0xA, Control Mode Page
3316 	 */
3317 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3318 	    sizeof (struct mode_control_scsi3);
3319 	header = kmem_zalloc(buflen, KM_SLEEP);
3320 
3321 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3322 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3323 
3324 	if (status != 0) {
3325 		SD_ERROR(SD_LOG_COMMON, un,
3326 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
3327 		goto eds_exit;
3328 	}
3329 
3330 	/*
3331 	 * Determine size of Block Descriptors in order to locate
3332 	 * the mode page data. ATAPI devices return 0, SCSI devices
3333 	 * should return MODE_BLK_DESC_LENGTH.
3334 	 */
3335 	bd_len  = ((struct mode_header *)header)->bdesc_length;
3336 
3337 	/* Clear the mode data length field for MODE SELECT */
3338 	((struct mode_header *)header)->length = 0;
3339 
3340 	ctrl_bufp = (struct mode_control_scsi3 *)
3341 	    (header + MODE_HEADER_LENGTH + bd_len);
3342 
3343 	/*
3344 	 * If the page length is smaller than the expected value,
3345 	 * the target device doesn't support D_SENSE. Bail out here.
3346 	 */
3347 	if (ctrl_bufp->mode_page.length <
3348 	    sizeof (struct mode_control_scsi3) - 2) {
3349 		SD_ERROR(SD_LOG_COMMON, un,
3350 		    "sd_enable_descr_sense: enable D_SENSE failed\n");
3351 		goto eds_exit;
3352 	}
3353 
3354 	/*
3355 	 * Clear PS bit for MODE SELECT
3356 	 */
3357 	ctrl_bufp->mode_page.ps = 0;
3358 
3359 	/*
3360 	 * Set D_SENSE to enable descriptor sense format.
3361 	 */
3362 	ctrl_bufp->d_sense = 1;
3363 
3364 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3365 
3366 	/*
3367 	 * Use MODE SELECT to commit the change to the D_SENSE bit
3368 	 */
3369 	status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3370 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3371 
3372 	if (status != 0) {
3373 		SD_INFO(SD_LOG_COMMON, un,
3374 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
3375 	} else {
3376 		kmem_free(header, buflen);
3377 		return;
3378 	}
3379 
3380 eds_exit:
3381 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3382 	kmem_free(header, buflen);
3383 }
3384 
3385 /*
3386  *    Function: sd_reenable_dsense_task
3387  *
3388  * Description: Re-enable descriptor sense after device or bus reset
3389  *
3390  *     Context: Executes in a taskq() thread context
3391  */
3392 static void
3393 sd_reenable_dsense_task(void *arg)
3394 {
3395 	struct	sd_lun	*un = arg;
3396 	sd_ssc_t	*ssc;
3397 
3398 	ASSERT(un != NULL);
3399 
3400 	ssc = sd_ssc_init(un);
3401 	sd_enable_descr_sense(ssc);
3402 	sd_ssc_fini(ssc);
3403 }
3404 #endif /* _LP64 */
3405 
3406 /*
3407  *    Function: sd_set_mmc_caps
3408  *
3409  * Description: This routine determines if the device is MMC compliant and if
3410  *		the device supports CDDA via a mode sense of the CDVD
3411  *		capabilities mode page. Also checks if the device is a
3412  *		dvdram writable device.
3413  *
3414  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3415  *                      structure for this target.
3416  *
3417  *     Context: Kernel thread context only
3418  */
3419 
3420 static void
3421 sd_set_mmc_caps(sd_ssc_t *ssc)
3422 {
3423 	struct mode_header_grp2		*sense_mhp;
3424 	uchar_t				*sense_page;
3425 	caddr_t				buf;
3426 	int				bd_len;
3427 	int				status;
3428 	struct uscsi_cmd		com;
3429 	int				rtn;
3430 	uchar_t				*out_data_rw, *out_data_hd;
3431 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3432 	uchar_t				*out_data_gesn;
3433 	int				gesn_len;
3434 	struct sd_lun			*un;
3435 
3436 	ASSERT(ssc != NULL);
3437 	un = ssc->ssc_un;
3438 	ASSERT(un != NULL);
3439 
3440 	/*
3441 	 * The flags which will be set in this function are - mmc compliant,
3442 	 * dvdram writable device, cdda support. Initialize them to FALSE
3443 	 * and if a capability is detected - it will be set to TRUE.
3444 	 */
3445 	un->un_f_mmc_cap = FALSE;
3446 	un->un_f_dvdram_writable_device = FALSE;
3447 	un->un_f_cfg_cdda = FALSE;
3448 
3449 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3450 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3451 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3452 
3453 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3454 
3455 	if (status != 0) {
3456 		/* command failed; just return */
3457 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3458 		return;
3459 	}
3460 	/*
3461 	 * If the mode sense request for the CDROM CAPABILITIES
3462 	 * page (0x2A) succeeds the device is assumed to be MMC.
3463 	 */
3464 	un->un_f_mmc_cap = TRUE;
3465 
3466 	/* See if GET STATUS EVENT NOTIFICATION is supported */
3467 	if (un->un_f_mmc_gesn_polling) {
3468 		gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3469 		out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3470 
3471 		rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3472 		    out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3473 
3474 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3475 
3476 		if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3477 			un->un_f_mmc_gesn_polling = FALSE;
3478 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3479 			    "sd_set_mmc_caps: gesn not supported "
3480 			    "%d %x %x %x %x\n", rtn,
3481 			    out_data_gesn[0], out_data_gesn[1],
3482 			    out_data_gesn[2], out_data_gesn[3]);
3483 		}
3484 
3485 		kmem_free(out_data_gesn, gesn_len);
3486 	}
3487 
3488 	/* Get to the page data */
3489 	sense_mhp = (struct mode_header_grp2 *)buf;
3490 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3491 	    sense_mhp->bdesc_length_lo;
3492 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3493 		/*
3494 		 * We did not get back the expected block descriptor
3495 		 * length so we cannot determine if the device supports
3496 		 * CDDA. However, we still indicate the device is MMC
3497 		 * according to the successful response to the page
3498 		 * 0x2A mode sense request.
3499 		 */
3500 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3501 		    "sd_set_mmc_caps: Mode Sense returned "
3502 		    "invalid block descriptor length\n");
3503 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3504 		return;
3505 	}
3506 
3507 	/* See if read CDDA is supported */
3508 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3509 	    bd_len);
3510 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3511 
3512 	/* See if writing DVD RAM is supported. */
3513 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3514 	if (un->un_f_dvdram_writable_device == TRUE) {
3515 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3516 		return;
3517 	}
3518 
3519 	/*
3520 	 * If the device presents DVD or CD capabilities in the mode
3521 	 * page, we can return here since a RRD will not have
3522 	 * these capabilities.
3523 	 */
3524 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3525 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3526 		return;
3527 	}
3528 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3529 
3530 	/*
3531 	 * If un->un_f_dvdram_writable_device is still FALSE,
3532 	 * check for a Removable Rigid Disk (RRD).  A RRD
3533 	 * device is identified by the features RANDOM_WRITABLE and
3534 	 * HARDWARE_DEFECT_MANAGEMENT.
3535 	 */
3536 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3537 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3538 
3539 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3540 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3541 	    RANDOM_WRITABLE, SD_PATH_STANDARD);
3542 
3543 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3544 
3545 	if (rtn != 0) {
3546 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3547 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3548 		return;
3549 	}
3550 
3551 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3552 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3553 
3554 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3555 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3556 	    HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3557 
3558 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3559 
3560 	if (rtn == 0) {
3561 		/*
3562 		 * We have good information, check for random writable
3563 		 * and hardware defect features.
3564 		 */
3565 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3566 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3567 			un->un_f_dvdram_writable_device = TRUE;
3568 		}
3569 	}
3570 
3571 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3572 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3573 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3574 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3575 }
3576 
3577 /*
3578  *    Function: sd_check_for_writable_cd
3579  *
3580  * Description: This routine determines if the media in the device is
3581  *		writable or not. It uses the get configuration command (0x46)
3582  *		to determine if the media is writable
3583  *
3584  *   Arguments: un - driver soft state (unit) structure
3585  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3586  *                           chain and the normal command waitq, or
3587  *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
3588  *                           "direct" chain and bypass the normal command
3589  *                           waitq.
3590  *
3591  *     Context: Never called at interrupt context.
3592  */
3593 
3594 static void
3595 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3596 {
3597 	struct uscsi_cmd		com;
3598 	uchar_t				*out_data;
3599 	uchar_t				*rqbuf;
3600 	int				rtn;
3601 	uchar_t				*out_data_rw, *out_data_hd;
3602 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3603 	struct mode_header_grp2		*sense_mhp;
3604 	uchar_t				*sense_page;
3605 	caddr_t				buf;
3606 	int				bd_len;
3607 	int				status;
3608 	struct sd_lun			*un;
3609 
3610 	ASSERT(ssc != NULL);
3611 	un = ssc->ssc_un;
3612 	ASSERT(un != NULL);
3613 	ASSERT(mutex_owned(SD_MUTEX(un)));
3614 
3615 	/*
3616 	 * Initialize the writable media to false, if configuration info.
3617 	 * tells us otherwise then only we will set it.
3618 	 */
3619 	un->un_f_mmc_writable_media = FALSE;
3620 	mutex_exit(SD_MUTEX(un));
3621 
3622 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3623 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3624 
3625 	rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3626 	    out_data, SD_PROFILE_HEADER_LEN, path_flag);
3627 
3628 	if (rtn != 0)
3629 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3630 
3631 	mutex_enter(SD_MUTEX(un));
3632 	if (rtn == 0) {
3633 		/*
3634 		 * We have good information, check for writable DVD.
3635 		 */
3636 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3637 			un->un_f_mmc_writable_media = TRUE;
3638 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3639 			kmem_free(rqbuf, SENSE_LENGTH);
3640 			return;
3641 		}
3642 	}
3643 
3644 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3645 	kmem_free(rqbuf, SENSE_LENGTH);
3646 
3647 	/*
3648 	 * Determine if this is a RRD type device.
3649 	 */
3650 	mutex_exit(SD_MUTEX(un));
3651 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3652 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3653 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3654 
3655 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3656 
3657 	mutex_enter(SD_MUTEX(un));
3658 	if (status != 0) {
3659 		/* command failed; just return */
3660 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3661 		return;
3662 	}
3663 
3664 	/* Get to the page data */
3665 	sense_mhp = (struct mode_header_grp2 *)buf;
3666 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3667 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3668 		/*
3669 		 * We did not get back the expected block descriptor length so
3670 		 * we cannot check the mode page.
3671 		 */
3672 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3673 		    "sd_check_for_writable_cd: Mode Sense returned "
3674 		    "invalid block descriptor length\n");
3675 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3676 		return;
3677 	}
3678 
3679 	/*
3680 	 * If the device presents DVD or CD capabilities in the mode
3681 	 * page, we can return here since a RRD device will not have
3682 	 * these capabilities.
3683 	 */
3684 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3685 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3686 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3687 		return;
3688 	}
3689 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3690 
3691 	/*
3692 	 * If un->un_f_mmc_writable_media is still FALSE,
3693 	 * check for RRD type media.  A RRD device is identified
3694 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3695 	 */
3696 	mutex_exit(SD_MUTEX(un));
3697 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3698 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3699 
3700 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3701 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3702 	    RANDOM_WRITABLE, path_flag);
3703 
3704 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3705 	if (rtn != 0) {
3706 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3707 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3708 		mutex_enter(SD_MUTEX(un));
3709 		return;
3710 	}
3711 
3712 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3713 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3714 
3715 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3716 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3717 	    HARDWARE_DEFECT_MANAGEMENT, path_flag);
3718 
3719 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3720 	mutex_enter(SD_MUTEX(un));
3721 	if (rtn == 0) {
3722 		/*
3723 		 * We have good information, check for random writable
3724 		 * and hardware defect features as current.
3725 		 */
3726 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3727 		    (out_data_rw[10] & 0x1) &&
3728 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3729 		    (out_data_hd[10] & 0x1)) {
3730 			un->un_f_mmc_writable_media = TRUE;
3731 		}
3732 	}
3733 
3734 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3735 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3736 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3737 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3738 }
3739 
3740 /*
3741  *    Function: sd_read_unit_properties
3742  *
3743  * Description: The following implements a property lookup mechanism.
3744  *		Properties for particular disks (keyed on vendor, model
3745  *		and rev numbers) are sought in the sd.conf file via
3746  *		sd_process_sdconf_file(), and if not found there, are
3747  *		looked for in a list hardcoded in this driver via
3748  *		sd_process_sdconf_table() Once located the properties
3749  *		are used to update the driver unit structure.
3750  *
3751  *   Arguments: un - driver soft state (unit) structure
3752  */
3753 
3754 static void
3755 sd_read_unit_properties(struct sd_lun *un)
3756 {
3757 	/*
3758 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3759 	 * the "sd-config-list" property (from the sd.conf file) or if
3760 	 * there was not a match for the inquiry vid/pid. If this event
3761 	 * occurs the static driver configuration table is searched for
3762 	 * a match.
3763 	 */
3764 	ASSERT(un != NULL);
3765 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3766 		sd_process_sdconf_table(un);
3767 	}
3768 
3769 	/* check for LSI device */
3770 	sd_is_lsi(un);
3771 
3772 
3773 }
3774 
3775 
3776 /*
3777  *    Function: sd_process_sdconf_file
3778  *
3779  * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3780  *		driver's config file (ie, sd.conf) and update the driver
3781  *		soft state structure accordingly.
3782  *
3783  *   Arguments: un - driver soft state (unit) structure
3784  *
3785  * Return Code: SD_SUCCESS - The properties were successfully set according
3786  *			     to the driver configuration file.
3787  *		SD_FAILURE - The driver config list was not obtained or
3788  *			     there was no vid/pid match. This indicates that
3789  *			     the static config table should be used.
3790  *
3791  * The config file has a property, "sd-config-list". Currently we support
3792  * two kinds of formats. For both formats, the value of this property
3793  * is a list of duplets:
3794  *
3795  *  sd-config-list=
3796  *	<duplet>,
3797  *	[,<duplet>]*;
3798  *
3799  * For the improved format, where
3800  *
3801  *     <duplet>:= "<vid+pid>","<tunable-list>"
3802  *
3803  * and
3804  *
3805  *     <tunable-list>:=   <tunable> [, <tunable> ]*;
3806  *     <tunable> =        <name> : <value>
3807  *
3808  * The <vid+pid> is the string that is returned by the target device on a
3809  * SCSI inquiry command, the <tunable-list> contains one or more tunables
3810  * to apply to all target devices with the specified <vid+pid>.
3811  *
3812  * Each <tunable> is a "<name> : <value>" pair.
3813  *
3814  * For the old format, the structure of each duplet is as follows:
3815  *
3816  *  <duplet>:= "<vid+pid>","<data-property-name_list>"
3817  *
3818  * The first entry of the duplet is the device ID string (the concatenated
3819  * vid & pid; not to be confused with a device_id).  This is defined in
3820  * the same way as in the sd_disk_table.
3821  *
3822  * The second part of the duplet is a string that identifies a
3823  * data-property-name-list. The data-property-name-list is defined as
3824  * follows:
3825  *
3826  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3827  *
3828  * The syntax of <data-property-name> depends on the <version> field.
3829  *
3830  * If version = SD_CONF_VERSION_1 we have the following syntax:
3831  *
3832  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3833  *
3834  * where the prop0 value will be used to set prop0 if bit0 set in the
3835  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3836  *
3837  */
3838 
3839 static int
3840 sd_process_sdconf_file(struct sd_lun *un)
3841 {
3842 	char	**config_list = NULL;
3843 	uint_t	nelements;
3844 	char	*vidptr;
3845 	int	vidlen;
3846 	char	*dnlist_ptr;
3847 	char	*dataname_ptr;
3848 	char	*dataname_lasts;
3849 	int	*data_list = NULL;
3850 	uint_t	data_list_len;
3851 	int	rval = SD_FAILURE;
3852 	int	i;
3853 
3854 	ASSERT(un != NULL);
3855 
3856 	/* Obtain the configuration list associated with the .conf file */
3857 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3858 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3859 	    &config_list, &nelements) != DDI_PROP_SUCCESS) {
3860 		return (SD_FAILURE);
3861 	}
3862 
3863 	/*
3864 	 * Compare vids in each duplet to the inquiry vid - if a match is
3865 	 * made, get the data value and update the soft state structure
3866 	 * accordingly.
3867 	 *
3868 	 * Each duplet should show as a pair of strings, return SD_FAILURE
3869 	 * otherwise.
3870 	 */
3871 	if (nelements & 1) {
3872 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3873 		    "sd-config-list should show as pairs of strings.\n");
3874 		if (config_list)
3875 			ddi_prop_free(config_list);
3876 		return (SD_FAILURE);
3877 	}
3878 
3879 	for (i = 0; i < nelements; i += 2) {
3880 		/*
3881 		 * Note: The assumption here is that each vid entry is on
3882 		 * a unique line from its associated duplet.
3883 		 */
3884 		vidptr = config_list[i];
3885 		vidlen = (int)strlen(vidptr);
3886 		if (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS) {
3887 			continue;
3888 		}
3889 
3890 		/*
3891 		 * dnlist contains 1 or more blank separated
3892 		 * data-property-name entries
3893 		 */
3894 		dnlist_ptr = config_list[i + 1];
3895 
3896 		if (strchr(dnlist_ptr, ':') != NULL) {
3897 			/*
3898 			 * Decode the improved format sd-config-list.
3899 			 */
3900 			sd_nvpair_str_decode(un, dnlist_ptr);
3901 		} else {
3902 			/*
3903 			 * The old format sd-config-list, loop through all
3904 			 * data-property-name entries in the
3905 			 * data-property-name-list
3906 			 * setting the properties for each.
3907 			 */
3908 			for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3909 			    &dataname_lasts); dataname_ptr != NULL;
3910 			    dataname_ptr = sd_strtok_r(NULL, " \t",
3911 			    &dataname_lasts)) {
3912 				int version;
3913 
3914 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3915 				    "sd_process_sdconf_file: disk:%s, "
3916 				    "data:%s\n", vidptr, dataname_ptr);
3917 
3918 				/* Get the data list */
3919 				if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3920 				    SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3921 				    &data_list_len) != DDI_PROP_SUCCESS) {
3922 					SD_INFO(SD_LOG_ATTACH_DETACH, un,
3923 					    "sd_process_sdconf_file: data "
3924 					    "property (%s) has no value\n",
3925 					    dataname_ptr);
3926 					continue;
3927 				}
3928 
3929 				version = data_list[0];
3930 
3931 				if (version == SD_CONF_VERSION_1) {
3932 					sd_tunables values;
3933 
3934 					/* Set the properties */
3935 					if (sd_chk_vers1_data(un, data_list[1],
3936 					    &data_list[2], data_list_len,
3937 					    dataname_ptr) == SD_SUCCESS) {
3938 						sd_get_tunables_from_conf(un,
3939 						    data_list[1], &data_list[2],
3940 						    &values);
3941 						sd_set_vers1_properties(un,
3942 						    data_list[1], &values);
3943 						rval = SD_SUCCESS;
3944 					} else {
3945 						rval = SD_FAILURE;
3946 					}
3947 				} else {
3948 					scsi_log(SD_DEVINFO(un), sd_label,
3949 					    CE_WARN, "data property %s version "
3950 					    "0x%x is invalid.",
3951 					    dataname_ptr, version);
3952 					rval = SD_FAILURE;
3953 				}
3954 				if (data_list)
3955 					ddi_prop_free(data_list);
3956 			}
3957 		}
3958 	}
3959 
3960 	/* free up the memory allocated by ddi_prop_lookup_string_array(). */
3961 	if (config_list) {
3962 		ddi_prop_free(config_list);
3963 	}
3964 
3965 	return (rval);
3966 }
3967 
3968 /*
3969  *    Function: sd_nvpair_str_decode()
3970  *
3971  * Description: Parse the improved format sd-config-list to get
3972  *    each entry of tunable, which includes a name-value pair.
3973  *    Then call sd_set_properties() to set the property.
3974  *
3975  *   Arguments: un - driver soft state (unit) structure
3976  *    nvpair_str - the tunable list
3977  */
3978 static void
3979 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3980 {
3981 	char	*nv, *name, *value, *token;
3982 	char	*nv_lasts, *v_lasts, *x_lasts;
3983 
3984 	for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3985 	    nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3986 		token = sd_strtok_r(nv, ":", &v_lasts);
3987 		name  = sd_strtok_r(token, " \t", &x_lasts);
3988 		token = sd_strtok_r(NULL, ":", &v_lasts);
3989 		value = sd_strtok_r(token, " \t", &x_lasts);
3990 		if (name == NULL || value == NULL) {
3991 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3992 			    "sd_nvpair_str_decode: "
3993 			    "name or value is not valid!\n");
3994 		} else {
3995 			sd_set_properties(un, name, value);
3996 		}
3997 	}
3998 }
3999 
4000 /*
4001  *    Function: sd_strtok_r()
4002  *
4003  * Description: This function uses strpbrk and strspn to break
4004  *    string into tokens on sequentially subsequent calls. Return
4005  *    NULL when no non-separator characters remain. The first
4006  *    argument is NULL for subsequent calls.
4007  */
4008 static char *
4009 sd_strtok_r(char *string, const char *sepset, char **lasts)
4010 {
4011 	char	*q, *r;
4012 
4013 	/* First or subsequent call */
4014 	if (string == NULL)
4015 		string = *lasts;
4016 
4017 	if (string == NULL)
4018 		return (NULL);
4019 
4020 	/* Skip leading separators */
4021 	q = string + strspn(string, sepset);
4022 
4023 	if (*q == '\0')
4024 		return (NULL);
4025 
4026 	if ((r = strpbrk(q, sepset)) == NULL)
4027 		*lasts = NULL;
4028 	else {
4029 		*r = '\0';
4030 		*lasts = r + 1;
4031 	}
4032 	return (q);
4033 }
4034 
4035 /*
4036  *    Function: sd_set_properties()
4037  *
4038  * Description: Set device properties based on the improved
4039  *    format sd-config-list.
4040  *
4041  *   Arguments: un - driver soft state (unit) structure
4042  *    name  - supported tunable name
4043  *    value - tunable value
4044  */
4045 static void
4046 sd_set_properties(struct sd_lun *un, char *name, char *value)
4047 {
4048 	char	*endptr = NULL;
4049 	long	val = 0;
4050 
4051 	if (strcasecmp(name, "cache-nonvolatile") == 0) {
4052 		if (strcasecmp(value, "true") == 0) {
4053 			un->un_f_suppress_cache_flush = TRUE;
4054 		} else if (strcasecmp(value, "false") == 0) {
4055 			un->un_f_suppress_cache_flush = FALSE;
4056 		} else {
4057 			goto value_invalid;
4058 		}
4059 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4060 		    "suppress_cache_flush flag set to %d\n",
4061 		    un->un_f_suppress_cache_flush);
4062 		return;
4063 	}
4064 
4065 	if (strcasecmp(name, "controller-type") == 0) {
4066 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4067 			un->un_ctype = val;
4068 		} else {
4069 			goto value_invalid;
4070 		}
4071 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4072 		    "ctype set to %d\n", un->un_ctype);
4073 		return;
4074 	}
4075 
4076 	if (strcasecmp(name, "delay-busy") == 0) {
4077 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4078 			un->un_busy_timeout = drv_usectohz(val / 1000);
4079 		} else {
4080 			goto value_invalid;
4081 		}
4082 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4083 		    "busy_timeout set to %d\n", un->un_busy_timeout);
4084 		return;
4085 	}
4086 
4087 	if (strcasecmp(name, "disksort") == 0) {
4088 		if (strcasecmp(value, "true") == 0) {
4089 			un->un_f_disksort_disabled = FALSE;
4090 		} else if (strcasecmp(value, "false") == 0) {
4091 			un->un_f_disksort_disabled = TRUE;
4092 		} else {
4093 			goto value_invalid;
4094 		}
4095 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4096 		    "disksort disabled flag set to %d\n",
4097 		    un->un_f_disksort_disabled);
4098 		return;
4099 	}
4100 
4101 	if (strcasecmp(name, "power-condition") == 0) {
4102 		if (strcasecmp(value, "true") == 0) {
4103 			un->un_f_power_condition_disabled = FALSE;
4104 		} else if (strcasecmp(value, "false") == 0) {
4105 			un->un_f_power_condition_disabled = TRUE;
4106 		} else {
4107 			goto value_invalid;
4108 		}
4109 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4110 		    "power condition disabled flag set to %d\n",
4111 		    un->un_f_power_condition_disabled);
4112 		return;
4113 	}
4114 
4115 	if (strcasecmp(name, "timeout-releasereservation") == 0) {
4116 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4117 			un->un_reserve_release_time = val;
4118 		} else {
4119 			goto value_invalid;
4120 		}
4121 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4122 		    "reservation release timeout set to %d\n",
4123 		    un->un_reserve_release_time);
4124 		return;
4125 	}
4126 
4127 	if (strcasecmp(name, "reset-lun") == 0) {
4128 		if (strcasecmp(value, "true") == 0) {
4129 			un->un_f_lun_reset_enabled = TRUE;
4130 		} else if (strcasecmp(value, "false") == 0) {
4131 			un->un_f_lun_reset_enabled = FALSE;
4132 		} else {
4133 			goto value_invalid;
4134 		}
4135 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4136 		    "lun reset enabled flag set to %d\n",
4137 		    un->un_f_lun_reset_enabled);
4138 		return;
4139 	}
4140 
4141 	if (strcasecmp(name, "retries-busy") == 0) {
4142 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4143 			un->un_busy_retry_count = val;
4144 		} else {
4145 			goto value_invalid;
4146 		}
4147 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4148 		    "busy retry count set to %d\n", un->un_busy_retry_count);
4149 		return;
4150 	}
4151 
4152 	if (strcasecmp(name, "retries-timeout") == 0) {
4153 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4154 			un->un_retry_count = val;
4155 		} else {
4156 			goto value_invalid;
4157 		}
4158 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4159 		    "timeout retry count set to %d\n", un->un_retry_count);
4160 		return;
4161 	}
4162 
4163 	if (strcasecmp(name, "retries-notready") == 0) {
4164 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4165 			un->un_notready_retry_count = val;
4166 		} else {
4167 			goto value_invalid;
4168 		}
4169 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4170 		    "notready retry count set to %d\n",
4171 		    un->un_notready_retry_count);
4172 		return;
4173 	}
4174 
4175 	if (strcasecmp(name, "retries-reset") == 0) {
4176 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4177 			un->un_reset_retry_count = val;
4178 		} else {
4179 			goto value_invalid;
4180 		}
4181 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4182 		    "reset retry count set to %d\n",
4183 		    un->un_reset_retry_count);
4184 		return;
4185 	}
4186 
4187 	if (strcasecmp(name, "throttle-max") == 0) {
4188 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4189 			un->un_saved_throttle = un->un_throttle = val;
4190 		} else {
4191 			goto value_invalid;
4192 		}
4193 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4194 		    "throttle set to %d\n", un->un_throttle);
4195 	}
4196 
4197 	if (strcasecmp(name, "throttle-min") == 0) {
4198 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4199 			un->un_min_throttle = val;
4200 		} else {
4201 			goto value_invalid;
4202 		}
4203 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4204 		    "min throttle set to %d\n", un->un_min_throttle);
4205 	}
4206 
4207 	if (strcasecmp(name, "rmw-type") == 0) {
4208 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4209 			un->un_f_rmw_type = val;
4210 		} else {
4211 			goto value_invalid;
4212 		}
4213 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4214 		    "RMW type set to %d\n", un->un_f_rmw_type);
4215 	}
4216 
4217 	if (strcasecmp(name, "physical-block-size") == 0) {
4218 		if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
4219 		    ISP2(val) && val >= un->un_tgt_blocksize &&
4220 		    val >= un->un_sys_blocksize) {
4221 			un->un_phy_blocksize = val;
4222 		} else {
4223 			goto value_invalid;
4224 		}
4225 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4226 		    "physical block size set to %d\n", un->un_phy_blocksize);
4227 	}
4228 
4229 	if (strcasecmp(name, "retries-victim") == 0) {
4230 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4231 			un->un_victim_retry_count = val;
4232 		} else {
4233 			goto value_invalid;
4234 		}
4235 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4236 		    "victim retry count set to %d\n",
4237 		    un->un_victim_retry_count);
4238 		return;
4239 	}
4240 
4241 	/*
4242 	 * Validate the throttle values.
4243 	 * If any of the numbers are invalid, set everything to defaults.
4244 	 */
4245 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4246 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4247 	    (un->un_min_throttle > un->un_throttle)) {
4248 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4249 		un->un_min_throttle = sd_min_throttle;
4250 	}
4251 
4252 	if (strcasecmp(name, "mmc-gesn-polling") == 0) {
4253 		if (strcasecmp(value, "true") == 0) {
4254 			un->un_f_mmc_gesn_polling = TRUE;
4255 		} else if (strcasecmp(value, "false") == 0) {
4256 			un->un_f_mmc_gesn_polling = FALSE;
4257 		} else {
4258 			goto value_invalid;
4259 		}
4260 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4261 		    "mmc-gesn-polling set to %d\n",
4262 		    un->un_f_mmc_gesn_polling);
4263 	}
4264 
4265 	return;
4266 
4267 value_invalid:
4268 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4269 	    "value of prop %s is invalid\n", name);
4270 }
4271 
4272 /*
4273  *    Function: sd_get_tunables_from_conf()
4274  *
4275  *
4276  *    This function reads the data list from the sd.conf file and pulls
4277  *    the values that can have numeric values as arguments and places
4278  *    the values in the appropriate sd_tunables member.
4279  *    Since the order of the data list members varies across platforms
4280  *    This function reads them from the data list in a platform specific
4281  *    order and places them into the correct sd_tunable member that is
4282  *    consistent across all platforms.
4283  */
4284 static void
4285 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
4286     sd_tunables *values)
4287 {
4288 	int i;
4289 	int mask;
4290 
4291 	bzero(values, sizeof (sd_tunables));
4292 
4293 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4294 
4295 		mask = 1 << i;
4296 		if (mask > flags) {
4297 			break;
4298 		}
4299 
4300 		switch (mask & flags) {
4301 		case 0:	/* This mask bit not set in flags */
4302 			continue;
4303 		case SD_CONF_BSET_THROTTLE:
4304 			values->sdt_throttle = data_list[i];
4305 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4306 			    "sd_get_tunables_from_conf: throttle = %d\n",
4307 			    values->sdt_throttle);
4308 			break;
4309 		case SD_CONF_BSET_CTYPE:
4310 			values->sdt_ctype = data_list[i];
4311 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4312 			    "sd_get_tunables_from_conf: ctype = %d\n",
4313 			    values->sdt_ctype);
4314 			break;
4315 		case SD_CONF_BSET_NRR_COUNT:
4316 			values->sdt_not_rdy_retries = data_list[i];
4317 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4318 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
4319 			    values->sdt_not_rdy_retries);
4320 			break;
4321 		case SD_CONF_BSET_BSY_RETRY_COUNT:
4322 			values->sdt_busy_retries = data_list[i];
4323 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4324 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
4325 			    values->sdt_busy_retries);
4326 			break;
4327 		case SD_CONF_BSET_RST_RETRIES:
4328 			values->sdt_reset_retries = data_list[i];
4329 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4330 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
4331 			    values->sdt_reset_retries);
4332 			break;
4333 		case SD_CONF_BSET_RSV_REL_TIME:
4334 			values->sdt_reserv_rel_time = data_list[i];
4335 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4336 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
4337 			    values->sdt_reserv_rel_time);
4338 			break;
4339 		case SD_CONF_BSET_MIN_THROTTLE:
4340 			values->sdt_min_throttle = data_list[i];
4341 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4342 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
4343 			    values->sdt_min_throttle);
4344 			break;
4345 		case SD_CONF_BSET_DISKSORT_DISABLED:
4346 			values->sdt_disk_sort_dis = data_list[i];
4347 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4348 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
4349 			    values->sdt_disk_sort_dis);
4350 			break;
4351 		case SD_CONF_BSET_LUN_RESET_ENABLED:
4352 			values->sdt_lun_reset_enable = data_list[i];
4353 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4354 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
4355 			    "\n", values->sdt_lun_reset_enable);
4356 			break;
4357 		case SD_CONF_BSET_CACHE_IS_NV:
4358 			values->sdt_suppress_cache_flush = data_list[i];
4359 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4360 			    "sd_get_tunables_from_conf: \
4361 			    suppress_cache_flush = %d"
4362 			    "\n", values->sdt_suppress_cache_flush);
4363 			break;
4364 		case SD_CONF_BSET_PC_DISABLED:
4365 			values->sdt_disk_sort_dis = data_list[i];
4366 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4367 			    "sd_get_tunables_from_conf: power_condition_dis = "
4368 			    "%d\n", values->sdt_power_condition_dis);
4369 			break;
4370 		}
4371 	}
4372 }
4373 
4374 /*
4375  *    Function: sd_process_sdconf_table
4376  *
4377  * Description: Search the static configuration table for a match on the
4378  *		inquiry vid/pid and update the driver soft state structure
4379  *		according to the table property values for the device.
4380  *
4381  *		The form of a configuration table entry is:
4382  *		  <vid+pid>,<flags>,<property-data>
4383  *		  "SEAGATE ST42400N",1,0x40000,
4384  *		  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
4385  *
4386  *   Arguments: un - driver soft state (unit) structure
4387  */
4388 
4389 static void
4390 sd_process_sdconf_table(struct sd_lun *un)
4391 {
4392 	char	*id = NULL;
4393 	int	table_index;
4394 	int	idlen;
4395 
4396 	ASSERT(un != NULL);
4397 	for (table_index = 0; table_index < sd_disk_table_size;
4398 	    table_index++) {
4399 		id = sd_disk_table[table_index].device_id;
4400 		idlen = strlen(id);
4401 
4402 		/*
4403 		 * The static configuration table currently does not
4404 		 * implement version 10 properties. Additionally,
4405 		 * multiple data-property-name entries are not
4406 		 * implemented in the static configuration table.
4407 		 */
4408 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4409 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4410 			    "sd_process_sdconf_table: disk %s\n", id);
4411 			sd_set_vers1_properties(un,
4412 			    sd_disk_table[table_index].flags,
4413 			    sd_disk_table[table_index].properties);
4414 			break;
4415 		}
4416 	}
4417 }
4418 
4419 
4420 /*
4421  *    Function: sd_sdconf_id_match
4422  *
4423  * Description: This local function implements a case sensitive vid/pid
4424  *		comparison as well as the boundary cases of wild card and
4425  *		multiple blanks.
4426  *
4427  *		Note: An implicit assumption made here is that the scsi
4428  *		inquiry structure will always keep the vid, pid and
4429  *		revision strings in consecutive sequence, so they can be
4430  *		read as a single string. If this assumption is not the
4431  *		case, a separate string, to be used for the check, needs
4432  *		to be built with these strings concatenated.
4433  *
4434  *   Arguments: un - driver soft state (unit) structure
4435  *		id - table or config file vid/pid
4436  *		idlen  - length of the vid/pid (bytes)
4437  *
4438  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4439  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4440  */
4441 
4442 static int
4443 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4444 {
4445 	struct scsi_inquiry	*sd_inq;
4446 	int 			rval = SD_SUCCESS;
4447 
4448 	ASSERT(un != NULL);
4449 	sd_inq = un->un_sd->sd_inq;
4450 	ASSERT(id != NULL);
4451 
4452 	/*
4453 	 * We use the inq_vid as a pointer to a buffer containing the
4454 	 * vid and pid and use the entire vid/pid length of the table
4455 	 * entry for the comparison. This works because the inq_pid
4456 	 * data member follows inq_vid in the scsi_inquiry structure.
4457 	 */
4458 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4459 		/*
4460 		 * The user id string is compared to the inquiry vid/pid
4461 		 * using a case insensitive comparison and ignoring
4462 		 * multiple spaces.
4463 		 */
4464 		rval = sd_blank_cmp(un, id, idlen);
4465 		if (rval != SD_SUCCESS) {
4466 			/*
4467 			 * User id strings that start and end with a "*"
4468 			 * are a special case. These do not have a
4469 			 * specific vendor, and the product string can
4470 			 * appear anywhere in the 16 byte PID portion of
4471 			 * the inquiry data. This is a simple strstr()
4472 			 * type search for the user id in the inquiry data.
4473 			 */
4474 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4475 				char	*pidptr = &id[1];
4476 				int	i;
4477 				int	j;
4478 				int	pidstrlen = idlen - 2;
4479 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
4480 				    pidstrlen;
4481 
4482 				if (j < 0) {
4483 					return (SD_FAILURE);
4484 				}
4485 				for (i = 0; i < j; i++) {
4486 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4487 					    pidptr, pidstrlen) == 0) {
4488 						rval = SD_SUCCESS;
4489 						break;
4490 					}
4491 				}
4492 			}
4493 		}
4494 	}
4495 	return (rval);
4496 }
4497 
4498 
4499 /*
4500  *    Function: sd_blank_cmp
4501  *
4502  * Description: If the id string starts and ends with a space, treat
4503  *		multiple consecutive spaces as equivalent to a single
4504  *		space. For example, this causes a sd_disk_table entry
4505  *		of " NEC CDROM " to match a device's id string of
4506  *		"NEC       CDROM".
4507  *
4508  *		Note: The success exit condition for this routine is if
4509  *		the pointer to the table entry is '\0' and the cnt of
4510  *		the inquiry length is zero. This will happen if the inquiry
4511  *		string returned by the device is padded with spaces to be
4512  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4513  *		SCSI spec states that the inquiry string is to be padded with
4514  *		spaces.
4515  *
4516  *   Arguments: un - driver soft state (unit) structure
4517  *		id - table or config file vid/pid
4518  *		idlen  - length of the vid/pid (bytes)
4519  *
4520  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4521  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4522  */
4523 
4524 static int
4525 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4526 {
4527 	char		*p1;
4528 	char		*p2;
4529 	int		cnt;
4530 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4531 	    sizeof (SD_INQUIRY(un)->inq_pid);
4532 
4533 	ASSERT(un != NULL);
4534 	p2 = un->un_sd->sd_inq->inq_vid;
4535 	ASSERT(id != NULL);
4536 	p1 = id;
4537 
4538 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4539 		/*
4540 		 * Note: string p1 is terminated by a NUL but string p2
4541 		 * isn't.  The end of p2 is determined by cnt.
4542 		 */
4543 		for (;;) {
4544 			/* skip over any extra blanks in both strings */
4545 			while ((*p1 != '\0') && (*p1 == ' ')) {
4546 				p1++;
4547 			}
4548 			while ((cnt != 0) && (*p2 == ' ')) {
4549 				p2++;
4550 				cnt--;
4551 			}
4552 
4553 			/* compare the two strings */
4554 			if ((cnt == 0) ||
4555 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4556 				break;
4557 			}
4558 			while ((cnt > 0) &&
4559 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4560 				p1++;
4561 				p2++;
4562 				cnt--;
4563 			}
4564 		}
4565 	}
4566 
4567 	/* return SD_SUCCESS if both strings match */
4568 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4569 }
4570 
4571 
4572 /*
4573  *    Function: sd_chk_vers1_data
4574  *
4575  * Description: Verify the version 1 device properties provided by the
4576  *		user via the configuration file
4577  *
4578  *   Arguments: un	     - driver soft state (unit) structure
4579  *		flags	     - integer mask indicating properties to be set
4580  *		prop_list    - integer list of property values
4581  *		list_len     - number of the elements
4582  *
4583  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4584  *		SD_FAILURE - Indicates the user provided data is invalid
4585  */
4586 
4587 static int
4588 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4589     int list_len, char *dataname_ptr)
4590 {
4591 	int i;
4592 	int mask = 1;
4593 	int index = 0;
4594 
4595 	ASSERT(un != NULL);
4596 
4597 	/* Check for a NULL property name and list */
4598 	if (dataname_ptr == NULL) {
4599 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4600 		    "sd_chk_vers1_data: NULL data property name.");
4601 		return (SD_FAILURE);
4602 	}
4603 	if (prop_list == NULL) {
4604 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4605 		    "sd_chk_vers1_data: %s NULL data property list.",
4606 		    dataname_ptr);
4607 		return (SD_FAILURE);
4608 	}
4609 
4610 	/* Display a warning if undefined bits are set in the flags */
4611 	if (flags & ~SD_CONF_BIT_MASK) {
4612 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4613 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4614 		    "Properties not set.",
4615 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4616 		return (SD_FAILURE);
4617 	}
4618 
4619 	/*
4620 	 * Verify the length of the list by identifying the highest bit set
4621 	 * in the flags and validating that the property list has a length
4622 	 * up to the index of this bit.
4623 	 */
4624 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4625 		if (flags & mask) {
4626 			index++;
4627 		}
4628 		mask = 1 << i;
4629 	}
4630 	if (list_len < (index + 2)) {
4631 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4632 		    "sd_chk_vers1_data: "
4633 		    "Data property list %s size is incorrect. "
4634 		    "Properties not set.", dataname_ptr);
4635 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4636 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4637 		return (SD_FAILURE);
4638 	}
4639 	return (SD_SUCCESS);
4640 }
4641 
4642 
4643 /*
4644  *    Function: sd_set_vers1_properties
4645  *
4646  * Description: Set version 1 device properties based on a property list
4647  *		retrieved from the driver configuration file or static
4648  *		configuration table. Version 1 properties have the format:
4649  *
4650  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4651  *
4652  *		where the prop0 value will be used to set prop0 if bit0
4653  *		is set in the flags
4654  *
4655  *   Arguments: un	     - driver soft state (unit) structure
4656  *		flags	     - integer mask indicating properties to be set
4657  *		prop_list    - integer list of property values
4658  */
4659 
4660 static void
4661 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4662 {
4663 	ASSERT(un != NULL);
4664 
4665 	/*
4666 	 * Set the flag to indicate cache is to be disabled. An attempt
4667 	 * to disable the cache via sd_cache_control() will be made
4668 	 * later during attach once the basic initialization is complete.
4669 	 */
4670 	if (flags & SD_CONF_BSET_NOCACHE) {
4671 		un->un_f_opt_disable_cache = TRUE;
4672 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4673 		    "sd_set_vers1_properties: caching disabled flag set\n");
4674 	}
4675 
4676 	/* CD-specific configuration parameters */
4677 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4678 		un->un_f_cfg_playmsf_bcd = TRUE;
4679 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4680 		    "sd_set_vers1_properties: playmsf_bcd set\n");
4681 	}
4682 	if (flags & SD_CONF_BSET_READSUB_BCD) {
4683 		un->un_f_cfg_readsub_bcd = TRUE;
4684 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4685 		    "sd_set_vers1_properties: readsub_bcd set\n");
4686 	}
4687 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4688 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
4689 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4690 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4691 	}
4692 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4693 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
4694 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4695 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4696 	}
4697 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4698 		un->un_f_cfg_no_read_header = TRUE;
4699 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4700 		    "sd_set_vers1_properties: no_read_header set\n");
4701 	}
4702 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
4703 		un->un_f_cfg_read_cd_xd4 = TRUE;
4704 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4705 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
4706 	}
4707 
4708 	/* Support for devices which do not have valid/unique serial numbers */
4709 	if (flags & SD_CONF_BSET_FAB_DEVID) {
4710 		un->un_f_opt_fab_devid = TRUE;
4711 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4712 		    "sd_set_vers1_properties: fab_devid bit set\n");
4713 	}
4714 
4715 	/* Support for user throttle configuration */
4716 	if (flags & SD_CONF_BSET_THROTTLE) {
4717 		ASSERT(prop_list != NULL);
4718 		un->un_saved_throttle = un->un_throttle =
4719 		    prop_list->sdt_throttle;
4720 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4721 		    "sd_set_vers1_properties: throttle set to %d\n",
4722 		    prop_list->sdt_throttle);
4723 	}
4724 
4725 	/* Set the per disk retry count according to the conf file or table. */
4726 	if (flags & SD_CONF_BSET_NRR_COUNT) {
4727 		ASSERT(prop_list != NULL);
4728 		if (prop_list->sdt_not_rdy_retries) {
4729 			un->un_notready_retry_count =
4730 			    prop_list->sdt_not_rdy_retries;
4731 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4732 			    "sd_set_vers1_properties: not ready retry count"
4733 			    " set to %d\n", un->un_notready_retry_count);
4734 		}
4735 	}
4736 
4737 	/* The controller type is reported for generic disk driver ioctls */
4738 	if (flags & SD_CONF_BSET_CTYPE) {
4739 		ASSERT(prop_list != NULL);
4740 		switch (prop_list->sdt_ctype) {
4741 		case CTYPE_CDROM:
4742 			un->un_ctype = prop_list->sdt_ctype;
4743 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4744 			    "sd_set_vers1_properties: ctype set to "
4745 			    "CTYPE_CDROM\n");
4746 			break;
4747 		case CTYPE_CCS:
4748 			un->un_ctype = prop_list->sdt_ctype;
4749 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4750 			    "sd_set_vers1_properties: ctype set to "
4751 			    "CTYPE_CCS\n");
4752 			break;
4753 		case CTYPE_ROD:		/* RW optical */
4754 			un->un_ctype = prop_list->sdt_ctype;
4755 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4756 			    "sd_set_vers1_properties: ctype set to "
4757 			    "CTYPE_ROD\n");
4758 			break;
4759 		default:
4760 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4761 			    "sd_set_vers1_properties: Could not set "
4762 			    "invalid ctype value (%d)",
4763 			    prop_list->sdt_ctype);
4764 		}
4765 	}
4766 
4767 	/* Purple failover timeout */
4768 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4769 		ASSERT(prop_list != NULL);
4770 		un->un_busy_retry_count =
4771 		    prop_list->sdt_busy_retries;
4772 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4773 		    "sd_set_vers1_properties: "
4774 		    "busy retry count set to %d\n",
4775 		    un->un_busy_retry_count);
4776 	}
4777 
4778 	/* Purple reset retry count */
4779 	if (flags & SD_CONF_BSET_RST_RETRIES) {
4780 		ASSERT(prop_list != NULL);
4781 		un->un_reset_retry_count =
4782 		    prop_list->sdt_reset_retries;
4783 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4784 		    "sd_set_vers1_properties: "
4785 		    "reset retry count set to %d\n",
4786 		    un->un_reset_retry_count);
4787 	}
4788 
4789 	/* Purple reservation release timeout */
4790 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4791 		ASSERT(prop_list != NULL);
4792 		un->un_reserve_release_time =
4793 		    prop_list->sdt_reserv_rel_time;
4794 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4795 		    "sd_set_vers1_properties: "
4796 		    "reservation release timeout set to %d\n",
4797 		    un->un_reserve_release_time);
4798 	}
4799 
4800 	/*
4801 	 * Driver flag telling the driver to verify that no commands are pending
4802 	 * for a device before issuing a Test Unit Ready. This is a workaround
4803 	 * for a firmware bug in some Seagate eliteI drives.
4804 	 */
4805 	if (flags & SD_CONF_BSET_TUR_CHECK) {
4806 		un->un_f_cfg_tur_check = TRUE;
4807 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4808 		    "sd_set_vers1_properties: tur queue check set\n");
4809 	}
4810 
4811 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4812 		un->un_min_throttle = prop_list->sdt_min_throttle;
4813 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4814 		    "sd_set_vers1_properties: min throttle set to %d\n",
4815 		    un->un_min_throttle);
4816 	}
4817 
4818 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4819 		un->un_f_disksort_disabled =
4820 		    (prop_list->sdt_disk_sort_dis != 0) ?
4821 		    TRUE : FALSE;
4822 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4823 		    "sd_set_vers1_properties: disksort disabled "
4824 		    "flag set to %d\n",
4825 		    prop_list->sdt_disk_sort_dis);
4826 	}
4827 
4828 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4829 		un->un_f_lun_reset_enabled =
4830 		    (prop_list->sdt_lun_reset_enable != 0) ?
4831 		    TRUE : FALSE;
4832 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4833 		    "sd_set_vers1_properties: lun reset enabled "
4834 		    "flag set to %d\n",
4835 		    prop_list->sdt_lun_reset_enable);
4836 	}
4837 
4838 	if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4839 		un->un_f_suppress_cache_flush =
4840 		    (prop_list->sdt_suppress_cache_flush != 0) ?
4841 		    TRUE : FALSE;
4842 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4843 		    "sd_set_vers1_properties: suppress_cache_flush "
4844 		    "flag set to %d\n",
4845 		    prop_list->sdt_suppress_cache_flush);
4846 	}
4847 
4848 	if (flags & SD_CONF_BSET_PC_DISABLED) {
4849 		un->un_f_power_condition_disabled =
4850 		    (prop_list->sdt_power_condition_dis != 0) ?
4851 		    TRUE : FALSE;
4852 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4853 		    "sd_set_vers1_properties: power_condition_disabled "
4854 		    "flag set to %d\n",
4855 		    prop_list->sdt_power_condition_dis);
4856 	}
4857 
4858 	/*
4859 	 * Validate the throttle values.
4860 	 * If any of the numbers are invalid, set everything to defaults.
4861 	 */
4862 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4863 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4864 	    (un->un_min_throttle > un->un_throttle)) {
4865 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4866 		un->un_min_throttle = sd_min_throttle;
4867 	}
4868 }
4869 
4870 /*
4871  *   Function: sd_is_lsi()
4872  *
4873  *   Description: Check for lsi devices, step through the static device
4874  *	table to match vid/pid.
4875  *
4876  *   Args: un - ptr to sd_lun
4877  *
4878  *   Notes:  When creating new LSI property, need to add the new LSI property
4879  *		to this function.
4880  */
4881 static void
4882 sd_is_lsi(struct sd_lun *un)
4883 {
4884 	char	*id = NULL;
4885 	int	table_index;
4886 	int	idlen;
4887 	void	*prop;
4888 
4889 	ASSERT(un != NULL);
4890 	for (table_index = 0; table_index < sd_disk_table_size;
4891 	    table_index++) {
4892 		id = sd_disk_table[table_index].device_id;
4893 		idlen = strlen(id);
4894 		if (idlen == 0) {
4895 			continue;
4896 		}
4897 
4898 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4899 			prop = sd_disk_table[table_index].properties;
4900 			if (prop == &lsi_properties ||
4901 			    prop == &lsi_oem_properties ||
4902 			    prop == &lsi_properties_scsi ||
4903 			    prop == &symbios_properties) {
4904 				un->un_f_cfg_is_lsi = TRUE;
4905 			}
4906 			break;
4907 		}
4908 	}
4909 }
4910 
4911 /*
4912  *    Function: sd_get_physical_geometry
4913  *
4914  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4915  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4916  *		target, and use this information to initialize the physical
4917  *		geometry cache specified by pgeom_p.
4918  *
4919  *		MODE SENSE is an optional command, so failure in this case
4920  *		does not necessarily denote an error. We want to use the
4921  *		MODE SENSE commands to derive the physical geometry of the
4922  *		device, but if either command fails, the logical geometry is
4923  *		used as the fallback for disk label geometry in cmlb.
4924  *
4925  *		This requires that un->un_blockcount and un->un_tgt_blocksize
4926  *		have already been initialized for the current target and
4927  *		that the current values be passed as args so that we don't
4928  *		end up ever trying to use -1 as a valid value. This could
4929  *		happen if either value is reset while we're not holding
4930  *		the mutex.
4931  *
4932  *   Arguments: un - driver soft state (unit) structure
4933  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4934  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4935  *			to use the USCSI "direct" chain and bypass the normal
4936  *			command waitq.
4937  *
4938  *     Context: Kernel thread only (can sleep).
4939  */
4940 
4941 static int
4942 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4943     diskaddr_t capacity, int lbasize, int path_flag)
4944 {
4945 	struct	mode_format	*page3p;
4946 	struct	mode_geometry	*page4p;
4947 	struct	mode_header	*headerp;
4948 	int	sector_size;
4949 	int	nsect;
4950 	int	nhead;
4951 	int	ncyl;
4952 	int	intrlv;
4953 	int	spc;
4954 	diskaddr_t	modesense_capacity;
4955 	int	rpm;
4956 	int	bd_len;
4957 	int	mode_header_length;
4958 	uchar_t	*p3bufp;
4959 	uchar_t	*p4bufp;
4960 	int	cdbsize;
4961 	int 	ret = EIO;
4962 	sd_ssc_t *ssc;
4963 	int	status;
4964 
4965 	ASSERT(un != NULL);
4966 
4967 	if (lbasize == 0) {
4968 		if (ISCD(un)) {
4969 			lbasize = 2048;
4970 		} else {
4971 			lbasize = un->un_sys_blocksize;
4972 		}
4973 	}
4974 	pgeom_p->g_secsize = (unsigned short)lbasize;
4975 
4976 	/*
4977 	 * If the unit is a cd/dvd drive MODE SENSE page three
4978 	 * and MODE SENSE page four are reserved (see SBC spec
4979 	 * and MMC spec). To prevent soft errors just return
4980 	 * using the default LBA size.
4981 	 *
4982 	 * Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
4983 	 * implement support for mode pages 3 and 4 return here to prevent
4984 	 * illegal requests on SATA drives.
4985 	 *
4986 	 * These pages are also reserved in SBC-2 and later.  We assume SBC-2
4987 	 * or later for a direct-attached block device if the SCSI version is
4988 	 * at least SPC-3.
4989 	 */
4990 
4991 	if (ISCD(un) ||
4992 	    un->un_interconnect_type == SD_INTERCONNECT_SATA ||
4993 	    (un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
4994 		return (ret);
4995 
4996 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4997 
4998 	/*
4999 	 * Retrieve MODE SENSE page 3 - Format Device Page
5000 	 */
5001 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
5002 	ssc = sd_ssc_init(un);
5003 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
5004 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
5005 	if (status != 0) {
5006 		SD_ERROR(SD_LOG_COMMON, un,
5007 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
5008 		goto page3_exit;
5009 	}
5010 
5011 	/*
5012 	 * Determine size of Block Descriptors in order to locate the mode
5013 	 * page data.  ATAPI devices return 0, SCSI devices should return
5014 	 * MODE_BLK_DESC_LENGTH.
5015 	 */
5016 	headerp = (struct mode_header *)p3bufp;
5017 	if (un->un_f_cfg_is_atapi == TRUE) {
5018 		struct mode_header_grp2 *mhp =
5019 		    (struct mode_header_grp2 *)headerp;
5020 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
5021 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5022 	} else {
5023 		mode_header_length = MODE_HEADER_LENGTH;
5024 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5025 	}
5026 
5027 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5028 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5029 		    "sd_get_physical_geometry: received unexpected bd_len "
5030 		    "of %d, page3\n", bd_len);
5031 		status = EIO;
5032 		goto page3_exit;
5033 	}
5034 
5035 	page3p = (struct mode_format *)
5036 	    ((caddr_t)headerp + mode_header_length + bd_len);
5037 
5038 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5039 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5040 		    "sd_get_physical_geometry: mode sense pg3 code mismatch "
5041 		    "%d\n", page3p->mode_page.code);
5042 		status = EIO;
5043 		goto page3_exit;
5044 	}
5045 
5046 	/*
5047 	 * Use this physical geometry data only if BOTH MODE SENSE commands
5048 	 * complete successfully; otherwise, revert to the logical geometry.
5049 	 * So, we need to save everything in temporary variables.
5050 	 */
5051 	sector_size = BE_16(page3p->data_bytes_sect);
5052 
5053 	/*
5054 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5055 	 */
5056 	if (sector_size == 0) {
5057 		sector_size = un->un_sys_blocksize;
5058 	} else {
5059 		sector_size &= ~(un->un_sys_blocksize - 1);
5060 	}
5061 
5062 	nsect  = BE_16(page3p->sect_track);
5063 	intrlv = BE_16(page3p->interleave);
5064 
5065 	SD_INFO(SD_LOG_COMMON, un,
5066 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
5067 	SD_INFO(SD_LOG_COMMON, un,
5068 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
5069 	    page3p->mode_page.code, nsect, sector_size);
5070 	SD_INFO(SD_LOG_COMMON, un,
5071 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5072 	    BE_16(page3p->track_skew),
5073 	    BE_16(page3p->cylinder_skew));
5074 
5075 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5076 
5077 	/*
5078 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5079 	 */
5080 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5081 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
5082 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
5083 	if (status != 0) {
5084 		SD_ERROR(SD_LOG_COMMON, un,
5085 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
5086 		goto page4_exit;
5087 	}
5088 
5089 	/*
5090 	 * Determine size of Block Descriptors in order to locate the mode
5091 	 * page data.  ATAPI devices return 0, SCSI devices should return
5092 	 * MODE_BLK_DESC_LENGTH.
5093 	 */
5094 	headerp = (struct mode_header *)p4bufp;
5095 	if (un->un_f_cfg_is_atapi == TRUE) {
5096 		struct mode_header_grp2 *mhp =
5097 		    (struct mode_header_grp2 *)headerp;
5098 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5099 	} else {
5100 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5101 	}
5102 
5103 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5104 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5105 		    "sd_get_physical_geometry: received unexpected bd_len of "
5106 		    "%d, page4\n", bd_len);
5107 		status = EIO;
5108 		goto page4_exit;
5109 	}
5110 
5111 	page4p = (struct mode_geometry *)
5112 	    ((caddr_t)headerp + mode_header_length + bd_len);
5113 
5114 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5115 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5116 		    "sd_get_physical_geometry: mode sense pg4 code mismatch "
5117 		    "%d\n", page4p->mode_page.code);
5118 		status = EIO;
5119 		goto page4_exit;
5120 	}
5121 
5122 	/*
5123 	 * Stash the data now, after we know that both commands completed.
5124 	 */
5125 
5126 
5127 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
5128 	spc   = nhead * nsect;
5129 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5130 	rpm   = BE_16(page4p->rpm);
5131 
5132 	modesense_capacity = spc * ncyl;
5133 
5134 	SD_INFO(SD_LOG_COMMON, un,
5135 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5136 	SD_INFO(SD_LOG_COMMON, un,
5137 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5138 	SD_INFO(SD_LOG_COMMON, un,
5139 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
5140 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
5141 	    (void *)pgeom_p, capacity);
5142 
5143 	/*
5144 	 * Compensate if the drive's geometry is not rectangular, i.e.,
5145 	 * the product of C * H * S returned by MODE SENSE >= that returned
5146 	 * by read capacity. This is an idiosyncrasy of the original x86
5147 	 * disk subsystem.
5148 	 */
5149 	if (modesense_capacity >= capacity) {
5150 		SD_INFO(SD_LOG_COMMON, un,
5151 		    "sd_get_physical_geometry: adjusting acyl; "
5152 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
5153 		    (modesense_capacity - capacity + spc - 1) / spc);
5154 		if (sector_size != 0) {
5155 			/* 1243403: NEC D38x7 drives don't support sec size */
5156 			pgeom_p->g_secsize = (unsigned short)sector_size;
5157 		}
5158 		pgeom_p->g_nsect    = (unsigned short)nsect;
5159 		pgeom_p->g_nhead    = (unsigned short)nhead;
5160 		pgeom_p->g_capacity = capacity;
5161 		pgeom_p->g_acyl	    =
5162 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5163 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5164 	}
5165 
5166 	pgeom_p->g_rpm    = (unsigned short)rpm;
5167 	pgeom_p->g_intrlv = (unsigned short)intrlv;
5168 	ret = 0;
5169 
5170 	SD_INFO(SD_LOG_COMMON, un,
5171 	    "sd_get_physical_geometry: mode sense geometry:\n");
5172 	SD_INFO(SD_LOG_COMMON, un,
5173 	    "   nsect: %d; sector size: %d; interlv: %d\n",
5174 	    nsect, sector_size, intrlv);
5175 	SD_INFO(SD_LOG_COMMON, un,
5176 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5177 	    nhead, ncyl, rpm, modesense_capacity);
5178 	SD_INFO(SD_LOG_COMMON, un,
5179 	    "sd_get_physical_geometry: (cached)\n");
5180 	SD_INFO(SD_LOG_COMMON, un,
5181 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5182 	    pgeom_p->g_ncyl,  pgeom_p->g_acyl,
5183 	    pgeom_p->g_nhead, pgeom_p->g_nsect);
5184 	SD_INFO(SD_LOG_COMMON, un,
5185 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5186 	    pgeom_p->g_secsize, pgeom_p->g_capacity,
5187 	    pgeom_p->g_intrlv, pgeom_p->g_rpm);
5188 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5189 
5190 page4_exit:
5191 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5192 
5193 page3_exit:
5194 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5195 
5196 	if (status != 0) {
5197 		if (status == EIO) {
5198 			/*
5199 			 * Some disks do not support mode sense(6), we
5200 			 * should ignore this kind of error(sense key is
5201 			 * 0x5 - illegal request).
5202 			 */
5203 			uint8_t *sensep;
5204 			int senlen;
5205 
5206 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
5207 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
5208 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
5209 
5210 			if (senlen > 0 &&
5211 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
5212 				sd_ssc_assessment(ssc,
5213 				    SD_FMT_IGNORE_COMPROMISE);
5214 			} else {
5215 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
5216 			}
5217 		} else {
5218 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5219 		}
5220 	}
5221 	sd_ssc_fini(ssc);
5222 	return (ret);
5223 }
5224 
5225 /*
5226  *    Function: sd_get_virtual_geometry
5227  *
5228  * Description: Ask the controller to tell us about the target device.
5229  *
5230  *   Arguments: un - pointer to softstate
5231  *		capacity - disk capacity in #blocks
5232  *		lbasize - disk block size in bytes
5233  *
5234  *     Context: Kernel thread only
5235  */
5236 
5237 static int
5238 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
5239     diskaddr_t capacity, int lbasize)
5240 {
5241 	uint_t	geombuf;
5242 	int	spc;
5243 
5244 	ASSERT(un != NULL);
5245 
5246 	/* Set sector size, and total number of sectors */
5247 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5248 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5249 
5250 	/* Let the HBA tell us its geometry */
5251 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5252 
5253 	/* A value of -1 indicates an undefined "geometry" property */
5254 	if (geombuf == (-1)) {
5255 		return (EINVAL);
5256 	}
5257 
5258 	/* Initialize the logical geometry cache. */
5259 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5260 	lgeom_p->g_nsect   = geombuf & 0xffff;
5261 	lgeom_p->g_secsize = un->un_sys_blocksize;
5262 
5263 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5264 
5265 	/*
5266 	 * Note: The driver originally converted the capacity value from
5267 	 * target blocks to system blocks. However, the capacity value passed
5268 	 * to this routine is already in terms of system blocks (this scaling
5269 	 * is done when the READ CAPACITY command is issued and processed).
5270 	 * This 'error' may have gone undetected because the usage of g_ncyl
5271 	 * (which is based upon g_capacity) is very limited within the driver
5272 	 */
5273 	lgeom_p->g_capacity = capacity;
5274 
5275 	/*
5276 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5277 	 * hba may return zero values if the device has been removed.
5278 	 */
5279 	if (spc == 0) {
5280 		lgeom_p->g_ncyl = 0;
5281 	} else {
5282 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5283 	}
5284 	lgeom_p->g_acyl = 0;
5285 
5286 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5287 	return (0);
5288 
5289 }
5290 /*
5291  *    Function: sd_update_block_info
5292  *
5293  * Description: Calculate a byte count to sector count bitshift value
5294  *		from sector size.
5295  *
5296  *   Arguments: un: unit struct.
5297  *		lbasize: new target sector size
5298  *		capacity: new target capacity, ie. block count
5299  *
5300  *     Context: Kernel thread context
5301  */
5302 
5303 static void
5304 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5305 {
5306 	if (lbasize != 0) {
5307 		un->un_tgt_blocksize = lbasize;
5308 		un->un_f_tgt_blocksize_is_valid = TRUE;
5309 		if (!un->un_f_has_removable_media) {
5310 			un->un_sys_blocksize = lbasize;
5311 		}
5312 	}
5313 
5314 	if (capacity != 0) {
5315 		un->un_blockcount		= capacity;
5316 		un->un_f_blockcount_is_valid	= TRUE;
5317 
5318 		/*
5319 		 * The capacity has changed so update the errstats.
5320 		 */
5321 		if (un->un_errstats != NULL) {
5322 			struct sd_errstats *stp;
5323 
5324 			capacity *= un->un_sys_blocksize;
5325 			stp = (struct sd_errstats *)un->un_errstats->ks_data;
5326 			if (stp->sd_capacity.value.ui64 < capacity)
5327 				stp->sd_capacity.value.ui64 = capacity;
5328 		}
5329 	}
5330 }
5331 
5332 /*
5333  * Parses the SCSI Block Limits VPD page (0xB0). It's legal to pass NULL for
5334  * vpd_pg, in which case all the block limits will be reset to the defaults.
5335  */
5336 static void
5337 sd_parse_blk_limits_vpd(struct sd_lun *un, uchar_t *vpd_pg)
5338 {
5339 	sd_blk_limits_t *lim = &un->un_blk_lim;
5340 	unsigned pg_len;
5341 
5342 	if (vpd_pg != NULL)
5343 		pg_len = BE_IN16(&vpd_pg[2]);
5344 	else
5345 		pg_len = 0;
5346 
5347 	/* Block Limits VPD can be 16 bytes or 64 bytes long - support both */
5348 	if (pg_len >= 0x10) {
5349 		lim->lim_opt_xfer_len_gran = BE_IN16(&vpd_pg[6]);
5350 		lim->lim_max_xfer_len = BE_IN32(&vpd_pg[8]);
5351 		lim->lim_opt_xfer_len = BE_IN32(&vpd_pg[12]);
5352 
5353 		/* Zero means not reported, so use "unlimited" */
5354 		if (lim->lim_max_xfer_len == 0)
5355 			lim->lim_max_xfer_len = UINT32_MAX;
5356 		if (lim->lim_opt_xfer_len == 0)
5357 			lim->lim_opt_xfer_len = UINT32_MAX;
5358 	} else {
5359 		lim->lim_opt_xfer_len_gran = 0;
5360 		lim->lim_max_xfer_len = UINT32_MAX;
5361 		lim->lim_opt_xfer_len = UINT32_MAX;
5362 	}
5363 	if (pg_len >= 0x3c) {
5364 		lim->lim_max_pfetch_len = BE_IN32(&vpd_pg[16]);
5365 		/*
5366 		 * A zero in either of the following two fields indicates lack
5367 		 * of UNMAP support.
5368 		 */
5369 		lim->lim_max_unmap_lba_cnt = BE_IN32(&vpd_pg[20]);
5370 		lim->lim_max_unmap_descr_cnt = BE_IN32(&vpd_pg[24]);
5371 		lim->lim_opt_unmap_gran = BE_IN32(&vpd_pg[28]);
5372 		if ((vpd_pg[32] >> 7) == 1) {
5373 			lim->lim_unmap_gran_align =
5374 			    ((vpd_pg[32] & 0x7f) << 24) | (vpd_pg[33] << 16) |
5375 			    (vpd_pg[34] << 8) | vpd_pg[35];
5376 		} else {
5377 			lim->lim_unmap_gran_align = 0;
5378 		}
5379 		lim->lim_max_write_same_len = BE_IN64(&vpd_pg[36]);
5380 	} else {
5381 		lim->lim_max_pfetch_len = UINT32_MAX;
5382 		lim->lim_max_unmap_lba_cnt = UINT32_MAX;
5383 		lim->lim_max_unmap_descr_cnt = SD_UNMAP_MAX_DESCR;
5384 		lim->lim_opt_unmap_gran = 0;
5385 		lim->lim_unmap_gran_align = 0;
5386 		lim->lim_max_write_same_len = UINT64_MAX;
5387 	}
5388 }
5389 
5390 /*
5391  * Collects VPD page B0 data if available (block limits). If the data is
5392  * not available or querying the device failed, we revert to the defaults.
5393  */
5394 static void
5395 sd_setup_blk_limits(sd_ssc_t *ssc)
5396 {
5397 	struct sd_lun	*un		= ssc->ssc_un;
5398 	uchar_t		*inqB0		= NULL;
5399 	size_t		inqB0_resid	= 0;
5400 	int		rval;
5401 
5402 	if (un->un_vpd_page_mask & SD_VPD_BLK_LIMITS_PG) {
5403 		inqB0 = kmem_zalloc(MAX_INQUIRY_SIZE, KM_SLEEP);
5404 		rval = sd_send_scsi_INQUIRY(ssc, inqB0, MAX_INQUIRY_SIZE, 0x01,
5405 		    0xB0, &inqB0_resid);
5406 		if (rval != 0) {
5407 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5408 			kmem_free(inqB0, MAX_INQUIRY_SIZE);
5409 			inqB0 = NULL;
5410 		}
5411 	}
5412 	/* passing NULL inqB0 will reset to defaults */
5413 	sd_parse_blk_limits_vpd(ssc->ssc_un, inqB0);
5414 	if (inqB0)
5415 		kmem_free(inqB0, MAX_INQUIRY_SIZE);
5416 }
5417 
5418 /*
5419  *    Function: sd_register_devid
5420  *
5421  * Description: This routine will obtain the device id information from the
5422  *		target, obtain the serial number, and register the device
5423  *		id with the ddi framework.
5424  *
5425  *   Arguments: devi - the system's dev_info_t for the device.
5426  *		un - driver soft state (unit) structure
5427  *		reservation_flag - indicates if a reservation conflict
5428  *		occurred during attach
5429  *
5430  *     Context: Kernel Thread
5431  */
5432 static void
5433 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5434 {
5435 	int		rval		= 0;
5436 	uchar_t		*inq80		= NULL;
5437 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
5438 	size_t		inq80_resid	= 0;
5439 	uchar_t		*inq83		= NULL;
5440 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
5441 	size_t		inq83_resid	= 0;
5442 	int		dlen, len;
5443 	char		*sn;
5444 	struct sd_lun	*un;
5445 
5446 	ASSERT(ssc != NULL);
5447 	un = ssc->ssc_un;
5448 	ASSERT(un != NULL);
5449 	ASSERT(mutex_owned(SD_MUTEX(un)));
5450 	ASSERT((SD_DEVINFO(un)) == devi);
5451 
5452 
5453 	/*
5454 	 * We check the availability of the World Wide Name (0x83) and Unit
5455 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5456 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5457 	 * 0x83 is available, that is the best choice.  Our next choice is
5458 	 * 0x80.  If neither are available, we munge the devid from the device
5459 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5460 	 * to fabricate a devid for non-Sun qualified disks.
5461 	 */
5462 	if (sd_check_vpd_page_support(ssc) == 0) {
5463 		/* collect page 80 data if available */
5464 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5465 
5466 			mutex_exit(SD_MUTEX(un));
5467 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5468 
5469 			rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5470 			    0x01, 0x80, &inq80_resid);
5471 
5472 			if (rval != 0) {
5473 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5474 				kmem_free(inq80, inq80_len);
5475 				inq80 = NULL;
5476 				inq80_len = 0;
5477 			} else if (ddi_prop_exists(
5478 			    DDI_DEV_T_NONE, SD_DEVINFO(un),
5479 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5480 			    INQUIRY_SERIAL_NO) == 0) {
5481 				/*
5482 				 * If we don't already have a serial number
5483 				 * property, do quick verify of data returned
5484 				 * and define property.
5485 				 */
5486 				dlen = inq80_len - inq80_resid;
5487 				len = (size_t)inq80[3];
5488 				if ((dlen >= 4) && ((len + 4) <= dlen)) {
5489 					/*
5490 					 * Ensure sn termination, skip leading
5491 					 * blanks, and create property
5492 					 * 'inquiry-serial-no'.
5493 					 */
5494 					sn = (char *)&inq80[4];
5495 					sn[len] = 0;
5496 					while (*sn && (*sn == ' '))
5497 						sn++;
5498 					if (*sn) {
5499 						(void) ddi_prop_update_string(
5500 						    DDI_DEV_T_NONE,
5501 						    SD_DEVINFO(un),
5502 						    INQUIRY_SERIAL_NO, sn);
5503 					}
5504 				}
5505 			}
5506 			mutex_enter(SD_MUTEX(un));
5507 		}
5508 
5509 		/* collect page 83 data if available */
5510 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5511 			mutex_exit(SD_MUTEX(un));
5512 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5513 
5514 			rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5515 			    0x01, 0x83, &inq83_resid);
5516 
5517 			if (rval != 0) {
5518 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5519 				kmem_free(inq83, inq83_len);
5520 				inq83 = NULL;
5521 				inq83_len = 0;
5522 			}
5523 			mutex_enter(SD_MUTEX(un));
5524 		}
5525 	}
5526 
5527 	/*
5528 	 * If transport has already registered a devid for this target
5529 	 * then that takes precedence over the driver's determination
5530 	 * of the devid.
5531 	 *
5532 	 * NOTE: The reason this check is done here instead of at the beginning
5533 	 * of the function is to allow the code above to create the
5534 	 * 'inquiry-serial-no' property.
5535 	 */
5536 	if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5537 		ASSERT(un->un_devid);
5538 		un->un_f_devid_transport_defined = TRUE;
5539 		goto cleanup; /* use devid registered by the transport */
5540 	}
5541 
5542 	/*
5543 	 * This is the case of antiquated Sun disk drives that have the
5544 	 * FAB_DEVID property set in the disk_table.  These drives
5545 	 * manage the devid's by storing them in last 2 available sectors
5546 	 * on the drive and have them fabricated by the ddi layer by calling
5547 	 * ddi_devid_init and passing the DEVID_FAB flag.
5548 	 */
5549 	if (un->un_f_opt_fab_devid == TRUE) {
5550 		/*
5551 		 * Depending on EINVAL isn't reliable, since a reserved disk
5552 		 * may result in invalid geometry, so check to make sure a
5553 		 * reservation conflict did not occur during attach.
5554 		 */
5555 		if ((sd_get_devid(ssc) == EINVAL) &&
5556 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
5557 			/*
5558 			 * The devid is invalid AND there is no reservation
5559 			 * conflict.  Fabricate a new devid.
5560 			 */
5561 			(void) sd_create_devid(ssc);
5562 		}
5563 
5564 		/* Register the devid if it exists */
5565 		if (un->un_devid != NULL) {
5566 			(void) ddi_devid_register(SD_DEVINFO(un),
5567 			    un->un_devid);
5568 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5569 			    "sd_register_devid: Devid Fabricated\n");
5570 		}
5571 		goto cleanup;
5572 	}
5573 
5574 	/* encode best devid possible based on data available */
5575 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5576 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
5577 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5578 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
5579 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5580 
5581 		/* devid successfully encoded, register devid */
5582 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5583 
5584 	} else {
5585 		/*
5586 		 * Unable to encode a devid based on data available.
5587 		 * This is not a Sun qualified disk.  Older Sun disk
5588 		 * drives that have the SD_FAB_DEVID property
5589 		 * set in the disk_table and non Sun qualified
5590 		 * disks are treated in the same manner.  These
5591 		 * drives manage the devid's by storing them in
5592 		 * last 2 available sectors on the drive and
5593 		 * have them fabricated by the ddi layer by
5594 		 * calling ddi_devid_init and passing the
5595 		 * DEVID_FAB flag.
5596 		 * Create a fabricate devid only if there's no
5597 		 * fabricate devid existed.
5598 		 */
5599 		if (sd_get_devid(ssc) == EINVAL) {
5600 			(void) sd_create_devid(ssc);
5601 		}
5602 		un->un_f_opt_fab_devid = TRUE;
5603 
5604 		/* Register the devid if it exists */
5605 		if (un->un_devid != NULL) {
5606 			(void) ddi_devid_register(SD_DEVINFO(un),
5607 			    un->un_devid);
5608 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5609 			    "sd_register_devid: devid fabricated using "
5610 			    "ddi framework\n");
5611 		}
5612 	}
5613 
5614 cleanup:
5615 	/* clean up resources */
5616 	if (inq80 != NULL) {
5617 		kmem_free(inq80, inq80_len);
5618 	}
5619 	if (inq83 != NULL) {
5620 		kmem_free(inq83, inq83_len);
5621 	}
5622 }
5623 
5624 
5625 
5626 /*
5627  *    Function: sd_get_devid
5628  *
5629  * Description: This routine will return 0 if a valid device id has been
5630  *		obtained from the target and stored in the soft state. If a
5631  *		valid device id has not been previously read and stored, a
5632  *		read attempt will be made.
5633  *
5634  *   Arguments: un - driver soft state (unit) structure
5635  *
5636  * Return Code: 0 if we successfully get the device id
5637  *
5638  *     Context: Kernel Thread
5639  */
5640 
5641 static int
5642 sd_get_devid(sd_ssc_t *ssc)
5643 {
5644 	struct dk_devid		*dkdevid;
5645 	ddi_devid_t		tmpid;
5646 	uint_t			*ip;
5647 	size_t			sz;
5648 	diskaddr_t		blk;
5649 	int			status;
5650 	int			chksum;
5651 	int			i;
5652 	size_t			buffer_size;
5653 	struct sd_lun		*un;
5654 
5655 	ASSERT(ssc != NULL);
5656 	un = ssc->ssc_un;
5657 	ASSERT(un != NULL);
5658 	ASSERT(mutex_owned(SD_MUTEX(un)));
5659 
5660 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5661 	    un);
5662 
5663 	if (un->un_devid != NULL) {
5664 		return (0);
5665 	}
5666 
5667 	mutex_exit(SD_MUTEX(un));
5668 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5669 	    (void *)SD_PATH_DIRECT) != 0) {
5670 		mutex_enter(SD_MUTEX(un));
5671 		return (EINVAL);
5672 	}
5673 
5674 	/*
5675 	 * Read and verify device id, stored in the reserved cylinders at the
5676 	 * end of the disk. Backup label is on the odd sectors of the last
5677 	 * track of the last cylinder. Device id will be on track of the next
5678 	 * to last cylinder.
5679 	 */
5680 	mutex_enter(SD_MUTEX(un));
5681 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5682 	mutex_exit(SD_MUTEX(un));
5683 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5684 	status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5685 	    SD_PATH_DIRECT);
5686 
5687 	if (status != 0) {
5688 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5689 		goto error;
5690 	}
5691 
5692 	/* Validate the revision */
5693 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5694 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5695 		status = EINVAL;
5696 		goto error;
5697 	}
5698 
5699 	/* Calculate the checksum */
5700 	chksum = 0;
5701 	ip = (uint_t *)dkdevid;
5702 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5703 	    i++) {
5704 		chksum ^= ip[i];
5705 	}
5706 
5707 	/* Compare the checksums */
5708 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
5709 		status = EINVAL;
5710 		goto error;
5711 	}
5712 
5713 	/* Validate the device id */
5714 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5715 		status = EINVAL;
5716 		goto error;
5717 	}
5718 
5719 	/*
5720 	 * Store the device id in the driver soft state
5721 	 */
5722 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5723 	tmpid = kmem_alloc(sz, KM_SLEEP);
5724 
5725 	mutex_enter(SD_MUTEX(un));
5726 
5727 	un->un_devid = tmpid;
5728 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5729 
5730 	kmem_free(dkdevid, buffer_size);
5731 
5732 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5733 
5734 	return (status);
5735 error:
5736 	mutex_enter(SD_MUTEX(un));
5737 	kmem_free(dkdevid, buffer_size);
5738 	return (status);
5739 }
5740 
5741 
5742 /*
5743  *    Function: sd_create_devid
5744  *
5745  * Description: This routine will fabricate the device id and write it
5746  *		to the disk.
5747  *
5748  *   Arguments: un - driver soft state (unit) structure
5749  *
5750  * Return Code: value of the fabricated device id
5751  *
5752  *     Context: Kernel Thread
5753  */
5754 
5755 static ddi_devid_t
5756 sd_create_devid(sd_ssc_t *ssc)
5757 {
5758 	struct sd_lun	*un;
5759 
5760 	ASSERT(ssc != NULL);
5761 	un = ssc->ssc_un;
5762 	ASSERT(un != NULL);
5763 
5764 	/* Fabricate the devid */
5765 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5766 	    == DDI_FAILURE) {
5767 		return (NULL);
5768 	}
5769 
5770 	/* Write the devid to disk */
5771 	if (sd_write_deviceid(ssc) != 0) {
5772 		ddi_devid_free(un->un_devid);
5773 		un->un_devid = NULL;
5774 	}
5775 
5776 	return (un->un_devid);
5777 }
5778 
5779 
5780 /*
5781  *    Function: sd_write_deviceid
5782  *
5783  * Description: This routine will write the device id to the disk
5784  *		reserved sector.
5785  *
5786  *   Arguments: un - driver soft state (unit) structure
5787  *
5788  * Return Code: EINVAL
5789  *		value returned by sd_send_scsi_cmd
5790  *
5791  *     Context: Kernel Thread
5792  */
5793 
5794 static int
5795 sd_write_deviceid(sd_ssc_t *ssc)
5796 {
5797 	struct dk_devid		*dkdevid;
5798 	uchar_t			*buf;
5799 	diskaddr_t		blk;
5800 	uint_t			*ip, chksum;
5801 	int			status;
5802 	int			i;
5803 	struct sd_lun		*un;
5804 
5805 	ASSERT(ssc != NULL);
5806 	un = ssc->ssc_un;
5807 	ASSERT(un != NULL);
5808 	ASSERT(mutex_owned(SD_MUTEX(un)));
5809 
5810 	mutex_exit(SD_MUTEX(un));
5811 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5812 	    (void *)SD_PATH_DIRECT) != 0) {
5813 		mutex_enter(SD_MUTEX(un));
5814 		return (-1);
5815 	}
5816 
5817 
5818 	/* Allocate the buffer */
5819 	buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5820 	dkdevid = (struct dk_devid *)buf;
5821 
5822 	/* Fill in the revision */
5823 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5824 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5825 
5826 	/* Copy in the device id */
5827 	mutex_enter(SD_MUTEX(un));
5828 	bcopy(un->un_devid, &dkdevid->dkd_devid,
5829 	    ddi_devid_sizeof(un->un_devid));
5830 	mutex_exit(SD_MUTEX(un));
5831 
5832 	/* Calculate the checksum */
5833 	chksum = 0;
5834 	ip = (uint_t *)dkdevid;
5835 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5836 	    i++) {
5837 		chksum ^= ip[i];
5838 	}
5839 
5840 	/* Fill-in checksum */
5841 	DKD_FORMCHKSUM(chksum, dkdevid);
5842 
5843 	/* Write the reserved sector */
5844 	status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5845 	    SD_PATH_DIRECT);
5846 	if (status != 0)
5847 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5848 
5849 	kmem_free(buf, un->un_sys_blocksize);
5850 
5851 	mutex_enter(SD_MUTEX(un));
5852 	return (status);
5853 }
5854 
5855 
5856 /*
5857  *    Function: sd_check_vpd_page_support
5858  *
5859  * Description: This routine sends an inquiry command with the EVPD bit set and
5860  *		a page code of 0x00 to the device. It is used to determine which
5861  *		vital product pages are available to find the devid. We are
5862  *		looking for pages 0x83 0x80 or 0xB1.  If we return a negative 1,
5863  *		the device does not support that command.
5864  *
5865  *   Arguments: un  - driver soft state (unit) structure
5866  *
5867  * Return Code: 0 - success
5868  *		1 - check condition
5869  *
5870  *     Context: This routine can sleep.
5871  */
5872 
5873 static int
5874 sd_check_vpd_page_support(sd_ssc_t *ssc)
5875 {
5876 	uchar_t	*page_list	= NULL;
5877 	uchar_t	page_length	= 0xff;	/* Use max possible length */
5878 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
5879 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
5880 	int    	rval		= 0;
5881 	int	counter;
5882 	struct sd_lun		*un;
5883 
5884 	ASSERT(ssc != NULL);
5885 	un = ssc->ssc_un;
5886 	ASSERT(un != NULL);
5887 	ASSERT(mutex_owned(SD_MUTEX(un)));
5888 
5889 	mutex_exit(SD_MUTEX(un));
5890 
5891 	/*
5892 	 * We'll set the page length to the maximum to save figuring it out
5893 	 * with an additional call.
5894 	 */
5895 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
5896 
5897 	rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5898 	    page_code, NULL);
5899 
5900 	if (rval != 0)
5901 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5902 
5903 	mutex_enter(SD_MUTEX(un));
5904 
5905 	/*
5906 	 * Now we must validate that the device accepted the command, as some
5907 	 * drives do not support it.  If the drive does support it, we will
5908 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
5909 	 * not, we return -1.
5910 	 */
5911 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5912 		/* Loop to find one of the 2 pages we need */
5913 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
5914 
5915 		/*
5916 		 * Pages are returned in ascending order, and 0x83 is what we
5917 		 * are hoping for.
5918 		 */
5919 		while ((page_list[counter] <= 0xB1) &&
5920 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
5921 		    VPD_HEAD_OFFSET))) {
5922 			/*
5923 			 * Add 3 because page_list[3] is the number of
5924 			 * pages minus 3
5925 			 */
5926 
5927 			switch (page_list[counter]) {
5928 			case 0x00:
5929 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5930 				break;
5931 			case 0x80:
5932 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5933 				break;
5934 			case 0x81:
5935 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5936 				break;
5937 			case 0x82:
5938 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5939 				break;
5940 			case 0x83:
5941 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5942 				break;
5943 			case 0x86:
5944 				un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5945 				break;
5946 			case 0xB0:
5947 				un->un_vpd_page_mask |= SD_VPD_BLK_LIMITS_PG;
5948 				break;
5949 			case 0xB1:
5950 				un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5951 				break;
5952 			}
5953 			counter++;
5954 		}
5955 
5956 	} else {
5957 		rval = -1;
5958 
5959 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
5960 		    "sd_check_vpd_page_support: This drive does not implement "
5961 		    "VPD pages.\n");
5962 	}
5963 
5964 	kmem_free(page_list, page_length);
5965 
5966 	return (rval);
5967 }
5968 
5969 
5970 /*
5971  *    Function: sd_setup_pm
5972  *
5973  * Description: Initialize Power Management on the device
5974  *
5975  *     Context: Kernel Thread
5976  */
5977 
5978 static void
5979 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5980 {
5981 	uint_t		log_page_size;
5982 	uchar_t		*log_page_data;
5983 	int		rval = 0;
5984 	struct sd_lun	*un;
5985 
5986 	ASSERT(ssc != NULL);
5987 	un = ssc->ssc_un;
5988 	ASSERT(un != NULL);
5989 
5990 	/*
5991 	 * Since we are called from attach, holding a mutex for
5992 	 * un is unnecessary. Because some of the routines called
5993 	 * from here require SD_MUTEX to not be held, assert this
5994 	 * right up front.
5995 	 */
5996 	ASSERT(!mutex_owned(SD_MUTEX(un)));
5997 	/*
5998 	 * Since the sd device does not have the 'reg' property,
5999 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
6000 	 * The following code is to tell cpr that this device
6001 	 * DOES need to be suspended and resumed.
6002 	 */
6003 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
6004 	    "pm-hardware-state", "needs-suspend-resume");
6005 
6006 	/*
6007 	 * This complies with the new power management framework
6008 	 * for certain desktop machines. Create the pm_components
6009 	 * property as a string array property.
6010 	 * If un_f_pm_supported is TRUE, that means the disk
6011 	 * attached HBA has set the "pm-capable" property and
6012 	 * the value of this property is bigger than 0.
6013 	 */
6014 	if (un->un_f_pm_supported) {
6015 		/*
6016 		 * not all devices have a motor, try it first.
6017 		 * some devices may return ILLEGAL REQUEST, some
6018 		 * will hang
6019 		 * The following START_STOP_UNIT is used to check if target
6020 		 * device has a motor.
6021 		 */
6022 		un->un_f_start_stop_supported = TRUE;
6023 
6024 		if (un->un_f_power_condition_supported) {
6025 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
6026 			    SD_POWER_CONDITION, SD_TARGET_ACTIVE,
6027 			    SD_PATH_DIRECT);
6028 			if (rval != 0) {
6029 				un->un_f_power_condition_supported = FALSE;
6030 			}
6031 		}
6032 		if (!un->un_f_power_condition_supported) {
6033 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
6034 			    SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
6035 		}
6036 		if (rval != 0) {
6037 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6038 			un->un_f_start_stop_supported = FALSE;
6039 		}
6040 
6041 		/*
6042 		 * create pm properties anyways otherwise the parent can't
6043 		 * go to sleep
6044 		 */
6045 		un->un_f_pm_is_enabled = TRUE;
6046 		(void) sd_create_pm_components(devi, un);
6047 
6048 		/*
6049 		 * If it claims that log sense is supported, check it out.
6050 		 */
6051 		if (un->un_f_log_sense_supported) {
6052 			rval = sd_log_page_supported(ssc,
6053 			    START_STOP_CYCLE_PAGE);
6054 			if (rval == 1) {
6055 				/* Page found, use it. */
6056 				un->un_start_stop_cycle_page =
6057 				    START_STOP_CYCLE_PAGE;
6058 			} else {
6059 				/*
6060 				 * Page not found or log sense is not
6061 				 * supported.
6062 				 * Notice we do not check the old style
6063 				 * START_STOP_CYCLE_VU_PAGE because this
6064 				 * code path does not apply to old disks.
6065 				 */
6066 				un->un_f_log_sense_supported = FALSE;
6067 				un->un_f_pm_log_sense_smart = FALSE;
6068 			}
6069 		}
6070 
6071 		return;
6072 	}
6073 
6074 	/*
6075 	 * For the disk whose attached HBA has not set the "pm-capable"
6076 	 * property, check if it supports the power management.
6077 	 */
6078 	if (!un->un_f_log_sense_supported) {
6079 		un->un_power_level = SD_SPINDLE_ON;
6080 		un->un_f_pm_is_enabled = FALSE;
6081 		return;
6082 	}
6083 
6084 	rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
6085 
6086 #ifdef	SDDEBUG
6087 	if (sd_force_pm_supported) {
6088 		/* Force a successful result */
6089 		rval = 1;
6090 	}
6091 #endif
6092 
6093 	/*
6094 	 * If the start-stop cycle counter log page is not supported
6095 	 * or if the pm-capable property is set to be false (0),
6096 	 * then we should not create the pm_components property.
6097 	 */
6098 	if (rval == -1) {
6099 		/*
6100 		 * Error.
6101 		 * Reading log sense failed, most likely this is
6102 		 * an older drive that does not support log sense.
6103 		 * If this fails auto-pm is not supported.
6104 		 */
6105 		un->un_power_level = SD_SPINDLE_ON;
6106 		un->un_f_pm_is_enabled = FALSE;
6107 
6108 	} else if (rval == 0) {
6109 		/*
6110 		 * Page not found.
6111 		 * The start stop cycle counter is implemented as page
6112 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6113 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6114 		 */
6115 		if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
6116 			/*
6117 			 * Page found, use this one.
6118 			 */
6119 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6120 			un->un_f_pm_is_enabled = TRUE;
6121 		} else {
6122 			/*
6123 			 * Error or page not found.
6124 			 * auto-pm is not supported for this device.
6125 			 */
6126 			un->un_power_level = SD_SPINDLE_ON;
6127 			un->un_f_pm_is_enabled = FALSE;
6128 		}
6129 	} else {
6130 		/*
6131 		 * Page found, use it.
6132 		 */
6133 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6134 		un->un_f_pm_is_enabled = TRUE;
6135 	}
6136 
6137 
6138 	if (un->un_f_pm_is_enabled == TRUE) {
6139 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6140 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6141 
6142 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6143 		    log_page_size, un->un_start_stop_cycle_page,
6144 		    0x01, 0, SD_PATH_DIRECT);
6145 
6146 		if (rval != 0) {
6147 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6148 		}
6149 
6150 #ifdef	SDDEBUG
6151 		if (sd_force_pm_supported) {
6152 			/* Force a successful result */
6153 			rval = 0;
6154 		}
6155 #endif
6156 
6157 		/*
6158 		 * If the Log sense for Page( Start/stop cycle counter page)
6159 		 * succeeds, then power management is supported and we can
6160 		 * enable auto-pm.
6161 		 */
6162 		if (rval == 0)  {
6163 			(void) sd_create_pm_components(devi, un);
6164 		} else {
6165 			un->un_power_level = SD_SPINDLE_ON;
6166 			un->un_f_pm_is_enabled = FALSE;
6167 		}
6168 
6169 		kmem_free(log_page_data, log_page_size);
6170 	}
6171 }
6172 
6173 
6174 /*
6175  *    Function: sd_create_pm_components
6176  *
6177  * Description: Initialize PM property.
6178  *
6179  *     Context: Kernel thread context
6180  */
6181 
6182 static void
6183 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6184 {
6185 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6186 
6187 	if (un->un_f_power_condition_supported) {
6188 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6189 		    "pm-components", sd_pwr_pc.pm_comp, 5)
6190 		    != DDI_PROP_SUCCESS) {
6191 			un->un_power_level = SD_SPINDLE_ACTIVE;
6192 			un->un_f_pm_is_enabled = FALSE;
6193 			return;
6194 		}
6195 	} else {
6196 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6197 		    "pm-components", sd_pwr_ss.pm_comp, 3)
6198 		    != DDI_PROP_SUCCESS) {
6199 			un->un_power_level = SD_SPINDLE_ON;
6200 			un->un_f_pm_is_enabled = FALSE;
6201 			return;
6202 		}
6203 	}
6204 	/*
6205 	 * When components are initially created they are idle,
6206 	 * power up any non-removables.
6207 	 * Note: the return value of pm_raise_power can't be used
6208 	 * for determining if PM should be enabled for this device.
6209 	 * Even if you check the return values and remove this
6210 	 * property created above, the PM framework will not honor the
6211 	 * change after the first call to pm_raise_power. Hence,
6212 	 * removal of that property does not help if pm_raise_power
6213 	 * fails. In the case of removable media, the start/stop
6214 	 * will fail if the media is not present.
6215 	 */
6216 	if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6217 	    SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6218 		mutex_enter(SD_MUTEX(un));
6219 		un->un_power_level = SD_PM_STATE_ACTIVE(un);
6220 		mutex_enter(&un->un_pm_mutex);
6221 		/* Set to on and not busy. */
6222 		un->un_pm_count = 0;
6223 	} else {
6224 		mutex_enter(SD_MUTEX(un));
6225 		un->un_power_level = SD_PM_STATE_STOPPED(un);
6226 		mutex_enter(&un->un_pm_mutex);
6227 		/* Set to off. */
6228 		un->un_pm_count = -1;
6229 	}
6230 	mutex_exit(&un->un_pm_mutex);
6231 	mutex_exit(SD_MUTEX(un));
6232 }
6233 
6234 
6235 /*
6236  *    Function: sd_ddi_suspend
6237  *
6238  * Description: Performs system power-down operations. This includes
6239  *		setting the drive state to indicate its suspended so
6240  *		that no new commands will be accepted. Also, wait for
6241  *		all commands that are in transport or queued to a timer
6242  *		for retry to complete. All timeout threads are cancelled.
6243  *
6244  * Return Code: DDI_FAILURE or DDI_SUCCESS
6245  *
6246  *     Context: Kernel thread context
6247  */
6248 
6249 static int
6250 sd_ddi_suspend(dev_info_t *devi)
6251 {
6252 	struct	sd_lun	*un;
6253 	clock_t		wait_cmds_complete;
6254 
6255 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6256 	if (un == NULL) {
6257 		return (DDI_FAILURE);
6258 	}
6259 
6260 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6261 
6262 	mutex_enter(SD_MUTEX(un));
6263 
6264 	/* Return success if the device is already suspended. */
6265 	if (un->un_state == SD_STATE_SUSPENDED) {
6266 		mutex_exit(SD_MUTEX(un));
6267 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6268 		    "device already suspended, exiting\n");
6269 		return (DDI_SUCCESS);
6270 	}
6271 
6272 	/* Return failure if the device is being used by HA */
6273 	if (un->un_resvd_status &
6274 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6275 		mutex_exit(SD_MUTEX(un));
6276 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6277 		    "device in use by HA, exiting\n");
6278 		return (DDI_FAILURE);
6279 	}
6280 
6281 	/*
6282 	 * Return failure if the device is in a resource wait
6283 	 * or power changing state.
6284 	 */
6285 	if ((un->un_state == SD_STATE_RWAIT) ||
6286 	    (un->un_state == SD_STATE_PM_CHANGING)) {
6287 		mutex_exit(SD_MUTEX(un));
6288 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6289 		    "device in resource wait state, exiting\n");
6290 		return (DDI_FAILURE);
6291 	}
6292 
6293 
6294 	un->un_save_state = un->un_last_state;
6295 	New_state(un, SD_STATE_SUSPENDED);
6296 
6297 	/*
6298 	 * Wait for all commands that are in transport or queued to a timer
6299 	 * for retry to complete.
6300 	 *
6301 	 * While waiting, no new commands will be accepted or sent because of
6302 	 * the new state we set above.
6303 	 *
6304 	 * Wait till current operation has completed. If we are in the resource
6305 	 * wait state (with an intr outstanding) then we need to wait till the
6306 	 * intr completes and starts the next cmd. We want to wait for
6307 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6308 	 */
6309 	wait_cmds_complete = ddi_get_lbolt() +
6310 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
6311 
6312 	while (un->un_ncmds_in_transport != 0) {
6313 		/*
6314 		 * Fail if commands do not finish in the specified time.
6315 		 */
6316 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6317 		    wait_cmds_complete) == -1) {
6318 			/*
6319 			 * Undo the state changes made above. Everything
6320 			 * must go back to it's original value.
6321 			 */
6322 			Restore_state(un);
6323 			un->un_last_state = un->un_save_state;
6324 			/* Wake up any threads that might be waiting. */
6325 			cv_broadcast(&un->un_suspend_cv);
6326 			mutex_exit(SD_MUTEX(un));
6327 			SD_ERROR(SD_LOG_IO_PM, un,
6328 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
6329 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6330 			return (DDI_FAILURE);
6331 		}
6332 	}
6333 
6334 	/*
6335 	 * Cancel SCSI watch thread and timeouts, if any are active
6336 	 */
6337 
6338 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6339 		opaque_t temp_token = un->un_swr_token;
6340 		mutex_exit(SD_MUTEX(un));
6341 		scsi_watch_suspend(temp_token);
6342 		mutex_enter(SD_MUTEX(un));
6343 	}
6344 
6345 	if (un->un_reset_throttle_timeid != NULL) {
6346 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
6347 		un->un_reset_throttle_timeid = NULL;
6348 		mutex_exit(SD_MUTEX(un));
6349 		(void) untimeout(temp_id);
6350 		mutex_enter(SD_MUTEX(un));
6351 	}
6352 
6353 	if (un->un_dcvb_timeid != NULL) {
6354 		timeout_id_t temp_id = un->un_dcvb_timeid;
6355 		un->un_dcvb_timeid = NULL;
6356 		mutex_exit(SD_MUTEX(un));
6357 		(void) untimeout(temp_id);
6358 		mutex_enter(SD_MUTEX(un));
6359 	}
6360 
6361 	mutex_enter(&un->un_pm_mutex);
6362 	if (un->un_pm_timeid != NULL) {
6363 		timeout_id_t temp_id = un->un_pm_timeid;
6364 		un->un_pm_timeid = NULL;
6365 		mutex_exit(&un->un_pm_mutex);
6366 		mutex_exit(SD_MUTEX(un));
6367 		(void) untimeout(temp_id);
6368 		mutex_enter(SD_MUTEX(un));
6369 	} else {
6370 		mutex_exit(&un->un_pm_mutex);
6371 	}
6372 
6373 	if (un->un_rmw_msg_timeid != NULL) {
6374 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
6375 		un->un_rmw_msg_timeid = NULL;
6376 		mutex_exit(SD_MUTEX(un));
6377 		(void) untimeout(temp_id);
6378 		mutex_enter(SD_MUTEX(un));
6379 	}
6380 
6381 	if (un->un_retry_timeid != NULL) {
6382 		timeout_id_t temp_id = un->un_retry_timeid;
6383 		un->un_retry_timeid = NULL;
6384 		mutex_exit(SD_MUTEX(un));
6385 		(void) untimeout(temp_id);
6386 		mutex_enter(SD_MUTEX(un));
6387 
6388 		if (un->un_retry_bp != NULL) {
6389 			un->un_retry_bp->av_forw = un->un_waitq_headp;
6390 			un->un_waitq_headp = un->un_retry_bp;
6391 			if (un->un_waitq_tailp == NULL) {
6392 				un->un_waitq_tailp = un->un_retry_bp;
6393 			}
6394 			un->un_retry_bp = NULL;
6395 			un->un_retry_statp = NULL;
6396 		}
6397 	}
6398 
6399 	if (un->un_direct_priority_timeid != NULL) {
6400 		timeout_id_t temp_id = un->un_direct_priority_timeid;
6401 		un->un_direct_priority_timeid = NULL;
6402 		mutex_exit(SD_MUTEX(un));
6403 		(void) untimeout(temp_id);
6404 		mutex_enter(SD_MUTEX(un));
6405 	}
6406 
6407 	if (un->un_f_is_fibre == TRUE) {
6408 		/*
6409 		 * Remove callbacks for insert and remove events
6410 		 */
6411 		if (un->un_insert_event != NULL) {
6412 			mutex_exit(SD_MUTEX(un));
6413 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
6414 			mutex_enter(SD_MUTEX(un));
6415 			un->un_insert_event = NULL;
6416 		}
6417 
6418 		if (un->un_remove_event != NULL) {
6419 			mutex_exit(SD_MUTEX(un));
6420 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
6421 			mutex_enter(SD_MUTEX(un));
6422 			un->un_remove_event = NULL;
6423 		}
6424 	}
6425 
6426 	mutex_exit(SD_MUTEX(un));
6427 
6428 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6429 
6430 	return (DDI_SUCCESS);
6431 }
6432 
6433 
6434 /*
6435  *    Function: sd_ddi_resume
6436  *
6437  * Description: Performs system power-up operations..
6438  *
6439  * Return Code: DDI_SUCCESS
6440  *		DDI_FAILURE
6441  *
6442  *     Context: Kernel thread context
6443  */
6444 
6445 static int
6446 sd_ddi_resume(dev_info_t *devi)
6447 {
6448 	struct	sd_lun	*un;
6449 
6450 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6451 	if (un == NULL) {
6452 		return (DDI_FAILURE);
6453 	}
6454 
6455 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6456 
6457 	mutex_enter(SD_MUTEX(un));
6458 	Restore_state(un);
6459 
6460 	/*
6461 	 * Restore the state which was saved to give the
6462 	 * the right state in un_last_state
6463 	 */
6464 	un->un_last_state = un->un_save_state;
6465 	/*
6466 	 * Note: throttle comes back at full.
6467 	 * Also note: this MUST be done before calling pm_raise_power
6468 	 * otherwise the system can get hung in biowait. The scenario where
6469 	 * this'll happen is under cpr suspend. Writing of the system
6470 	 * state goes through sddump, which writes 0 to un_throttle. If
6471 	 * writing the system state then fails, example if the partition is
6472 	 * too small, then cpr attempts a resume. If throttle isn't restored
6473 	 * from the saved value until after calling pm_raise_power then
6474 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6475 	 * in biowait.
6476 	 */
6477 	un->un_throttle = un->un_saved_throttle;
6478 
6479 	/*
6480 	 * The chance of failure is very rare as the only command done in power
6481 	 * entry point is START command when you transition from 0->1 or
6482 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6483 	 * which suspend was done. Ignore the return value as the resume should
6484 	 * not be failed. In the case of removable media the media need not be
6485 	 * inserted and hence there is a chance that raise power will fail with
6486 	 * media not present.
6487 	 */
6488 	if (un->un_f_attach_spinup) {
6489 		mutex_exit(SD_MUTEX(un));
6490 		(void) pm_raise_power(SD_DEVINFO(un), 0,
6491 		    SD_PM_STATE_ACTIVE(un));
6492 		mutex_enter(SD_MUTEX(un));
6493 	}
6494 
6495 	/*
6496 	 * Don't broadcast to the suspend cv and therefore possibly
6497 	 * start I/O until after power has been restored.
6498 	 */
6499 	cv_broadcast(&un->un_suspend_cv);
6500 	cv_broadcast(&un->un_state_cv);
6501 
6502 	/* restart thread */
6503 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6504 		scsi_watch_resume(un->un_swr_token);
6505 	}
6506 
6507 #if (defined(__fibre))
6508 	if (un->un_f_is_fibre == TRUE) {
6509 		/*
6510 		 * Add callbacks for insert and remove events
6511 		 */
6512 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6513 			sd_init_event_callbacks(un);
6514 		}
6515 	}
6516 #endif
6517 
6518 	/*
6519 	 * Transport any pending commands to the target.
6520 	 *
6521 	 * If this is a low-activity device commands in queue will have to wait
6522 	 * until new commands come in, which may take awhile. Also, we
6523 	 * specifically don't check un_ncmds_in_transport because we know that
6524 	 * there really are no commands in progress after the unit was
6525 	 * suspended and we could have reached the throttle level, been
6526 	 * suspended, and have no new commands coming in for awhile. Highly
6527 	 * unlikely, but so is the low-activity disk scenario.
6528 	 */
6529 	ddi_xbuf_dispatch(un->un_xbuf_attr);
6530 
6531 	sd_start_cmds(un, NULL);
6532 	mutex_exit(SD_MUTEX(un));
6533 
6534 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6535 
6536 	return (DDI_SUCCESS);
6537 }
6538 
6539 
6540 /*
6541  *    Function: sd_pm_state_change
6542  *
6543  * Description: Change the driver power state.
6544  * 		Someone else is required to actually change the driver
6545  * 		power level.
6546  *
6547  *   Arguments: un - driver soft state (unit) structure
6548  *              level - the power level that is changed to
6549  *              flag - to decide how to change the power state
6550  *
6551  * Return Code: DDI_SUCCESS
6552  *
6553  *     Context: Kernel thread context
6554  */
6555 static int
6556 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6557 {
6558 	ASSERT(un != NULL);
6559 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6560 
6561 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6562 	mutex_enter(SD_MUTEX(un));
6563 
6564 	if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6565 		un->un_power_level = level;
6566 		ASSERT(!mutex_owned(&un->un_pm_mutex));
6567 		mutex_enter(&un->un_pm_mutex);
6568 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6569 			un->un_pm_count++;
6570 			ASSERT(un->un_pm_count == 0);
6571 		}
6572 		mutex_exit(&un->un_pm_mutex);
6573 	} else {
6574 		/*
6575 		 * Exit if power management is not enabled for this device,
6576 		 * or if the device is being used by HA.
6577 		 */
6578 		if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6579 		    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6580 			mutex_exit(SD_MUTEX(un));
6581 			SD_TRACE(SD_LOG_POWER, un,
6582 			    "sd_pm_state_change: exiting\n");
6583 			return (DDI_FAILURE);
6584 		}
6585 
6586 		SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6587 		    "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6588 
6589 		/*
6590 		 * See if the device is not busy, ie.:
6591 		 *    - we have no commands in the driver for this device
6592 		 *    - not waiting for resources
6593 		 */
6594 		if ((un->un_ncmds_in_driver == 0) &&
6595 		    (un->un_state != SD_STATE_RWAIT)) {
6596 			/*
6597 			 * The device is not busy, so it is OK to go to low
6598 			 * power state. Indicate low power, but rely on someone
6599 			 * else to actually change it.
6600 			 */
6601 			mutex_enter(&un->un_pm_mutex);
6602 			un->un_pm_count = -1;
6603 			mutex_exit(&un->un_pm_mutex);
6604 			un->un_power_level = level;
6605 		}
6606 	}
6607 
6608 	mutex_exit(SD_MUTEX(un));
6609 
6610 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6611 
6612 	return (DDI_SUCCESS);
6613 }
6614 
6615 
6616 /*
6617  *    Function: sd_pm_idletimeout_handler
6618  *
6619  * Description: A timer routine that's active only while a device is busy.
6620  *		The purpose is to extend slightly the pm framework's busy
6621  *		view of the device to prevent busy/idle thrashing for
6622  *		back-to-back commands. Do this by comparing the current time
6623  *		to the time at which the last command completed and when the
6624  *		difference is greater than sd_pm_idletime, call
6625  *		pm_idle_component. In addition to indicating idle to the pm
6626  *		framework, update the chain type to again use the internal pm
6627  *		layers of the driver.
6628  *
6629  *   Arguments: arg - driver soft state (unit) structure
6630  *
6631  *     Context: Executes in a timeout(9F) thread context
6632  */
6633 
6634 static void
6635 sd_pm_idletimeout_handler(void *arg)
6636 {
6637 	const hrtime_t idletime = sd_pm_idletime * NANOSEC;
6638 	struct sd_lun *un = arg;
6639 
6640 	mutex_enter(&sd_detach_mutex);
6641 	if (un->un_detach_count != 0) {
6642 		/* Abort if the instance is detaching */
6643 		mutex_exit(&sd_detach_mutex);
6644 		return;
6645 	}
6646 	mutex_exit(&sd_detach_mutex);
6647 
6648 	/*
6649 	 * Grab both mutexes, in the proper order, since we're accessing
6650 	 * both PM and softstate variables.
6651 	 */
6652 	mutex_enter(SD_MUTEX(un));
6653 	mutex_enter(&un->un_pm_mutex);
6654 	if (((gethrtime() - un->un_pm_idle_time) > idletime) &&
6655 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6656 		/*
6657 		 * Update the chain types.
6658 		 * This takes affect on the next new command received.
6659 		 */
6660 		if (un->un_f_non_devbsize_supported) {
6661 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6662 		} else {
6663 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6664 		}
6665 		un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6666 
6667 		SD_TRACE(SD_LOG_IO_PM, un,
6668 		    "sd_pm_idletimeout_handler: idling device\n");
6669 		(void) pm_idle_component(SD_DEVINFO(un), 0);
6670 		un->un_pm_idle_timeid = NULL;
6671 	} else {
6672 		un->un_pm_idle_timeid =
6673 		    timeout(sd_pm_idletimeout_handler, un,
6674 		    (drv_usectohz((clock_t)300000))); /* 300 ms. */
6675 	}
6676 	mutex_exit(&un->un_pm_mutex);
6677 	mutex_exit(SD_MUTEX(un));
6678 }
6679 
6680 
6681 /*
6682  *    Function: sd_pm_timeout_handler
6683  *
6684  * Description: Callback to tell framework we are idle.
6685  *
6686  *     Context: timeout(9f) thread context.
6687  */
6688 
6689 static void
6690 sd_pm_timeout_handler(void *arg)
6691 {
6692 	struct sd_lun *un = arg;
6693 
6694 	(void) pm_idle_component(SD_DEVINFO(un), 0);
6695 	mutex_enter(&un->un_pm_mutex);
6696 	un->un_pm_timeid = NULL;
6697 	mutex_exit(&un->un_pm_mutex);
6698 }
6699 
6700 
6701 /*
6702  *    Function: sdpower
6703  *
6704  * Description: PM entry point.
6705  *
6706  * Return Code: DDI_SUCCESS
6707  *		DDI_FAILURE
6708  *
6709  *     Context: Kernel thread context
6710  */
6711 
6712 static int
6713 sdpower(dev_info_t *devi, int component, int level)
6714 {
6715 	struct sd_lun	*un;
6716 	int		instance;
6717 	int		rval = DDI_SUCCESS;
6718 	uint_t		i, log_page_size, maxcycles, ncycles;
6719 	uchar_t		*log_page_data;
6720 	int		log_sense_page;
6721 	int		medium_present;
6722 	time_t		intvlp;
6723 	struct pm_trans_data	sd_pm_tran_data;
6724 	uchar_t		save_state = SD_STATE_NORMAL;
6725 	int		sval;
6726 	uchar_t		state_before_pm;
6727 	int		got_semaphore_here;
6728 	sd_ssc_t	*ssc;
6729 	int	last_power_level = SD_SPINDLE_UNINIT;
6730 
6731 	instance = ddi_get_instance(devi);
6732 
6733 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6734 	    !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6735 		return (DDI_FAILURE);
6736 	}
6737 
6738 	ssc = sd_ssc_init(un);
6739 
6740 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6741 
6742 	/*
6743 	 * Must synchronize power down with close.
6744 	 * Attempt to decrement/acquire the open/close semaphore,
6745 	 * but do NOT wait on it. If it's not greater than zero,
6746 	 * ie. it can't be decremented without waiting, then
6747 	 * someone else, either open or close, already has it
6748 	 * and the try returns 0. Use that knowledge here to determine
6749 	 * if it's OK to change the device power level.
6750 	 * Also, only increment it on exit if it was decremented, ie. gotten,
6751 	 * here.
6752 	 */
6753 	got_semaphore_here = sema_tryp(&un->un_semoclose);
6754 
6755 	mutex_enter(SD_MUTEX(un));
6756 
6757 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6758 	    un->un_ncmds_in_driver);
6759 
6760 	/*
6761 	 * If un_ncmds_in_driver is non-zero it indicates commands are
6762 	 * already being processed in the driver, or if the semaphore was
6763 	 * not gotten here it indicates an open or close is being processed.
6764 	 * At the same time somebody is requesting to go to a lower power
6765 	 * that can't perform I/O, which can't happen, therefore we need to
6766 	 * return failure.
6767 	 */
6768 	if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6769 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6770 		mutex_exit(SD_MUTEX(un));
6771 
6772 		if (got_semaphore_here != 0) {
6773 			sema_v(&un->un_semoclose);
6774 		}
6775 		SD_TRACE(SD_LOG_IO_PM, un,
6776 		    "sdpower: exit, device has queued cmds.\n");
6777 
6778 		goto sdpower_failed;
6779 	}
6780 
6781 	/*
6782 	 * if it is OFFLINE that means the disk is completely dead
6783 	 * in our case we have to put the disk in on or off by sending commands
6784 	 * Of course that will fail anyway so return back here.
6785 	 *
6786 	 * Power changes to a device that's OFFLINE or SUSPENDED
6787 	 * are not allowed.
6788 	 */
6789 	if ((un->un_state == SD_STATE_OFFLINE) ||
6790 	    (un->un_state == SD_STATE_SUSPENDED)) {
6791 		mutex_exit(SD_MUTEX(un));
6792 
6793 		if (got_semaphore_here != 0) {
6794 			sema_v(&un->un_semoclose);
6795 		}
6796 		SD_TRACE(SD_LOG_IO_PM, un,
6797 		    "sdpower: exit, device is off-line.\n");
6798 
6799 		goto sdpower_failed;
6800 	}
6801 
6802 	/*
6803 	 * Change the device's state to indicate it's power level
6804 	 * is being changed. Do this to prevent a power off in the
6805 	 * middle of commands, which is especially bad on devices
6806 	 * that are really powered off instead of just spun down.
6807 	 */
6808 	state_before_pm = un->un_state;
6809 	un->un_state = SD_STATE_PM_CHANGING;
6810 
6811 	mutex_exit(SD_MUTEX(un));
6812 
6813 	/*
6814 	 * If log sense command is not supported, bypass the
6815 	 * following checking, otherwise, check the log sense
6816 	 * information for this device.
6817 	 */
6818 	if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6819 	    un->un_f_log_sense_supported) {
6820 		/*
6821 		 * Get the log sense information to understand whether the
6822 		 * the powercycle counts have gone beyond the threshhold.
6823 		 */
6824 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6825 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6826 
6827 		mutex_enter(SD_MUTEX(un));
6828 		log_sense_page = un->un_start_stop_cycle_page;
6829 		mutex_exit(SD_MUTEX(un));
6830 
6831 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6832 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6833 
6834 		if (rval != 0) {
6835 			if (rval == EIO)
6836 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6837 			else
6838 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6839 		}
6840 
6841 #ifdef	SDDEBUG
6842 		if (sd_force_pm_supported) {
6843 			/* Force a successful result */
6844 			rval = 0;
6845 		}
6846 #endif
6847 		if (rval != 0) {
6848 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6849 			    "Log Sense Failed\n");
6850 
6851 			kmem_free(log_page_data, log_page_size);
6852 			/* Cannot support power management on those drives */
6853 
6854 			if (got_semaphore_here != 0) {
6855 				sema_v(&un->un_semoclose);
6856 			}
6857 			/*
6858 			 * On exit put the state back to it's original value
6859 			 * and broadcast to anyone waiting for the power
6860 			 * change completion.
6861 			 */
6862 			mutex_enter(SD_MUTEX(un));
6863 			un->un_state = state_before_pm;
6864 			cv_broadcast(&un->un_suspend_cv);
6865 			mutex_exit(SD_MUTEX(un));
6866 			SD_TRACE(SD_LOG_IO_PM, un,
6867 			    "sdpower: exit, Log Sense Failed.\n");
6868 
6869 			goto sdpower_failed;
6870 		}
6871 
6872 		/*
6873 		 * From the page data - Convert the essential information to
6874 		 * pm_trans_data
6875 		 */
6876 		maxcycles =
6877 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6878 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
6879 
6880 		ncycles =
6881 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6882 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
6883 
6884 		if (un->un_f_pm_log_sense_smart) {
6885 			sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6886 			sd_pm_tran_data.un.smart_count.consumed = ncycles;
6887 			sd_pm_tran_data.un.smart_count.flag = 0;
6888 			sd_pm_tran_data.format = DC_SMART_FORMAT;
6889 		} else {
6890 			sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6891 			sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6892 			for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6893 				sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6894 				    log_page_data[8+i];
6895 			}
6896 			sd_pm_tran_data.un.scsi_cycles.flag = 0;
6897 			sd_pm_tran_data.format = DC_SCSI_FORMAT;
6898 		}
6899 
6900 		kmem_free(log_page_data, log_page_size);
6901 
6902 		/*
6903 		 * Call pm_trans_check routine to get the Ok from
6904 		 * the global policy
6905 		 */
6906 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6907 #ifdef	SDDEBUG
6908 		if (sd_force_pm_supported) {
6909 			/* Force a successful result */
6910 			rval = 1;
6911 		}
6912 #endif
6913 		switch (rval) {
6914 		case 0:
6915 			/*
6916 			 * Not Ok to Power cycle or error in parameters passed
6917 			 * Would have given the advised time to consider power
6918 			 * cycle. Based on the new intvlp parameter we are
6919 			 * supposed to pretend we are busy so that pm framework
6920 			 * will never call our power entry point. Because of
6921 			 * that install a timeout handler and wait for the
6922 			 * recommended time to elapse so that power management
6923 			 * can be effective again.
6924 			 *
6925 			 * To effect this behavior, call pm_busy_component to
6926 			 * indicate to the framework this device is busy.
6927 			 * By not adjusting un_pm_count the rest of PM in
6928 			 * the driver will function normally, and independent
6929 			 * of this but because the framework is told the device
6930 			 * is busy it won't attempt powering down until it gets
6931 			 * a matching idle. The timeout handler sends this.
6932 			 * Note: sd_pm_entry can't be called here to do this
6933 			 * because sdpower may have been called as a result
6934 			 * of a call to pm_raise_power from within sd_pm_entry.
6935 			 *
6936 			 * If a timeout handler is already active then
6937 			 * don't install another.
6938 			 */
6939 			mutex_enter(&un->un_pm_mutex);
6940 			if (un->un_pm_timeid == NULL) {
6941 				un->un_pm_timeid =
6942 				    timeout(sd_pm_timeout_handler,
6943 				    un, intvlp * drv_usectohz(1000000));
6944 				mutex_exit(&un->un_pm_mutex);
6945 				(void) pm_busy_component(SD_DEVINFO(un), 0);
6946 			} else {
6947 				mutex_exit(&un->un_pm_mutex);
6948 			}
6949 			if (got_semaphore_here != 0) {
6950 				sema_v(&un->un_semoclose);
6951 			}
6952 			/*
6953 			 * On exit put the state back to it's original value
6954 			 * and broadcast to anyone waiting for the power
6955 			 * change completion.
6956 			 */
6957 			mutex_enter(SD_MUTEX(un));
6958 			un->un_state = state_before_pm;
6959 			cv_broadcast(&un->un_suspend_cv);
6960 			mutex_exit(SD_MUTEX(un));
6961 
6962 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6963 			    "trans check Failed, not ok to power cycle.\n");
6964 
6965 			goto sdpower_failed;
6966 		case -1:
6967 			if (got_semaphore_here != 0) {
6968 				sema_v(&un->un_semoclose);
6969 			}
6970 			/*
6971 			 * On exit put the state back to it's original value
6972 			 * and broadcast to anyone waiting for the power
6973 			 * change completion.
6974 			 */
6975 			mutex_enter(SD_MUTEX(un));
6976 			un->un_state = state_before_pm;
6977 			cv_broadcast(&un->un_suspend_cv);
6978 			mutex_exit(SD_MUTEX(un));
6979 			SD_TRACE(SD_LOG_IO_PM, un,
6980 			    "sdpower: exit, trans check command Failed.\n");
6981 
6982 			goto sdpower_failed;
6983 		}
6984 	}
6985 
6986 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6987 		/*
6988 		 * Save the last state... if the STOP FAILS we need it
6989 		 * for restoring
6990 		 */
6991 		mutex_enter(SD_MUTEX(un));
6992 		save_state = un->un_last_state;
6993 		last_power_level = un->un_power_level;
6994 		/*
6995 		 * There must not be any cmds. getting processed
6996 		 * in the driver when we get here. Power to the
6997 		 * device is potentially going off.
6998 		 */
6999 		ASSERT(un->un_ncmds_in_driver == 0);
7000 		mutex_exit(SD_MUTEX(un));
7001 
7002 		/*
7003 		 * For now PM suspend the device completely before spindle is
7004 		 * turned off
7005 		 */
7006 		if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
7007 		    == DDI_FAILURE) {
7008 			if (got_semaphore_here != 0) {
7009 				sema_v(&un->un_semoclose);
7010 			}
7011 			/*
7012 			 * On exit put the state back to it's original value
7013 			 * and broadcast to anyone waiting for the power
7014 			 * change completion.
7015 			 */
7016 			mutex_enter(SD_MUTEX(un));
7017 			un->un_state = state_before_pm;
7018 			un->un_power_level = last_power_level;
7019 			cv_broadcast(&un->un_suspend_cv);
7020 			mutex_exit(SD_MUTEX(un));
7021 			SD_TRACE(SD_LOG_IO_PM, un,
7022 			    "sdpower: exit, PM suspend Failed.\n");
7023 
7024 			goto sdpower_failed;
7025 		}
7026 	}
7027 
7028 	/*
7029 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
7030 	 * close, or strategy. Dump no long uses this routine, it uses it's
7031 	 * own code so it can be done in polled mode.
7032 	 */
7033 
7034 	medium_present = TRUE;
7035 
7036 	/*
7037 	 * When powering up, issue a TUR in case the device is at unit
7038 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
7039 	 * a deadlock on un_pm_busy_cv will occur.
7040 	 */
7041 	if (SD_PM_IS_IO_CAPABLE(un, level)) {
7042 		sval = sd_send_scsi_TEST_UNIT_READY(ssc,
7043 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
7044 		if (sval != 0)
7045 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7046 	}
7047 
7048 	if (un->un_f_power_condition_supported) {
7049 		char *pm_condition_name[] = {"STOPPED", "STANDBY",
7050 		    "IDLE", "ACTIVE"};
7051 		SD_TRACE(SD_LOG_IO_PM, un,
7052 		    "sdpower: sending \'%s\' power condition",
7053 		    pm_condition_name[level]);
7054 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
7055 		    sd_pl2pc[level], SD_PATH_DIRECT);
7056 	} else {
7057 		SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
7058 		    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
7059 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
7060 		    ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
7061 		    SD_TARGET_STOP), SD_PATH_DIRECT);
7062 	}
7063 	if (sval != 0) {
7064 		if (sval == EIO)
7065 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
7066 		else
7067 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7068 	}
7069 
7070 	/* Command failed, check for media present. */
7071 	if ((sval == ENXIO) && un->un_f_has_removable_media) {
7072 		medium_present = FALSE;
7073 	}
7074 
7075 	/*
7076 	 * The conditions of interest here are:
7077 	 *   if a spindle off with media present fails,
7078 	 *	then restore the state and return an error.
7079 	 *   else if a spindle on fails,
7080 	 *	then return an error (there's no state to restore).
7081 	 * In all other cases we setup for the new state
7082 	 * and return success.
7083 	 */
7084 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
7085 		if ((medium_present == TRUE) && (sval != 0)) {
7086 			/* The stop command from above failed */
7087 			rval = DDI_FAILURE;
7088 			/*
7089 			 * The stop command failed, and we have media
7090 			 * present. Put the level back by calling the
7091 			 * sd_pm_resume() and set the state back to
7092 			 * it's previous value.
7093 			 */
7094 			(void) sd_pm_state_change(un, last_power_level,
7095 			    SD_PM_STATE_ROLLBACK);
7096 			mutex_enter(SD_MUTEX(un));
7097 			un->un_last_state = save_state;
7098 			mutex_exit(SD_MUTEX(un));
7099 		} else if (un->un_f_monitor_media_state) {
7100 			/*
7101 			 * The stop command from above succeeded.
7102 			 * Terminate watch thread in case of removable media
7103 			 * devices going into low power state. This is as per
7104 			 * the requirements of pm framework, otherwise commands
7105 			 * will be generated for the device (through watch
7106 			 * thread), even when the device is in low power state.
7107 			 */
7108 			mutex_enter(SD_MUTEX(un));
7109 			un->un_f_watcht_stopped = FALSE;
7110 			if (un->un_swr_token != NULL) {
7111 				opaque_t temp_token = un->un_swr_token;
7112 				un->un_f_watcht_stopped = TRUE;
7113 				un->un_swr_token = NULL;
7114 				mutex_exit(SD_MUTEX(un));
7115 				(void) scsi_watch_request_terminate(temp_token,
7116 				    SCSI_WATCH_TERMINATE_ALL_WAIT);
7117 			} else {
7118 				mutex_exit(SD_MUTEX(un));
7119 			}
7120 		}
7121 	} else {
7122 		/*
7123 		 * The level requested is I/O capable.
7124 		 * Legacy behavior: return success on a failed spinup
7125 		 * if there is no media in the drive.
7126 		 * Do this by looking at medium_present here.
7127 		 */
7128 		if ((sval != 0) && medium_present) {
7129 			/* The start command from above failed */
7130 			rval = DDI_FAILURE;
7131 		} else {
7132 			/*
7133 			 * The start command from above succeeded
7134 			 * PM resume the devices now that we have
7135 			 * started the disks
7136 			 */
7137 			(void) sd_pm_state_change(un, level,
7138 			    SD_PM_STATE_CHANGE);
7139 
7140 			/*
7141 			 * Resume the watch thread since it was suspended
7142 			 * when the device went into low power mode.
7143 			 */
7144 			if (un->un_f_monitor_media_state) {
7145 				mutex_enter(SD_MUTEX(un));
7146 				if (un->un_f_watcht_stopped == TRUE) {
7147 					opaque_t temp_token;
7148 
7149 					un->un_f_watcht_stopped = FALSE;
7150 					mutex_exit(SD_MUTEX(un));
7151 					temp_token =
7152 					    sd_watch_request_submit(un);
7153 					mutex_enter(SD_MUTEX(un));
7154 					un->un_swr_token = temp_token;
7155 				}
7156 				mutex_exit(SD_MUTEX(un));
7157 			}
7158 		}
7159 	}
7160 
7161 	if (got_semaphore_here != 0) {
7162 		sema_v(&un->un_semoclose);
7163 	}
7164 	/*
7165 	 * On exit put the state back to it's original value
7166 	 * and broadcast to anyone waiting for the power
7167 	 * change completion.
7168 	 */
7169 	mutex_enter(SD_MUTEX(un));
7170 	un->un_state = state_before_pm;
7171 	cv_broadcast(&un->un_suspend_cv);
7172 	mutex_exit(SD_MUTEX(un));
7173 
7174 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7175 
7176 	sd_ssc_fini(ssc);
7177 	return (rval);
7178 
7179 sdpower_failed:
7180 
7181 	sd_ssc_fini(ssc);
7182 	return (DDI_FAILURE);
7183 }
7184 
7185 
7186 
7187 /*
7188  *    Function: sdattach
7189  *
7190  * Description: Driver's attach(9e) entry point function.
7191  *
7192  *   Arguments: devi - opaque device info handle
7193  *		cmd  - attach  type
7194  *
7195  * Return Code: DDI_SUCCESS
7196  *		DDI_FAILURE
7197  *
7198  *     Context: Kernel thread context
7199  */
7200 
7201 static int
7202 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7203 {
7204 	switch (cmd) {
7205 	case DDI_ATTACH:
7206 		return (sd_unit_attach(devi));
7207 	case DDI_RESUME:
7208 		return (sd_ddi_resume(devi));
7209 	default:
7210 		break;
7211 	}
7212 	return (DDI_FAILURE);
7213 }
7214 
7215 
7216 /*
7217  *    Function: sddetach
7218  *
7219  * Description: Driver's detach(9E) entry point function.
7220  *
7221  *   Arguments: devi - opaque device info handle
7222  *		cmd  - detach  type
7223  *
7224  * Return Code: DDI_SUCCESS
7225  *		DDI_FAILURE
7226  *
7227  *     Context: Kernel thread context
7228  */
7229 
7230 static int
7231 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7232 {
7233 	switch (cmd) {
7234 	case DDI_DETACH:
7235 		return (sd_unit_detach(devi));
7236 	case DDI_SUSPEND:
7237 		return (sd_ddi_suspend(devi));
7238 	default:
7239 		break;
7240 	}
7241 	return (DDI_FAILURE);
7242 }
7243 
7244 
7245 /*
7246  *     Function: sd_sync_with_callback
7247  *
7248  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7249  *		 state while the callback routine is active.
7250  *
7251  *    Arguments: un: softstate structure for the instance
7252  *
7253  *	Context: Kernel thread context
7254  */
7255 
7256 static void
7257 sd_sync_with_callback(struct sd_lun *un)
7258 {
7259 	ASSERT(un != NULL);
7260 
7261 	mutex_enter(SD_MUTEX(un));
7262 
7263 	ASSERT(un->un_in_callback >= 0);
7264 
7265 	while (un->un_in_callback > 0) {
7266 		mutex_exit(SD_MUTEX(un));
7267 		delay(2);
7268 		mutex_enter(SD_MUTEX(un));
7269 	}
7270 
7271 	mutex_exit(SD_MUTEX(un));
7272 }
7273 
7274 /*
7275  *    Function: sd_unit_attach
7276  *
7277  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7278  *		the soft state structure for the device and performs
7279  *		all necessary structure and device initializations.
7280  *
7281  *   Arguments: devi: the system's dev_info_t for the device.
7282  *
7283  * Return Code: DDI_SUCCESS if attach is successful.
7284  *		DDI_FAILURE if any part of the attach fails.
7285  *
7286  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7287  *		Kernel thread context only.  Can sleep.
7288  */
7289 
7290 static int
7291 sd_unit_attach(dev_info_t *devi)
7292 {
7293 	struct	scsi_device	*devp;
7294 	struct	sd_lun		*un;
7295 	char			*variantp;
7296 	char			name_str[48];
7297 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
7298 	int	instance;
7299 	int	rval;
7300 	int	wc_enabled;
7301 	int	wc_changeable;
7302 	int	tgt;
7303 	uint64_t	capacity;
7304 	uint_t		lbasize = 0;
7305 	dev_info_t	*pdip = ddi_get_parent(devi);
7306 	int		offbyone = 0;
7307 	int		geom_label_valid = 0;
7308 	sd_ssc_t	*ssc;
7309 	int		status;
7310 	struct sd_fm_internal	*sfip = NULL;
7311 	int		max_xfer_size;
7312 
7313 	/*
7314 	 * Retrieve the target driver's private data area. This was set
7315 	 * up by the HBA.
7316 	 */
7317 	devp = ddi_get_driver_private(devi);
7318 
7319 	/*
7320 	 * Retrieve the target ID of the device.
7321 	 */
7322 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7323 	    SCSI_ADDR_PROP_TARGET, -1);
7324 
7325 	/*
7326 	 * Since we have no idea what state things were left in by the last
7327 	 * user of the device, set up some 'default' settings, ie. turn 'em
7328 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7329 	 * Do this before the scsi_probe, which sends an inquiry.
7330 	 * This is a fix for bug (4430280).
7331 	 * Of special importance is wide-xfer. The drive could have been left
7332 	 * in wide transfer mode by the last driver to communicate with it,
7333 	 * this includes us. If that's the case, and if the following is not
7334 	 * setup properly or we don't re-negotiate with the drive prior to
7335 	 * transferring data to/from the drive, it causes bus parity errors,
7336 	 * data overruns, and unexpected interrupts. This first occurred when
7337 	 * the fix for bug (4378686) was made.
7338 	 */
7339 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7340 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7341 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7342 
7343 	/*
7344 	 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
7345 	 * on a target. Setting it per lun instance actually sets the
7346 	 * capability of this target, which affects those luns already
7347 	 * attached on the same target. So during attach, we can only disable
7348 	 * this capability only when no other lun has been attached on this
7349 	 * target. By doing this, we assume a target has the same tagged-qing
7350 	 * capability for every lun. The condition can be removed when HBA
7351 	 * is changed to support per lun based tagged-qing capability.
7352 	 */
7353 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
7354 		(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7355 	}
7356 
7357 	/*
7358 	 * Use scsi_probe() to issue an INQUIRY command to the device.
7359 	 * This call will allocate and fill in the scsi_inquiry structure
7360 	 * and point the sd_inq member of the scsi_device structure to it.
7361 	 * If the attach succeeds, then this memory will not be de-allocated
7362 	 * (via scsi_unprobe()) until the instance is detached.
7363 	 */
7364 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7365 		goto probe_failed;
7366 	}
7367 
7368 	/*
7369 	 * Check the device type as specified in the inquiry data and
7370 	 * claim it if it is of a type that we support.
7371 	 */
7372 	switch (devp->sd_inq->inq_dtype) {
7373 	case DTYPE_DIRECT:
7374 		break;
7375 	case DTYPE_RODIRECT:
7376 		break;
7377 	case DTYPE_OPTICAL:
7378 		break;
7379 	case DTYPE_NOTPRESENT:
7380 	default:
7381 		/* Unsupported device type; fail the attach. */
7382 		goto probe_failed;
7383 	}
7384 
7385 	/*
7386 	 * Allocate the soft state structure for this unit.
7387 	 *
7388 	 * We rely upon this memory being set to all zeroes by
7389 	 * ddi_soft_state_zalloc().  We assume that any member of the
7390 	 * soft state structure that is not explicitly initialized by
7391 	 * this routine will have a value of zero.
7392 	 */
7393 	instance = ddi_get_instance(devp->sd_dev);
7394 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7395 		goto probe_failed;
7396 	}
7397 
7398 	/*
7399 	 * Retrieve a pointer to the newly-allocated soft state.
7400 	 *
7401 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7402 	 * was successful, unless something has gone horribly wrong and the
7403 	 * ddi's soft state internals are corrupt (in which case it is
7404 	 * probably better to halt here than just fail the attach....)
7405 	 */
7406 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7407 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
7408 		    instance);
7409 		/*NOTREACHED*/
7410 	}
7411 
7412 	/*
7413 	 * Link the back ptr of the driver soft state to the scsi_device
7414 	 * struct for this lun.
7415 	 * Save a pointer to the softstate in the driver-private area of
7416 	 * the scsi_device struct.
7417 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7418 	 * we first set un->un_sd below.
7419 	 */
7420 	un->un_sd = devp;
7421 	devp->sd_private = (opaque_t)un;
7422 
7423 	/*
7424 	 * The following must be after devp is stored in the soft state struct.
7425 	 */
7426 #ifdef SDDEBUG
7427 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7428 	    "%s_unit_attach: un:0x%p instance:%d\n",
7429 	    ddi_driver_name(devi), un, instance);
7430 #endif
7431 
7432 	/*
7433 	 * Set up the device type and node type (for the minor nodes).
7434 	 * By default we assume that the device can at least support the
7435 	 * Common Command Set. Call it a CD-ROM if it reports itself
7436 	 * as a RODIRECT device.
7437 	 */
7438 	switch (devp->sd_inq->inq_dtype) {
7439 	case DTYPE_RODIRECT:
7440 		un->un_node_type = DDI_NT_CD_CHAN;
7441 		un->un_ctype	 = CTYPE_CDROM;
7442 		break;
7443 	case DTYPE_OPTICAL:
7444 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7445 		un->un_ctype	 = CTYPE_ROD;
7446 		break;
7447 	default:
7448 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7449 		un->un_ctype	 = CTYPE_CCS;
7450 		break;
7451 	}
7452 
7453 	/*
7454 	 * Try to read the interconnect type from the HBA.
7455 	 *
7456 	 * Note: This driver is currently compiled as two binaries, a parallel
7457 	 * scsi version (sd) and a fibre channel version (ssd). All functional
7458 	 * differences are determined at compile time. In the future a single
7459 	 * binary will be provided and the interconnect type will be used to
7460 	 * differentiate between fibre and parallel scsi behaviors. At that time
7461 	 * it will be necessary for all fibre channel HBAs to support this
7462 	 * property.
7463 	 *
7464 	 * set un_f_is_fiber to TRUE ( default fiber )
7465 	 */
7466 	un->un_f_is_fibre = TRUE;
7467 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7468 	case INTERCONNECT_SSA:
7469 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
7470 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7471 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7472 		break;
7473 	case INTERCONNECT_PARALLEL:
7474 		un->un_f_is_fibre = FALSE;
7475 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7476 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7477 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7478 		break;
7479 	case INTERCONNECT_SAS:
7480 		un->un_f_is_fibre = FALSE;
7481 		un->un_interconnect_type = SD_INTERCONNECT_SAS;
7482 		un->un_node_type = DDI_NT_BLOCK_SAS;
7483 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7484 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7485 		break;
7486 	case INTERCONNECT_SATA:
7487 		un->un_f_is_fibre = FALSE;
7488 		un->un_interconnect_type = SD_INTERCONNECT_SATA;
7489 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7490 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7491 		break;
7492 	case INTERCONNECT_FIBRE:
7493 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7494 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7495 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7496 		break;
7497 	case INTERCONNECT_FABRIC:
7498 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7499 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
7500 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7501 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7502 		break;
7503 	default:
7504 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7505 		/*
7506 		 * The HBA does not support the "interconnect-type" property
7507 		 * (or did not provide a recognized type).
7508 		 *
7509 		 * Note: This will be obsoleted when a single fibre channel
7510 		 * and parallel scsi driver is delivered. In the meantime the
7511 		 * interconnect type will be set to the platform default.If that
7512 		 * type is not parallel SCSI, it means that we should be
7513 		 * assuming "ssd" semantics. However, here this also means that
7514 		 * the FC HBA is not supporting the "interconnect-type" property
7515 		 * like we expect it to, so log this occurrence.
7516 		 */
7517 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7518 		if (!SD_IS_PARALLEL_SCSI(un)) {
7519 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7520 			    "sd_unit_attach: un:0x%p Assuming "
7521 			    "INTERCONNECT_FIBRE\n", un);
7522 		} else {
7523 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7524 			    "sd_unit_attach: un:0x%p Assuming "
7525 			    "INTERCONNECT_PARALLEL\n", un);
7526 			un->un_f_is_fibre = FALSE;
7527 		}
7528 #else
7529 		/*
7530 		 * Note: This source will be implemented when a single fibre
7531 		 * channel and parallel scsi driver is delivered. The default
7532 		 * will be to assume that if a device does not support the
7533 		 * "interconnect-type" property it is a parallel SCSI HBA and
7534 		 * we will set the interconnect type for parallel scsi.
7535 		 */
7536 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7537 		un->un_f_is_fibre = FALSE;
7538 #endif
7539 		break;
7540 	}
7541 
7542 	if (un->un_f_is_fibre == TRUE) {
7543 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7544 		    SCSI_VERSION_3) {
7545 			switch (un->un_interconnect_type) {
7546 			case SD_INTERCONNECT_FIBRE:
7547 			case SD_INTERCONNECT_SSA:
7548 				un->un_node_type = DDI_NT_BLOCK_WWN;
7549 				break;
7550 			default:
7551 				break;
7552 			}
7553 		}
7554 	}
7555 
7556 	/*
7557 	 * Initialize the Request Sense command for the target
7558 	 */
7559 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7560 		goto alloc_rqs_failed;
7561 	}
7562 
7563 	/*
7564 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7565 	 * with separate binary for sd and ssd.
7566 	 *
7567 	 * x86 has 1 binary, un_retry_count is set base on connection type.
7568 	 * The hardcoded values will go away when Sparc uses 1 binary
7569 	 * for sd and ssd.  This hardcoded values need to match
7570 	 * SD_RETRY_COUNT in sddef.h
7571 	 * The value used is base on interconnect type.
7572 	 * fibre = 3, parallel = 5
7573 	 */
7574 #if defined(__i386) || defined(__amd64)
7575 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7576 #else
7577 	un->un_retry_count = SD_RETRY_COUNT;
7578 #endif
7579 
7580 	/*
7581 	 * Set the per disk retry count to the default number of retries
7582 	 * for disks and CDROMs. This value can be overridden by the
7583 	 * disk property list or an entry in sd.conf.
7584 	 */
7585 	un->un_notready_retry_count =
7586 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7587 	    : DISK_NOT_READY_RETRY_COUNT(un);
7588 
7589 	/*
7590 	 * Set the busy retry count to the default value of un_retry_count.
7591 	 * This can be overridden by entries in sd.conf or the device
7592 	 * config table.
7593 	 */
7594 	un->un_busy_retry_count = un->un_retry_count;
7595 
7596 	/*
7597 	 * Init the reset threshold for retries.  This number determines
7598 	 * how many retries must be performed before a reset can be issued
7599 	 * (for certain error conditions). This can be overridden by entries
7600 	 * in sd.conf or the device config table.
7601 	 */
7602 	un->un_reset_retry_count = (un->un_retry_count / 2);
7603 
7604 	/*
7605 	 * Set the victim_retry_count to the default un_retry_count
7606 	 */
7607 	un->un_victim_retry_count = (2 * un->un_retry_count);
7608 
7609 	/*
7610 	 * Set the reservation release timeout to the default value of
7611 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
7612 	 * device config table.
7613 	 */
7614 	un->un_reserve_release_time = 5;
7615 
7616 	/*
7617 	 * Set up the default maximum transfer size. Note that this may
7618 	 * get updated later in the attach, when setting up default wide
7619 	 * operations for disks.
7620 	 */
7621 #if defined(__i386) || defined(__amd64)
7622 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7623 	un->un_partial_dma_supported = 1;
7624 #else
7625 	un->un_max_xfer_size = (uint_t)maxphys;
7626 #endif
7627 
7628 	/*
7629 	 * Get "allow bus device reset" property (defaults to "enabled" if
7630 	 * the property was not defined). This is to disable bus resets for
7631 	 * certain kinds of error recovery. Note: In the future when a run-time
7632 	 * fibre check is available the soft state flag should default to
7633 	 * enabled.
7634 	 */
7635 	if (un->un_f_is_fibre == TRUE) {
7636 		un->un_f_allow_bus_device_reset = TRUE;
7637 	} else {
7638 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7639 		    "allow-bus-device-reset", 1) != 0) {
7640 			un->un_f_allow_bus_device_reset = TRUE;
7641 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7642 			    "sd_unit_attach: un:0x%p Bus device reset "
7643 			    "enabled\n", un);
7644 		} else {
7645 			un->un_f_allow_bus_device_reset = FALSE;
7646 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7647 			    "sd_unit_attach: un:0x%p Bus device reset "
7648 			    "disabled\n", un);
7649 		}
7650 	}
7651 
7652 	/*
7653 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
7654 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
7655 	 *
7656 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
7657 	 * property. The new "variant" property with a value of "atapi" has been
7658 	 * introduced so that future 'variants' of standard SCSI behavior (like
7659 	 * atapi) could be specified by the underlying HBA drivers by supplying
7660 	 * a new value for the "variant" property, instead of having to define a
7661 	 * new property.
7662 	 */
7663 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7664 		un->un_f_cfg_is_atapi = TRUE;
7665 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7666 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
7667 	}
7668 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7669 	    &variantp) == DDI_PROP_SUCCESS) {
7670 		if (strcmp(variantp, "atapi") == 0) {
7671 			un->un_f_cfg_is_atapi = TRUE;
7672 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7673 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
7674 		}
7675 		ddi_prop_free(variantp);
7676 	}
7677 
7678 	un->un_cmd_timeout	= SD_IO_TIME;
7679 
7680 	un->un_busy_timeout  = SD_BSY_TIMEOUT;
7681 
7682 	/* Info on current states, statuses, etc. (Updated frequently) */
7683 	un->un_state		= SD_STATE_NORMAL;
7684 	un->un_last_state	= SD_STATE_NORMAL;
7685 
7686 	/* Control & status info for command throttling */
7687 	un->un_throttle		= sd_max_throttle;
7688 	un->un_saved_throttle	= sd_max_throttle;
7689 	un->un_min_throttle	= sd_min_throttle;
7690 
7691 	if (un->un_f_is_fibre == TRUE) {
7692 		un->un_f_use_adaptive_throttle = TRUE;
7693 	} else {
7694 		un->un_f_use_adaptive_throttle = FALSE;
7695 	}
7696 
7697 	/* Removable media support. */
7698 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7699 	un->un_mediastate		= DKIO_NONE;
7700 	un->un_specified_mediastate	= DKIO_NONE;
7701 
7702 	/* CVs for suspend/resume (PM or DR) */
7703 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
7704 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7705 
7706 	/* Power management support. */
7707 	un->un_power_level = SD_SPINDLE_UNINIT;
7708 
7709 	cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
7710 	un->un_f_wcc_inprog = 0;
7711 
7712 	/*
7713 	 * The open/close semaphore is used to serialize threads executing
7714 	 * in the driver's open & close entry point routines for a given
7715 	 * instance.
7716 	 */
7717 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7718 
7719 	/*
7720 	 * The conf file entry and softstate variable is a forceful override,
7721 	 * meaning a non-zero value must be entered to change the default.
7722 	 */
7723 	un->un_f_disksort_disabled = FALSE;
7724 	un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7725 	un->un_f_enable_rmw = FALSE;
7726 
7727 	/*
7728 	 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7729 	 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7730 	 */
7731 	un->un_f_mmc_gesn_polling = TRUE;
7732 
7733 	/*
7734 	 * physical sector size defaults to DEV_BSIZE currently. We can
7735 	 * override this value via the driver configuration file so we must
7736 	 * set it before calling sd_read_unit_properties().
7737 	 */
7738 	un->un_phy_blocksize = DEV_BSIZE;
7739 
7740 	/*
7741 	 * Retrieve the properties from the static driver table or the driver
7742 	 * configuration file (.conf) for this unit and update the soft state
7743 	 * for the device as needed for the indicated properties.
7744 	 * Note: the property configuration needs to occur here as some of the
7745 	 * following routines may have dependencies on soft state flags set
7746 	 * as part of the driver property configuration.
7747 	 */
7748 	sd_read_unit_properties(un);
7749 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7750 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7751 
7752 	/*
7753 	 * Only if a device has "hotpluggable" property, it is
7754 	 * treated as hotpluggable device. Otherwise, it is
7755 	 * regarded as non-hotpluggable one.
7756 	 */
7757 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7758 	    -1) != -1) {
7759 		un->un_f_is_hotpluggable = TRUE;
7760 	}
7761 
7762 	/*
7763 	 * set unit's attributes(flags) according to "hotpluggable" and
7764 	 * RMB bit in INQUIRY data.
7765 	 */
7766 	sd_set_unit_attributes(un, devi);
7767 
7768 	/*
7769 	 * By default, we mark the capacity, lbasize, and geometry
7770 	 * as invalid. Only if we successfully read a valid capacity
7771 	 * will we update the un_blockcount and un_tgt_blocksize with the
7772 	 * valid values (the geometry will be validated later).
7773 	 */
7774 	un->un_f_blockcount_is_valid	= FALSE;
7775 	un->un_f_tgt_blocksize_is_valid	= FALSE;
7776 
7777 	/*
7778 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7779 	 * otherwise.
7780 	 */
7781 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
7782 	un->un_blockcount = 0;
7783 
7784 	/*
7785 	 * Set up the per-instance info needed to determine the correct
7786 	 * CDBs and other info for issuing commands to the target.
7787 	 */
7788 	sd_init_cdb_limits(un);
7789 
7790 	/*
7791 	 * Set up the IO chains to use, based upon the target type.
7792 	 */
7793 	if (un->un_f_non_devbsize_supported) {
7794 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7795 	} else {
7796 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7797 	}
7798 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7799 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7800 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7801 
7802 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7803 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
7804 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7805 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7806 
7807 
7808 	if (ISCD(un)) {
7809 		un->un_additional_codes = sd_additional_codes;
7810 	} else {
7811 		un->un_additional_codes = NULL;
7812 	}
7813 
7814 	/*
7815 	 * Create the kstats here so they can be available for attach-time
7816 	 * routines that send commands to the unit (either polled or via
7817 	 * sd_send_scsi_cmd).
7818 	 *
7819 	 * Note: This is a critical sequence that needs to be maintained:
7820 	 *	1) Instantiate the kstats here, before any routines using the
7821 	 *	   iopath (i.e. sd_send_scsi_cmd).
7822 	 *	2) Instantiate and initialize the partition stats
7823 	 *	   (sd_set_pstats).
7824 	 *	3) Initialize the error stats (sd_set_errstats), following
7825 	 *	   sd_validate_geometry(),sd_register_devid(),
7826 	 *	   and sd_cache_control().
7827 	 */
7828 
7829 	un->un_stats = kstat_create(sd_label, instance,
7830 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7831 	if (un->un_stats != NULL) {
7832 		un->un_stats->ks_lock = SD_MUTEX(un);
7833 		kstat_install(un->un_stats);
7834 	}
7835 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7836 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
7837 
7838 	un->un_unmapstats_ks = kstat_create(sd_label, instance, "unmapstats",
7839 	    "misc", KSTAT_TYPE_NAMED, sizeof (*un->un_unmapstats) /
7840 	    sizeof (kstat_named_t), 0);
7841 	if (un->un_unmapstats_ks) {
7842 		un->un_unmapstats = un->un_unmapstats_ks->ks_data;
7843 
7844 		kstat_named_init(&un->un_unmapstats->us_cmds,
7845 		    "commands", KSTAT_DATA_UINT64);
7846 		kstat_named_init(&un->un_unmapstats->us_errs,
7847 		    "errors", KSTAT_DATA_UINT64);
7848 		kstat_named_init(&un->un_unmapstats->us_extents,
7849 		    "extents", KSTAT_DATA_UINT64);
7850 		kstat_named_init(&un->un_unmapstats->us_bytes,
7851 		    "bytes", KSTAT_DATA_UINT64);
7852 
7853 		kstat_install(un->un_unmapstats_ks);
7854 	} else {
7855 		cmn_err(CE_NOTE, "!Cannot create unmap kstats for disk %d",
7856 		    instance);
7857 	}
7858 
7859 	sd_create_errstats(un, instance);
7860 	if (un->un_errstats == NULL) {
7861 		goto create_errstats_failed;
7862 	}
7863 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7864 	    "sd_unit_attach: un:0x%p errstats created\n", un);
7865 
7866 	/*
7867 	 * The following if/else code was relocated here from below as part
7868 	 * of the fix for bug (4430280). However with the default setup added
7869 	 * on entry to this routine, it's no longer absolutely necessary for
7870 	 * this to be before the call to sd_spin_up_unit.
7871 	 */
7872 	if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7873 		int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7874 		    (devp->sd_inq->inq_ansi == 5)) &&
7875 		    devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7876 
7877 		/*
7878 		 * If tagged queueing is supported by the target
7879 		 * and by the host adapter then we will enable it
7880 		 */
7881 		un->un_tagflags = 0;
7882 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7883 		    (un->un_f_arq_enabled == TRUE)) {
7884 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7885 			    1, 1) == 1) {
7886 				un->un_tagflags = FLAG_STAG;
7887 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7888 				    "sd_unit_attach: un:0x%p tag queueing "
7889 				    "enabled\n", un);
7890 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
7891 			    "untagged-qing", 0) == 1) {
7892 				un->un_f_opt_queueing = TRUE;
7893 				un->un_saved_throttle = un->un_throttle =
7894 				    min(un->un_throttle, 3);
7895 			} else {
7896 				un->un_f_opt_queueing = FALSE;
7897 				un->un_saved_throttle = un->un_throttle = 1;
7898 			}
7899 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7900 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
7901 			/* The Host Adapter supports internal queueing. */
7902 			un->un_f_opt_queueing = TRUE;
7903 			un->un_saved_throttle = un->un_throttle =
7904 			    min(un->un_throttle, 3);
7905 		} else {
7906 			un->un_f_opt_queueing = FALSE;
7907 			un->un_saved_throttle = un->un_throttle = 1;
7908 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7909 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
7910 		}
7911 
7912 		/*
7913 		 * Enable large transfers for SATA/SAS drives
7914 		 */
7915 		if (SD_IS_SERIAL(un)) {
7916 			un->un_max_xfer_size =
7917 			    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7918 			    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7919 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7920 			    "sd_unit_attach: un:0x%p max transfer "
7921 			    "size=0x%x\n", un, un->un_max_xfer_size);
7922 
7923 		}
7924 
7925 		/* Setup or tear down default wide operations for disks */
7926 
7927 		/*
7928 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7929 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
7930 		 * system and be set to different values. In the future this
7931 		 * code may need to be updated when the ssd module is
7932 		 * obsoleted and removed from the system. (4299588)
7933 		 */
7934 		if (SD_IS_PARALLEL_SCSI(un) &&
7935 		    (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7936 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7937 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7938 			    1, 1) == 1) {
7939 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7940 				    "sd_unit_attach: un:0x%p Wide Transfer "
7941 				    "enabled\n", un);
7942 			}
7943 
7944 			/*
7945 			 * If tagged queuing has also been enabled, then
7946 			 * enable large xfers
7947 			 */
7948 			if (un->un_saved_throttle == sd_max_throttle) {
7949 				un->un_max_xfer_size =
7950 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7951 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7952 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7953 				    "sd_unit_attach: un:0x%p max transfer "
7954 				    "size=0x%x\n", un, un->un_max_xfer_size);
7955 			}
7956 		} else {
7957 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7958 			    0, 1) == 1) {
7959 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7960 				    "sd_unit_attach: un:0x%p "
7961 				    "Wide Transfer disabled\n", un);
7962 			}
7963 		}
7964 	} else {
7965 		un->un_tagflags = FLAG_STAG;
7966 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7967 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7968 	}
7969 
7970 	/*
7971 	 * If this target supports LUN reset, try to enable it.
7972 	 */
7973 	if (un->un_f_lun_reset_enabled) {
7974 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7975 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7976 			    "un:0x%p lun_reset capability set\n", un);
7977 		} else {
7978 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7979 			    "un:0x%p lun-reset capability not set\n", un);
7980 		}
7981 	}
7982 
7983 	/*
7984 	 * Adjust the maximum transfer size. This is to fix
7985 	 * the problem of partial DMA support on SPARC. Some
7986 	 * HBA driver, like aac, has very small dma_attr_maxxfer
7987 	 * size, which requires partial DMA support on SPARC.
7988 	 * In the future the SPARC pci nexus driver may solve
7989 	 * the problem instead of this fix.
7990 	 */
7991 	max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7992 	if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7993 		/* We need DMA partial even on sparc to ensure sddump() works */
7994 		un->un_max_xfer_size = max_xfer_size;
7995 		if (un->un_partial_dma_supported == 0)
7996 			un->un_partial_dma_supported = 1;
7997 	}
7998 	if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7999 	    DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
8000 		if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
8001 		    un->un_max_xfer_size) == 1) {
8002 			un->un_buf_breakup_supported = 1;
8003 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
8004 			    "un:0x%p Buf breakup enabled\n", un);
8005 		}
8006 	}
8007 
8008 	/*
8009 	 * Set PKT_DMA_PARTIAL flag.
8010 	 */
8011 	if (un->un_partial_dma_supported == 1) {
8012 		un->un_pkt_flags = PKT_DMA_PARTIAL;
8013 	} else {
8014 		un->un_pkt_flags = 0;
8015 	}
8016 
8017 	/* Initialize sd_ssc_t for internal uscsi commands */
8018 	ssc = sd_ssc_init(un);
8019 	scsi_fm_init(devp);
8020 
8021 	/*
8022 	 * Allocate memory for SCSI FMA stuffs.
8023 	 */
8024 	un->un_fm_private =
8025 	    kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
8026 	sfip = (struct sd_fm_internal *)un->un_fm_private;
8027 	sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
8028 	sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
8029 	sfip->fm_ssc.ssc_un = un;
8030 
8031 	if (ISCD(un) ||
8032 	    un->un_f_has_removable_media ||
8033 	    devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
8034 		/*
8035 		 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
8036 		 * Their log are unchanged.
8037 		 */
8038 		sfip->fm_log_level = SD_FM_LOG_NSUP;
8039 	} else {
8040 		/*
8041 		 * If enter here, it should be non-CDROM and FM-capable
8042 		 * device, and it will not keep the old scsi_log as before
8043 		 * in /var/adm/messages. However, the property
8044 		 * "fm-scsi-log" will control whether the FM telemetry will
8045 		 * be logged in /var/adm/messages.
8046 		 */
8047 		int fm_scsi_log;
8048 		fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
8049 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
8050 
8051 		if (fm_scsi_log)
8052 			sfip->fm_log_level = SD_FM_LOG_EREPORT;
8053 		else
8054 			sfip->fm_log_level = SD_FM_LOG_SILENT;
8055 	}
8056 
8057 	/*
8058 	 * At this point in the attach, we have enough info in the
8059 	 * soft state to be able to issue commands to the target.
8060 	 *
8061 	 * All command paths used below MUST issue their commands as
8062 	 * SD_PATH_DIRECT. This is important as intermediate layers
8063 	 * are not all initialized yet (such as PM).
8064 	 */
8065 
8066 	/*
8067 	 * Send a TEST UNIT READY command to the device. This should clear
8068 	 * any outstanding UNIT ATTENTION that may be present.
8069 	 *
8070 	 * Note: Don't check for success, just track if there is a reservation,
8071 	 * this is a throw away command to clear any unit attentions.
8072 	 *
8073 	 * Note: This MUST be the first command issued to the target during
8074 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
8075 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
8076 	 * with attempts at spinning up a device with no media.
8077 	 */
8078 	status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
8079 	if (status != 0) {
8080 		if (status == EACCES)
8081 			reservation_flag = SD_TARGET_IS_RESERVED;
8082 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8083 	}
8084 
8085 	/*
8086 	 * If the device is NOT a removable media device, attempt to spin
8087 	 * it up (using the START_STOP_UNIT command) and read its capacity
8088 	 * (using the READ CAPACITY command).  Note, however, that either
8089 	 * of these could fail and in some cases we would continue with
8090 	 * the attach despite the failure (see below).
8091 	 */
8092 	if (un->un_f_descr_format_supported) {
8093 
8094 		switch (sd_spin_up_unit(ssc)) {
8095 		case 0:
8096 			/*
8097 			 * Spin-up was successful; now try to read the
8098 			 * capacity.  If successful then save the results
8099 			 * and mark the capacity & lbasize as valid.
8100 			 */
8101 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8102 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
8103 
8104 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
8105 			    &lbasize, SD_PATH_DIRECT);
8106 
8107 			switch (status) {
8108 			case 0: {
8109 				if (capacity > DK_MAX_BLOCKS) {
8110 #ifdef _LP64
8111 					if ((capacity + 1) >
8112 					    SD_GROUP1_MAX_ADDRESS) {
8113 						/*
8114 						 * Enable descriptor format
8115 						 * sense data so that we can
8116 						 * get 64 bit sense data
8117 						 * fields.
8118 						 */
8119 						sd_enable_descr_sense(ssc);
8120 					}
8121 #else
8122 					/* 32-bit kernels can't handle this */
8123 					scsi_log(SD_DEVINFO(un),
8124 					    sd_label, CE_WARN,
8125 					    "disk has %llu blocks, which "
8126 					    "is too large for a 32-bit "
8127 					    "kernel", capacity);
8128 
8129 #if defined(__i386) || defined(__amd64)
8130 					/*
8131 					 * 1TB disk was treated as (1T - 512)B
8132 					 * in the past, so that it might have
8133 					 * valid VTOC and solaris partitions,
8134 					 * we have to allow it to continue to
8135 					 * work.
8136 					 */
8137 					if (capacity -1 > DK_MAX_BLOCKS)
8138 #endif
8139 					goto spinup_failed;
8140 #endif
8141 				}
8142 
8143 				/*
8144 				 * Here it's not necessary to check the case:
8145 				 * the capacity of the device is bigger than
8146 				 * what the max hba cdb can support. Because
8147 				 * sd_send_scsi_READ_CAPACITY will retrieve
8148 				 * the capacity by sending USCSI command, which
8149 				 * is constrained by the max hba cdb. Actually,
8150 				 * sd_send_scsi_READ_CAPACITY will return
8151 				 * EINVAL when using bigger cdb than required
8152 				 * cdb length. Will handle this case in
8153 				 * "case EINVAL".
8154 				 */
8155 
8156 				/*
8157 				 * The following relies on
8158 				 * sd_send_scsi_READ_CAPACITY never
8159 				 * returning 0 for capacity and/or lbasize.
8160 				 */
8161 				sd_update_block_info(un, lbasize, capacity);
8162 
8163 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8164 				    "sd_unit_attach: un:0x%p capacity = %ld "
8165 				    "blocks; lbasize= %ld.\n", un,
8166 				    un->un_blockcount, un->un_tgt_blocksize);
8167 
8168 				break;
8169 			}
8170 			case EINVAL:
8171 				/*
8172 				 * In the case where the max-cdb-length property
8173 				 * is smaller than the required CDB length for
8174 				 * a SCSI device, a target driver can fail to
8175 				 * attach to that device.
8176 				 */
8177 				scsi_log(SD_DEVINFO(un),
8178 				    sd_label, CE_WARN,
8179 				    "disk capacity is too large "
8180 				    "for current cdb length");
8181 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8182 
8183 				goto spinup_failed;
8184 			case EACCES:
8185 				/*
8186 				 * Should never get here if the spin-up
8187 				 * succeeded, but code it in anyway.
8188 				 * From here, just continue with the attach...
8189 				 */
8190 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8191 				    "sd_unit_attach: un:0x%p "
8192 				    "sd_send_scsi_READ_CAPACITY "
8193 				    "returned reservation conflict\n", un);
8194 				reservation_flag = SD_TARGET_IS_RESERVED;
8195 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8196 				break;
8197 			default:
8198 				/*
8199 				 * Likewise, should never get here if the
8200 				 * spin-up succeeded. Just continue with
8201 				 * the attach...
8202 				 */
8203 				if (status == EIO)
8204 					sd_ssc_assessment(ssc,
8205 					    SD_FMT_STATUS_CHECK);
8206 				else
8207 					sd_ssc_assessment(ssc,
8208 					    SD_FMT_IGNORE);
8209 				break;
8210 			}
8211 			break;
8212 		case EACCES:
8213 			/*
8214 			 * Device is reserved by another host.  In this case
8215 			 * we could not spin it up or read the capacity, but
8216 			 * we continue with the attach anyway.
8217 			 */
8218 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8219 			    "sd_unit_attach: un:0x%p spin-up reservation "
8220 			    "conflict.\n", un);
8221 			reservation_flag = SD_TARGET_IS_RESERVED;
8222 			break;
8223 		default:
8224 			/* Fail the attach if the spin-up failed. */
8225 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8226 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8227 			goto spinup_failed;
8228 		}
8229 
8230 	}
8231 
8232 	/*
8233 	 * Check to see if this is a MMC drive
8234 	 */
8235 	if (ISCD(un)) {
8236 		sd_set_mmc_caps(ssc);
8237 	}
8238 
8239 	/*
8240 	 * Add a zero-length attribute to tell the world we support
8241 	 * kernel ioctls (for layered drivers)
8242 	 */
8243 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8244 	    DDI_KERNEL_IOCTL, NULL, 0);
8245 
8246 	/*
8247 	 * Add a boolean property to tell the world we support
8248 	 * the B_FAILFAST flag (for layered drivers)
8249 	 */
8250 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8251 	    "ddi-failfast-supported", NULL, 0);
8252 
8253 	/*
8254 	 * Initialize power management
8255 	 */
8256 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8257 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8258 	sd_setup_pm(ssc, devi);
8259 	if (un->un_f_pm_is_enabled == FALSE) {
8260 		/*
8261 		 * For performance, point to a jump table that does
8262 		 * not include pm.
8263 		 * The direct and priority chains don't change with PM.
8264 		 *
8265 		 * Note: this is currently done based on individual device
8266 		 * capabilities. When an interface for determining system
8267 		 * power enabled state becomes available, or when additional
8268 		 * layers are added to the command chain, these values will
8269 		 * have to be re-evaluated for correctness.
8270 		 */
8271 		if (un->un_f_non_devbsize_supported) {
8272 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8273 		} else {
8274 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8275 		}
8276 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8277 	}
8278 
8279 	/*
8280 	 * This property is set to 0 by HA software to avoid retries
8281 	 * on a reserved disk. (The preferred property name is
8282 	 * "retry-on-reservation-conflict") (1189689)
8283 	 *
8284 	 * Note: The use of a global here can have unintended consequences. A
8285 	 * per instance variable is preferable to match the capabilities of
8286 	 * different underlying hba's (4402600)
8287 	 */
8288 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8289 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8290 	    sd_retry_on_reservation_conflict);
8291 	if (sd_retry_on_reservation_conflict != 0) {
8292 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8293 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8294 		    sd_retry_on_reservation_conflict);
8295 	}
8296 
8297 	/* Set up options for QFULL handling. */
8298 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8299 	    "qfull-retries", -1)) != -1) {
8300 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8301 		    rval, 1);
8302 	}
8303 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8304 	    "qfull-retry-interval", -1)) != -1) {
8305 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8306 		    rval, 1);
8307 	}
8308 
8309 	/*
8310 	 * This just prints a message that announces the existence of the
8311 	 * device. The message is always printed in the system logfile, but
8312 	 * only appears on the console if the system is booted with the
8313 	 * -v (verbose) argument.
8314 	 */
8315 	ddi_report_dev(devi);
8316 
8317 	un->un_mediastate = DKIO_NONE;
8318 
8319 	/*
8320 	 * Check Block Device Characteristics VPD.
8321 	 */
8322 	sd_check_bdc_vpd(ssc);
8323 
8324 	/*
8325 	 * Check whether the drive is in emulation mode.
8326 	 */
8327 	sd_check_emulation_mode(ssc);
8328 
8329 	cmlb_alloc_handle(&un->un_cmlbhandle);
8330 
8331 #if defined(__i386) || defined(__amd64)
8332 	/*
8333 	 * On x86, compensate for off-by-1 legacy error
8334 	 */
8335 	if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8336 	    (lbasize == un->un_sys_blocksize))
8337 		offbyone = CMLB_OFF_BY_ONE;
8338 #endif
8339 
8340 	if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
8341 	    VOID2BOOLEAN(un->un_f_has_removable_media != 0),
8342 	    VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
8343 	    un->un_node_type, offbyone, un->un_cmlbhandle,
8344 	    (void *)SD_PATH_DIRECT) != 0) {
8345 		goto cmlb_attach_failed;
8346 	}
8347 
8348 
8349 	/*
8350 	 * Read and validate the device's geometry (ie, disk label)
8351 	 * A new unformatted drive will not have a valid geometry, but
8352 	 * the driver needs to successfully attach to this device so
8353 	 * the drive can be formatted via ioctls.
8354 	 */
8355 	geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8356 	    (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8357 
8358 	mutex_enter(SD_MUTEX(un));
8359 
8360 	/*
8361 	 * Read and initialize the devid for the unit.
8362 	 */
8363 	if (un->un_f_devid_supported) {
8364 		sd_register_devid(ssc, devi, reservation_flag);
8365 	}
8366 	mutex_exit(SD_MUTEX(un));
8367 
8368 #if (defined(__fibre))
8369 	/*
8370 	 * Register callbacks for fibre only.  You can't do this solely
8371 	 * on the basis of the devid_type because this is hba specific.
8372 	 * We need to query our hba capabilities to find out whether to
8373 	 * register or not.
8374 	 */
8375 	if (un->un_f_is_fibre) {
8376 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8377 			sd_init_event_callbacks(un);
8378 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8379 			    "sd_unit_attach: un:0x%p event callbacks inserted",
8380 			    un);
8381 		}
8382 	}
8383 #endif
8384 
8385 	if (un->un_f_opt_disable_cache == TRUE) {
8386 		/*
8387 		 * Disable both read cache and write cache.  This is
8388 		 * the historic behavior of the keywords in the config file.
8389 		 */
8390 		if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8391 		    0) {
8392 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8393 			    "sd_unit_attach: un:0x%p Could not disable "
8394 			    "caching", un);
8395 			goto devid_failed;
8396 		}
8397 	}
8398 
8399 	/*
8400 	 * Check the value of the WCE bit and if it's allowed to be changed,
8401 	 * set un_f_write_cache_enabled and un_f_cache_mode_changeable
8402 	 * accordingly.
8403 	 */
8404 	(void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8405 	sd_get_write_cache_changeable(ssc, &wc_changeable);
8406 	mutex_enter(SD_MUTEX(un));
8407 	un->un_f_write_cache_enabled = (wc_enabled != 0);
8408 	un->un_f_cache_mode_changeable = (wc_changeable != 0);
8409 	mutex_exit(SD_MUTEX(un));
8410 
8411 	if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
8412 	    un->un_tgt_blocksize != DEV_BSIZE) ||
8413 	    un->un_f_enable_rmw) {
8414 		if (!(un->un_wm_cache)) {
8415 			(void) snprintf(name_str, sizeof (name_str),
8416 			    "%s%d_cache",
8417 			    ddi_driver_name(SD_DEVINFO(un)),
8418 			    ddi_get_instance(SD_DEVINFO(un)));
8419 			un->un_wm_cache = kmem_cache_create(
8420 			    name_str, sizeof (struct sd_w_map),
8421 			    8, sd_wm_cache_constructor,
8422 			    sd_wm_cache_destructor, NULL,
8423 			    (void *)un, NULL, 0);
8424 			if (!(un->un_wm_cache)) {
8425 				goto wm_cache_failed;
8426 			}
8427 		}
8428 	}
8429 
8430 	/*
8431 	 * Check the value of the NV_SUP bit and set
8432 	 * un_f_suppress_cache_flush accordingly.
8433 	 */
8434 	sd_get_nv_sup(ssc);
8435 
8436 	/*
8437 	 * Find out what type of reservation this disk supports.
8438 	 */
8439 	status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
8440 
8441 	switch (status) {
8442 	case 0:
8443 		/*
8444 		 * SCSI-3 reservations are supported.
8445 		 */
8446 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8447 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8448 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8449 		break;
8450 	case ENOTSUP:
8451 		/*
8452 		 * The PERSISTENT RESERVE IN command would not be recognized by
8453 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8454 		 */
8455 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8456 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8457 		un->un_reservation_type = SD_SCSI2_RESERVATION;
8458 
8459 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8460 		break;
8461 	default:
8462 		/*
8463 		 * default to SCSI-3 reservations
8464 		 */
8465 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8466 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8467 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8468 
8469 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8470 		break;
8471 	}
8472 
8473 	/*
8474 	 * Set the pstat and error stat values here, so data obtained during the
8475 	 * previous attach-time routines is available.
8476 	 *
8477 	 * Note: This is a critical sequence that needs to be maintained:
8478 	 *	1) Instantiate the kstats before any routines using the iopath
8479 	 *	   (i.e. sd_send_scsi_cmd).
8480 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8481 	 *	   stats (sd_set_pstats)here, following
8482 	 *	   cmlb_validate_geometry(), sd_register_devid(), and
8483 	 *	   sd_cache_control().
8484 	 */
8485 
8486 	if (un->un_f_pkstats_enabled && geom_label_valid) {
8487 		sd_set_pstats(un);
8488 		SD_TRACE(SD_LOG_IO_PARTITION, un,
8489 		    "sd_unit_attach: un:0x%p pstats created and set\n", un);
8490 	}
8491 
8492 	sd_set_errstats(un);
8493 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8494 	    "sd_unit_attach: un:0x%p errstats set\n", un);
8495 
8496 	sd_setup_blk_limits(ssc);
8497 
8498 	/*
8499 	 * After successfully attaching an instance, we record the information
8500 	 * of how many luns have been attached on the relative target and
8501 	 * controller for parallel SCSI. This information is used when sd tries
8502 	 * to set the tagged queuing capability in HBA.
8503 	 */
8504 	if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8505 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8506 	}
8507 
8508 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8509 	    "sd_unit_attach: un:0x%p exit success\n", un);
8510 
8511 	/* Uninitialize sd_ssc_t pointer */
8512 	sd_ssc_fini(ssc);
8513 
8514 	return (DDI_SUCCESS);
8515 
8516 	/*
8517 	 * An error occurred during the attach; clean up & return failure.
8518 	 */
8519 wm_cache_failed:
8520 devid_failed:
8521 	ddi_remove_minor_node(devi, NULL);
8522 
8523 cmlb_attach_failed:
8524 	/*
8525 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8526 	 */
8527 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8528 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8529 
8530 	/*
8531 	 * Refer to the comments of setting tagged-qing in the beginning of
8532 	 * sd_unit_attach. We can only disable tagged queuing when there is
8533 	 * no lun attached on the target.
8534 	 */
8535 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8536 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8537 	}
8538 
8539 	if (un->un_f_is_fibre == FALSE) {
8540 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8541 	}
8542 
8543 spinup_failed:
8544 
8545 	/* Uninitialize sd_ssc_t pointer */
8546 	sd_ssc_fini(ssc);
8547 
8548 	mutex_enter(SD_MUTEX(un));
8549 
8550 	/* Deallocate SCSI FMA memory spaces */
8551 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8552 
8553 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8554 	if (un->un_direct_priority_timeid != NULL) {
8555 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8556 		un->un_direct_priority_timeid = NULL;
8557 		mutex_exit(SD_MUTEX(un));
8558 		(void) untimeout(temp_id);
8559 		mutex_enter(SD_MUTEX(un));
8560 	}
8561 
8562 	/* Cancel any pending start/stop timeouts */
8563 	if (un->un_startstop_timeid != NULL) {
8564 		timeout_id_t temp_id = un->un_startstop_timeid;
8565 		un->un_startstop_timeid = NULL;
8566 		mutex_exit(SD_MUTEX(un));
8567 		(void) untimeout(temp_id);
8568 		mutex_enter(SD_MUTEX(un));
8569 	}
8570 
8571 	/* Cancel any pending reset-throttle timeouts */
8572 	if (un->un_reset_throttle_timeid != NULL) {
8573 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8574 		un->un_reset_throttle_timeid = NULL;
8575 		mutex_exit(SD_MUTEX(un));
8576 		(void) untimeout(temp_id);
8577 		mutex_enter(SD_MUTEX(un));
8578 	}
8579 
8580 	/* Cancel rmw warning message timeouts */
8581 	if (un->un_rmw_msg_timeid != NULL) {
8582 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8583 		un->un_rmw_msg_timeid = NULL;
8584 		mutex_exit(SD_MUTEX(un));
8585 		(void) untimeout(temp_id);
8586 		mutex_enter(SD_MUTEX(un));
8587 	}
8588 
8589 	/* Cancel any pending retry timeouts */
8590 	if (un->un_retry_timeid != NULL) {
8591 		timeout_id_t temp_id = un->un_retry_timeid;
8592 		un->un_retry_timeid = NULL;
8593 		mutex_exit(SD_MUTEX(un));
8594 		(void) untimeout(temp_id);
8595 		mutex_enter(SD_MUTEX(un));
8596 	}
8597 
8598 	/* Cancel any pending delayed cv broadcast timeouts */
8599 	if (un->un_dcvb_timeid != NULL) {
8600 		timeout_id_t temp_id = un->un_dcvb_timeid;
8601 		un->un_dcvb_timeid = NULL;
8602 		mutex_exit(SD_MUTEX(un));
8603 		(void) untimeout(temp_id);
8604 		mutex_enter(SD_MUTEX(un));
8605 	}
8606 
8607 	mutex_exit(SD_MUTEX(un));
8608 
8609 	/* There should not be any in-progress I/O so ASSERT this check */
8610 	ASSERT(un->un_ncmds_in_transport == 0);
8611 	ASSERT(un->un_ncmds_in_driver == 0);
8612 
8613 	/* Do not free the softstate if the callback routine is active */
8614 	sd_sync_with_callback(un);
8615 
8616 	/*
8617 	 * Partition stats apparently are not used with removables. These would
8618 	 * not have been created during attach, so no need to clean them up...
8619 	 */
8620 	if (un->un_errstats != NULL) {
8621 		kstat_delete(un->un_errstats);
8622 		un->un_errstats = NULL;
8623 	}
8624 
8625 create_errstats_failed:
8626 
8627 	if (un->un_stats != NULL) {
8628 		kstat_delete(un->un_stats);
8629 		un->un_stats = NULL;
8630 	}
8631 
8632 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8633 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8634 
8635 	ddi_prop_remove_all(devi);
8636 	sema_destroy(&un->un_semoclose);
8637 	cv_destroy(&un->un_state_cv);
8638 
8639 	sd_free_rqs(un);
8640 
8641 alloc_rqs_failed:
8642 
8643 	devp->sd_private = NULL;
8644 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
8645 
8646 	/*
8647 	 * Note: the man pages are unclear as to whether or not doing a
8648 	 * ddi_soft_state_free(sd_state, instance) is the right way to
8649 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
8650 	 * ddi_get_soft_state() fails.  The implication seems to be
8651 	 * that the get_soft_state cannot fail if the zalloc succeeds.
8652 	 */
8653 #ifndef XPV_HVM_DRIVER
8654 	ddi_soft_state_free(sd_state, instance);
8655 #endif /* !XPV_HVM_DRIVER */
8656 
8657 probe_failed:
8658 	scsi_unprobe(devp);
8659 
8660 	return (DDI_FAILURE);
8661 }
8662 
8663 
8664 /*
8665  *    Function: sd_unit_detach
8666  *
8667  * Description: Performs DDI_DETACH processing for sddetach().
8668  *
8669  * Return Code: DDI_SUCCESS
8670  *		DDI_FAILURE
8671  *
8672  *     Context: Kernel thread context
8673  */
8674 
8675 static int
8676 sd_unit_detach(dev_info_t *devi)
8677 {
8678 	struct scsi_device	*devp;
8679 	struct sd_lun		*un;
8680 	int			i;
8681 	int			tgt;
8682 	dev_t			dev;
8683 	dev_info_t		*pdip = ddi_get_parent(devi);
8684 	int			instance = ddi_get_instance(devi);
8685 
8686 	mutex_enter(&sd_detach_mutex);
8687 
8688 	/*
8689 	 * Fail the detach for any of the following:
8690 	 *  - Unable to get the sd_lun struct for the instance
8691 	 *  - A layered driver has an outstanding open on the instance
8692 	 *  - Another thread is already detaching this instance
8693 	 *  - Another thread is currently performing an open
8694 	 */
8695 	devp = ddi_get_driver_private(devi);
8696 	if ((devp == NULL) ||
8697 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8698 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8699 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8700 		mutex_exit(&sd_detach_mutex);
8701 		return (DDI_FAILURE);
8702 	}
8703 
8704 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8705 
8706 	/*
8707 	 * Mark this instance as currently in a detach, to inhibit any
8708 	 * opens from a layered driver.
8709 	 */
8710 	un->un_detach_count++;
8711 	mutex_exit(&sd_detach_mutex);
8712 
8713 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8714 	    SCSI_ADDR_PROP_TARGET, -1);
8715 
8716 	dev = sd_make_device(SD_DEVINFO(un));
8717 
8718 #ifndef lint
8719 	_NOTE(COMPETING_THREADS_NOW);
8720 #endif
8721 
8722 	mutex_enter(SD_MUTEX(un));
8723 
8724 	/*
8725 	 * Fail the detach if there are any outstanding layered
8726 	 * opens on this device.
8727 	 */
8728 	for (i = 0; i < NDKMAP; i++) {
8729 		if (un->un_ocmap.lyropen[i] != 0) {
8730 			goto err_notclosed;
8731 		}
8732 	}
8733 
8734 	/*
8735 	 * Verify there are NO outstanding commands issued to this device.
8736 	 * ie, un_ncmds_in_transport == 0.
8737 	 * It's possible to have outstanding commands through the physio
8738 	 * code path, even though everything's closed.
8739 	 */
8740 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8741 	    (un->un_direct_priority_timeid != NULL) ||
8742 	    (un->un_state == SD_STATE_RWAIT)) {
8743 		mutex_exit(SD_MUTEX(un));
8744 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8745 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
8746 		goto err_stillbusy;
8747 	}
8748 
8749 	/*
8750 	 * If we have the device reserved, release the reservation.
8751 	 */
8752 	if ((un->un_resvd_status & SD_RESERVE) &&
8753 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
8754 		mutex_exit(SD_MUTEX(un));
8755 		/*
8756 		 * Note: sd_reserve_release sends a command to the device
8757 		 * via the sd_ioctlcmd() path, and can sleep.
8758 		 */
8759 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8760 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8761 			    "sd_dr_detach: Cannot release reservation \n");
8762 		}
8763 	} else {
8764 		mutex_exit(SD_MUTEX(un));
8765 	}
8766 
8767 	/*
8768 	 * Untimeout any reserve recover, throttle reset, restart unit
8769 	 * and delayed broadcast timeout threads. Protect the timeout pointer
8770 	 * from getting nulled by their callback functions.
8771 	 */
8772 	mutex_enter(SD_MUTEX(un));
8773 	if (un->un_resvd_timeid != NULL) {
8774 		timeout_id_t temp_id = un->un_resvd_timeid;
8775 		un->un_resvd_timeid = NULL;
8776 		mutex_exit(SD_MUTEX(un));
8777 		(void) untimeout(temp_id);
8778 		mutex_enter(SD_MUTEX(un));
8779 	}
8780 
8781 	if (un->un_reset_throttle_timeid != NULL) {
8782 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8783 		un->un_reset_throttle_timeid = NULL;
8784 		mutex_exit(SD_MUTEX(un));
8785 		(void) untimeout(temp_id);
8786 		mutex_enter(SD_MUTEX(un));
8787 	}
8788 
8789 	if (un->un_startstop_timeid != NULL) {
8790 		timeout_id_t temp_id = un->un_startstop_timeid;
8791 		un->un_startstop_timeid = NULL;
8792 		mutex_exit(SD_MUTEX(un));
8793 		(void) untimeout(temp_id);
8794 		mutex_enter(SD_MUTEX(un));
8795 	}
8796 
8797 	if (un->un_rmw_msg_timeid != NULL) {
8798 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8799 		un->un_rmw_msg_timeid = NULL;
8800 		mutex_exit(SD_MUTEX(un));
8801 		(void) untimeout(temp_id);
8802 		mutex_enter(SD_MUTEX(un));
8803 	}
8804 
8805 	if (un->un_dcvb_timeid != NULL) {
8806 		timeout_id_t temp_id = un->un_dcvb_timeid;
8807 		un->un_dcvb_timeid = NULL;
8808 		mutex_exit(SD_MUTEX(un));
8809 		(void) untimeout(temp_id);
8810 	} else {
8811 		mutex_exit(SD_MUTEX(un));
8812 	}
8813 
8814 	/* Remove any pending reservation reclaim requests for this device */
8815 	sd_rmv_resv_reclaim_req(dev);
8816 
8817 	mutex_enter(SD_MUTEX(un));
8818 
8819 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8820 	if (un->un_direct_priority_timeid != NULL) {
8821 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8822 		un->un_direct_priority_timeid = NULL;
8823 		mutex_exit(SD_MUTEX(un));
8824 		(void) untimeout(temp_id);
8825 		mutex_enter(SD_MUTEX(un));
8826 	}
8827 
8828 	/* Cancel any active multi-host disk watch thread requests */
8829 	if (un->un_mhd_token != NULL) {
8830 		mutex_exit(SD_MUTEX(un));
8831 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8832 		if (scsi_watch_request_terminate(un->un_mhd_token,
8833 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8834 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8835 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
8836 			/*
8837 			 * Note: We are returning here after having removed
8838 			 * some driver timeouts above. This is consistent with
8839 			 * the legacy implementation but perhaps the watch
8840 			 * terminate call should be made with the wait flag set.
8841 			 */
8842 			goto err_stillbusy;
8843 		}
8844 		mutex_enter(SD_MUTEX(un));
8845 		un->un_mhd_token = NULL;
8846 	}
8847 
8848 	if (un->un_swr_token != NULL) {
8849 		mutex_exit(SD_MUTEX(un));
8850 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8851 		if (scsi_watch_request_terminate(un->un_swr_token,
8852 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8853 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8854 			    "sd_dr_detach: Cannot cancel swr watch request\n");
8855 			/*
8856 			 * Note: We are returning here after having removed
8857 			 * some driver timeouts above. This is consistent with
8858 			 * the legacy implementation but perhaps the watch
8859 			 * terminate call should be made with the wait flag set.
8860 			 */
8861 			goto err_stillbusy;
8862 		}
8863 		mutex_enter(SD_MUTEX(un));
8864 		un->un_swr_token = NULL;
8865 	}
8866 
8867 	mutex_exit(SD_MUTEX(un));
8868 
8869 	/*
8870 	 * Clear any scsi_reset_notifies. We clear the reset notifies
8871 	 * if we have not registered one.
8872 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8873 	 */
8874 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8875 	    sd_mhd_reset_notify_cb, (caddr_t)un);
8876 
8877 	/*
8878 	 * protect the timeout pointers from getting nulled by
8879 	 * their callback functions during the cancellation process.
8880 	 * In such a scenario untimeout can be invoked with a null value.
8881 	 */
8882 	_NOTE(NO_COMPETING_THREADS_NOW);
8883 
8884 	mutex_enter(&un->un_pm_mutex);
8885 	if (un->un_pm_idle_timeid != NULL) {
8886 		timeout_id_t temp_id = un->un_pm_idle_timeid;
8887 		un->un_pm_idle_timeid = NULL;
8888 		mutex_exit(&un->un_pm_mutex);
8889 
8890 		/*
8891 		 * Timeout is active; cancel it.
8892 		 * Note that it'll never be active on a device
8893 		 * that does not support PM therefore we don't
8894 		 * have to check before calling pm_idle_component.
8895 		 */
8896 		(void) untimeout(temp_id);
8897 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8898 		mutex_enter(&un->un_pm_mutex);
8899 	}
8900 
8901 	/*
8902 	 * Check whether there is already a timeout scheduled for power
8903 	 * management. If yes then don't lower the power here, that's.
8904 	 * the timeout handler's job.
8905 	 */
8906 	if (un->un_pm_timeid != NULL) {
8907 		timeout_id_t temp_id = un->un_pm_timeid;
8908 		un->un_pm_timeid = NULL;
8909 		mutex_exit(&un->un_pm_mutex);
8910 		/*
8911 		 * Timeout is active; cancel it.
8912 		 * Note that it'll never be active on a device
8913 		 * that does not support PM therefore we don't
8914 		 * have to check before calling pm_idle_component.
8915 		 */
8916 		(void) untimeout(temp_id);
8917 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8918 
8919 	} else {
8920 		mutex_exit(&un->un_pm_mutex);
8921 		if ((un->un_f_pm_is_enabled == TRUE) &&
8922 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8923 		    != DDI_SUCCESS)) {
8924 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8925 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
8926 			/*
8927 			 * Fix for bug: 4297749, item # 13
8928 			 * The above test now includes a check to see if PM is
8929 			 * supported by this device before call
8930 			 * pm_lower_power().
8931 			 * Note, the following is not dead code. The call to
8932 			 * pm_lower_power above will generate a call back into
8933 			 * our sdpower routine which might result in a timeout
8934 			 * handler getting activated. Therefore the following
8935 			 * code is valid and necessary.
8936 			 */
8937 			mutex_enter(&un->un_pm_mutex);
8938 			if (un->un_pm_timeid != NULL) {
8939 				timeout_id_t temp_id = un->un_pm_timeid;
8940 				un->un_pm_timeid = NULL;
8941 				mutex_exit(&un->un_pm_mutex);
8942 				(void) untimeout(temp_id);
8943 				(void) pm_idle_component(SD_DEVINFO(un), 0);
8944 			} else {
8945 				mutex_exit(&un->un_pm_mutex);
8946 			}
8947 		}
8948 	}
8949 
8950 	/*
8951 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8952 	 * Relocated here from above to be after the call to
8953 	 * pm_lower_power, which was getting errors.
8954 	 */
8955 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8956 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8957 
8958 	/*
8959 	 * Currently, tagged queuing is supported per target based by HBA.
8960 	 * Setting this per lun instance actually sets the capability of this
8961 	 * target in HBA, which affects those luns already attached on the
8962 	 * same target. So during detach, we can only disable this capability
8963 	 * only when this is the only lun left on this target. By doing
8964 	 * this, we assume a target has the same tagged queuing capability
8965 	 * for every lun. The condition can be removed when HBA is changed to
8966 	 * support per lun based tagged queuing capability.
8967 	 */
8968 	if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8969 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8970 	}
8971 
8972 	if (un->un_f_is_fibre == FALSE) {
8973 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8974 	}
8975 
8976 	/*
8977 	 * Remove any event callbacks, fibre only
8978 	 */
8979 	if (un->un_f_is_fibre == TRUE) {
8980 		if ((un->un_insert_event != NULL) &&
8981 		    (ddi_remove_event_handler(un->un_insert_cb_id) !=
8982 		    DDI_SUCCESS)) {
8983 			/*
8984 			 * Note: We are returning here after having done
8985 			 * substantial cleanup above. This is consistent
8986 			 * with the legacy implementation but this may not
8987 			 * be the right thing to do.
8988 			 */
8989 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8990 			    "sd_dr_detach: Cannot cancel insert event\n");
8991 			goto err_remove_event;
8992 		}
8993 		un->un_insert_event = NULL;
8994 
8995 		if ((un->un_remove_event != NULL) &&
8996 		    (ddi_remove_event_handler(un->un_remove_cb_id) !=
8997 		    DDI_SUCCESS)) {
8998 			/*
8999 			 * Note: We are returning here after having done
9000 			 * substantial cleanup above. This is consistent
9001 			 * with the legacy implementation but this may not
9002 			 * be the right thing to do.
9003 			 */
9004 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9005 			    "sd_dr_detach: Cannot cancel remove event\n");
9006 			goto err_remove_event;
9007 		}
9008 		un->un_remove_event = NULL;
9009 	}
9010 
9011 	/* Do not free the softstate if the callback routine is active */
9012 	sd_sync_with_callback(un);
9013 
9014 	cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
9015 	cmlb_free_handle(&un->un_cmlbhandle);
9016 
9017 	/*
9018 	 * Hold the detach mutex here, to make sure that no other threads ever
9019 	 * can access a (partially) freed soft state structure.
9020 	 */
9021 	mutex_enter(&sd_detach_mutex);
9022 
9023 	/*
9024 	 * Clean up the soft state struct.
9025 	 * Cleanup is done in reverse order of allocs/inits.
9026 	 * At this point there should be no competing threads anymore.
9027 	 */
9028 
9029 	scsi_fm_fini(devp);
9030 
9031 	/*
9032 	 * Deallocate memory for SCSI FMA.
9033 	 */
9034 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
9035 
9036 	/*
9037 	 * Unregister and free device id if it was not registered
9038 	 * by the transport.
9039 	 */
9040 	if (un->un_f_devid_transport_defined == FALSE)
9041 		ddi_devid_unregister(devi);
9042 
9043 	/*
9044 	 * free the devid structure if allocated before (by ddi_devid_init()
9045 	 * or ddi_devid_get()).
9046 	 */
9047 	if (un->un_devid) {
9048 		ddi_devid_free(un->un_devid);
9049 		un->un_devid = NULL;
9050 	}
9051 
9052 	/*
9053 	 * Destroy wmap cache if it exists.
9054 	 */
9055 	if (un->un_wm_cache != NULL) {
9056 		kmem_cache_destroy(un->un_wm_cache);
9057 		un->un_wm_cache = NULL;
9058 	}
9059 
9060 	/*
9061 	 * kstat cleanup is done in detach for all device types (4363169).
9062 	 * We do not want to fail detach if the device kstats are not deleted
9063 	 * since there is a confusion about the devo_refcnt for the device.
9064 	 * We just delete the kstats and let detach complete successfully.
9065 	 */
9066 	if (un->un_stats != NULL) {
9067 		kstat_delete(un->un_stats);
9068 		un->un_stats = NULL;
9069 	}
9070 	if (un->un_unmapstats != NULL) {
9071 		kstat_delete(un->un_unmapstats_ks);
9072 		un->un_unmapstats_ks = NULL;
9073 		un->un_unmapstats = NULL;
9074 	}
9075 	if (un->un_errstats != NULL) {
9076 		kstat_delete(un->un_errstats);
9077 		un->un_errstats = NULL;
9078 	}
9079 
9080 	/* Remove partition stats */
9081 	if (un->un_f_pkstats_enabled) {
9082 		for (i = 0; i < NSDMAP; i++) {
9083 			if (un->un_pstats[i] != NULL) {
9084 				kstat_delete(un->un_pstats[i]);
9085 				un->un_pstats[i] = NULL;
9086 			}
9087 		}
9088 	}
9089 
9090 	/* Remove xbuf registration */
9091 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
9092 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
9093 
9094 	/* Remove driver properties */
9095 	ddi_prop_remove_all(devi);
9096 
9097 	mutex_destroy(&un->un_pm_mutex);
9098 	cv_destroy(&un->un_pm_busy_cv);
9099 
9100 	cv_destroy(&un->un_wcc_cv);
9101 
9102 	/* Open/close semaphore */
9103 	sema_destroy(&un->un_semoclose);
9104 
9105 	/* Removable media condvar. */
9106 	cv_destroy(&un->un_state_cv);
9107 
9108 	/* Suspend/resume condvar. */
9109 	cv_destroy(&un->un_suspend_cv);
9110 	cv_destroy(&un->un_disk_busy_cv);
9111 
9112 	sd_free_rqs(un);
9113 
9114 	/* Free up soft state */
9115 	devp->sd_private = NULL;
9116 
9117 	bzero(un, sizeof (struct sd_lun));
9118 
9119 	ddi_soft_state_free(sd_state, instance);
9120 
9121 	mutex_exit(&sd_detach_mutex);
9122 
9123 	/* This frees up the INQUIRY data associated with the device. */
9124 	scsi_unprobe(devp);
9125 
9126 	/*
9127 	 * After successfully detaching an instance, we update the information
9128 	 * of how many luns have been attached in the relative target and
9129 	 * controller for parallel SCSI. This information is used when sd tries
9130 	 * to set the tagged queuing capability in HBA.
9131 	 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
9132 	 * check if the device is parallel SCSI. However, we don't need to
9133 	 * check here because we've already checked during attach. No device
9134 	 * that is not parallel SCSI is in the chain.
9135 	 */
9136 	if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9137 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
9138 	}
9139 
9140 	return (DDI_SUCCESS);
9141 
9142 err_notclosed:
9143 	mutex_exit(SD_MUTEX(un));
9144 
9145 err_stillbusy:
9146 	_NOTE(NO_COMPETING_THREADS_NOW);
9147 
9148 err_remove_event:
9149 	mutex_enter(&sd_detach_mutex);
9150 	un->un_detach_count--;
9151 	mutex_exit(&sd_detach_mutex);
9152 
9153 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9154 	return (DDI_FAILURE);
9155 }
9156 
9157 
9158 /*
9159  *    Function: sd_create_errstats
9160  *
9161  * Description: This routine instantiates the device error stats.
9162  *
9163  *		Note: During attach the stats are instantiated first so they are
9164  *		available for attach-time routines that utilize the driver
9165  *		iopath to send commands to the device. The stats are initialized
9166  *		separately so data obtained during some attach-time routines is
9167  *		available. (4362483)
9168  *
9169  *   Arguments: un - driver soft state (unit) structure
9170  *		instance - driver instance
9171  *
9172  *     Context: Kernel thread context
9173  */
9174 
9175 static void
9176 sd_create_errstats(struct sd_lun *un, int instance)
9177 {
9178 	struct	sd_errstats	*stp;
9179 	char	kstatmodule_err[KSTAT_STRLEN];
9180 	char	kstatname[KSTAT_STRLEN];
9181 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9182 
9183 	ASSERT(un != NULL);
9184 
9185 	if (un->un_errstats != NULL) {
9186 		return;
9187 	}
9188 
9189 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9190 	    "%serr", sd_label);
9191 	(void) snprintf(kstatname, sizeof (kstatname),
9192 	    "%s%d,err", sd_label, instance);
9193 
9194 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9195 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9196 
9197 	if (un->un_errstats == NULL) {
9198 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9199 		    "sd_create_errstats: Failed kstat_create\n");
9200 		return;
9201 	}
9202 
9203 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9204 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9205 	    KSTAT_DATA_UINT32);
9206 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9207 	    KSTAT_DATA_UINT32);
9208 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9209 	    KSTAT_DATA_UINT32);
9210 	kstat_named_init(&stp->sd_vid,		"Vendor",
9211 	    KSTAT_DATA_CHAR);
9212 	kstat_named_init(&stp->sd_pid,		"Product",
9213 	    KSTAT_DATA_CHAR);
9214 	kstat_named_init(&stp->sd_revision,	"Revision",
9215 	    KSTAT_DATA_CHAR);
9216 	kstat_named_init(&stp->sd_serial,	"Serial No",
9217 	    KSTAT_DATA_CHAR);
9218 	kstat_named_init(&stp->sd_capacity,	"Size",
9219 	    KSTAT_DATA_ULONGLONG);
9220 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9221 	    KSTAT_DATA_UINT32);
9222 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9223 	    KSTAT_DATA_UINT32);
9224 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9225 	    KSTAT_DATA_UINT32);
9226 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9227 	    KSTAT_DATA_UINT32);
9228 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9229 	    KSTAT_DATA_UINT32);
9230 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9231 	    KSTAT_DATA_UINT32);
9232 
9233 	un->un_errstats->ks_private = un;
9234 	un->un_errstats->ks_update  = nulldev;
9235 
9236 	kstat_install(un->un_errstats);
9237 }
9238 
9239 
9240 /*
9241  *    Function: sd_set_errstats
9242  *
9243  * Description: This routine sets the value of the vendor id, product id,
9244  *		revision, serial number, and capacity device error stats.
9245  *
9246  *		Note: During attach the stats are instantiated first so they are
9247  *		available for attach-time routines that utilize the driver
9248  *		iopath to send commands to the device. The stats are initialized
9249  *		separately so data obtained during some attach-time routines is
9250  *		available. (4362483)
9251  *
9252  *   Arguments: un - driver soft state (unit) structure
9253  *
9254  *     Context: Kernel thread context
9255  */
9256 
9257 static void
9258 sd_set_errstats(struct sd_lun *un)
9259 {
9260 	struct	sd_errstats	*stp;
9261 	char 			*sn;
9262 
9263 	ASSERT(un != NULL);
9264 	ASSERT(un->un_errstats != NULL);
9265 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9266 	ASSERT(stp != NULL);
9267 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9268 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9269 	(void) strncpy(stp->sd_revision.value.c,
9270 	    un->un_sd->sd_inq->inq_revision, 4);
9271 
9272 	/*
9273 	 * All the errstats are persistent across detach/attach,
9274 	 * so reset all the errstats here in case of the hot
9275 	 * replacement of disk drives, except for not changed
9276 	 * Sun qualified drives.
9277 	 */
9278 	if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9279 	    (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9280 	    sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9281 		stp->sd_softerrs.value.ui32 = 0;
9282 		stp->sd_harderrs.value.ui32 = 0;
9283 		stp->sd_transerrs.value.ui32 = 0;
9284 		stp->sd_rq_media_err.value.ui32 = 0;
9285 		stp->sd_rq_ntrdy_err.value.ui32 = 0;
9286 		stp->sd_rq_nodev_err.value.ui32 = 0;
9287 		stp->sd_rq_recov_err.value.ui32 = 0;
9288 		stp->sd_rq_illrq_err.value.ui32 = 0;
9289 		stp->sd_rq_pfa_err.value.ui32 = 0;
9290 	}
9291 
9292 	/*
9293 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9294 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9295 	 * (4376302))
9296 	 */
9297 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9298 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9299 		    sizeof (SD_INQUIRY(un)->inq_serial));
9300 	} else {
9301 		/*
9302 		 * Set the "Serial No" kstat for non-Sun qualified drives
9303 		 */
9304 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
9305 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
9306 		    INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
9307 			(void) strlcpy(stp->sd_serial.value.c, sn,
9308 			    sizeof (stp->sd_serial.value.c));
9309 			ddi_prop_free(sn);
9310 		}
9311 	}
9312 
9313 	if (un->un_f_blockcount_is_valid != TRUE) {
9314 		/*
9315 		 * Set capacity error stat to 0 for no media. This ensures
9316 		 * a valid capacity is displayed in response to 'iostat -E'
9317 		 * when no media is present in the device.
9318 		 */
9319 		stp->sd_capacity.value.ui64 = 0;
9320 	} else {
9321 		/*
9322 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9323 		 * capacity.
9324 		 *
9325 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9326 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9327 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9328 		 */
9329 		stp->sd_capacity.value.ui64 = (uint64_t)
9330 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9331 	}
9332 }
9333 
9334 
9335 /*
9336  *    Function: sd_set_pstats
9337  *
9338  * Description: This routine instantiates and initializes the partition
9339  *              stats for each partition with more than zero blocks.
9340  *		(4363169)
9341  *
9342  *   Arguments: un - driver soft state (unit) structure
9343  *
9344  *     Context: Kernel thread context
9345  */
9346 
9347 static void
9348 sd_set_pstats(struct sd_lun *un)
9349 {
9350 	char	kstatname[KSTAT_STRLEN];
9351 	int	instance;
9352 	int	i;
9353 	diskaddr_t	nblks = 0;
9354 	char	*partname = NULL;
9355 
9356 	ASSERT(un != NULL);
9357 
9358 	instance = ddi_get_instance(SD_DEVINFO(un));
9359 
9360 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
9361 	for (i = 0; i < NSDMAP; i++) {
9362 
9363 		if (cmlb_partinfo(un->un_cmlbhandle, i,
9364 		    &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9365 			continue;
9366 		mutex_enter(SD_MUTEX(un));
9367 
9368 		if ((un->un_pstats[i] == NULL) &&
9369 		    (nblks != 0)) {
9370 
9371 			(void) snprintf(kstatname, sizeof (kstatname),
9372 			    "%s%d,%s", sd_label, instance,
9373 			    partname);
9374 
9375 			un->un_pstats[i] = kstat_create(sd_label,
9376 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
9377 			    1, KSTAT_FLAG_PERSISTENT);
9378 			if (un->un_pstats[i] != NULL) {
9379 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9380 				kstat_install(un->un_pstats[i]);
9381 			}
9382 		}
9383 		mutex_exit(SD_MUTEX(un));
9384 	}
9385 }
9386 
9387 
9388 #if (defined(__fibre))
9389 /*
9390  *    Function: sd_init_event_callbacks
9391  *
9392  * Description: This routine initializes the insertion and removal event
9393  *		callbacks. (fibre only)
9394  *
9395  *   Arguments: un - driver soft state (unit) structure
9396  *
9397  *     Context: Kernel thread context
9398  */
9399 
9400 static void
9401 sd_init_event_callbacks(struct sd_lun *un)
9402 {
9403 	ASSERT(un != NULL);
9404 
9405 	if ((un->un_insert_event == NULL) &&
9406 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9407 	    &un->un_insert_event) == DDI_SUCCESS)) {
9408 		/*
9409 		 * Add the callback for an insertion event
9410 		 */
9411 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9412 		    un->un_insert_event, sd_event_callback, (void *)un,
9413 		    &(un->un_insert_cb_id));
9414 	}
9415 
9416 	if ((un->un_remove_event == NULL) &&
9417 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9418 	    &un->un_remove_event) == DDI_SUCCESS)) {
9419 		/*
9420 		 * Add the callback for a removal event
9421 		 */
9422 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9423 		    un->un_remove_event, sd_event_callback, (void *)un,
9424 		    &(un->un_remove_cb_id));
9425 	}
9426 }
9427 
9428 
9429 /*
9430  *    Function: sd_event_callback
9431  *
9432  * Description: This routine handles insert/remove events (photon). The
9433  *		state is changed to OFFLINE which can be used to supress
9434  *		error msgs. (fibre only)
9435  *
9436  *   Arguments: un - driver soft state (unit) structure
9437  *
9438  *     Context: Callout thread context
9439  */
9440 /* ARGSUSED */
9441 static void
9442 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9443     void *bus_impldata)
9444 {
9445 	struct sd_lun *un = (struct sd_lun *)arg;
9446 
9447 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9448 	if (event == un->un_insert_event) {
9449 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9450 		mutex_enter(SD_MUTEX(un));
9451 		if (un->un_state == SD_STATE_OFFLINE) {
9452 			if (un->un_last_state != SD_STATE_SUSPENDED) {
9453 				un->un_state = un->un_last_state;
9454 			} else {
9455 				/*
9456 				 * We have gone through SUSPEND/RESUME while
9457 				 * we were offline. Restore the last state
9458 				 */
9459 				un->un_state = un->un_save_state;
9460 			}
9461 		}
9462 		mutex_exit(SD_MUTEX(un));
9463 
9464 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9465 	} else if (event == un->un_remove_event) {
9466 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9467 		mutex_enter(SD_MUTEX(un));
9468 		/*
9469 		 * We need to handle an event callback that occurs during
9470 		 * the suspend operation, since we don't prevent it.
9471 		 */
9472 		if (un->un_state != SD_STATE_OFFLINE) {
9473 			if (un->un_state != SD_STATE_SUSPENDED) {
9474 				New_state(un, SD_STATE_OFFLINE);
9475 			} else {
9476 				un->un_last_state = SD_STATE_OFFLINE;
9477 			}
9478 		}
9479 		mutex_exit(SD_MUTEX(un));
9480 	} else {
9481 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9482 		    "!Unknown event\n");
9483 	}
9484 
9485 }
9486 #endif
9487 
9488 /*
9489  * Values related to caching mode page depending on whether the unit is ATAPI.
9490  */
9491 #define	SDC_CDB_GROUP(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9492 	CDB_GROUP1 : CDB_GROUP0)
9493 #define	SDC_HDRLEN(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9494 	MODE_HEADER_LENGTH_GRP2 : MODE_HEADER_LENGTH)
9495 /*
9496  * Use mode_cache_scsi3 to ensure we get all of the mode sense data, otherwise
9497  * the mode select will fail (mode_cache_scsi3 is a superset of mode_caching).
9498  */
9499 #define	SDC_BUFLEN(un) (SDC_HDRLEN(un) + MODE_BLK_DESC_LENGTH + \
9500 	sizeof (struct mode_cache_scsi3))
9501 
9502 static int
9503 sd_get_caching_mode_page(sd_ssc_t *ssc, uchar_t page_control, uchar_t **header,
9504     int *bdlen)
9505 {
9506 	struct sd_lun	*un = ssc->ssc_un;
9507 	struct mode_caching *mode_caching_page;
9508 	size_t		buflen = SDC_BUFLEN(un);
9509 	int		hdrlen = SDC_HDRLEN(un);
9510 	int		rval;
9511 
9512 	/*
9513 	 * Do a test unit ready, otherwise a mode sense may not work if this
9514 	 * is the first command sent to the device after boot.
9515 	 */
9516 	if (sd_send_scsi_TEST_UNIT_READY(ssc, 0) != 0)
9517 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9518 
9519 	/*
9520 	 * Allocate memory for the retrieved mode page and its headers.  Set
9521 	 * a pointer to the page itself.
9522 	 */
9523 	*header = kmem_zalloc(buflen, KM_SLEEP);
9524 
9525 	/* Get the information from the device */
9526 	rval = sd_send_scsi_MODE_SENSE(ssc, SDC_CDB_GROUP(un), *header, buflen,
9527 	    page_control | MODEPAGE_CACHING, SD_PATH_DIRECT);
9528 	if (rval != 0) {
9529 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, "%s: Mode Sense Failed\n",
9530 		    __func__);
9531 		goto mode_sense_failed;
9532 	}
9533 
9534 	/*
9535 	 * Determine size of Block Descriptors in order to locate
9536 	 * the mode page data. ATAPI devices return 0, SCSI devices
9537 	 * should return MODE_BLK_DESC_LENGTH.
9538 	 */
9539 	if (un->un_f_cfg_is_atapi == TRUE) {
9540 		struct mode_header_grp2 *mhp =
9541 		    (struct mode_header_grp2 *)(*header);
9542 		*bdlen = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9543 	} else {
9544 		*bdlen = ((struct mode_header *)(*header))->bdesc_length;
9545 	}
9546 
9547 	if (*bdlen > MODE_BLK_DESC_LENGTH) {
9548 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9549 		    "%s: Mode Sense returned invalid block descriptor length\n",
9550 		    __func__);
9551 		rval = EIO;
9552 		goto mode_sense_failed;
9553 	}
9554 
9555 	mode_caching_page = (struct mode_caching *)(*header + hdrlen + *bdlen);
9556 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9557 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9558 		    "%s: Mode Sense caching page code mismatch %d\n",
9559 		    __func__, mode_caching_page->mode_page.code);
9560 		rval = EIO;
9561 	}
9562 
9563 mode_sense_failed:
9564 	if (rval != 0) {
9565 		kmem_free(*header, buflen);
9566 		*header = NULL;
9567 		*bdlen = 0;
9568 	}
9569 	return (rval);
9570 }
9571 
9572 /*
9573  *    Function: sd_cache_control()
9574  *
9575  * Description: This routine is the driver entry point for setting
9576  *		read and write caching by modifying the WCE (write cache
9577  *		enable) and RCD (read cache disable) bits of mode
9578  *		page 8 (MODEPAGE_CACHING).
9579  *
9580  *   Arguments: ssc		- ssc contains pointer to driver soft state
9581  *				  (unit) structure for this target.
9582  *		rcd_flag	- flag for controlling the read cache
9583  *		wce_flag	- flag for controlling the write cache
9584  *
9585  * Return Code: EIO
9586  *		code returned by sd_send_scsi_MODE_SENSE and
9587  *		sd_send_scsi_MODE_SELECT
9588  *
9589  *     Context: Kernel Thread
9590  */
9591 
9592 static int
9593 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9594 {
9595 	struct sd_lun	*un = ssc->ssc_un;
9596 	struct mode_caching *mode_caching_page;
9597 	uchar_t		*header;
9598 	size_t		buflen = SDC_BUFLEN(un);
9599 	int		hdrlen = SDC_HDRLEN(un);
9600 	int		bdlen;
9601 	int		rval;
9602 
9603 	rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9604 	switch (rval) {
9605 	case 0:
9606 		/* Check the relevant bits on successful mode sense */
9607 		mode_caching_page = (struct mode_caching *)(header + hdrlen +
9608 		    bdlen);
9609 		if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9610 		    (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9611 		    (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9612 		    (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9613 			size_t sbuflen;
9614 			uchar_t save_pg;
9615 
9616 			/*
9617 			 * Construct select buffer length based on the
9618 			 * length of the sense data returned.
9619 			 */
9620 			sbuflen = hdrlen + bdlen + sizeof (struct mode_page) +
9621 			    (int)mode_caching_page->mode_page.length;
9622 
9623 			/* Set the caching bits as requested */
9624 			if (rcd_flag == SD_CACHE_ENABLE)
9625 				mode_caching_page->rcd = 0;
9626 			else if (rcd_flag == SD_CACHE_DISABLE)
9627 				mode_caching_page->rcd = 1;
9628 
9629 			if (wce_flag == SD_CACHE_ENABLE)
9630 				mode_caching_page->wce = 1;
9631 			else if (wce_flag == SD_CACHE_DISABLE)
9632 				mode_caching_page->wce = 0;
9633 
9634 			/*
9635 			 * Save the page if the mode sense says the
9636 			 * drive supports it.
9637 			 */
9638 			save_pg = mode_caching_page->mode_page.ps ?
9639 			    SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9640 
9641 			/* Clear reserved bits before mode select */
9642 			mode_caching_page->mode_page.ps = 0;
9643 
9644 			/*
9645 			 * Clear out mode header for mode select.
9646 			 * The rest of the retrieved page will be reused.
9647 			 */
9648 			bzero(header, hdrlen);
9649 
9650 			if (un->un_f_cfg_is_atapi == TRUE) {
9651 				struct mode_header_grp2 *mhp =
9652 				    (struct mode_header_grp2 *)header;
9653 				mhp->bdesc_length_hi = bdlen >> 8;
9654 				mhp->bdesc_length_lo = (uchar_t)bdlen & 0xff;
9655 			} else {
9656 				((struct mode_header *)header)->bdesc_length =
9657 				    bdlen;
9658 			}
9659 
9660 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9661 
9662 			/* Issue mode select to change the cache settings */
9663 			rval = sd_send_scsi_MODE_SELECT(ssc, SDC_CDB_GROUP(un),
9664 			    header, sbuflen, save_pg, SD_PATH_DIRECT);
9665 		}
9666 		kmem_free(header, buflen);
9667 		break;
9668 	case EIO:
9669 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9670 		break;
9671 	default:
9672 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9673 		break;
9674 	}
9675 
9676 	return (rval);
9677 }
9678 
9679 
9680 /*
9681  *    Function: sd_get_write_cache_enabled()
9682  *
9683  * Description: This routine is the driver entry point for determining if write
9684  *		caching is enabled.  It examines the WCE (write cache enable)
9685  *		bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9686  *		bits set to MODEPAGE_CURRENT.
9687  *
9688  *   Arguments: ssc		- ssc contains pointer to driver soft state
9689  *				  (unit) structure for this target.
9690  *		is_enabled	- pointer to int where write cache enabled state
9691  *				  is returned (non-zero -> write cache enabled)
9692  *
9693  * Return Code: EIO
9694  *		code returned by sd_send_scsi_MODE_SENSE
9695  *
9696  *     Context: Kernel Thread
9697  *
9698  * NOTE: If ioctl is added to disable write cache, this sequence should
9699  * be followed so that no locking is required for accesses to
9700  * un->un_f_write_cache_enabled:
9701  * 	do mode select to clear wce
9702  * 	do synchronize cache to flush cache
9703  * 	set un->un_f_write_cache_enabled = FALSE
9704  *
9705  * Conversely, an ioctl to enable the write cache should be done
9706  * in this order:
9707  * 	set un->un_f_write_cache_enabled = TRUE
9708  * 	do mode select to set wce
9709  */
9710 
9711 static int
9712 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9713 {
9714 	struct sd_lun	*un = ssc->ssc_un;
9715 	struct mode_caching *mode_caching_page;
9716 	uchar_t		*header;
9717 	size_t		buflen = SDC_BUFLEN(un);
9718 	int		hdrlen = SDC_HDRLEN(un);
9719 	int		bdlen;
9720 	int		rval;
9721 
9722 	/* In case of error, flag as enabled */
9723 	*is_enabled = TRUE;
9724 
9725 	rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9726 	switch (rval) {
9727 	case 0:
9728 		mode_caching_page = (struct mode_caching *)(header + hdrlen +
9729 		    bdlen);
9730 		*is_enabled = mode_caching_page->wce;
9731 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9732 		kmem_free(header, buflen);
9733 		break;
9734 	case EIO: {
9735 		/*
9736 		 * Some disks do not support Mode Sense(6), we
9737 		 * should ignore this kind of error (sense key is
9738 		 * 0x5 - illegal request).
9739 		 */
9740 		uint8_t *sensep;
9741 		int senlen;
9742 
9743 		sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9744 		senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9745 		    ssc->ssc_uscsi_cmd->uscsi_rqresid);
9746 
9747 		if (senlen > 0 &&
9748 		    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9749 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9750 		} else {
9751 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9752 		}
9753 		break;
9754 	}
9755 	default:
9756 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9757 		break;
9758 	}
9759 
9760 	return (rval);
9761 }
9762 
9763 /*
9764  *    Function: sd_get_write_cache_changeable()
9765  *
9766  * Description: This routine is the driver entry point for determining if write
9767  *		caching is changeable.  It examines the WCE (write cache enable)
9768  *		bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9769  *		bits set to MODEPAGE_CHANGEABLE.
9770  *
9771  *   Arguments: ssc		- ssc contains pointer to driver soft state
9772  *				  (unit) structure for this target.
9773  *		is_changeable	- pointer to int where write cache changeable
9774  *				  state is returned (non-zero -> write cache
9775  *				  changeable)
9776  *
9777  *     Context: Kernel Thread
9778  */
9779 
9780 static void
9781 sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable)
9782 {
9783 	struct sd_lun	*un = ssc->ssc_un;
9784 	struct mode_caching *mode_caching_page;
9785 	uchar_t		*header;
9786 	size_t		buflen = SDC_BUFLEN(un);
9787 	int		hdrlen = SDC_HDRLEN(un);
9788 	int		bdlen;
9789 	int		rval;
9790 
9791 	/* In case of error, flag as enabled */
9792 	*is_changeable = TRUE;
9793 
9794 	rval = sd_get_caching_mode_page(ssc, MODEPAGE_CHANGEABLE, &header,
9795 	    &bdlen);
9796 	switch (rval) {
9797 	case 0:
9798 		mode_caching_page = (struct mode_caching *)(header + hdrlen +
9799 		    bdlen);
9800 		*is_changeable = mode_caching_page->wce;
9801 		kmem_free(header, buflen);
9802 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9803 		break;
9804 	case EIO:
9805 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9806 		break;
9807 	default:
9808 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9809 		break;
9810 	}
9811 }
9812 
9813 /*
9814  *    Function: sd_get_nv_sup()
9815  *
9816  * Description: This routine is the driver entry point for
9817  * determining whether non-volatile cache is supported. This
9818  * determination process works as follows:
9819  *
9820  * 1. sd first queries sd.conf on whether
9821  * suppress_cache_flush bit is set for this device.
9822  *
9823  * 2. if not there, then queries the internal disk table.
9824  *
9825  * 3. if either sd.conf or internal disk table specifies
9826  * cache flush be suppressed, we don't bother checking
9827  * NV_SUP bit.
9828  *
9829  * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9830  * the optional INQUIRY VPD page 0x86. If the device
9831  * supports VPD page 0x86, sd examines the NV_SUP
9832  * (non-volatile cache support) bit in the INQUIRY VPD page
9833  * 0x86:
9834  *   o If NV_SUP bit is set, sd assumes the device has a
9835  *   non-volatile cache and set the
9836  *   un_f_sync_nv_supported to TRUE.
9837  *   o Otherwise cache is not non-volatile,
9838  *   un_f_sync_nv_supported is set to FALSE.
9839  *
9840  * Arguments: un - driver soft state (unit) structure
9841  *
9842  * Return Code:
9843  *
9844  *     Context: Kernel Thread
9845  */
9846 
9847 static void
9848 sd_get_nv_sup(sd_ssc_t *ssc)
9849 {
9850 	int		rval		= 0;
9851 	uchar_t		*inq86		= NULL;
9852 	size_t		inq86_len	= MAX_INQUIRY_SIZE;
9853 	size_t		inq86_resid	= 0;
9854 	struct		dk_callback *dkc;
9855 	struct sd_lun	*un;
9856 
9857 	ASSERT(ssc != NULL);
9858 	un = ssc->ssc_un;
9859 	ASSERT(un != NULL);
9860 
9861 	mutex_enter(SD_MUTEX(un));
9862 
9863 	/*
9864 	 * Be conservative on the device's support of
9865 	 * SYNC_NV bit: un_f_sync_nv_supported is
9866 	 * initialized to be false.
9867 	 */
9868 	un->un_f_sync_nv_supported = FALSE;
9869 
9870 	/*
9871 	 * If either sd.conf or internal disk table
9872 	 * specifies cache flush be suppressed, then
9873 	 * we don't bother checking NV_SUP bit.
9874 	 */
9875 	if (un->un_f_suppress_cache_flush == TRUE) {
9876 		mutex_exit(SD_MUTEX(un));
9877 		return;
9878 	}
9879 
9880 	if (sd_check_vpd_page_support(ssc) == 0 &&
9881 	    un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9882 		mutex_exit(SD_MUTEX(un));
9883 		/* collect page 86 data if available */
9884 		inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9885 
9886 		rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9887 		    0x01, 0x86, &inq86_resid);
9888 
9889 		if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9890 			SD_TRACE(SD_LOG_COMMON, un,
9891 			    "sd_get_nv_sup: \
9892 			    successfully get VPD page: %x \
9893 			    PAGE LENGTH: %x BYTE 6: %x\n",
9894 			    inq86[1], inq86[3], inq86[6]);
9895 
9896 			mutex_enter(SD_MUTEX(un));
9897 			/*
9898 			 * check the value of NV_SUP bit: only if the device
9899 			 * reports NV_SUP bit to be 1, the
9900 			 * un_f_sync_nv_supported bit will be set to true.
9901 			 */
9902 			if (inq86[6] & SD_VPD_NV_SUP) {
9903 				un->un_f_sync_nv_supported = TRUE;
9904 			}
9905 			mutex_exit(SD_MUTEX(un));
9906 		} else if (rval != 0) {
9907 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9908 		}
9909 
9910 		kmem_free(inq86, inq86_len);
9911 	} else {
9912 		mutex_exit(SD_MUTEX(un));
9913 	}
9914 
9915 	/*
9916 	 * Send a SYNC CACHE command to check whether
9917 	 * SYNC_NV bit is supported. This command should have
9918 	 * un_f_sync_nv_supported set to correct value.
9919 	 */
9920 	mutex_enter(SD_MUTEX(un));
9921 	if (un->un_f_sync_nv_supported) {
9922 		mutex_exit(SD_MUTEX(un));
9923 		dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9924 		dkc->dkc_flag = FLUSH_VOLATILE;
9925 		(void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9926 
9927 		/*
9928 		 * Send a TEST UNIT READY command to the device. This should
9929 		 * clear any outstanding UNIT ATTENTION that may be present.
9930 		 */
9931 		rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9932 		if (rval != 0)
9933 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9934 
9935 		kmem_free(dkc, sizeof (struct dk_callback));
9936 	} else {
9937 		mutex_exit(SD_MUTEX(un));
9938 	}
9939 
9940 	SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9941 	    un_f_suppress_cache_flush is set to %d\n",
9942 	    un->un_f_suppress_cache_flush);
9943 }
9944 
9945 /*
9946  *    Function: sd_make_device
9947  *
9948  * Description: Utility routine to return the Solaris device number from
9949  *		the data in the device's dev_info structure.
9950  *
9951  * Return Code: The Solaris device number
9952  *
9953  *     Context: Any
9954  */
9955 
9956 static dev_t
9957 sd_make_device(dev_info_t *devi)
9958 {
9959 	return (makedevice(ddi_driver_major(devi),
9960 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
9961 }
9962 
9963 
9964 /*
9965  *    Function: sd_pm_entry
9966  *
9967  * Description: Called at the start of a new command to manage power
9968  *		and busy status of a device. This includes determining whether
9969  *		the current power state of the device is sufficient for
9970  *		performing the command or whether it must be changed.
9971  *		The PM framework is notified appropriately.
9972  *		Only with a return status of DDI_SUCCESS will the
9973  *		component be busy to the framework.
9974  *
9975  *		All callers of sd_pm_entry must check the return status
9976  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
9977  *		of DDI_FAILURE indicates the device failed to power up.
9978  *		In this case un_pm_count has been adjusted so the result
9979  *		on exit is still powered down, ie. count is less than 0.
9980  *		Calling sd_pm_exit with this count value hits an ASSERT.
9981  *
9982  * Return Code: DDI_SUCCESS or DDI_FAILURE
9983  *
9984  *     Context: Kernel thread context.
9985  */
9986 
9987 static int
9988 sd_pm_entry(struct sd_lun *un)
9989 {
9990 	int return_status = DDI_SUCCESS;
9991 
9992 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9993 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9994 
9995 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9996 
9997 	if (un->un_f_pm_is_enabled == FALSE) {
9998 		SD_TRACE(SD_LOG_IO_PM, un,
9999 		    "sd_pm_entry: exiting, PM not enabled\n");
10000 		return (return_status);
10001 	}
10002 
10003 	/*
10004 	 * Just increment a counter if PM is enabled. On the transition from
10005 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
10006 	 * the count with each IO and mark the device as idle when the count
10007 	 * hits 0.
10008 	 *
10009 	 * If the count is less than 0 the device is powered down. If a powered
10010 	 * down device is successfully powered up then the count must be
10011 	 * incremented to reflect the power up. Note that it'll get incremented
10012 	 * a second time to become busy.
10013 	 *
10014 	 * Because the following has the potential to change the device state
10015 	 * and must release the un_pm_mutex to do so, only one thread can be
10016 	 * allowed through at a time.
10017 	 */
10018 
10019 	mutex_enter(&un->un_pm_mutex);
10020 	while (un->un_pm_busy == TRUE) {
10021 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
10022 	}
10023 	un->un_pm_busy = TRUE;
10024 
10025 	if (un->un_pm_count < 1) {
10026 
10027 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
10028 
10029 		/*
10030 		 * Indicate we are now busy so the framework won't attempt to
10031 		 * power down the device. This call will only fail if either
10032 		 * we passed a bad component number or the device has no
10033 		 * components. Neither of these should ever happen.
10034 		 */
10035 		mutex_exit(&un->un_pm_mutex);
10036 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
10037 		ASSERT(return_status == DDI_SUCCESS);
10038 
10039 		mutex_enter(&un->un_pm_mutex);
10040 
10041 		if (un->un_pm_count < 0) {
10042 			mutex_exit(&un->un_pm_mutex);
10043 
10044 			SD_TRACE(SD_LOG_IO_PM, un,
10045 			    "sd_pm_entry: power up component\n");
10046 
10047 			/*
10048 			 * pm_raise_power will cause sdpower to be called
10049 			 * which brings the device power level to the
10050 			 * desired state, If successful, un_pm_count and
10051 			 * un_power_level will be updated appropriately.
10052 			 */
10053 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
10054 			    SD_PM_STATE_ACTIVE(un));
10055 
10056 			mutex_enter(&un->un_pm_mutex);
10057 
10058 			if (return_status != DDI_SUCCESS) {
10059 				/*
10060 				 * Power up failed.
10061 				 * Idle the device and adjust the count
10062 				 * so the result on exit is that we're
10063 				 * still powered down, ie. count is less than 0.
10064 				 */
10065 				SD_TRACE(SD_LOG_IO_PM, un,
10066 				    "sd_pm_entry: power up failed,"
10067 				    " idle the component\n");
10068 
10069 				(void) pm_idle_component(SD_DEVINFO(un), 0);
10070 				un->un_pm_count--;
10071 			} else {
10072 				/*
10073 				 * Device is powered up, verify the
10074 				 * count is non-negative.
10075 				 * This is debug only.
10076 				 */
10077 				ASSERT(un->un_pm_count == 0);
10078 			}
10079 		}
10080 
10081 		if (return_status == DDI_SUCCESS) {
10082 			/*
10083 			 * For performance, now that the device has been tagged
10084 			 * as busy, and it's known to be powered up, update the
10085 			 * chain types to use jump tables that do not include
10086 			 * pm. This significantly lowers the overhead and
10087 			 * therefore improves performance.
10088 			 */
10089 
10090 			mutex_exit(&un->un_pm_mutex);
10091 			mutex_enter(SD_MUTEX(un));
10092 			SD_TRACE(SD_LOG_IO_PM, un,
10093 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
10094 			    un->un_uscsi_chain_type);
10095 
10096 			if (un->un_f_non_devbsize_supported) {
10097 				un->un_buf_chain_type =
10098 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
10099 			} else {
10100 				un->un_buf_chain_type =
10101 				    SD_CHAIN_INFO_DISK_NO_PM;
10102 			}
10103 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
10104 
10105 			SD_TRACE(SD_LOG_IO_PM, un,
10106 			    "             changed  uscsi_chain_type to   %d\n",
10107 			    un->un_uscsi_chain_type);
10108 			mutex_exit(SD_MUTEX(un));
10109 			mutex_enter(&un->un_pm_mutex);
10110 
10111 			if (un->un_pm_idle_timeid == NULL) {
10112 				/* 300 ms. */
10113 				un->un_pm_idle_timeid =
10114 				    timeout(sd_pm_idletimeout_handler, un,
10115 				    (drv_usectohz((clock_t)300000)));
10116 				/*
10117 				 * Include an extra call to busy which keeps the
10118 				 * device busy with-respect-to the PM layer
10119 				 * until the timer fires, at which time it'll
10120 				 * get the extra idle call.
10121 				 */
10122 				(void) pm_busy_component(SD_DEVINFO(un), 0);
10123 			}
10124 		}
10125 	}
10126 	un->un_pm_busy = FALSE;
10127 	/* Next... */
10128 	cv_signal(&un->un_pm_busy_cv);
10129 
10130 	un->un_pm_count++;
10131 
10132 	SD_TRACE(SD_LOG_IO_PM, un,
10133 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
10134 
10135 	mutex_exit(&un->un_pm_mutex);
10136 
10137 	return (return_status);
10138 }
10139 
10140 
10141 /*
10142  *    Function: sd_pm_exit
10143  *
10144  * Description: Called at the completion of a command to manage busy
10145  *		status for the device. If the device becomes idle the
10146  *		PM framework is notified.
10147  *
10148  *     Context: Kernel thread context
10149  */
10150 
10151 static void
10152 sd_pm_exit(struct sd_lun *un)
10153 {
10154 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10155 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10156 
10157 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10158 
10159 	/*
10160 	 * After attach the following flag is only read, so don't
10161 	 * take the penalty of acquiring a mutex for it.
10162 	 */
10163 	if (un->un_f_pm_is_enabled == TRUE) {
10164 
10165 		mutex_enter(&un->un_pm_mutex);
10166 		un->un_pm_count--;
10167 
10168 		SD_TRACE(SD_LOG_IO_PM, un,
10169 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10170 
10171 		ASSERT(un->un_pm_count >= 0);
10172 		if (un->un_pm_count == 0) {
10173 			mutex_exit(&un->un_pm_mutex);
10174 
10175 			SD_TRACE(SD_LOG_IO_PM, un,
10176 			    "sd_pm_exit: idle component\n");
10177 
10178 			(void) pm_idle_component(SD_DEVINFO(un), 0);
10179 
10180 		} else {
10181 			mutex_exit(&un->un_pm_mutex);
10182 		}
10183 	}
10184 
10185 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10186 }
10187 
10188 
10189 /*
10190  *    Function: sdopen
10191  *
10192  * Description: Driver's open(9e) entry point function.
10193  *
10194  *   Arguments: dev_i   - pointer to device number
10195  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10196  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10197  *		cred_p  - user credential pointer
10198  *
10199  * Return Code: EINVAL
10200  *		ENXIO
10201  *		EIO
10202  *		EROFS
10203  *		EBUSY
10204  *
10205  *     Context: Kernel thread context
10206  */
10207 /* ARGSUSED */
10208 static int
10209 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10210 {
10211 	struct sd_lun	*un;
10212 	int		nodelay;
10213 	int		part;
10214 	uint64_t	partmask;
10215 	int		instance;
10216 	dev_t		dev;
10217 	int		rval = EIO;
10218 	diskaddr_t	nblks = 0;
10219 	diskaddr_t	label_cap;
10220 
10221 	/* Validate the open type */
10222 	if (otyp >= OTYPCNT) {
10223 		return (EINVAL);
10224 	}
10225 
10226 	dev = *dev_p;
10227 	instance = SDUNIT(dev);
10228 	mutex_enter(&sd_detach_mutex);
10229 
10230 	/*
10231 	 * Fail the open if there is no softstate for the instance, or
10232 	 * if another thread somewhere is trying to detach the instance.
10233 	 */
10234 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10235 	    (un->un_detach_count != 0)) {
10236 		mutex_exit(&sd_detach_mutex);
10237 		/*
10238 		 * The probe cache only needs to be cleared when open (9e) fails
10239 		 * with ENXIO (4238046).
10240 		 */
10241 		/*
10242 		 * un-conditionally clearing probe cache is ok with
10243 		 * separate sd/ssd binaries
10244 		 * x86 platform can be an issue with both parallel
10245 		 * and fibre in 1 binary
10246 		 */
10247 		sd_scsi_clear_probe_cache();
10248 		return (ENXIO);
10249 	}
10250 
10251 	/*
10252 	 * The un_layer_count is to prevent another thread in specfs from
10253 	 * trying to detach the instance, which can happen when we are
10254 	 * called from a higher-layer driver instead of thru specfs.
10255 	 * This will not be needed when DDI provides a layered driver
10256 	 * interface that allows specfs to know that an instance is in
10257 	 * use by a layered driver & should not be detached.
10258 	 *
10259 	 * Note: the semantics for layered driver opens are exactly one
10260 	 * close for every open.
10261 	 */
10262 	if (otyp == OTYP_LYR) {
10263 		un->un_layer_count++;
10264 	}
10265 
10266 	/*
10267 	 * Keep a count of the current # of opens in progress. This is because
10268 	 * some layered drivers try to call us as a regular open. This can
10269 	 * cause problems that we cannot prevent, however by keeping this count
10270 	 * we can at least keep our open and detach routines from racing against
10271 	 * each other under such conditions.
10272 	 */
10273 	un->un_opens_in_progress++;
10274 	mutex_exit(&sd_detach_mutex);
10275 
10276 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
10277 	part	 = SDPART(dev);
10278 	partmask = 1 << part;
10279 
10280 	/*
10281 	 * We use a semaphore here in order to serialize
10282 	 * open and close requests on the device.
10283 	 */
10284 	sema_p(&un->un_semoclose);
10285 
10286 	mutex_enter(SD_MUTEX(un));
10287 
10288 	/*
10289 	 * All device accesses go thru sdstrategy() where we check
10290 	 * on suspend status but there could be a scsi_poll command,
10291 	 * which bypasses sdstrategy(), so we need to check pm
10292 	 * status.
10293 	 */
10294 
10295 	if (!nodelay) {
10296 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10297 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10298 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10299 		}
10300 
10301 		mutex_exit(SD_MUTEX(un));
10302 		if (sd_pm_entry(un) != DDI_SUCCESS) {
10303 			rval = EIO;
10304 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10305 			    "sdopen: sd_pm_entry failed\n");
10306 			goto open_failed_with_pm;
10307 		}
10308 		mutex_enter(SD_MUTEX(un));
10309 	}
10310 
10311 	/* check for previous exclusive open */
10312 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10313 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10314 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10315 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10316 
10317 	if (un->un_exclopen & (partmask)) {
10318 		goto excl_open_fail;
10319 	}
10320 
10321 	if (flag & FEXCL) {
10322 		int i;
10323 		if (un->un_ocmap.lyropen[part]) {
10324 			goto excl_open_fail;
10325 		}
10326 		for (i = 0; i < (OTYPCNT - 1); i++) {
10327 			if (un->un_ocmap.regopen[i] & (partmask)) {
10328 				goto excl_open_fail;
10329 			}
10330 		}
10331 	}
10332 
10333 	/*
10334 	 * Check the write permission if this is a removable media device,
10335 	 * NDELAY has not been set, and writable permission is requested.
10336 	 *
10337 	 * Note: If NDELAY was set and this is write-protected media the WRITE
10338 	 * attempt will fail with EIO as part of the I/O processing. This is a
10339 	 * more permissive implementation that allows the open to succeed and
10340 	 * WRITE attempts to fail when appropriate.
10341 	 */
10342 	if (un->un_f_chk_wp_open) {
10343 		if ((flag & FWRITE) && (!nodelay)) {
10344 			mutex_exit(SD_MUTEX(un));
10345 			/*
10346 			 * Defer the check for write permission on writable
10347 			 * DVD drive till sdstrategy and will not fail open even
10348 			 * if FWRITE is set as the device can be writable
10349 			 * depending upon the media and the media can change
10350 			 * after the call to open().
10351 			 */
10352 			if (un->un_f_dvdram_writable_device == FALSE) {
10353 				if (ISCD(un) || sr_check_wp(dev)) {
10354 				rval = EROFS;
10355 				mutex_enter(SD_MUTEX(un));
10356 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10357 				    "write to cd or write protected media\n");
10358 				goto open_fail;
10359 				}
10360 			}
10361 			mutex_enter(SD_MUTEX(un));
10362 		}
10363 	}
10364 
10365 	/*
10366 	 * If opening in NDELAY/NONBLOCK mode, just return.
10367 	 * Check if disk is ready and has a valid geometry later.
10368 	 */
10369 	if (!nodelay) {
10370 		sd_ssc_t	*ssc;
10371 
10372 		mutex_exit(SD_MUTEX(un));
10373 		ssc = sd_ssc_init(un);
10374 		rval = sd_ready_and_valid(ssc, part);
10375 		sd_ssc_fini(ssc);
10376 		mutex_enter(SD_MUTEX(un));
10377 		/*
10378 		 * Fail if device is not ready or if the number of disk
10379 		 * blocks is zero or negative for non CD devices.
10380 		 */
10381 
10382 		nblks = 0;
10383 
10384 		if (rval == SD_READY_VALID && (!ISCD(un))) {
10385 			/* if cmlb_partinfo fails, nblks remains 0 */
10386 			mutex_exit(SD_MUTEX(un));
10387 			(void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10388 			    NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10389 			mutex_enter(SD_MUTEX(un));
10390 		}
10391 
10392 		if ((rval != SD_READY_VALID) ||
10393 		    (!ISCD(un) && nblks <= 0)) {
10394 			rval = un->un_f_has_removable_media ? ENXIO : EIO;
10395 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10396 			    "device not ready or invalid disk block value\n");
10397 			goto open_fail;
10398 		}
10399 #if defined(__i386) || defined(__amd64)
10400 	} else {
10401 		uchar_t *cp;
10402 		/*
10403 		 * x86 requires special nodelay handling, so that p0 is
10404 		 * always defined and accessible.
10405 		 * Invalidate geometry only if device is not already open.
10406 		 */
10407 		cp = &un->un_ocmap.chkd[0];
10408 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10409 			if (*cp != (uchar_t)0) {
10410 				break;
10411 			}
10412 			cp++;
10413 		}
10414 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10415 			mutex_exit(SD_MUTEX(un));
10416 			cmlb_invalidate(un->un_cmlbhandle,
10417 			    (void *)SD_PATH_DIRECT);
10418 			mutex_enter(SD_MUTEX(un));
10419 		}
10420 
10421 #endif
10422 	}
10423 
10424 	if (otyp == OTYP_LYR) {
10425 		un->un_ocmap.lyropen[part]++;
10426 	} else {
10427 		un->un_ocmap.regopen[otyp] |= partmask;
10428 	}
10429 
10430 	/* Set up open and exclusive open flags */
10431 	if (flag & FEXCL) {
10432 		un->un_exclopen |= (partmask);
10433 	}
10434 
10435 	/*
10436 	 * If the lun is EFI labeled and lun capacity is greater than the
10437 	 * capacity contained in the label, log a sys-event to notify the
10438 	 * interested module.
10439 	 * To avoid an infinite loop of logging sys-event, we only log the
10440 	 * event when the lun is not opened in NDELAY mode. The event handler
10441 	 * should open the lun in NDELAY mode.
10442 	 */
10443 	if (!nodelay) {
10444 		mutex_exit(SD_MUTEX(un));
10445 		if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10446 		    (void*)SD_PATH_DIRECT) == 0) {
10447 			mutex_enter(SD_MUTEX(un));
10448 			if (un->un_f_blockcount_is_valid &&
10449 			    un->un_blockcount > label_cap &&
10450 			    un->un_f_expnevent == B_FALSE) {
10451 				un->un_f_expnevent = B_TRUE;
10452 				mutex_exit(SD_MUTEX(un));
10453 				sd_log_lun_expansion_event(un,
10454 				    (nodelay ? KM_NOSLEEP : KM_SLEEP));
10455 				mutex_enter(SD_MUTEX(un));
10456 			}
10457 		} else {
10458 			mutex_enter(SD_MUTEX(un));
10459 		}
10460 	}
10461 
10462 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10463 	    "open of part %d type %d\n", part, otyp);
10464 
10465 	mutex_exit(SD_MUTEX(un));
10466 	if (!nodelay) {
10467 		sd_pm_exit(un);
10468 	}
10469 
10470 	sema_v(&un->un_semoclose);
10471 
10472 	mutex_enter(&sd_detach_mutex);
10473 	un->un_opens_in_progress--;
10474 	mutex_exit(&sd_detach_mutex);
10475 
10476 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10477 	return (DDI_SUCCESS);
10478 
10479 excl_open_fail:
10480 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10481 	rval = EBUSY;
10482 
10483 open_fail:
10484 	mutex_exit(SD_MUTEX(un));
10485 
10486 	/*
10487 	 * On a failed open we must exit the pm management.
10488 	 */
10489 	if (!nodelay) {
10490 		sd_pm_exit(un);
10491 	}
10492 open_failed_with_pm:
10493 	sema_v(&un->un_semoclose);
10494 
10495 	mutex_enter(&sd_detach_mutex);
10496 	un->un_opens_in_progress--;
10497 	if (otyp == OTYP_LYR) {
10498 		un->un_layer_count--;
10499 	}
10500 	mutex_exit(&sd_detach_mutex);
10501 
10502 	return (rval);
10503 }
10504 
10505 
10506 /*
10507  *    Function: sdclose
10508  *
10509  * Description: Driver's close(9e) entry point function.
10510  *
10511  *   Arguments: dev    - device number
10512  *		flag   - file status flag, informational only
10513  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10514  *		cred_p - user credential pointer
10515  *
10516  * Return Code: ENXIO
10517  *
10518  *     Context: Kernel thread context
10519  */
10520 /* ARGSUSED */
10521 static int
10522 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10523 {
10524 	struct sd_lun	*un;
10525 	uchar_t		*cp;
10526 	int		part;
10527 	int		nodelay;
10528 	int		rval = 0;
10529 
10530 	/* Validate the open type */
10531 	if (otyp >= OTYPCNT) {
10532 		return (ENXIO);
10533 	}
10534 
10535 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10536 		return (ENXIO);
10537 	}
10538 
10539 	part = SDPART(dev);
10540 	nodelay = flag & (FNDELAY | FNONBLOCK);
10541 
10542 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10543 	    "sdclose: close of part %d type %d\n", part, otyp);
10544 
10545 	/*
10546 	 * We use a semaphore here in order to serialize
10547 	 * open and close requests on the device.
10548 	 */
10549 	sema_p(&un->un_semoclose);
10550 
10551 	mutex_enter(SD_MUTEX(un));
10552 
10553 	/* Don't proceed if power is being changed. */
10554 	while (un->un_state == SD_STATE_PM_CHANGING) {
10555 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10556 	}
10557 
10558 	if (un->un_exclopen & (1 << part)) {
10559 		un->un_exclopen &= ~(1 << part);
10560 	}
10561 
10562 	/* Update the open partition map */
10563 	if (otyp == OTYP_LYR) {
10564 		un->un_ocmap.lyropen[part] -= 1;
10565 	} else {
10566 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10567 	}
10568 
10569 	cp = &un->un_ocmap.chkd[0];
10570 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10571 		if (*cp != NULL) {
10572 			break;
10573 		}
10574 		cp++;
10575 	}
10576 
10577 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10578 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10579 
10580 		/*
10581 		 * We avoid persistance upon the last close, and set
10582 		 * the throttle back to the maximum.
10583 		 */
10584 		un->un_throttle = un->un_saved_throttle;
10585 
10586 		if (un->un_state == SD_STATE_OFFLINE) {
10587 			if (un->un_f_is_fibre == FALSE) {
10588 				scsi_log(SD_DEVINFO(un), sd_label,
10589 				    CE_WARN, "offline\n");
10590 			}
10591 			mutex_exit(SD_MUTEX(un));
10592 			cmlb_invalidate(un->un_cmlbhandle,
10593 			    (void *)SD_PATH_DIRECT);
10594 			mutex_enter(SD_MUTEX(un));
10595 
10596 		} else {
10597 			/*
10598 			 * Flush any outstanding writes in NVRAM cache.
10599 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10600 			 * cmd, it may not work for non-Pluto devices.
10601 			 * SYNCHRONIZE CACHE is not required for removables,
10602 			 * except DVD-RAM drives.
10603 			 *
10604 			 * Also note: because SYNCHRONIZE CACHE is currently
10605 			 * the only command issued here that requires the
10606 			 * drive be powered up, only do the power up before
10607 			 * sending the Sync Cache command. If additional
10608 			 * commands are added which require a powered up
10609 			 * drive, the following sequence may have to change.
10610 			 *
10611 			 * And finally, note that parallel SCSI on SPARC
10612 			 * only issues a Sync Cache to DVD-RAM, a newly
10613 			 * supported device.
10614 			 */
10615 #if defined(__i386) || defined(__amd64)
10616 			if ((un->un_f_sync_cache_supported &&
10617 			    un->un_f_sync_cache_required) ||
10618 			    un->un_f_dvdram_writable_device == TRUE) {
10619 #else
10620 			if (un->un_f_dvdram_writable_device == TRUE) {
10621 #endif
10622 				mutex_exit(SD_MUTEX(un));
10623 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10624 					rval =
10625 					    sd_send_scsi_SYNCHRONIZE_CACHE(un,
10626 					    NULL);
10627 					/* ignore error if not supported */
10628 					if (rval == ENOTSUP) {
10629 						rval = 0;
10630 					} else if (rval != 0) {
10631 						rval = EIO;
10632 					}
10633 					sd_pm_exit(un);
10634 				} else {
10635 					rval = EIO;
10636 				}
10637 				mutex_enter(SD_MUTEX(un));
10638 			}
10639 
10640 			/*
10641 			 * For devices which supports DOOR_LOCK, send an ALLOW
10642 			 * MEDIA REMOVAL command, but don't get upset if it
10643 			 * fails. We need to raise the power of the drive before
10644 			 * we can call sd_send_scsi_DOORLOCK()
10645 			 */
10646 			if (un->un_f_doorlock_supported) {
10647 				mutex_exit(SD_MUTEX(un));
10648 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10649 					sd_ssc_t	*ssc;
10650 
10651 					ssc = sd_ssc_init(un);
10652 					rval = sd_send_scsi_DOORLOCK(ssc,
10653 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10654 					if (rval != 0)
10655 						sd_ssc_assessment(ssc,
10656 						    SD_FMT_IGNORE);
10657 					sd_ssc_fini(ssc);
10658 
10659 					sd_pm_exit(un);
10660 					if (ISCD(un) && (rval != 0) &&
10661 					    (nodelay != 0)) {
10662 						rval = ENXIO;
10663 					}
10664 				} else {
10665 					rval = EIO;
10666 				}
10667 				mutex_enter(SD_MUTEX(un));
10668 			}
10669 
10670 			/*
10671 			 * If a device has removable media, invalidate all
10672 			 * parameters related to media, such as geometry,
10673 			 * blocksize, and blockcount.
10674 			 */
10675 			if (un->un_f_has_removable_media) {
10676 				sr_ejected(un);
10677 			}
10678 
10679 			/*
10680 			 * Destroy the cache (if it exists) which was
10681 			 * allocated for the write maps since this is
10682 			 * the last close for this media.
10683 			 */
10684 			if (un->un_wm_cache) {
10685 				/*
10686 				 * Check if there are pending commands.
10687 				 * and if there are give a warning and
10688 				 * do not destroy the cache.
10689 				 */
10690 				if (un->un_ncmds_in_driver > 0) {
10691 					scsi_log(SD_DEVINFO(un),
10692 					    sd_label, CE_WARN,
10693 					    "Unable to clean up memory "
10694 					    "because of pending I/O\n");
10695 				} else {
10696 					kmem_cache_destroy(
10697 					    un->un_wm_cache);
10698 					un->un_wm_cache = NULL;
10699 				}
10700 			}
10701 		}
10702 	}
10703 
10704 	mutex_exit(SD_MUTEX(un));
10705 	sema_v(&un->un_semoclose);
10706 
10707 	if (otyp == OTYP_LYR) {
10708 		mutex_enter(&sd_detach_mutex);
10709 		/*
10710 		 * The detach routine may run when the layer count
10711 		 * drops to zero.
10712 		 */
10713 		un->un_layer_count--;
10714 		mutex_exit(&sd_detach_mutex);
10715 	}
10716 
10717 	return (rval);
10718 }
10719 
10720 
10721 /*
10722  *    Function: sd_ready_and_valid
10723  *
10724  * Description: Test if device is ready and has a valid geometry.
10725  *
10726  *   Arguments: ssc - sd_ssc_t will contain un
10727  *		un  - driver soft state (unit) structure
10728  *
10729  * Return Code: SD_READY_VALID		ready and valid label
10730  *		SD_NOT_READY_VALID	not ready, no label
10731  *		SD_RESERVED_BY_OTHERS	reservation conflict
10732  *
10733  *     Context: Never called at interrupt context.
10734  */
10735 
10736 static int
10737 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10738 {
10739 	struct sd_errstats	*stp;
10740 	uint64_t		capacity;
10741 	uint_t			lbasize;
10742 	int			rval = SD_READY_VALID;
10743 	char			name_str[48];
10744 	boolean_t		is_valid;
10745 	struct sd_lun		*un;
10746 	int			status;
10747 
10748 	ASSERT(ssc != NULL);
10749 	un = ssc->ssc_un;
10750 	ASSERT(un != NULL);
10751 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10752 
10753 	mutex_enter(SD_MUTEX(un));
10754 	/*
10755 	 * If a device has removable media, we must check if media is
10756 	 * ready when checking if this device is ready and valid.
10757 	 */
10758 	if (un->un_f_has_removable_media) {
10759 		mutex_exit(SD_MUTEX(un));
10760 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10761 
10762 		if (status != 0) {
10763 			rval = SD_NOT_READY_VALID;
10764 			mutex_enter(SD_MUTEX(un));
10765 
10766 			/* Ignore all failed status for removalbe media */
10767 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10768 
10769 			goto done;
10770 		}
10771 
10772 		is_valid = SD_IS_VALID_LABEL(un);
10773 		mutex_enter(SD_MUTEX(un));
10774 		if (!is_valid ||
10775 		    (un->un_f_blockcount_is_valid == FALSE) ||
10776 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10777 
10778 			/* capacity has to be read every open. */
10779 			mutex_exit(SD_MUTEX(un));
10780 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10781 			    &lbasize, SD_PATH_DIRECT);
10782 
10783 			if (status != 0) {
10784 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10785 
10786 				cmlb_invalidate(un->un_cmlbhandle,
10787 				    (void *)SD_PATH_DIRECT);
10788 				mutex_enter(SD_MUTEX(un));
10789 				rval = SD_NOT_READY_VALID;
10790 
10791 				goto done;
10792 			} else {
10793 				mutex_enter(SD_MUTEX(un));
10794 				sd_update_block_info(un, lbasize, capacity);
10795 			}
10796 		}
10797 
10798 		/*
10799 		 * Check if the media in the device is writable or not.
10800 		 */
10801 		if (!is_valid && ISCD(un)) {
10802 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10803 		}
10804 
10805 	} else {
10806 		/*
10807 		 * Do a test unit ready to clear any unit attention from non-cd
10808 		 * devices.
10809 		 */
10810 		mutex_exit(SD_MUTEX(un));
10811 
10812 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10813 		if (status != 0) {
10814 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10815 		}
10816 
10817 		mutex_enter(SD_MUTEX(un));
10818 	}
10819 
10820 
10821 	/*
10822 	 * If this is a non 512 block device, allocate space for
10823 	 * the wmap cache. This is being done here since every time
10824 	 * a media is changed this routine will be called and the
10825 	 * block size is a function of media rather than device.
10826 	 */
10827 	if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10828 	    un->un_f_non_devbsize_supported) &&
10829 	    un->un_tgt_blocksize != DEV_BSIZE) ||
10830 	    un->un_f_enable_rmw) {
10831 		if (!(un->un_wm_cache)) {
10832 			(void) snprintf(name_str, sizeof (name_str),
10833 			    "%s%d_cache",
10834 			    ddi_driver_name(SD_DEVINFO(un)),
10835 			    ddi_get_instance(SD_DEVINFO(un)));
10836 			un->un_wm_cache = kmem_cache_create(
10837 			    name_str, sizeof (struct sd_w_map),
10838 			    8, sd_wm_cache_constructor,
10839 			    sd_wm_cache_destructor, NULL,
10840 			    (void *)un, NULL, 0);
10841 			if (!(un->un_wm_cache)) {
10842 				rval = ENOMEM;
10843 				goto done;
10844 			}
10845 		}
10846 	}
10847 
10848 	if (un->un_state == SD_STATE_NORMAL) {
10849 		/*
10850 		 * If the target is not yet ready here (defined by a TUR
10851 		 * failure), invalidate the geometry and print an 'offline'
10852 		 * message. This is a legacy message, as the state of the
10853 		 * target is not actually changed to SD_STATE_OFFLINE.
10854 		 *
10855 		 * If the TUR fails for EACCES (Reservation Conflict),
10856 		 * SD_RESERVED_BY_OTHERS will be returned to indicate
10857 		 * reservation conflict. If the TUR fails for other
10858 		 * reasons, SD_NOT_READY_VALID will be returned.
10859 		 */
10860 		int err;
10861 
10862 		mutex_exit(SD_MUTEX(un));
10863 		err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10864 		mutex_enter(SD_MUTEX(un));
10865 
10866 		if (err != 0) {
10867 			mutex_exit(SD_MUTEX(un));
10868 			cmlb_invalidate(un->un_cmlbhandle,
10869 			    (void *)SD_PATH_DIRECT);
10870 			mutex_enter(SD_MUTEX(un));
10871 			if (err == EACCES) {
10872 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10873 				    "reservation conflict\n");
10874 				rval = SD_RESERVED_BY_OTHERS;
10875 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10876 			} else {
10877 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10878 				    "drive offline\n");
10879 				rval = SD_NOT_READY_VALID;
10880 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10881 			}
10882 			goto done;
10883 		}
10884 	}
10885 
10886 	if (un->un_f_format_in_progress == FALSE) {
10887 		mutex_exit(SD_MUTEX(un));
10888 
10889 		(void) cmlb_validate(un->un_cmlbhandle, 0,
10890 		    (void *)SD_PATH_DIRECT);
10891 		if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10892 		    NULL, (void *) SD_PATH_DIRECT) != 0) {
10893 			rval = SD_NOT_READY_VALID;
10894 			mutex_enter(SD_MUTEX(un));
10895 
10896 			goto done;
10897 		}
10898 		if (un->un_f_pkstats_enabled) {
10899 			sd_set_pstats(un);
10900 			SD_TRACE(SD_LOG_IO_PARTITION, un,
10901 			    "sd_ready_and_valid: un:0x%p pstats created and "
10902 			    "set\n", un);
10903 		}
10904 		mutex_enter(SD_MUTEX(un));
10905 	}
10906 
10907 	/*
10908 	 * If this device supports DOOR_LOCK command, try and send
10909 	 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10910 	 * if it fails. For a CD, however, it is an error
10911 	 */
10912 	if (un->un_f_doorlock_supported) {
10913 		mutex_exit(SD_MUTEX(un));
10914 		status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10915 		    SD_PATH_DIRECT);
10916 
10917 		if ((status != 0) && ISCD(un)) {
10918 			rval = SD_NOT_READY_VALID;
10919 			mutex_enter(SD_MUTEX(un));
10920 
10921 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10922 
10923 			goto done;
10924 		} else if (status != 0)
10925 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10926 		mutex_enter(SD_MUTEX(un));
10927 	}
10928 
10929 	/* The state has changed, inform the media watch routines */
10930 	un->un_mediastate = DKIO_INSERTED;
10931 	cv_broadcast(&un->un_state_cv);
10932 	rval = SD_READY_VALID;
10933 
10934 done:
10935 
10936 	/*
10937 	 * Initialize the capacity kstat value, if no media previously
10938 	 * (capacity kstat is 0) and a media has been inserted
10939 	 * (un_blockcount > 0).
10940 	 */
10941 	if (un->un_errstats != NULL) {
10942 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
10943 		if ((stp->sd_capacity.value.ui64 == 0) &&
10944 		    (un->un_f_blockcount_is_valid == TRUE)) {
10945 			stp->sd_capacity.value.ui64 =
10946 			    (uint64_t)((uint64_t)un->un_blockcount *
10947 			    un->un_sys_blocksize);
10948 		}
10949 	}
10950 
10951 	mutex_exit(SD_MUTEX(un));
10952 	return (rval);
10953 }
10954 
10955 
10956 /*
10957  *    Function: sdmin
10958  *
10959  * Description: Routine to limit the size of a data transfer. Used in
10960  *		conjunction with physio(9F).
10961  *
10962  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10963  *
10964  *     Context: Kernel thread context.
10965  */
10966 
10967 static void
10968 sdmin(struct buf *bp)
10969 {
10970 	struct sd_lun	*un;
10971 	int		instance;
10972 
10973 	instance = SDUNIT(bp->b_edev);
10974 
10975 	un = ddi_get_soft_state(sd_state, instance);
10976 	ASSERT(un != NULL);
10977 
10978 	/*
10979 	 * We depend on buf breakup to restrict
10980 	 * IO size if it is enabled.
10981 	 */
10982 	if (un->un_buf_breakup_supported) {
10983 		return;
10984 	}
10985 
10986 	if (bp->b_bcount > un->un_max_xfer_size) {
10987 		bp->b_bcount = un->un_max_xfer_size;
10988 	}
10989 }
10990 
10991 
10992 /*
10993  *    Function: sdread
10994  *
10995  * Description: Driver's read(9e) entry point function.
10996  *
10997  *   Arguments: dev   - device number
10998  *		uio   - structure pointer describing where data is to be stored
10999  *			in user's space
11000  *		cred_p  - user credential pointer
11001  *
11002  * Return Code: ENXIO
11003  *		EIO
11004  *		EINVAL
11005  *		value returned by physio
11006  *
11007  *     Context: Kernel thread context.
11008  */
11009 /* ARGSUSED */
11010 static int
11011 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
11012 {
11013 	struct sd_lun	*un = NULL;
11014 	int		secmask;
11015 	int		err = 0;
11016 	sd_ssc_t	*ssc;
11017 
11018 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11019 		return (ENXIO);
11020 	}
11021 
11022 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11023 
11024 
11025 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11026 		mutex_enter(SD_MUTEX(un));
11027 		/*
11028 		 * Because the call to sd_ready_and_valid will issue I/O we
11029 		 * must wait here if either the device is suspended or
11030 		 * if it's power level is changing.
11031 		 */
11032 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11033 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11034 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11035 		}
11036 		un->un_ncmds_in_driver++;
11037 		mutex_exit(SD_MUTEX(un));
11038 
11039 		/* Initialize sd_ssc_t for internal uscsi commands */
11040 		ssc = sd_ssc_init(un);
11041 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11042 			err = EIO;
11043 		} else {
11044 			err = 0;
11045 		}
11046 		sd_ssc_fini(ssc);
11047 
11048 		mutex_enter(SD_MUTEX(un));
11049 		un->un_ncmds_in_driver--;
11050 		ASSERT(un->un_ncmds_in_driver >= 0);
11051 		mutex_exit(SD_MUTEX(un));
11052 		if (err != 0)
11053 			return (err);
11054 	}
11055 
11056 	/*
11057 	 * Read requests are restricted to multiples of the system block size.
11058 	 */
11059 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11060 	    !un->un_f_enable_rmw)
11061 		secmask = un->un_tgt_blocksize - 1;
11062 	else
11063 		secmask = DEV_BSIZE - 1;
11064 
11065 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11066 		SD_ERROR(SD_LOG_READ_WRITE, un,
11067 		    "sdread: file offset not modulo %d\n",
11068 		    secmask + 1);
11069 		err = EINVAL;
11070 	} else if (uio->uio_iov->iov_len & (secmask)) {
11071 		SD_ERROR(SD_LOG_READ_WRITE, un,
11072 		    "sdread: transfer length not modulo %d\n",
11073 		    secmask + 1);
11074 		err = EINVAL;
11075 	} else {
11076 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
11077 	}
11078 
11079 	return (err);
11080 }
11081 
11082 
11083 /*
11084  *    Function: sdwrite
11085  *
11086  * Description: Driver's write(9e) entry point function.
11087  *
11088  *   Arguments: dev   - device number
11089  *		uio   - structure pointer describing where data is stored in
11090  *			user's space
11091  *		cred_p  - user credential pointer
11092  *
11093  * Return Code: ENXIO
11094  *		EIO
11095  *		EINVAL
11096  *		value returned by physio
11097  *
11098  *     Context: Kernel thread context.
11099  */
11100 /* ARGSUSED */
11101 static int
11102 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
11103 {
11104 	struct sd_lun	*un = NULL;
11105 	int		secmask;
11106 	int		err = 0;
11107 	sd_ssc_t	*ssc;
11108 
11109 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11110 		return (ENXIO);
11111 	}
11112 
11113 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11114 
11115 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11116 		mutex_enter(SD_MUTEX(un));
11117 		/*
11118 		 * Because the call to sd_ready_and_valid will issue I/O we
11119 		 * must wait here if either the device is suspended or
11120 		 * if it's power level is changing.
11121 		 */
11122 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11123 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11124 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11125 		}
11126 		un->un_ncmds_in_driver++;
11127 		mutex_exit(SD_MUTEX(un));
11128 
11129 		/* Initialize sd_ssc_t for internal uscsi commands */
11130 		ssc = sd_ssc_init(un);
11131 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11132 			err = EIO;
11133 		} else {
11134 			err = 0;
11135 		}
11136 		sd_ssc_fini(ssc);
11137 
11138 		mutex_enter(SD_MUTEX(un));
11139 		un->un_ncmds_in_driver--;
11140 		ASSERT(un->un_ncmds_in_driver >= 0);
11141 		mutex_exit(SD_MUTEX(un));
11142 		if (err != 0)
11143 			return (err);
11144 	}
11145 
11146 	/*
11147 	 * Write requests are restricted to multiples of the system block size.
11148 	 */
11149 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11150 	    !un->un_f_enable_rmw)
11151 		secmask = un->un_tgt_blocksize - 1;
11152 	else
11153 		secmask = DEV_BSIZE - 1;
11154 
11155 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11156 		SD_ERROR(SD_LOG_READ_WRITE, un,
11157 		    "sdwrite: file offset not modulo %d\n",
11158 		    secmask + 1);
11159 		err = EINVAL;
11160 	} else if (uio->uio_iov->iov_len & (secmask)) {
11161 		SD_ERROR(SD_LOG_READ_WRITE, un,
11162 		    "sdwrite: transfer length not modulo %d\n",
11163 		    secmask + 1);
11164 		err = EINVAL;
11165 	} else {
11166 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11167 	}
11168 
11169 	return (err);
11170 }
11171 
11172 
11173 /*
11174  *    Function: sdaread
11175  *
11176  * Description: Driver's aread(9e) entry point function.
11177  *
11178  *   Arguments: dev   - device number
11179  *		aio   - structure pointer describing where data is to be stored
11180  *		cred_p  - user credential pointer
11181  *
11182  * Return Code: ENXIO
11183  *		EIO
11184  *		EINVAL
11185  *		value returned by aphysio
11186  *
11187  *     Context: Kernel thread context.
11188  */
11189 /* ARGSUSED */
11190 static int
11191 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11192 {
11193 	struct sd_lun	*un = NULL;
11194 	struct uio	*uio = aio->aio_uio;
11195 	int		secmask;
11196 	int		err = 0;
11197 	sd_ssc_t	*ssc;
11198 
11199 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11200 		return (ENXIO);
11201 	}
11202 
11203 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11204 
11205 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11206 		mutex_enter(SD_MUTEX(un));
11207 		/*
11208 		 * Because the call to sd_ready_and_valid will issue I/O we
11209 		 * must wait here if either the device is suspended or
11210 		 * if it's power level is changing.
11211 		 */
11212 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11213 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11214 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11215 		}
11216 		un->un_ncmds_in_driver++;
11217 		mutex_exit(SD_MUTEX(un));
11218 
11219 		/* Initialize sd_ssc_t for internal uscsi commands */
11220 		ssc = sd_ssc_init(un);
11221 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11222 			err = EIO;
11223 		} else {
11224 			err = 0;
11225 		}
11226 		sd_ssc_fini(ssc);
11227 
11228 		mutex_enter(SD_MUTEX(un));
11229 		un->un_ncmds_in_driver--;
11230 		ASSERT(un->un_ncmds_in_driver >= 0);
11231 		mutex_exit(SD_MUTEX(un));
11232 		if (err != 0)
11233 			return (err);
11234 	}
11235 
11236 	/*
11237 	 * Read requests are restricted to multiples of the system block size.
11238 	 */
11239 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11240 	    !un->un_f_enable_rmw)
11241 		secmask = un->un_tgt_blocksize - 1;
11242 	else
11243 		secmask = DEV_BSIZE - 1;
11244 
11245 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11246 		SD_ERROR(SD_LOG_READ_WRITE, un,
11247 		    "sdaread: file offset not modulo %d\n",
11248 		    secmask + 1);
11249 		err = EINVAL;
11250 	} else if (uio->uio_iov->iov_len & (secmask)) {
11251 		SD_ERROR(SD_LOG_READ_WRITE, un,
11252 		    "sdaread: transfer length not modulo %d\n",
11253 		    secmask + 1);
11254 		err = EINVAL;
11255 	} else {
11256 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11257 	}
11258 
11259 	return (err);
11260 }
11261 
11262 
11263 /*
11264  *    Function: sdawrite
11265  *
11266  * Description: Driver's awrite(9e) entry point function.
11267  *
11268  *   Arguments: dev   - device number
11269  *		aio   - structure pointer describing where data is stored
11270  *		cred_p  - user credential pointer
11271  *
11272  * Return Code: ENXIO
11273  *		EIO
11274  *		EINVAL
11275  *		value returned by aphysio
11276  *
11277  *     Context: Kernel thread context.
11278  */
11279 /* ARGSUSED */
11280 static int
11281 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11282 {
11283 	struct sd_lun	*un = NULL;
11284 	struct uio	*uio = aio->aio_uio;
11285 	int		secmask;
11286 	int		err = 0;
11287 	sd_ssc_t	*ssc;
11288 
11289 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11290 		return (ENXIO);
11291 	}
11292 
11293 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11294 
11295 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11296 		mutex_enter(SD_MUTEX(un));
11297 		/*
11298 		 * Because the call to sd_ready_and_valid will issue I/O we
11299 		 * must wait here if either the device is suspended or
11300 		 * if it's power level is changing.
11301 		 */
11302 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11303 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11304 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11305 		}
11306 		un->un_ncmds_in_driver++;
11307 		mutex_exit(SD_MUTEX(un));
11308 
11309 		/* Initialize sd_ssc_t for internal uscsi commands */
11310 		ssc = sd_ssc_init(un);
11311 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11312 			err = EIO;
11313 		} else {
11314 			err = 0;
11315 		}
11316 		sd_ssc_fini(ssc);
11317 
11318 		mutex_enter(SD_MUTEX(un));
11319 		un->un_ncmds_in_driver--;
11320 		ASSERT(un->un_ncmds_in_driver >= 0);
11321 		mutex_exit(SD_MUTEX(un));
11322 		if (err != 0)
11323 			return (err);
11324 	}
11325 
11326 	/*
11327 	 * Write requests are restricted to multiples of the system block size.
11328 	 */
11329 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11330 	    !un->un_f_enable_rmw)
11331 		secmask = un->un_tgt_blocksize - 1;
11332 	else
11333 		secmask = DEV_BSIZE - 1;
11334 
11335 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11336 		SD_ERROR(SD_LOG_READ_WRITE, un,
11337 		    "sdawrite: file offset not modulo %d\n",
11338 		    secmask + 1);
11339 		err = EINVAL;
11340 	} else if (uio->uio_iov->iov_len & (secmask)) {
11341 		SD_ERROR(SD_LOG_READ_WRITE, un,
11342 		    "sdawrite: transfer length not modulo %d\n",
11343 		    secmask + 1);
11344 		err = EINVAL;
11345 	} else {
11346 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11347 	}
11348 
11349 	return (err);
11350 }
11351 
11352 
11353 
11354 
11355 
11356 /*
11357  * Driver IO processing follows the following sequence:
11358  *
11359  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11360  *         |                |                     ^
11361  *         v                v                     |
11362  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11363  *         |                |                     |                   |
11364  *         v                |                     |                   |
11365  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11366  *         |                |                     ^                   ^
11367  *         v                v                     |                   |
11368  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11369  *         |                |                     |                   |
11370  *     +---+                |                     +------------+      +-------+
11371  *     |                    |                                  |              |
11372  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11373  *     |                    v                                  |              |
11374  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11375  *     |                    |                                  ^              |
11376  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11377  *     |                    v                                  |              |
11378  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11379  *     |                    |                                  ^              |
11380  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11381  *     |                    v                                  |              |
11382  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11383  *     |                    |                                  ^              |
11384  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11385  *     |                    v                                  |              |
11386  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11387  *     |                    |                                  ^              |
11388  *     |                    |                                  |              |
11389  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11390  *                          |                           ^
11391  *                          v                           |
11392  *                   sd_core_iostart()                  |
11393  *                          |                           |
11394  *                          |                           +------>(*destroypkt)()
11395  *                          +-> sd_start_cmds() <-+     |           |
11396  *                          |                     |     |           v
11397  *                          |                     |     |  scsi_destroy_pkt(9F)
11398  *                          |                     |     |
11399  *                          +->(*initpkt)()       +- sdintr()
11400  *                          |  |                        |  |
11401  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11402  *                          |  +-> scsi_setup_cdb(9F)   |
11403  *                          |                           |
11404  *                          +--> scsi_transport(9F)     |
11405  *                                     |                |
11406  *                                     +----> SCSA ---->+
11407  *
11408  *
11409  * This code is based upon the following presumptions:
11410  *
11411  *   - iostart and iodone functions operate on buf(9S) structures. These
11412  *     functions perform the necessary operations on the buf(9S) and pass
11413  *     them along to the next function in the chain by using the macros
11414  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11415  *     (for iodone side functions).
11416  *
11417  *   - The iostart side functions may sleep. The iodone side functions
11418  *     are called under interrupt context and may NOT sleep. Therefore
11419  *     iodone side functions also may not call iostart side functions.
11420  *     (NOTE: iostart side functions should NOT sleep for memory, as
11421  *     this could result in deadlock.)
11422  *
11423  *   - An iostart side function may call its corresponding iodone side
11424  *     function directly (if necessary).
11425  *
11426  *   - In the event of an error, an iostart side function can return a buf(9S)
11427  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11428  *     b_error in the usual way of course).
11429  *
11430  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11431  *     requests to the iostart side functions.  The iostart side functions in
11432  *     this case would be called under the context of a taskq thread, so it's
11433  *     OK for them to block/sleep/spin in this case.
11434  *
11435  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11436  *     pass them along to the next function in the chain.  The corresponding
11437  *     iodone side functions must coalesce the "shadow" bufs and return
11438  *     the "original" buf to the next higher layer.
11439  *
11440  *   - The b_private field of the buf(9S) struct holds a pointer to
11441  *     an sd_xbuf struct, which contains information needed to
11442  *     construct the scsi_pkt for the command.
11443  *
11444  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11445  *     layer must acquire & release the SD_MUTEX(un) as needed.
11446  */
11447 
11448 
11449 /*
11450  * Create taskq for all targets in the system. This is created at
11451  * _init(9E) and destroyed at _fini(9E).
11452  *
11453  * Note: here we set the minalloc to a reasonably high number to ensure that
11454  * we will have an adequate supply of task entries available at interrupt time.
11455  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11456  * sd_create_taskq().  Since we do not want to sleep for allocations at
11457  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11458  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11459  * requests any one instant in time.
11460  */
11461 #define	SD_TASKQ_NUMTHREADS	8
11462 #define	SD_TASKQ_MINALLOC	256
11463 #define	SD_TASKQ_MAXALLOC	256
11464 
11465 static taskq_t	*sd_tq = NULL;
11466 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11467 
11468 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11469 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11470 
11471 /*
11472  * The following task queue is being created for the write part of
11473  * read-modify-write of non-512 block size devices.
11474  * Limit the number of threads to 1 for now. This number has been chosen
11475  * considering the fact that it applies only to dvd ram drives/MO drives
11476  * currently. Performance for which is not main criteria at this stage.
11477  * Note: It needs to be explored if we can use a single taskq in future
11478  */
11479 #define	SD_WMR_TASKQ_NUMTHREADS	1
11480 static taskq_t	*sd_wmr_tq = NULL;
11481 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11482 
11483 /*
11484  *    Function: sd_taskq_create
11485  *
11486  * Description: Create taskq thread(s) and preallocate task entries
11487  *
11488  * Return Code: Returns a pointer to the allocated taskq_t.
11489  *
11490  *     Context: Can sleep. Requires blockable context.
11491  *
11492  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11493  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
11494  *		- taskq_create() will block for memory, also it will panic
11495  *		  if it cannot create the requested number of threads.
11496  *		- Currently taskq_create() creates threads that cannot be
11497  *		  swapped.
11498  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
11499  *		  supply of taskq entries at interrupt time (ie, so that we
11500  *		  do not have to sleep for memory)
11501  */
11502 
11503 static void
11504 sd_taskq_create(void)
11505 {
11506 	char	taskq_name[TASKQ_NAMELEN];
11507 
11508 	ASSERT(sd_tq == NULL);
11509 	ASSERT(sd_wmr_tq == NULL);
11510 
11511 	(void) snprintf(taskq_name, sizeof (taskq_name),
11512 	    "%s_drv_taskq", sd_label);
11513 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11514 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11515 	    TASKQ_PREPOPULATE));
11516 
11517 	(void) snprintf(taskq_name, sizeof (taskq_name),
11518 	    "%s_rmw_taskq", sd_label);
11519 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11520 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11521 	    TASKQ_PREPOPULATE));
11522 }
11523 
11524 
11525 /*
11526  *    Function: sd_taskq_delete
11527  *
11528  * Description: Complementary cleanup routine for sd_taskq_create().
11529  *
11530  *     Context: Kernel thread context.
11531  */
11532 
11533 static void
11534 sd_taskq_delete(void)
11535 {
11536 	ASSERT(sd_tq != NULL);
11537 	ASSERT(sd_wmr_tq != NULL);
11538 	taskq_destroy(sd_tq);
11539 	taskq_destroy(sd_wmr_tq);
11540 	sd_tq = NULL;
11541 	sd_wmr_tq = NULL;
11542 }
11543 
11544 
11545 /*
11546  *    Function: sdstrategy
11547  *
11548  * Description: Driver's strategy (9E) entry point function.
11549  *
11550  *   Arguments: bp - pointer to buf(9S)
11551  *
11552  * Return Code: Always returns zero
11553  *
11554  *     Context: Kernel thread context.
11555  */
11556 
11557 static int
11558 sdstrategy(struct buf *bp)
11559 {
11560 	struct sd_lun *un;
11561 
11562 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11563 	if (un == NULL) {
11564 		bioerror(bp, EIO);
11565 		bp->b_resid = bp->b_bcount;
11566 		biodone(bp);
11567 		return (0);
11568 	}
11569 
11570 	/* As was done in the past, fail new cmds. if state is dumping. */
11571 	if (un->un_state == SD_STATE_DUMPING) {
11572 		bioerror(bp, ENXIO);
11573 		bp->b_resid = bp->b_bcount;
11574 		biodone(bp);
11575 		return (0);
11576 	}
11577 
11578 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11579 
11580 	/*
11581 	 * Commands may sneak in while we released the mutex in
11582 	 * DDI_SUSPEND, we should block new commands. However, old
11583 	 * commands that are still in the driver at this point should
11584 	 * still be allowed to drain.
11585 	 */
11586 	mutex_enter(SD_MUTEX(un));
11587 	/*
11588 	 * Must wait here if either the device is suspended or
11589 	 * if it's power level is changing.
11590 	 */
11591 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11592 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11593 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11594 	}
11595 
11596 	un->un_ncmds_in_driver++;
11597 
11598 	/*
11599 	 * atapi: Since we are running the CD for now in PIO mode we need to
11600 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11601 	 * the HBA's init_pkt routine.
11602 	 */
11603 	if (un->un_f_cfg_is_atapi == TRUE) {
11604 		mutex_exit(SD_MUTEX(un));
11605 		bp_mapin(bp);
11606 		mutex_enter(SD_MUTEX(un));
11607 	}
11608 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11609 	    un->un_ncmds_in_driver);
11610 
11611 	if (bp->b_flags & B_WRITE)
11612 		un->un_f_sync_cache_required = TRUE;
11613 
11614 	mutex_exit(SD_MUTEX(un));
11615 
11616 	/*
11617 	 * This will (eventually) allocate the sd_xbuf area and
11618 	 * call sd_xbuf_strategy().  We just want to return the
11619 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11620 	 * imized tail call which saves us a stack frame.
11621 	 */
11622 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11623 }
11624 
11625 
11626 /*
11627  *    Function: sd_xbuf_strategy
11628  *
11629  * Description: Function for initiating IO operations via the
11630  *		ddi_xbuf_qstrategy() mechanism.
11631  *
11632  *     Context: Kernel thread context.
11633  */
11634 
11635 static void
11636 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11637 {
11638 	struct sd_lun *un = arg;
11639 
11640 	ASSERT(bp != NULL);
11641 	ASSERT(xp != NULL);
11642 	ASSERT(un != NULL);
11643 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11644 
11645 	/*
11646 	 * Initialize the fields in the xbuf and save a pointer to the
11647 	 * xbuf in bp->b_private.
11648 	 */
11649 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11650 
11651 	/* Send the buf down the iostart chain */
11652 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11653 }
11654 
11655 
11656 /*
11657  *    Function: sd_xbuf_init
11658  *
11659  * Description: Prepare the given sd_xbuf struct for use.
11660  *
11661  *   Arguments: un - ptr to softstate
11662  *		bp - ptr to associated buf(9S)
11663  *		xp - ptr to associated sd_xbuf
11664  *		chain_type - IO chain type to use:
11665  *			SD_CHAIN_NULL
11666  *			SD_CHAIN_BUFIO
11667  *			SD_CHAIN_USCSI
11668  *			SD_CHAIN_DIRECT
11669  *			SD_CHAIN_DIRECT_PRIORITY
11670  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11671  *			initialization; may be NULL if none.
11672  *
11673  *     Context: Kernel thread context
11674  */
11675 
11676 static void
11677 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11678     uchar_t chain_type, void *pktinfop)
11679 {
11680 	int index;
11681 
11682 	ASSERT(un != NULL);
11683 	ASSERT(bp != NULL);
11684 	ASSERT(xp != NULL);
11685 
11686 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11687 	    bp, chain_type);
11688 
11689 	xp->xb_un	= un;
11690 	xp->xb_pktp	= NULL;
11691 	xp->xb_pktinfo	= pktinfop;
11692 	xp->xb_private	= bp->b_private;
11693 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
11694 
11695 	/*
11696 	 * Set up the iostart and iodone chain indexes in the xbuf, based
11697 	 * upon the specified chain type to use.
11698 	 */
11699 	switch (chain_type) {
11700 	case SD_CHAIN_NULL:
11701 		/*
11702 		 * Fall thru to just use the values for the buf type, even
11703 		 * tho for the NULL chain these values will never be used.
11704 		 */
11705 		/* FALLTHRU */
11706 	case SD_CHAIN_BUFIO:
11707 		index = un->un_buf_chain_type;
11708 		if ((!un->un_f_has_removable_media) &&
11709 		    (un->un_tgt_blocksize != 0) &&
11710 		    (un->un_tgt_blocksize != DEV_BSIZE ||
11711 		    un->un_f_enable_rmw)) {
11712 			int secmask = 0, blknomask = 0;
11713 			if (un->un_f_enable_rmw) {
11714 				blknomask =
11715 				    (un->un_phy_blocksize / DEV_BSIZE) - 1;
11716 				secmask = un->un_phy_blocksize - 1;
11717 			} else {
11718 				blknomask =
11719 				    (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11720 				secmask = un->un_tgt_blocksize - 1;
11721 			}
11722 
11723 			if ((bp->b_lblkno & (blknomask)) ||
11724 			    (bp->b_bcount & (secmask))) {
11725 				if ((un->un_f_rmw_type !=
11726 				    SD_RMW_TYPE_RETURN_ERROR) ||
11727 				    un->un_f_enable_rmw) {
11728 					if (un->un_f_pm_is_enabled == FALSE)
11729 						index =
11730 						    SD_CHAIN_INFO_MSS_DSK_NO_PM;
11731 					else
11732 						index =
11733 						    SD_CHAIN_INFO_MSS_DISK;
11734 				}
11735 			}
11736 		}
11737 		break;
11738 	case SD_CHAIN_USCSI:
11739 		index = un->un_uscsi_chain_type;
11740 		break;
11741 	case SD_CHAIN_DIRECT:
11742 		index = un->un_direct_chain_type;
11743 		break;
11744 	case SD_CHAIN_DIRECT_PRIORITY:
11745 		index = un->un_priority_chain_type;
11746 		break;
11747 	default:
11748 		/* We're really broken if we ever get here... */
11749 		panic("sd_xbuf_init: illegal chain type!");
11750 		/*NOTREACHED*/
11751 	}
11752 
11753 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11754 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11755 
11756 	/*
11757 	 * It might be a bit easier to simply bzero the entire xbuf above,
11758 	 * but it turns out that since we init a fair number of members anyway,
11759 	 * we save a fair number cycles by doing explicit assignment of zero.
11760 	 */
11761 	xp->xb_pkt_flags	= 0;
11762 	xp->xb_dma_resid	= 0;
11763 	xp->xb_retry_count	= 0;
11764 	xp->xb_victim_retry_count = 0;
11765 	xp->xb_ua_retry_count	= 0;
11766 	xp->xb_nr_retry_count	= 0;
11767 	xp->xb_sense_bp		= NULL;
11768 	xp->xb_sense_status	= 0;
11769 	xp->xb_sense_state	= 0;
11770 	xp->xb_sense_resid	= 0;
11771 	xp->xb_ena		= 0;
11772 
11773 	bp->b_private	= xp;
11774 	bp->b_flags	&= ~(B_DONE | B_ERROR);
11775 	bp->b_resid	= 0;
11776 	bp->av_forw	= NULL;
11777 	bp->av_back	= NULL;
11778 	bioerror(bp, 0);
11779 
11780 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11781 }
11782 
11783 
11784 /*
11785  *    Function: sd_uscsi_strategy
11786  *
11787  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11788  *
11789  *   Arguments: bp - buf struct ptr
11790  *
11791  * Return Code: Always returns 0
11792  *
11793  *     Context: Kernel thread context
11794  */
11795 
11796 static int
11797 sd_uscsi_strategy(struct buf *bp)
11798 {
11799 	struct sd_lun		*un;
11800 	struct sd_uscsi_info	*uip;
11801 	struct sd_xbuf		*xp;
11802 	uchar_t			chain_type;
11803 	uchar_t			cmd;
11804 
11805 	ASSERT(bp != NULL);
11806 
11807 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11808 	if (un == NULL) {
11809 		bioerror(bp, EIO);
11810 		bp->b_resid = bp->b_bcount;
11811 		biodone(bp);
11812 		return (0);
11813 	}
11814 
11815 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11816 
11817 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11818 
11819 	/*
11820 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11821 	 */
11822 	ASSERT(bp->b_private != NULL);
11823 	uip = (struct sd_uscsi_info *)bp->b_private;
11824 	cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11825 
11826 	mutex_enter(SD_MUTEX(un));
11827 	/*
11828 	 * atapi: Since we are running the CD for now in PIO mode we need to
11829 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11830 	 * the HBA's init_pkt routine.
11831 	 */
11832 	if (un->un_f_cfg_is_atapi == TRUE) {
11833 		mutex_exit(SD_MUTEX(un));
11834 		bp_mapin(bp);
11835 		mutex_enter(SD_MUTEX(un));
11836 	}
11837 	un->un_ncmds_in_driver++;
11838 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11839 	    un->un_ncmds_in_driver);
11840 
11841 	if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11842 	    (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11843 		un->un_f_sync_cache_required = TRUE;
11844 
11845 	mutex_exit(SD_MUTEX(un));
11846 
11847 	switch (uip->ui_flags) {
11848 	case SD_PATH_DIRECT:
11849 		chain_type = SD_CHAIN_DIRECT;
11850 		break;
11851 	case SD_PATH_DIRECT_PRIORITY:
11852 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
11853 		break;
11854 	default:
11855 		chain_type = SD_CHAIN_USCSI;
11856 		break;
11857 	}
11858 
11859 	/*
11860 	 * We may allocate extra buf for external USCSI commands. If the
11861 	 * application asks for bigger than 20-byte sense data via USCSI,
11862 	 * SCSA layer will allocate 252 bytes sense buf for that command.
11863 	 */
11864 	if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11865 	    SENSE_LENGTH) {
11866 		xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11867 		    MAX_SENSE_LENGTH, KM_SLEEP);
11868 	} else {
11869 		xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11870 	}
11871 
11872 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11873 
11874 	/* Use the index obtained within xbuf_init */
11875 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11876 
11877 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11878 
11879 	return (0);
11880 }
11881 
11882 /*
11883  *    Function: sd_send_scsi_cmd
11884  *
11885  * Description: Runs a USCSI command for user (when called thru sdioctl),
11886  *		or for the driver
11887  *
11888  *   Arguments: dev - the dev_t for the device
11889  *		incmd - ptr to a valid uscsi_cmd struct
11890  *		flag - bit flag, indicating open settings, 32/64 bit type
11891  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11892  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11893  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11894  *			to use the USCSI "direct" chain and bypass the normal
11895  *			command waitq.
11896  *
11897  * Return Code: 0 -  successful completion of the given command
11898  *		EIO - scsi_uscsi_handle_command() failed
11899  *		ENXIO  - soft state not found for specified dev
11900  *		EINVAL
11901  *		EFAULT - copyin/copyout error
11902  *		return code of scsi_uscsi_handle_command():
11903  *			EIO
11904  *			ENXIO
11905  *			EACCES
11906  *
11907  *     Context: Waits for command to complete. Can sleep.
11908  */
11909 
11910 static int
11911 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11912     enum uio_seg dataspace, int path_flag)
11913 {
11914 	struct sd_lun	*un;
11915 	sd_ssc_t	*ssc;
11916 	int		rval;
11917 
11918 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11919 	if (un == NULL) {
11920 		return (ENXIO);
11921 	}
11922 
11923 	/*
11924 	 * Using sd_ssc_send to handle uscsi cmd
11925 	 */
11926 	ssc = sd_ssc_init(un);
11927 	rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11928 	sd_ssc_fini(ssc);
11929 
11930 	return (rval);
11931 }
11932 
11933 /*
11934  *    Function: sd_ssc_init
11935  *
11936  * Description: Uscsi end-user call this function to initialize necessary
11937  *              fields, such as uscsi_cmd and sd_uscsi_info struct.
11938  *
11939  *              The return value of sd_send_scsi_cmd will be treated as a
11940  *              fault in various conditions. Even it is not Zero, some
11941  *              callers may ignore the return value. That is to say, we can
11942  *              not make an accurate assessment in sdintr, since if a
11943  *              command is failed in sdintr it does not mean the caller of
11944  *              sd_send_scsi_cmd will treat it as a real failure.
11945  *
11946  *              To avoid printing too many error logs for a failed uscsi
11947  *              packet that the caller may not treat it as a failure, the
11948  *              sd will keep silent for handling all uscsi commands.
11949  *
11950  *              During detach->attach and attach-open, for some types of
11951  *              problems, the driver should be providing information about
11952  *              the problem encountered. Device use USCSI_SILENT, which
11953  *              suppresses all driver information. The result is that no
11954  *              information about the problem is available. Being
11955  *              completely silent during this time is inappropriate. The
11956  *              driver needs a more selective filter than USCSI_SILENT, so
11957  *              that information related to faults is provided.
11958  *
11959  *              To make the accurate accessment, the caller  of
11960  *              sd_send_scsi_USCSI_CMD should take the ownership and
11961  *              get necessary information to print error messages.
11962  *
11963  *              If we want to print necessary info of uscsi command, we need to
11964  *              keep the uscsi_cmd and sd_uscsi_info till we can make the
11965  *              assessment. We use sd_ssc_init to alloc necessary
11966  *              structs for sending an uscsi command and we are also
11967  *              responsible for free the memory by calling
11968  *              sd_ssc_fini.
11969  *
11970  *              The calling secquences will look like:
11971  *              sd_ssc_init->
11972  *
11973  *                  ...
11974  *
11975  *                  sd_send_scsi_USCSI_CMD->
11976  *                      sd_ssc_send-> - - - sdintr
11977  *                  ...
11978  *
11979  *                  if we think the return value should be treated as a
11980  *                  failure, we make the accessment here and print out
11981  *                  necessary by retrieving uscsi_cmd and sd_uscsi_info'
11982  *
11983  *                  ...
11984  *
11985  *              sd_ssc_fini
11986  *
11987  *
11988  *   Arguments: un - pointer to driver soft state (unit) structure for this
11989  *                   target.
11990  *
11991  * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11992  *                         uscsi_cmd and sd_uscsi_info.
11993  *                  NULL - if can not alloc memory for sd_ssc_t struct
11994  *
11995  *     Context: Kernel Thread.
11996  */
11997 static sd_ssc_t *
11998 sd_ssc_init(struct sd_lun *un)
11999 {
12000 	sd_ssc_t		*ssc;
12001 	struct uscsi_cmd	*ucmdp;
12002 	struct sd_uscsi_info	*uip;
12003 
12004 	ASSERT(un != NULL);
12005 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12006 
12007 	/*
12008 	 * Allocate sd_ssc_t structure
12009 	 */
12010 	ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
12011 
12012 	/*
12013 	 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
12014 	 */
12015 	ucmdp = scsi_uscsi_alloc();
12016 
12017 	/*
12018 	 * Allocate sd_uscsi_info structure
12019 	 */
12020 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
12021 
12022 	ssc->ssc_uscsi_cmd = ucmdp;
12023 	ssc->ssc_uscsi_info = uip;
12024 	ssc->ssc_un = un;
12025 
12026 	return (ssc);
12027 }
12028 
12029 /*
12030  * Function: sd_ssc_fini
12031  *
12032  * Description: To free sd_ssc_t and it's hanging off
12033  *
12034  * Arguments: ssc - struct pointer of sd_ssc_t.
12035  */
12036 static void
12037 sd_ssc_fini(sd_ssc_t *ssc)
12038 {
12039 	scsi_uscsi_free(ssc->ssc_uscsi_cmd);
12040 
12041 	if (ssc->ssc_uscsi_info != NULL) {
12042 		kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
12043 		ssc->ssc_uscsi_info = NULL;
12044 	}
12045 
12046 	kmem_free(ssc, sizeof (sd_ssc_t));
12047 	ssc = NULL;
12048 }
12049 
12050 /*
12051  * Function: sd_ssc_send
12052  *
12053  * Description: Runs a USCSI command for user when called through sdioctl,
12054  *              or for the driver.
12055  *
12056  *   Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12057  *                    sd_uscsi_info in.
12058  *		incmd - ptr to a valid uscsi_cmd struct
12059  *		flag - bit flag, indicating open settings, 32/64 bit type
12060  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
12061  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
12062  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
12063  *			to use the USCSI "direct" chain and bypass the normal
12064  *			command waitq.
12065  *
12066  * Return Code: 0 -  successful completion of the given command
12067  *		EIO - scsi_uscsi_handle_command() failed
12068  *		ENXIO  - soft state not found for specified dev
12069  *		ECANCELED - command cancelled due to low power
12070  *		EINVAL
12071  *		EFAULT - copyin/copyout error
12072  *		return code of scsi_uscsi_handle_command():
12073  *			EIO
12074  *			ENXIO
12075  *			EACCES
12076  *
12077  *     Context: Kernel Thread;
12078  *              Waits for command to complete. Can sleep.
12079  */
12080 static int
12081 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
12082     enum uio_seg dataspace, int path_flag)
12083 {
12084 	struct sd_uscsi_info	*uip;
12085 	struct uscsi_cmd	*uscmd;
12086 	struct sd_lun		*un;
12087 	dev_t			dev;
12088 
12089 	int	format = 0;
12090 	int	rval;
12091 
12092 	ASSERT(ssc != NULL);
12093 	un = ssc->ssc_un;
12094 	ASSERT(un != NULL);
12095 	uscmd = ssc->ssc_uscsi_cmd;
12096 	ASSERT(uscmd != NULL);
12097 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12098 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12099 		/*
12100 		 * If enter here, it indicates that the previous uscsi
12101 		 * command has not been processed by sd_ssc_assessment.
12102 		 * This is violating our rules of FMA telemetry processing.
12103 		 * We should print out this message and the last undisposed
12104 		 * uscsi command.
12105 		 */
12106 		if (uscmd->uscsi_cdb != NULL) {
12107 			SD_INFO(SD_LOG_SDTEST, un,
12108 			    "sd_ssc_send is missing the alternative "
12109 			    "sd_ssc_assessment when running command 0x%x.\n",
12110 			    uscmd->uscsi_cdb[0]);
12111 		}
12112 		/*
12113 		 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
12114 		 * the initial status.
12115 		 */
12116 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12117 	}
12118 
12119 	/*
12120 	 * We need to make sure sd_ssc_send will have sd_ssc_assessment
12121 	 * followed to avoid missing FMA telemetries.
12122 	 */
12123 	ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
12124 
12125 	/*
12126 	 * if USCSI_PMFAILFAST is set and un is in low power, fail the
12127 	 * command immediately.
12128 	 */
12129 	mutex_enter(SD_MUTEX(un));
12130 	mutex_enter(&un->un_pm_mutex);
12131 	if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
12132 	    SD_DEVICE_IS_IN_LOW_POWER(un)) {
12133 		SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
12134 		    "un:0x%p is in low power\n", un);
12135 		mutex_exit(&un->un_pm_mutex);
12136 		mutex_exit(SD_MUTEX(un));
12137 		return (ECANCELED);
12138 	}
12139 	mutex_exit(&un->un_pm_mutex);
12140 	mutex_exit(SD_MUTEX(un));
12141 
12142 #ifdef SDDEBUG
12143 	switch (dataspace) {
12144 	case UIO_USERSPACE:
12145 		SD_TRACE(SD_LOG_IO, un,
12146 		    "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
12147 		break;
12148 	case UIO_SYSSPACE:
12149 		SD_TRACE(SD_LOG_IO, un,
12150 		    "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
12151 		break;
12152 	default:
12153 		SD_TRACE(SD_LOG_IO, un,
12154 		    "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
12155 		break;
12156 	}
12157 #endif
12158 
12159 	rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
12160 	    SD_ADDRESS(un), &uscmd);
12161 	if (rval != 0) {
12162 		SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
12163 		    "scsi_uscsi_alloc_and_copyin failed\n", un);
12164 		return (rval);
12165 	}
12166 
12167 	if ((uscmd->uscsi_cdb != NULL) &&
12168 	    (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
12169 		mutex_enter(SD_MUTEX(un));
12170 		un->un_f_format_in_progress = TRUE;
12171 		mutex_exit(SD_MUTEX(un));
12172 		format = 1;
12173 	}
12174 
12175 	/*
12176 	 * Allocate an sd_uscsi_info struct and fill it with the info
12177 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
12178 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
12179 	 * since we allocate the buf here in this function, we do not
12180 	 * need to preserve the prior contents of b_private.
12181 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12182 	 */
12183 	uip = ssc->ssc_uscsi_info;
12184 	uip->ui_flags = path_flag;
12185 	uip->ui_cmdp = uscmd;
12186 
12187 	/*
12188 	 * Commands sent with priority are intended for error recovery
12189 	 * situations, and do not have retries performed.
12190 	 */
12191 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12192 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12193 	}
12194 	uscmd->uscsi_flags &= ~USCSI_NOINTR;
12195 
12196 	dev = SD_GET_DEV(un);
12197 	rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12198 	    sd_uscsi_strategy, NULL, uip);
12199 
12200 	/*
12201 	 * mark ssc_flags right after handle_cmd to make sure
12202 	 * the uscsi has been sent
12203 	 */
12204 	ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12205 
12206 #ifdef SDDEBUG
12207 	SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12208 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
12209 	    uscmd->uscsi_status, uscmd->uscsi_resid);
12210 	if (uscmd->uscsi_bufaddr != NULL) {
12211 		SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12212 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
12213 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12214 		if (dataspace == UIO_SYSSPACE) {
12215 			SD_DUMP_MEMORY(un, SD_LOG_IO,
12216 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
12217 			    uscmd->uscsi_buflen, SD_LOG_HEX);
12218 		}
12219 	}
12220 #endif
12221 
12222 	if (format == 1) {
12223 		mutex_enter(SD_MUTEX(un));
12224 		un->un_f_format_in_progress = FALSE;
12225 		mutex_exit(SD_MUTEX(un));
12226 	}
12227 
12228 	(void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
12229 
12230 	return (rval);
12231 }
12232 
12233 /*
12234  *     Function: sd_ssc_print
12235  *
12236  * Description: Print information available to the console.
12237  *
12238  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12239  *                    sd_uscsi_info in.
12240  *            sd_severity - log level.
12241  *     Context: Kernel thread or interrupt context.
12242  */
12243 static void
12244 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
12245 {
12246 	struct uscsi_cmd	*ucmdp;
12247 	struct scsi_device	*devp;
12248 	dev_info_t 		*devinfo;
12249 	uchar_t			*sensep;
12250 	int			senlen;
12251 	union scsi_cdb		*cdbp;
12252 	uchar_t			com;
12253 	extern struct scsi_key_strings scsi_cmds[];
12254 
12255 	ASSERT(ssc != NULL);
12256 	ASSERT(ssc->ssc_un != NULL);
12257 
12258 	if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
12259 		return;
12260 	ucmdp = ssc->ssc_uscsi_cmd;
12261 	devp = SD_SCSI_DEVP(ssc->ssc_un);
12262 	devinfo = SD_DEVINFO(ssc->ssc_un);
12263 	ASSERT(ucmdp != NULL);
12264 	ASSERT(devp != NULL);
12265 	ASSERT(devinfo != NULL);
12266 	sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
12267 	senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
12268 	cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
12269 
12270 	/* In certain case (like DOORLOCK), the cdb could be NULL. */
12271 	if (cdbp == NULL)
12272 		return;
12273 	/* We don't print log if no sense data available. */
12274 	if (senlen == 0)
12275 		sensep = NULL;
12276 	com = cdbp->scc_cmd;
12277 	scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
12278 	    scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
12279 }
12280 
12281 /*
12282  *     Function: sd_ssc_assessment
12283  *
12284  * Description: We use this function to make an assessment at the point
12285  *              where SD driver may encounter a potential error.
12286  *
12287  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12288  *                  sd_uscsi_info in.
12289  *            tp_assess - a hint of strategy for ereport posting.
12290  *            Possible values of tp_assess include:
12291  *                SD_FMT_IGNORE - we don't post any ereport because we're
12292  *                sure that it is ok to ignore the underlying problems.
12293  *                SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
12294  *                but it might be not correct to ignore the underlying hardware
12295  *                error.
12296  *                SD_FMT_STATUS_CHECK - we will post an ereport with the
12297  *                payload driver-assessment of value "fail" or
12298  *                "fatal"(depending on what information we have here). This
12299  *                assessment value is usually set when SD driver think there
12300  *                is a potential error occurred(Typically, when return value
12301  *                of the SCSI command is EIO).
12302  *                SD_FMT_STANDARD - we will post an ereport with the payload
12303  *                driver-assessment of value "info". This assessment value is
12304  *                set when the SCSI command returned successfully and with
12305  *                sense data sent back.
12306  *
12307  *     Context: Kernel thread.
12308  */
12309 static void
12310 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
12311 {
12312 	int senlen = 0;
12313 	struct uscsi_cmd *ucmdp = NULL;
12314 	struct sd_lun *un;
12315 
12316 	ASSERT(ssc != NULL);
12317 	un = ssc->ssc_un;
12318 	ASSERT(un != NULL);
12319 	ucmdp = ssc->ssc_uscsi_cmd;
12320 	ASSERT(ucmdp != NULL);
12321 
12322 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12323 		ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
12324 	} else {
12325 		/*
12326 		 * If enter here, it indicates that we have a wrong
12327 		 * calling sequence of sd_ssc_send and sd_ssc_assessment,
12328 		 * both of which should be called in a pair in case of
12329 		 * loss of FMA telemetries.
12330 		 */
12331 		if (ucmdp->uscsi_cdb != NULL) {
12332 			SD_INFO(SD_LOG_SDTEST, un,
12333 			    "sd_ssc_assessment is missing the "
12334 			    "alternative sd_ssc_send when running 0x%x, "
12335 			    "or there are superfluous sd_ssc_assessment for "
12336 			    "the same sd_ssc_send.\n",
12337 			    ucmdp->uscsi_cdb[0]);
12338 		}
12339 		/*
12340 		 * Set the ssc_flags to the initial value to avoid passing
12341 		 * down dirty flags to the following sd_ssc_send function.
12342 		 */
12343 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12344 		return;
12345 	}
12346 
12347 	/*
12348 	 * Only handle an issued command which is waiting for assessment.
12349 	 * A command which is not issued will not have
12350 	 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
12351 	 */
12352 	if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
12353 		sd_ssc_print(ssc, SCSI_ERR_INFO);
12354 		return;
12355 	} else {
12356 		/*
12357 		 * For an issued command, we should clear this flag in
12358 		 * order to make the sd_ssc_t structure be used off
12359 		 * multiple uscsi commands.
12360 		 */
12361 		ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
12362 	}
12363 
12364 	/*
12365 	 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
12366 	 * commands here. And we should clear the ssc_flags before return.
12367 	 */
12368 	if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
12369 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12370 		return;
12371 	}
12372 
12373 	switch (tp_assess) {
12374 	case SD_FMT_IGNORE:
12375 	case SD_FMT_IGNORE_COMPROMISE:
12376 		break;
12377 	case SD_FMT_STATUS_CHECK:
12378 		/*
12379 		 * For a failed command(including the succeeded command
12380 		 * with invalid data sent back).
12381 		 */
12382 		sd_ssc_post(ssc, SD_FM_DRV_FATAL);
12383 		break;
12384 	case SD_FMT_STANDARD:
12385 		/*
12386 		 * Always for the succeeded commands probably with sense
12387 		 * data sent back.
12388 		 * Limitation:
12389 		 *	We can only handle a succeeded command with sense
12390 		 *	data sent back when auto-request-sense is enabled.
12391 		 */
12392 		senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
12393 		    ssc->ssc_uscsi_cmd->uscsi_rqresid;
12394 		if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
12395 		    (un->un_f_arq_enabled == TRUE) &&
12396 		    senlen > 0 &&
12397 		    ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
12398 			sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
12399 		}
12400 		break;
12401 	default:
12402 		/*
12403 		 * Should not have other type of assessment.
12404 		 */
12405 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
12406 		    "sd_ssc_assessment got wrong "
12407 		    "sd_type_assessment %d.\n", tp_assess);
12408 		break;
12409 	}
12410 	/*
12411 	 * Clear up the ssc_flags before return.
12412 	 */
12413 	ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12414 }
12415 
12416 /*
12417  *    Function: sd_ssc_post
12418  *
12419  * Description: 1. read the driver property to get fm-scsi-log flag.
12420  *              2. print log if fm_log_capable is non-zero.
12421  *              3. call sd_ssc_ereport_post to post ereport if possible.
12422  *
12423  *    Context: May be called from kernel thread or interrupt context.
12424  */
12425 static void
12426 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
12427 {
12428 	struct sd_lun	*un;
12429 	int		sd_severity;
12430 
12431 	ASSERT(ssc != NULL);
12432 	un = ssc->ssc_un;
12433 	ASSERT(un != NULL);
12434 
12435 	/*
12436 	 * We may enter here from sd_ssc_assessment(for USCSI command) or
12437 	 * by directly called from sdintr context.
12438 	 * We don't handle a non-disk drive(CD-ROM, removable media).
12439 	 * Clear the ssc_flags before return in case we've set
12440 	 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12441 	 * driver.
12442 	 */
12443 	if (ISCD(un) || un->un_f_has_removable_media) {
12444 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12445 		return;
12446 	}
12447 
12448 	switch (sd_assess) {
12449 		case SD_FM_DRV_FATAL:
12450 			sd_severity = SCSI_ERR_FATAL;
12451 			break;
12452 		case SD_FM_DRV_RECOVERY:
12453 			sd_severity = SCSI_ERR_RECOVERED;
12454 			break;
12455 		case SD_FM_DRV_RETRY:
12456 			sd_severity = SCSI_ERR_RETRYABLE;
12457 			break;
12458 		case SD_FM_DRV_NOTICE:
12459 			sd_severity = SCSI_ERR_INFO;
12460 			break;
12461 		default:
12462 			sd_severity = SCSI_ERR_UNKNOWN;
12463 	}
12464 	/* print log */
12465 	sd_ssc_print(ssc, sd_severity);
12466 
12467 	/* always post ereport */
12468 	sd_ssc_ereport_post(ssc, sd_assess);
12469 }
12470 
12471 /*
12472  *    Function: sd_ssc_set_info
12473  *
12474  * Description: Mark ssc_flags and set ssc_info which would be the
12475  *              payload of uderr ereport. This function will cause
12476  *              sd_ssc_ereport_post to post uderr ereport only.
12477  *              Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12478  *              the function will also call SD_ERROR or scsi_log for a
12479  *              CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12480  *
12481  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12482  *                  sd_uscsi_info in.
12483  *            ssc_flags - indicate the sub-category of a uderr.
12484  *            comp - this argument is meaningful only when
12485  *                   ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12486  *                   values include:
12487  *                   > 0, SD_ERROR is used with comp as the driver logging
12488  *                   component;
12489  *                   = 0, scsi-log is used to log error telemetries;
12490  *                   < 0, no log available for this telemetry.
12491  *
12492  *    Context: Kernel thread or interrupt context
12493  */
12494 static void
12495 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12496 {
12497 	va_list	ap;
12498 
12499 	ASSERT(ssc != NULL);
12500 	ASSERT(ssc->ssc_un != NULL);
12501 
12502 	ssc->ssc_flags |= ssc_flags;
12503 	va_start(ap, fmt);
12504 	(void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12505 	va_end(ap);
12506 
12507 	/*
12508 	 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12509 	 * with invalid data sent back. For non-uscsi command, the
12510 	 * following code will be bypassed.
12511 	 */
12512 	if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12513 		if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12514 			/*
12515 			 * If the error belong to certain component and we
12516 			 * do not want it to show up on the console, we
12517 			 * will use SD_ERROR, otherwise scsi_log is
12518 			 * preferred.
12519 			 */
12520 			if (comp > 0) {
12521 				SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12522 			} else if (comp == 0) {
12523 				scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12524 				    CE_WARN, ssc->ssc_info);
12525 			}
12526 		}
12527 	}
12528 }
12529 
12530 /*
12531  *    Function: sd_buf_iodone
12532  *
12533  * Description: Frees the sd_xbuf & returns the buf to its originator.
12534  *
12535  *     Context: May be called from interrupt context.
12536  */
12537 /* ARGSUSED */
12538 static void
12539 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12540 {
12541 	struct sd_xbuf *xp;
12542 
12543 	ASSERT(un != NULL);
12544 	ASSERT(bp != NULL);
12545 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12546 
12547 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12548 
12549 	xp = SD_GET_XBUF(bp);
12550 	ASSERT(xp != NULL);
12551 
12552 	/* xbuf is gone after this */
12553 	if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12554 		mutex_enter(SD_MUTEX(un));
12555 
12556 		/*
12557 		 * Grab time when the cmd completed.
12558 		 * This is used for determining if the system has been
12559 		 * idle long enough to make it idle to the PM framework.
12560 		 * This is for lowering the overhead, and therefore improving
12561 		 * performance per I/O operation.
12562 		 */
12563 		un->un_pm_idle_time = gethrtime();
12564 
12565 		un->un_ncmds_in_driver--;
12566 		ASSERT(un->un_ncmds_in_driver >= 0);
12567 		SD_INFO(SD_LOG_IO, un,
12568 		    "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12569 		    un->un_ncmds_in_driver);
12570 
12571 		mutex_exit(SD_MUTEX(un));
12572 	}
12573 
12574 	biodone(bp);				/* bp is gone after this */
12575 
12576 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12577 }
12578 
12579 
12580 /*
12581  *    Function: sd_uscsi_iodone
12582  *
12583  * Description: Frees the sd_xbuf & returns the buf to its originator.
12584  *
12585  *     Context: May be called from interrupt context.
12586  */
12587 /* ARGSUSED */
12588 static void
12589 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12590 {
12591 	struct sd_xbuf *xp;
12592 
12593 	ASSERT(un != NULL);
12594 	ASSERT(bp != NULL);
12595 
12596 	xp = SD_GET_XBUF(bp);
12597 	ASSERT(xp != NULL);
12598 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12599 
12600 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12601 
12602 	bp->b_private = xp->xb_private;
12603 
12604 	mutex_enter(SD_MUTEX(un));
12605 
12606 	/*
12607 	 * Grab time when the cmd completed.
12608 	 * This is used for determining if the system has been
12609 	 * idle long enough to make it idle to the PM framework.
12610 	 * This is for lowering the overhead, and therefore improving
12611 	 * performance per I/O operation.
12612 	 */
12613 	un->un_pm_idle_time = gethrtime();
12614 
12615 	un->un_ncmds_in_driver--;
12616 	ASSERT(un->un_ncmds_in_driver >= 0);
12617 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12618 	    un->un_ncmds_in_driver);
12619 
12620 	mutex_exit(SD_MUTEX(un));
12621 
12622 	if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12623 	    SENSE_LENGTH) {
12624 		kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12625 		    MAX_SENSE_LENGTH);
12626 	} else {
12627 		kmem_free(xp, sizeof (struct sd_xbuf));
12628 	}
12629 
12630 	biodone(bp);
12631 
12632 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12633 }
12634 
12635 
12636 /*
12637  *    Function: sd_mapblockaddr_iostart
12638  *
12639  * Description: Verify request lies within the partition limits for
12640  *		the indicated minor device.  Issue "overrun" buf if
12641  *		request would exceed partition range.  Converts
12642  *		partition-relative block address to absolute.
12643  *
12644  *              Upon exit of this function:
12645  *              1.I/O is aligned
12646  *                 xp->xb_blkno represents the absolute sector address
12647  *              2.I/O is misaligned
12648  *                 xp->xb_blkno represents the absolute logical block address
12649  *                 based on DEV_BSIZE. The logical block address will be
12650  *                 converted to physical sector address in sd_mapblocksize_\
12651  *                 iostart.
12652  *              3.I/O is misaligned but is aligned in "overrun" buf
12653  *                 xp->xb_blkno represents the absolute logical block address
12654  *                 based on DEV_BSIZE. The logical block address will be
12655  *                 converted to physical sector address in sd_mapblocksize_\
12656  *                 iostart. But no RMW will be issued in this case.
12657  *
12658  *     Context: Can sleep
12659  *
12660  *      Issues: This follows what the old code did, in terms of accessing
12661  *		some of the partition info in the unit struct without holding
12662  *		the mutext.  This is a general issue, if the partition info
12663  *		can be altered while IO is in progress... as soon as we send
12664  *		a buf, its partitioning can be invalid before it gets to the
12665  *		device.  Probably the right fix is to move partitioning out
12666  *		of the driver entirely.
12667  */
12668 
12669 static void
12670 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12671 {
12672 	diskaddr_t	nblocks;	/* #blocks in the given partition */
12673 	daddr_t	blocknum;	/* Block number specified by the buf */
12674 	size_t	requested_nblocks;
12675 	size_t	available_nblocks;
12676 	int	partition;
12677 	diskaddr_t	partition_offset;
12678 	struct sd_xbuf *xp;
12679 	int secmask = 0, blknomask = 0;
12680 	ushort_t is_aligned = TRUE;
12681 
12682 	ASSERT(un != NULL);
12683 	ASSERT(bp != NULL);
12684 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12685 
12686 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12687 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12688 
12689 	xp = SD_GET_XBUF(bp);
12690 	ASSERT(xp != NULL);
12691 
12692 	/*
12693 	 * If the geometry is not indicated as valid, attempt to access
12694 	 * the unit & verify the geometry/label. This can be the case for
12695 	 * removable-media devices, of if the device was opened in
12696 	 * NDELAY/NONBLOCK mode.
12697 	 */
12698 	partition = SDPART(bp->b_edev);
12699 
12700 	if (!SD_IS_VALID_LABEL(un)) {
12701 		sd_ssc_t *ssc;
12702 		/*
12703 		 * Initialize sd_ssc_t for internal uscsi commands
12704 		 * In case of potential porformance issue, we need
12705 		 * to alloc memory only if there is invalid label
12706 		 */
12707 		ssc = sd_ssc_init(un);
12708 
12709 		if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12710 			/*
12711 			 * For removable devices it is possible to start an
12712 			 * I/O without a media by opening the device in nodelay
12713 			 * mode. Also for writable CDs there can be many
12714 			 * scenarios where there is no geometry yet but volume
12715 			 * manager is trying to issue a read() just because
12716 			 * it can see TOC on the CD. So do not print a message
12717 			 * for removables.
12718 			 */
12719 			if (!un->un_f_has_removable_media) {
12720 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12721 				    "i/o to invalid geometry\n");
12722 			}
12723 			bioerror(bp, EIO);
12724 			bp->b_resid = bp->b_bcount;
12725 			SD_BEGIN_IODONE(index, un, bp);
12726 
12727 			sd_ssc_fini(ssc);
12728 			return;
12729 		}
12730 		sd_ssc_fini(ssc);
12731 	}
12732 
12733 	nblocks = 0;
12734 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
12735 	    &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12736 
12737 	if (un->un_f_enable_rmw) {
12738 		blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12739 		secmask = un->un_phy_blocksize - 1;
12740 	} else {
12741 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12742 		secmask = un->un_tgt_blocksize - 1;
12743 	}
12744 
12745 	if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12746 		is_aligned = FALSE;
12747 	}
12748 
12749 	if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12750 		/*
12751 		 * If I/O is aligned, no need to involve RMW(Read Modify Write)
12752 		 * Convert the logical block number to target's physical sector
12753 		 * number.
12754 		 */
12755 		if (is_aligned) {
12756 			xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12757 		} else {
12758 			/*
12759 			 * There is no RMW if we're just reading, so don't
12760 			 * warn or error out because of it.
12761 			 */
12762 			if (bp->b_flags & B_READ) {
12763 				/*EMPTY*/
12764 			} else if (!un->un_f_enable_rmw &&
12765 			    un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) {
12766 				bp->b_flags |= B_ERROR;
12767 				goto error_exit;
12768 			} else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) {
12769 				mutex_enter(SD_MUTEX(un));
12770 				if (!un->un_f_enable_rmw &&
12771 				    un->un_rmw_msg_timeid == NULL) {
12772 					scsi_log(SD_DEVINFO(un), sd_label,
12773 					    CE_WARN, "I/O request is not "
12774 					    "aligned with %d disk sector size. "
12775 					    "It is handled through Read Modify "
12776 					    "Write but the performance is "
12777 					    "very low.\n",
12778 					    un->un_tgt_blocksize);
12779 					un->un_rmw_msg_timeid =
12780 					    timeout(sd_rmw_msg_print_handler,
12781 					    un, SD_RMW_MSG_PRINT_TIMEOUT);
12782 				} else {
12783 					un->un_rmw_incre_count ++;
12784 				}
12785 				mutex_exit(SD_MUTEX(un));
12786 			}
12787 
12788 			nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12789 			partition_offset = SD_TGT2SYSBLOCK(un,
12790 			    partition_offset);
12791 		}
12792 	}
12793 
12794 	/*
12795 	 * blocknum is the starting block number of the request. At this
12796 	 * point it is still relative to the start of the minor device.
12797 	 */
12798 	blocknum = xp->xb_blkno;
12799 
12800 	/*
12801 	 * Legacy: If the starting block number is one past the last block
12802 	 * in the partition, do not set B_ERROR in the buf.
12803 	 */
12804 	if (blocknum == nblocks)  {
12805 		goto error_exit;
12806 	}
12807 
12808 	/*
12809 	 * Confirm that the first block of the request lies within the
12810 	 * partition limits. Also the requested number of bytes must be
12811 	 * a multiple of the system block size.
12812 	 */
12813 	if ((blocknum < 0) || (blocknum >= nblocks) ||
12814 	    ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12815 		bp->b_flags |= B_ERROR;
12816 		goto error_exit;
12817 	}
12818 
12819 	/*
12820 	 * If the requsted # blocks exceeds the available # blocks, that
12821 	 * is an overrun of the partition.
12822 	 */
12823 	if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12824 		requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12825 	} else {
12826 		requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12827 	}
12828 
12829 	available_nblocks = (size_t)(nblocks - blocknum);
12830 	ASSERT(nblocks >= blocknum);
12831 
12832 	if (requested_nblocks > available_nblocks) {
12833 		size_t resid;
12834 
12835 		/*
12836 		 * Allocate an "overrun" buf to allow the request to proceed
12837 		 * for the amount of space available in the partition. The
12838 		 * amount not transferred will be added into the b_resid
12839 		 * when the operation is complete. The overrun buf
12840 		 * replaces the original buf here, and the original buf
12841 		 * is saved inside the overrun buf, for later use.
12842 		 */
12843 		if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12844 			resid = SD_TGTBLOCKS2BYTES(un,
12845 			    (offset_t)(requested_nblocks - available_nblocks));
12846 		} else {
12847 			resid = SD_SYSBLOCKS2BYTES(
12848 			    (offset_t)(requested_nblocks - available_nblocks));
12849 		}
12850 
12851 		size_t count = bp->b_bcount - resid;
12852 		/*
12853 		 * Note: count is an unsigned entity thus it'll NEVER
12854 		 * be less than 0 so ASSERT the original values are
12855 		 * correct.
12856 		 */
12857 		ASSERT(bp->b_bcount >= resid);
12858 
12859 		bp = sd_bioclone_alloc(bp, count, blocknum,
12860 		    (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12861 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12862 		ASSERT(xp != NULL);
12863 	}
12864 
12865 	/* At this point there should be no residual for this buf. */
12866 	ASSERT(bp->b_resid == 0);
12867 
12868 	/* Convert the block number to an absolute address. */
12869 	xp->xb_blkno += partition_offset;
12870 
12871 	SD_NEXT_IOSTART(index, un, bp);
12872 
12873 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12874 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12875 
12876 	return;
12877 
12878 error_exit:
12879 	bp->b_resid = bp->b_bcount;
12880 	SD_BEGIN_IODONE(index, un, bp);
12881 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12882 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12883 }
12884 
12885 
12886 /*
12887  *    Function: sd_mapblockaddr_iodone
12888  *
12889  * Description: Completion-side processing for partition management.
12890  *
12891  *     Context: May be called under interrupt context
12892  */
12893 
12894 static void
12895 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12896 {
12897 	/* int	partition; */	/* Not used, see below. */
12898 	ASSERT(un != NULL);
12899 	ASSERT(bp != NULL);
12900 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12901 
12902 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12903 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12904 
12905 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12906 		/*
12907 		 * We have an "overrun" buf to deal with...
12908 		 */
12909 		struct sd_xbuf	*xp;
12910 		struct buf	*obp;	/* ptr to the original buf */
12911 
12912 		xp = SD_GET_XBUF(bp);
12913 		ASSERT(xp != NULL);
12914 
12915 		/* Retrieve the pointer to the original buf */
12916 		obp = (struct buf *)xp->xb_private;
12917 		ASSERT(obp != NULL);
12918 
12919 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12920 		bioerror(obp, bp->b_error);
12921 
12922 		sd_bioclone_free(bp);
12923 
12924 		/*
12925 		 * Get back the original buf.
12926 		 * Note that since the restoration of xb_blkno below
12927 		 * was removed, the sd_xbuf is not needed.
12928 		 */
12929 		bp = obp;
12930 		/*
12931 		 * xp = SD_GET_XBUF(bp);
12932 		 * ASSERT(xp != NULL);
12933 		 */
12934 	}
12935 
12936 	/*
12937 	 * Convert sd->xb_blkno back to a minor-device relative value.
12938 	 * Note: this has been commented out, as it is not needed in the
12939 	 * current implementation of the driver (ie, since this function
12940 	 * is at the top of the layering chains, so the info will be
12941 	 * discarded) and it is in the "hot" IO path.
12942 	 *
12943 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
12944 	 * xp->xb_blkno -= un->un_offset[partition];
12945 	 */
12946 
12947 	SD_NEXT_IODONE(index, un, bp);
12948 
12949 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12950 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12951 }
12952 
12953 
12954 /*
12955  *    Function: sd_mapblocksize_iostart
12956  *
12957  * Description: Convert between system block size (un->un_sys_blocksize)
12958  *		and target block size (un->un_tgt_blocksize).
12959  *
12960  *     Context: Can sleep to allocate resources.
12961  *
12962  * Assumptions: A higher layer has already performed any partition validation,
12963  *		and converted the xp->xb_blkno to an absolute value relative
12964  *		to the start of the device.
12965  *
12966  *		It is also assumed that the higher layer has implemented
12967  *		an "overrun" mechanism for the case where the request would
12968  *		read/write beyond the end of a partition.  In this case we
12969  *		assume (and ASSERT) that bp->b_resid == 0.
12970  *
12971  *		Note: The implementation for this routine assumes the target
12972  *		block size remains constant between allocation and transport.
12973  */
12974 
12975 static void
12976 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12977 {
12978 	struct sd_mapblocksize_info	*bsp;
12979 	struct sd_xbuf			*xp;
12980 	offset_t first_byte;
12981 	daddr_t	start_block, end_block;
12982 	daddr_t	request_bytes;
12983 	ushort_t is_aligned = FALSE;
12984 
12985 	ASSERT(un != NULL);
12986 	ASSERT(bp != NULL);
12987 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12988 	ASSERT(bp->b_resid == 0);
12989 
12990 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12991 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12992 
12993 	/*
12994 	 * For a non-writable CD, a write request is an error
12995 	 */
12996 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12997 	    (un->un_f_mmc_writable_media == FALSE)) {
12998 		bioerror(bp, EIO);
12999 		bp->b_resid = bp->b_bcount;
13000 		SD_BEGIN_IODONE(index, un, bp);
13001 		return;
13002 	}
13003 
13004 	/*
13005 	 * We do not need a shadow buf if the device is using
13006 	 * un->un_sys_blocksize as its block size or if bcount == 0.
13007 	 * In this case there is no layer-private data block allocated.
13008 	 */
13009 	if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
13010 	    (bp->b_bcount == 0)) {
13011 		goto done;
13012 	}
13013 
13014 #if defined(__i386) || defined(__amd64)
13015 	/* We do not support non-block-aligned transfers for ROD devices */
13016 	ASSERT(!ISROD(un));
13017 #endif
13018 
13019 	xp = SD_GET_XBUF(bp);
13020 	ASSERT(xp != NULL);
13021 
13022 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
13023 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
13024 	    un->un_tgt_blocksize, DEV_BSIZE);
13025 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
13026 	    "request start block:0x%x\n", xp->xb_blkno);
13027 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
13028 	    "request len:0x%x\n", bp->b_bcount);
13029 
13030 	/*
13031 	 * Allocate the layer-private data area for the mapblocksize layer.
13032 	 * Layers are allowed to use the xp_private member of the sd_xbuf
13033 	 * struct to store the pointer to their layer-private data block, but
13034 	 * each layer also has the responsibility of restoring the prior
13035 	 * contents of xb_private before returning the buf/xbuf to the
13036 	 * higher layer that sent it.
13037 	 *
13038 	 * Here we save the prior contents of xp->xb_private into the
13039 	 * bsp->mbs_oprivate field of our layer-private data area. This value
13040 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
13041 	 * the layer-private area and returning the buf/xbuf to the layer
13042 	 * that sent it.
13043 	 *
13044 	 * Note that here we use kmem_zalloc for the allocation as there are
13045 	 * parts of the mapblocksize code that expect certain fields to be
13046 	 * zero unless explicitly set to a required value.
13047 	 */
13048 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
13049 	bsp->mbs_oprivate = xp->xb_private;
13050 	xp->xb_private = bsp;
13051 
13052 	/*
13053 	 * This treats the data on the disk (target) as an array of bytes.
13054 	 * first_byte is the byte offset, from the beginning of the device,
13055 	 * to the location of the request. This is converted from a
13056 	 * un->un_sys_blocksize block address to a byte offset, and then back
13057 	 * to a block address based upon a un->un_tgt_blocksize block size.
13058 	 *
13059 	 * xp->xb_blkno should be absolute upon entry into this function,
13060 	 * but, but it is based upon partitions that use the "system"
13061 	 * block size. It must be adjusted to reflect the block size of
13062 	 * the target.
13063 	 *
13064 	 * Note that end_block is actually the block that follows the last
13065 	 * block of the request, but that's what is needed for the computation.
13066 	 */
13067 	first_byte  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
13068 	if (un->un_f_enable_rmw) {
13069 		start_block = xp->xb_blkno =
13070 		    (first_byte / un->un_phy_blocksize) *
13071 		    (un->un_phy_blocksize / DEV_BSIZE);
13072 		end_block   = ((first_byte + bp->b_bcount +
13073 		    un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
13074 		    (un->un_phy_blocksize / DEV_BSIZE);
13075 	} else {
13076 		start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
13077 		end_block   = (first_byte + bp->b_bcount +
13078 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
13079 	}
13080 
13081 	/* request_bytes is rounded up to a multiple of the target block size */
13082 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
13083 
13084 	/*
13085 	 * See if the starting address of the request and the request
13086 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
13087 	 * then we do not need to allocate a shadow buf to handle the request.
13088 	 */
13089 	if (un->un_f_enable_rmw) {
13090 		if (((first_byte % un->un_phy_blocksize) == 0) &&
13091 		    ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
13092 			is_aligned = TRUE;
13093 		}
13094 	} else {
13095 		if (((first_byte % un->un_tgt_blocksize) == 0) &&
13096 		    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
13097 			is_aligned = TRUE;
13098 		}
13099 	}
13100 
13101 	if ((bp->b_flags & B_READ) == 0) {
13102 		/*
13103 		 * Lock the range for a write operation. An aligned request is
13104 		 * considered a simple write; otherwise the request must be a
13105 		 * read-modify-write.
13106 		 */
13107 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
13108 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
13109 	}
13110 
13111 	/*
13112 	 * Alloc a shadow buf if the request is not aligned. Also, this is
13113 	 * where the READ command is generated for a read-modify-write. (The
13114 	 * write phase is deferred until after the read completes.)
13115 	 */
13116 	if (is_aligned == FALSE) {
13117 
13118 		struct sd_mapblocksize_info	*shadow_bsp;
13119 		struct sd_xbuf	*shadow_xp;
13120 		struct buf	*shadow_bp;
13121 
13122 		/*
13123 		 * Allocate the shadow buf and it associated xbuf. Note that
13124 		 * after this call the xb_blkno value in both the original
13125 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
13126 		 * same: absolute relative to the start of the device, and
13127 		 * adjusted for the target block size. The b_blkno in the
13128 		 * shadow buf will also be set to this value. We should never
13129 		 * change b_blkno in the original bp however.
13130 		 *
13131 		 * Note also that the shadow buf will always need to be a
13132 		 * READ command, regardless of whether the incoming command
13133 		 * is a READ or a WRITE.
13134 		 */
13135 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
13136 		    xp->xb_blkno,
13137 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
13138 
13139 		shadow_xp = SD_GET_XBUF(shadow_bp);
13140 
13141 		/*
13142 		 * Allocate the layer-private data for the shadow buf.
13143 		 * (No need to preserve xb_private in the shadow xbuf.)
13144 		 */
13145 		shadow_xp->xb_private = shadow_bsp =
13146 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
13147 
13148 		/*
13149 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
13150 		 * to figure out where the start of the user data is (based upon
13151 		 * the system block size) in the data returned by the READ
13152 		 * command (which will be based upon the target blocksize). Note
13153 		 * that this is only really used if the request is unaligned.
13154 		 */
13155 		if (un->un_f_enable_rmw) {
13156 			bsp->mbs_copy_offset = (ssize_t)(first_byte -
13157 			    ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
13158 			ASSERT((bsp->mbs_copy_offset >= 0) &&
13159 			    (bsp->mbs_copy_offset < un->un_phy_blocksize));
13160 		} else {
13161 			bsp->mbs_copy_offset = (ssize_t)(first_byte -
13162 			    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
13163 			ASSERT((bsp->mbs_copy_offset >= 0) &&
13164 			    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
13165 		}
13166 
13167 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
13168 
13169 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
13170 
13171 		/* Transfer the wmap (if any) to the shadow buf */
13172 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
13173 		bsp->mbs_wmp = NULL;
13174 
13175 		/*
13176 		 * The shadow buf goes on from here in place of the
13177 		 * original buf.
13178 		 */
13179 		shadow_bsp->mbs_orig_bp = bp;
13180 		bp = shadow_bp;
13181 	}
13182 
13183 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13184 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
13185 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13186 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
13187 	    request_bytes);
13188 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13189 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
13190 
13191 done:
13192 	SD_NEXT_IOSTART(index, un, bp);
13193 
13194 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13195 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
13196 }
13197 
13198 
13199 /*
13200  *    Function: sd_mapblocksize_iodone
13201  *
13202  * Description: Completion side processing for block-size mapping.
13203  *
13204  *     Context: May be called under interrupt context
13205  */
13206 
13207 static void
13208 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
13209 {
13210 	struct sd_mapblocksize_info	*bsp;
13211 	struct sd_xbuf	*xp;
13212 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
13213 	struct buf	*orig_bp;	/* ptr to the original buf */
13214 	offset_t	shadow_end;
13215 	offset_t	request_end;
13216 	offset_t	shadow_start;
13217 	ssize_t		copy_offset;
13218 	size_t		copy_length;
13219 	size_t		shortfall;
13220 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
13221 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
13222 
13223 	ASSERT(un != NULL);
13224 	ASSERT(bp != NULL);
13225 
13226 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13227 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13228 
13229 	/*
13230 	 * There is no shadow buf or layer-private data if the target is
13231 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
13232 	 */
13233 	if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
13234 	    (bp->b_bcount == 0)) {
13235 		goto exit;
13236 	}
13237 
13238 	xp = SD_GET_XBUF(bp);
13239 	ASSERT(xp != NULL);
13240 
13241 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
13242 	bsp = xp->xb_private;
13243 
13244 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13245 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13246 
13247 	if (is_write) {
13248 		/*
13249 		 * For a WRITE request we must free up the block range that
13250 		 * we have locked up.  This holds regardless of whether this is
13251 		 * an aligned write request or a read-modify-write request.
13252 		 */
13253 		sd_range_unlock(un, bsp->mbs_wmp);
13254 		bsp->mbs_wmp = NULL;
13255 	}
13256 
13257 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13258 		/*
13259 		 * An aligned read or write command will have no shadow buf;
13260 		 * there is not much else to do with it.
13261 		 */
13262 		goto done;
13263 	}
13264 
13265 	orig_bp = bsp->mbs_orig_bp;
13266 	ASSERT(orig_bp != NULL);
13267 	orig_xp = SD_GET_XBUF(orig_bp);
13268 	ASSERT(orig_xp != NULL);
13269 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13270 
13271 	if (!is_write && has_wmap) {
13272 		/*
13273 		 * A READ with a wmap means this is the READ phase of a
13274 		 * read-modify-write. If an error occurred on the READ then
13275 		 * we do not proceed with the WRITE phase or copy any data.
13276 		 * Just release the write maps and return with an error.
13277 		 */
13278 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13279 			orig_bp->b_resid = orig_bp->b_bcount;
13280 			bioerror(orig_bp, bp->b_error);
13281 			sd_range_unlock(un, bsp->mbs_wmp);
13282 			goto freebuf_done;
13283 		}
13284 	}
13285 
13286 	/*
13287 	 * Here is where we set up to copy the data from the shadow buf
13288 	 * into the space associated with the original buf.
13289 	 *
13290 	 * To deal with the conversion between block sizes, these
13291 	 * computations treat the data as an array of bytes, with the
13292 	 * first byte (byte 0) corresponding to the first byte in the
13293 	 * first block on the disk.
13294 	 */
13295 
13296 	/*
13297 	 * shadow_start and shadow_len indicate the location and size of
13298 	 * the data returned with the shadow IO request.
13299 	 */
13300 	if (un->un_f_enable_rmw) {
13301 		shadow_start  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
13302 	} else {
13303 		shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13304 	}
13305 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
13306 
13307 	/*
13308 	 * copy_offset gives the offset (in bytes) from the start of the first
13309 	 * block of the READ request to the beginning of the data.  We retrieve
13310 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13311 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
13312 	 * data to be copied (in bytes).
13313 	 */
13314 	copy_offset  = bsp->mbs_copy_offset;
13315 	if (un->un_f_enable_rmw) {
13316 		ASSERT((copy_offset >= 0) &&
13317 		    (copy_offset < un->un_phy_blocksize));
13318 	} else {
13319 		ASSERT((copy_offset >= 0) &&
13320 		    (copy_offset < un->un_tgt_blocksize));
13321 	}
13322 
13323 	copy_length  = orig_bp->b_bcount;
13324 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
13325 
13326 	/*
13327 	 * Set up the resid and error fields of orig_bp as appropriate.
13328 	 */
13329 	if (shadow_end >= request_end) {
13330 		/* We got all the requested data; set resid to zero */
13331 		orig_bp->b_resid = 0;
13332 	} else {
13333 		/*
13334 		 * We failed to get enough data to fully satisfy the original
13335 		 * request. Just copy back whatever data we got and set
13336 		 * up the residual and error code as required.
13337 		 *
13338 		 * 'shortfall' is the amount by which the data received with the
13339 		 * shadow buf has "fallen short" of the requested amount.
13340 		 */
13341 		shortfall = (size_t)(request_end - shadow_end);
13342 
13343 		if (shortfall > orig_bp->b_bcount) {
13344 			/*
13345 			 * We did not get enough data to even partially
13346 			 * fulfill the original request.  The residual is
13347 			 * equal to the amount requested.
13348 			 */
13349 			orig_bp->b_resid = orig_bp->b_bcount;
13350 		} else {
13351 			/*
13352 			 * We did not get all the data that we requested
13353 			 * from the device, but we will try to return what
13354 			 * portion we did get.
13355 			 */
13356 			orig_bp->b_resid = shortfall;
13357 		}
13358 		ASSERT(copy_length >= orig_bp->b_resid);
13359 		copy_length  -= orig_bp->b_resid;
13360 	}
13361 
13362 	/* Propagate the error code from the shadow buf to the original buf */
13363 	bioerror(orig_bp, bp->b_error);
13364 
13365 	if (is_write) {
13366 		goto freebuf_done;	/* No data copying for a WRITE */
13367 	}
13368 
13369 	if (has_wmap) {
13370 		/*
13371 		 * This is a READ command from the READ phase of a
13372 		 * read-modify-write request. We have to copy the data given
13373 		 * by the user OVER the data returned by the READ command,
13374 		 * then convert the command from a READ to a WRITE and send
13375 		 * it back to the target.
13376 		 */
13377 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13378 		    copy_length);
13379 
13380 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
13381 
13382 		/*
13383 		 * Dispatch the WRITE command to the taskq thread, which
13384 		 * will in turn send the command to the target. When the
13385 		 * WRITE command completes, we (sd_mapblocksize_iodone())
13386 		 * will get called again as part of the iodone chain
13387 		 * processing for it. Note that we will still be dealing
13388 		 * with the shadow buf at that point.
13389 		 */
13390 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13391 		    KM_NOSLEEP) != 0) {
13392 			/*
13393 			 * Dispatch was successful so we are done. Return
13394 			 * without going any higher up the iodone chain. Do
13395 			 * not free up any layer-private data until after the
13396 			 * WRITE completes.
13397 			 */
13398 			return;
13399 		}
13400 
13401 		/*
13402 		 * Dispatch of the WRITE command failed; set up the error
13403 		 * condition and send this IO back up the iodone chain.
13404 		 */
13405 		bioerror(orig_bp, EIO);
13406 		orig_bp->b_resid = orig_bp->b_bcount;
13407 
13408 	} else {
13409 		/*
13410 		 * This is a regular READ request (ie, not a RMW). Copy the
13411 		 * data from the shadow buf into the original buf. The
13412 		 * copy_offset compensates for any "misalignment" between the
13413 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
13414 		 * original buf (with its un->un_sys_blocksize blocks).
13415 		 */
13416 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13417 		    copy_length);
13418 	}
13419 
13420 freebuf_done:
13421 
13422 	/*
13423 	 * At this point we still have both the shadow buf AND the original
13424 	 * buf to deal with, as well as the layer-private data area in each.
13425 	 * Local variables are as follows:
13426 	 *
13427 	 * bp -- points to shadow buf
13428 	 * xp -- points to xbuf of shadow buf
13429 	 * bsp -- points to layer-private data area of shadow buf
13430 	 * orig_bp -- points to original buf
13431 	 *
13432 	 * First free the shadow buf and its associated xbuf, then free the
13433 	 * layer-private data area from the shadow buf. There is no need to
13434 	 * restore xb_private in the shadow xbuf.
13435 	 */
13436 	sd_shadow_buf_free(bp);
13437 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13438 
13439 	/*
13440 	 * Now update the local variables to point to the original buf, xbuf,
13441 	 * and layer-private area.
13442 	 */
13443 	bp = orig_bp;
13444 	xp = SD_GET_XBUF(bp);
13445 	ASSERT(xp != NULL);
13446 	ASSERT(xp == orig_xp);
13447 	bsp = xp->xb_private;
13448 	ASSERT(bsp != NULL);
13449 
13450 done:
13451 	/*
13452 	 * Restore xb_private to whatever it was set to by the next higher
13453 	 * layer in the chain, then free the layer-private data area.
13454 	 */
13455 	xp->xb_private = bsp->mbs_oprivate;
13456 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13457 
13458 exit:
13459 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13460 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13461 
13462 	SD_NEXT_IODONE(index, un, bp);
13463 }
13464 
13465 
13466 /*
13467  *    Function: sd_checksum_iostart
13468  *
13469  * Description: A stub function for a layer that's currently not used.
13470  *		For now just a placeholder.
13471  *
13472  *     Context: Kernel thread context
13473  */
13474 
13475 static void
13476 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13477 {
13478 	ASSERT(un != NULL);
13479 	ASSERT(bp != NULL);
13480 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13481 	SD_NEXT_IOSTART(index, un, bp);
13482 }
13483 
13484 
13485 /*
13486  *    Function: sd_checksum_iodone
13487  *
13488  * Description: A stub function for a layer that's currently not used.
13489  *		For now just a placeholder.
13490  *
13491  *     Context: May be called under interrupt context
13492  */
13493 
13494 static void
13495 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13496 {
13497 	ASSERT(un != NULL);
13498 	ASSERT(bp != NULL);
13499 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13500 	SD_NEXT_IODONE(index, un, bp);
13501 }
13502 
13503 
13504 /*
13505  *    Function: sd_checksum_uscsi_iostart
13506  *
13507  * Description: A stub function for a layer that's currently not used.
13508  *		For now just a placeholder.
13509  *
13510  *     Context: Kernel thread context
13511  */
13512 
13513 static void
13514 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13515 {
13516 	ASSERT(un != NULL);
13517 	ASSERT(bp != NULL);
13518 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13519 	SD_NEXT_IOSTART(index, un, bp);
13520 }
13521 
13522 
13523 /*
13524  *    Function: sd_checksum_uscsi_iodone
13525  *
13526  * Description: A stub function for a layer that's currently not used.
13527  *		For now just a placeholder.
13528  *
13529  *     Context: May be called under interrupt context
13530  */
13531 
13532 static void
13533 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13534 {
13535 	ASSERT(un != NULL);
13536 	ASSERT(bp != NULL);
13537 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13538 	SD_NEXT_IODONE(index, un, bp);
13539 }
13540 
13541 
13542 /*
13543  *    Function: sd_pm_iostart
13544  *
13545  * Description: iostart-side routine for Power mangement.
13546  *
13547  *     Context: Kernel thread context
13548  */
13549 
13550 static void
13551 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13552 {
13553 	ASSERT(un != NULL);
13554 	ASSERT(bp != NULL);
13555 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13556 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13557 
13558 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13559 
13560 	if (sd_pm_entry(un) != DDI_SUCCESS) {
13561 		/*
13562 		 * Set up to return the failed buf back up the 'iodone'
13563 		 * side of the calling chain.
13564 		 */
13565 		bioerror(bp, EIO);
13566 		bp->b_resid = bp->b_bcount;
13567 
13568 		SD_BEGIN_IODONE(index, un, bp);
13569 
13570 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13571 		return;
13572 	}
13573 
13574 	SD_NEXT_IOSTART(index, un, bp);
13575 
13576 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13577 }
13578 
13579 
13580 /*
13581  *    Function: sd_pm_iodone
13582  *
13583  * Description: iodone-side routine for power mangement.
13584  *
13585  *     Context: may be called from interrupt context
13586  */
13587 
13588 static void
13589 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13590 {
13591 	ASSERT(un != NULL);
13592 	ASSERT(bp != NULL);
13593 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13594 
13595 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13596 
13597 	/*
13598 	 * After attach the following flag is only read, so don't
13599 	 * take the penalty of acquiring a mutex for it.
13600 	 */
13601 	if (un->un_f_pm_is_enabled == TRUE) {
13602 		sd_pm_exit(un);
13603 	}
13604 
13605 	SD_NEXT_IODONE(index, un, bp);
13606 
13607 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13608 }
13609 
13610 
13611 /*
13612  *    Function: sd_core_iostart
13613  *
13614  * Description: Primary driver function for enqueuing buf(9S) structs from
13615  *		the system and initiating IO to the target device
13616  *
13617  *     Context: Kernel thread context. Can sleep.
13618  *
13619  * Assumptions:  - The given xp->xb_blkno is absolute
13620  *		   (ie, relative to the start of the device).
13621  *		 - The IO is to be done using the native blocksize of
13622  *		   the device, as specified in un->un_tgt_blocksize.
13623  */
13624 /* ARGSUSED */
13625 static void
13626 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13627 {
13628 	struct sd_xbuf *xp;
13629 
13630 	ASSERT(un != NULL);
13631 	ASSERT(bp != NULL);
13632 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13633 	ASSERT(bp->b_resid == 0);
13634 
13635 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13636 
13637 	xp = SD_GET_XBUF(bp);
13638 	ASSERT(xp != NULL);
13639 
13640 	mutex_enter(SD_MUTEX(un));
13641 
13642 	/*
13643 	 * If we are currently in the failfast state, fail any new IO
13644 	 * that has B_FAILFAST set, then return.
13645 	 */
13646 	if ((bp->b_flags & B_FAILFAST) &&
13647 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13648 		mutex_exit(SD_MUTEX(un));
13649 		bioerror(bp, EIO);
13650 		bp->b_resid = bp->b_bcount;
13651 		SD_BEGIN_IODONE(index, un, bp);
13652 		return;
13653 	}
13654 
13655 	if (SD_IS_DIRECT_PRIORITY(xp)) {
13656 		/*
13657 		 * Priority command -- transport it immediately.
13658 		 *
13659 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13660 		 * because all direct priority commands should be associated
13661 		 * with error recovery actions which we don't want to retry.
13662 		 */
13663 		sd_start_cmds(un, bp);
13664 	} else {
13665 		/*
13666 		 * Normal command -- add it to the wait queue, then start
13667 		 * transporting commands from the wait queue.
13668 		 */
13669 		sd_add_buf_to_waitq(un, bp);
13670 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13671 		sd_start_cmds(un, NULL);
13672 	}
13673 
13674 	mutex_exit(SD_MUTEX(un));
13675 
13676 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13677 }
13678 
13679 
13680 /*
13681  *    Function: sd_init_cdb_limits
13682  *
13683  * Description: This is to handle scsi_pkt initialization differences
13684  *		between the driver platforms.
13685  *
13686  *		Legacy behaviors:
13687  *
13688  *		If the block number or the sector count exceeds the
13689  *		capabilities of a Group 0 command, shift over to a
13690  *		Group 1 command. We don't blindly use Group 1
13691  *		commands because a) some drives (CDC Wren IVs) get a
13692  *		bit confused, and b) there is probably a fair amount
13693  *		of speed difference for a target to receive and decode
13694  *		a 10 byte command instead of a 6 byte command.
13695  *
13696  *		The xfer time difference of 6 vs 10 byte CDBs is
13697  *		still significant so this code is still worthwhile.
13698  *		10 byte CDBs are very inefficient with the fas HBA driver
13699  *		and older disks. Each CDB byte took 1 usec with some
13700  *		popular disks.
13701  *
13702  *     Context: Must be called at attach time
13703  */
13704 
13705 static void
13706 sd_init_cdb_limits(struct sd_lun *un)
13707 {
13708 	int hba_cdb_limit;
13709 
13710 	/*
13711 	 * Use CDB_GROUP1 commands for most devices except for
13712 	 * parallel SCSI fixed drives in which case we get better
13713 	 * performance using CDB_GROUP0 commands (where applicable).
13714 	 */
13715 	un->un_mincdb = SD_CDB_GROUP1;
13716 #if !defined(__fibre)
13717 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13718 	    !un->un_f_has_removable_media) {
13719 		un->un_mincdb = SD_CDB_GROUP0;
13720 	}
13721 #endif
13722 
13723 	/*
13724 	 * Try to read the max-cdb-length supported by HBA.
13725 	 */
13726 	un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13727 	if (0 >= un->un_max_hba_cdb) {
13728 		un->un_max_hba_cdb = CDB_GROUP4;
13729 		hba_cdb_limit = SD_CDB_GROUP4;
13730 	} else if (0 < un->un_max_hba_cdb &&
13731 	    un->un_max_hba_cdb < CDB_GROUP1) {
13732 		hba_cdb_limit = SD_CDB_GROUP0;
13733 	} else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13734 	    un->un_max_hba_cdb < CDB_GROUP5) {
13735 		hba_cdb_limit = SD_CDB_GROUP1;
13736 	} else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13737 	    un->un_max_hba_cdb < CDB_GROUP4) {
13738 		hba_cdb_limit = SD_CDB_GROUP5;
13739 	} else {
13740 		hba_cdb_limit = SD_CDB_GROUP4;
13741 	}
13742 
13743 	/*
13744 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13745 	 * commands for fixed disks unless we are building for a 32 bit
13746 	 * kernel.
13747 	 */
13748 #ifdef _LP64
13749 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13750 	    min(hba_cdb_limit, SD_CDB_GROUP4);
13751 #else
13752 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13753 	    min(hba_cdb_limit, SD_CDB_GROUP1);
13754 #endif
13755 
13756 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13757 	    ? sizeof (struct scsi_arq_status) : 1);
13758 	if (!ISCD(un))
13759 		un->un_cmd_timeout = (ushort_t)sd_io_time;
13760 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13761 }
13762 
13763 
13764 /*
13765  *    Function: sd_initpkt_for_buf
13766  *
13767  * Description: Allocate and initialize for transport a scsi_pkt struct,
13768  *		based upon the info specified in the given buf struct.
13769  *
13770  *		Assumes the xb_blkno in the request is absolute (ie,
13771  *		relative to the start of the device (NOT partition!).
13772  *		Also assumes that the request is using the native block
13773  *		size of the device (as returned by the READ CAPACITY
13774  *		command).
13775  *
13776  * Return Code: SD_PKT_ALLOC_SUCCESS
13777  *		SD_PKT_ALLOC_FAILURE
13778  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13779  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13780  *
13781  *     Context: Kernel thread and may be called from software interrupt context
13782  *		as part of a sdrunout callback. This function may not block or
13783  *		call routines that block
13784  */
13785 
13786 static int
13787 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13788 {
13789 	struct sd_xbuf	*xp;
13790 	struct scsi_pkt *pktp = NULL;
13791 	struct sd_lun	*un;
13792 	size_t		blockcount;
13793 	daddr_t		startblock;
13794 	int		rval;
13795 	int		cmd_flags;
13796 
13797 	ASSERT(bp != NULL);
13798 	ASSERT(pktpp != NULL);
13799 	xp = SD_GET_XBUF(bp);
13800 	ASSERT(xp != NULL);
13801 	un = SD_GET_UN(bp);
13802 	ASSERT(un != NULL);
13803 	ASSERT(mutex_owned(SD_MUTEX(un)));
13804 	ASSERT(bp->b_resid == 0);
13805 
13806 	SD_TRACE(SD_LOG_IO_CORE, un,
13807 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13808 
13809 	mutex_exit(SD_MUTEX(un));
13810 
13811 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13812 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13813 		/*
13814 		 * Already have a scsi_pkt -- just need DMA resources.
13815 		 * We must recompute the CDB in case the mapping returns
13816 		 * a nonzero pkt_resid.
13817 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13818 		 * that is being retried, the unmap/remap of the DMA resouces
13819 		 * will result in the entire transfer starting over again
13820 		 * from the very first block.
13821 		 */
13822 		ASSERT(xp->xb_pktp != NULL);
13823 		pktp = xp->xb_pktp;
13824 	} else {
13825 		pktp = NULL;
13826 	}
13827 #endif /* __i386 || __amd64 */
13828 
13829 	startblock = xp->xb_blkno;	/* Absolute block num. */
13830 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13831 
13832 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13833 
13834 	/*
13835 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13836 	 * call scsi_init_pkt, and build the CDB.
13837 	 */
13838 	rval = sd_setup_rw_pkt(un, &pktp, bp,
13839 	    cmd_flags, sdrunout, (caddr_t)un,
13840 	    startblock, blockcount);
13841 
13842 	if (rval == 0) {
13843 		/*
13844 		 * Success.
13845 		 *
13846 		 * If partial DMA is being used and required for this transfer.
13847 		 * set it up here.
13848 		 */
13849 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13850 		    (pktp->pkt_resid != 0)) {
13851 
13852 			/*
13853 			 * Save the CDB length and pkt_resid for the
13854 			 * next xfer
13855 			 */
13856 			xp->xb_dma_resid = pktp->pkt_resid;
13857 
13858 			/* rezero resid */
13859 			pktp->pkt_resid = 0;
13860 
13861 		} else {
13862 			xp->xb_dma_resid = 0;
13863 		}
13864 
13865 		pktp->pkt_flags = un->un_tagflags;
13866 		pktp->pkt_time  = un->un_cmd_timeout;
13867 		pktp->pkt_comp  = sdintr;
13868 
13869 		pktp->pkt_private = bp;
13870 		*pktpp = pktp;
13871 
13872 		SD_TRACE(SD_LOG_IO_CORE, un,
13873 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13874 
13875 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13876 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13877 #endif
13878 
13879 		mutex_enter(SD_MUTEX(un));
13880 		return (SD_PKT_ALLOC_SUCCESS);
13881 
13882 	}
13883 
13884 	/*
13885 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13886 	 * from sd_setup_rw_pkt.
13887 	 */
13888 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13889 
13890 	if (rval == SD_PKT_ALLOC_FAILURE) {
13891 		*pktpp = NULL;
13892 		/*
13893 		 * Set the driver state to RWAIT to indicate the driver
13894 		 * is waiting on resource allocations. The driver will not
13895 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13896 		 */
13897 		mutex_enter(SD_MUTEX(un));
13898 		New_state(un, SD_STATE_RWAIT);
13899 
13900 		SD_ERROR(SD_LOG_IO_CORE, un,
13901 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13902 
13903 		if ((bp->b_flags & B_ERROR) != 0) {
13904 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13905 		}
13906 		return (SD_PKT_ALLOC_FAILURE);
13907 	} else {
13908 		/*
13909 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13910 		 *
13911 		 * This should never happen.  Maybe someone messed with the
13912 		 * kernel's minphys?
13913 		 */
13914 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13915 		    "Request rejected: too large for CDB: "
13916 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13917 		SD_ERROR(SD_LOG_IO_CORE, un,
13918 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13919 		mutex_enter(SD_MUTEX(un));
13920 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13921 
13922 	}
13923 }
13924 
13925 
13926 /*
13927  *    Function: sd_destroypkt_for_buf
13928  *
13929  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13930  *
13931  *     Context: Kernel thread or interrupt context
13932  */
13933 
13934 static void
13935 sd_destroypkt_for_buf(struct buf *bp)
13936 {
13937 	ASSERT(bp != NULL);
13938 	ASSERT(SD_GET_UN(bp) != NULL);
13939 
13940 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13941 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13942 
13943 	ASSERT(SD_GET_PKTP(bp) != NULL);
13944 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13945 
13946 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13947 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13948 }
13949 
13950 /*
13951  *    Function: sd_setup_rw_pkt
13952  *
13953  * Description: Determines appropriate CDB group for the requested LBA
13954  *		and transfer length, calls scsi_init_pkt, and builds
13955  *		the CDB.  Do not use for partial DMA transfers except
13956  *		for the initial transfer since the CDB size must
13957  *		remain constant.
13958  *
13959  *     Context: Kernel thread and may be called from software interrupt
13960  *		context as part of a sdrunout callback. This function may not
13961  *		block or call routines that block
13962  */
13963 
13964 
13965 int
13966 sd_setup_rw_pkt(struct sd_lun *un,
13967     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13968     int (*callback)(caddr_t), caddr_t callback_arg,
13969     diskaddr_t lba, uint32_t blockcount)
13970 {
13971 	struct scsi_pkt *return_pktp;
13972 	union scsi_cdb *cdbp;
13973 	struct sd_cdbinfo *cp = NULL;
13974 	int i;
13975 
13976 	/*
13977 	 * See which size CDB to use, based upon the request.
13978 	 */
13979 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13980 
13981 		/*
13982 		 * Check lba and block count against sd_cdbtab limits.
13983 		 * In the partial DMA case, we have to use the same size
13984 		 * CDB for all the transfers.  Check lba + blockcount
13985 		 * against the max LBA so we know that segment of the
13986 		 * transfer can use the CDB we select.
13987 		 */
13988 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13989 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13990 
13991 			/*
13992 			 * The command will fit into the CDB type
13993 			 * specified by sd_cdbtab[i].
13994 			 */
13995 			cp = sd_cdbtab + i;
13996 
13997 			/*
13998 			 * Call scsi_init_pkt so we can fill in the
13999 			 * CDB.
14000 			 */
14001 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
14002 			    bp, cp->sc_grpcode, un->un_status_len, 0,
14003 			    flags, callback, callback_arg);
14004 
14005 			if (return_pktp != NULL) {
14006 
14007 				/*
14008 				 * Return new value of pkt
14009 				 */
14010 				*pktpp = return_pktp;
14011 
14012 				/*
14013 				 * To be safe, zero the CDB insuring there is
14014 				 * no leftover data from a previous command.
14015 				 */
14016 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
14017 
14018 				/*
14019 				 * Handle partial DMA mapping
14020 				 */
14021 				if (return_pktp->pkt_resid != 0) {
14022 
14023 					/*
14024 					 * Not going to xfer as many blocks as
14025 					 * originally expected
14026 					 */
14027 					blockcount -=
14028 					    SD_BYTES2TGTBLOCKS(un,
14029 					    return_pktp->pkt_resid);
14030 				}
14031 
14032 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
14033 
14034 				/*
14035 				 * Set command byte based on the CDB
14036 				 * type we matched.
14037 				 */
14038 				cdbp->scc_cmd = cp->sc_grpmask |
14039 				    ((bp->b_flags & B_READ) ?
14040 				    SCMD_READ : SCMD_WRITE);
14041 
14042 				SD_FILL_SCSI1_LUN(un, return_pktp);
14043 
14044 				/*
14045 				 * Fill in LBA and length
14046 				 */
14047 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
14048 				    (cp->sc_grpcode == CDB_GROUP4) ||
14049 				    (cp->sc_grpcode == CDB_GROUP0) ||
14050 				    (cp->sc_grpcode == CDB_GROUP5));
14051 
14052 				if (cp->sc_grpcode == CDB_GROUP1) {
14053 					FORMG1ADDR(cdbp, lba);
14054 					FORMG1COUNT(cdbp, blockcount);
14055 					return (0);
14056 				} else if (cp->sc_grpcode == CDB_GROUP4) {
14057 					FORMG4LONGADDR(cdbp, lba);
14058 					FORMG4COUNT(cdbp, blockcount);
14059 					return (0);
14060 				} else if (cp->sc_grpcode == CDB_GROUP0) {
14061 					FORMG0ADDR(cdbp, lba);
14062 					FORMG0COUNT(cdbp, blockcount);
14063 					return (0);
14064 				} else if (cp->sc_grpcode == CDB_GROUP5) {
14065 					FORMG5ADDR(cdbp, lba);
14066 					FORMG5COUNT(cdbp, blockcount);
14067 					return (0);
14068 				}
14069 
14070 				/*
14071 				 * It should be impossible to not match one
14072 				 * of the CDB types above, so we should never
14073 				 * reach this point.  Set the CDB command byte
14074 				 * to test-unit-ready to avoid writing
14075 				 * to somewhere we don't intend.
14076 				 */
14077 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
14078 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14079 			} else {
14080 				/*
14081 				 * Couldn't get scsi_pkt
14082 				 */
14083 				return (SD_PKT_ALLOC_FAILURE);
14084 			}
14085 		}
14086 	}
14087 
14088 	/*
14089 	 * None of the available CDB types were suitable.  This really
14090 	 * should never happen:  on a 64 bit system we support
14091 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
14092 	 * and on a 32 bit system we will refuse to bind to a device
14093 	 * larger than 2TB so addresses will never be larger than 32 bits.
14094 	 */
14095 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14096 }
14097 
14098 /*
14099  *    Function: sd_setup_next_rw_pkt
14100  *
14101  * Description: Setup packet for partial DMA transfers, except for the
14102  * 		initial transfer.  sd_setup_rw_pkt should be used for
14103  *		the initial transfer.
14104  *
14105  *     Context: Kernel thread and may be called from interrupt context.
14106  */
14107 
14108 int
14109 sd_setup_next_rw_pkt(struct sd_lun *un,
14110     struct scsi_pkt *pktp, struct buf *bp,
14111     diskaddr_t lba, uint32_t blockcount)
14112 {
14113 	uchar_t com;
14114 	union scsi_cdb *cdbp;
14115 	uchar_t cdb_group_id;
14116 
14117 	ASSERT(pktp != NULL);
14118 	ASSERT(pktp->pkt_cdbp != NULL);
14119 
14120 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
14121 	com = cdbp->scc_cmd;
14122 	cdb_group_id = CDB_GROUPID(com);
14123 
14124 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
14125 	    (cdb_group_id == CDB_GROUPID_1) ||
14126 	    (cdb_group_id == CDB_GROUPID_4) ||
14127 	    (cdb_group_id == CDB_GROUPID_5));
14128 
14129 	/*
14130 	 * Move pkt to the next portion of the xfer.
14131 	 * func is NULL_FUNC so we do not have to release
14132 	 * the disk mutex here.
14133 	 */
14134 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
14135 	    NULL_FUNC, NULL) == pktp) {
14136 		/* Success.  Handle partial DMA */
14137 		if (pktp->pkt_resid != 0) {
14138 			blockcount -=
14139 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
14140 		}
14141 
14142 		cdbp->scc_cmd = com;
14143 		SD_FILL_SCSI1_LUN(un, pktp);
14144 		if (cdb_group_id == CDB_GROUPID_1) {
14145 			FORMG1ADDR(cdbp, lba);
14146 			FORMG1COUNT(cdbp, blockcount);
14147 			return (0);
14148 		} else if (cdb_group_id == CDB_GROUPID_4) {
14149 			FORMG4LONGADDR(cdbp, lba);
14150 			FORMG4COUNT(cdbp, blockcount);
14151 			return (0);
14152 		} else if (cdb_group_id == CDB_GROUPID_0) {
14153 			FORMG0ADDR(cdbp, lba);
14154 			FORMG0COUNT(cdbp, blockcount);
14155 			return (0);
14156 		} else if (cdb_group_id == CDB_GROUPID_5) {
14157 			FORMG5ADDR(cdbp, lba);
14158 			FORMG5COUNT(cdbp, blockcount);
14159 			return (0);
14160 		}
14161 
14162 		/* Unreachable */
14163 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14164 	}
14165 
14166 	/*
14167 	 * Error setting up next portion of cmd transfer.
14168 	 * Something is definitely very wrong and this
14169 	 * should not happen.
14170 	 */
14171 	return (SD_PKT_ALLOC_FAILURE);
14172 }
14173 
14174 /*
14175  *    Function: sd_initpkt_for_uscsi
14176  *
14177  * Description: Allocate and initialize for transport a scsi_pkt struct,
14178  *		based upon the info specified in the given uscsi_cmd struct.
14179  *
14180  * Return Code: SD_PKT_ALLOC_SUCCESS
14181  *		SD_PKT_ALLOC_FAILURE
14182  *		SD_PKT_ALLOC_FAILURE_NO_DMA
14183  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
14184  *
14185  *     Context: Kernel thread and may be called from software interrupt context
14186  *		as part of a sdrunout callback. This function may not block or
14187  *		call routines that block
14188  */
14189 
14190 static int
14191 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
14192 {
14193 	struct uscsi_cmd *uscmd;
14194 	struct sd_xbuf	*xp;
14195 	struct scsi_pkt	*pktp;
14196 	struct sd_lun	*un;
14197 	uint32_t	flags = 0;
14198 
14199 	ASSERT(bp != NULL);
14200 	ASSERT(pktpp != NULL);
14201 	xp = SD_GET_XBUF(bp);
14202 	ASSERT(xp != NULL);
14203 	un = SD_GET_UN(bp);
14204 	ASSERT(un != NULL);
14205 	ASSERT(mutex_owned(SD_MUTEX(un)));
14206 
14207 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14208 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14209 	ASSERT(uscmd != NULL);
14210 
14211 	SD_TRACE(SD_LOG_IO_CORE, un,
14212 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
14213 
14214 	/*
14215 	 * Allocate the scsi_pkt for the command.
14216 	 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
14217 	 *	 during scsi_init_pkt time and will continue to use the
14218 	 *	 same path as long as the same scsi_pkt is used without
14219 	 *	 intervening scsi_dma_free(). Since uscsi command does
14220 	 *	 not call scsi_dmafree() before retry failed command, it
14221 	 *	 is necessary to make sure PKT_DMA_PARTIAL flag is NOT
14222 	 *	 set such that scsi_vhci can use other available path for
14223 	 *	 retry. Besides, ucsci command does not allow DMA breakup,
14224 	 *	 so there is no need to set PKT_DMA_PARTIAL flag.
14225 	 */
14226 	if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14227 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14228 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14229 		    ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
14230 		    - sizeof (struct scsi_extended_sense)), 0,
14231 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
14232 		    sdrunout, (caddr_t)un);
14233 	} else {
14234 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14235 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14236 		    sizeof (struct scsi_arq_status), 0,
14237 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14238 		    sdrunout, (caddr_t)un);
14239 	}
14240 
14241 	if (pktp == NULL) {
14242 		*pktpp = NULL;
14243 		/*
14244 		 * Set the driver state to RWAIT to indicate the driver
14245 		 * is waiting on resource allocations. The driver will not
14246 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
14247 		 */
14248 		New_state(un, SD_STATE_RWAIT);
14249 
14250 		SD_ERROR(SD_LOG_IO_CORE, un,
14251 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14252 
14253 		if ((bp->b_flags & B_ERROR) != 0) {
14254 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14255 		}
14256 		return (SD_PKT_ALLOC_FAILURE);
14257 	}
14258 
14259 	/*
14260 	 * We do not do DMA breakup for USCSI commands, so return failure
14261 	 * here if all the needed DMA resources were not allocated.
14262 	 */
14263 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14264 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14265 		scsi_destroy_pkt(pktp);
14266 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14267 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14268 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14269 	}
14270 
14271 	/* Init the cdb from the given uscsi struct */
14272 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14273 	    uscmd->uscsi_cdb[0], 0, 0, 0);
14274 
14275 	SD_FILL_SCSI1_LUN(un, pktp);
14276 
14277 	/*
14278 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
14279 	 * for listing of the supported flags.
14280 	 */
14281 
14282 	if (uscmd->uscsi_flags & USCSI_SILENT) {
14283 		flags |= FLAG_SILENT;
14284 	}
14285 
14286 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14287 		flags |= FLAG_DIAGNOSE;
14288 	}
14289 
14290 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14291 		flags |= FLAG_ISOLATE;
14292 	}
14293 
14294 	if (un->un_f_is_fibre == FALSE) {
14295 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14296 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14297 		}
14298 	}
14299 
14300 	/*
14301 	 * Set the pkt flags here so we save time later.
14302 	 * Note: These flags are NOT in the uscsi man page!!!
14303 	 */
14304 	if (uscmd->uscsi_flags & USCSI_HEAD) {
14305 		flags |= FLAG_HEAD;
14306 	}
14307 
14308 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
14309 		flags |= FLAG_NOINTR;
14310 	}
14311 
14312 	/*
14313 	 * For tagged queueing, things get a bit complicated.
14314 	 * Check first for head of queue and last for ordered queue.
14315 	 * If neither head nor order, use the default driver tag flags.
14316 	 */
14317 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14318 		if (uscmd->uscsi_flags & USCSI_HTAG) {
14319 			flags |= FLAG_HTAG;
14320 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
14321 			flags |= FLAG_OTAG;
14322 		} else {
14323 			flags |= un->un_tagflags & FLAG_TAGMASK;
14324 		}
14325 	}
14326 
14327 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
14328 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14329 	}
14330 
14331 	pktp->pkt_flags = flags;
14332 
14333 	/* Transfer uscsi information to scsi_pkt */
14334 	(void) scsi_uscsi_pktinit(uscmd, pktp);
14335 
14336 	/* Copy the caller's CDB into the pkt... */
14337 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14338 
14339 	if (uscmd->uscsi_timeout == 0) {
14340 		pktp->pkt_time = un->un_uscsi_timeout;
14341 	} else {
14342 		pktp->pkt_time = uscmd->uscsi_timeout;
14343 	}
14344 
14345 	/* need it later to identify USCSI request in sdintr */
14346 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
14347 
14348 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
14349 
14350 	pktp->pkt_private = bp;
14351 	pktp->pkt_comp = sdintr;
14352 	*pktpp = pktp;
14353 
14354 	SD_TRACE(SD_LOG_IO_CORE, un,
14355 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14356 
14357 	return (SD_PKT_ALLOC_SUCCESS);
14358 }
14359 
14360 
14361 /*
14362  *    Function: sd_destroypkt_for_uscsi
14363  *
14364  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14365  *		IOs.. Also saves relevant info into the associated uscsi_cmd
14366  *		struct.
14367  *
14368  *     Context: May be called under interrupt context
14369  */
14370 
14371 static void
14372 sd_destroypkt_for_uscsi(struct buf *bp)
14373 {
14374 	struct uscsi_cmd *uscmd;
14375 	struct sd_xbuf	*xp;
14376 	struct scsi_pkt	*pktp;
14377 	struct sd_lun	*un;
14378 	struct sd_uscsi_info *suip;
14379 
14380 	ASSERT(bp != NULL);
14381 	xp = SD_GET_XBUF(bp);
14382 	ASSERT(xp != NULL);
14383 	un = SD_GET_UN(bp);
14384 	ASSERT(un != NULL);
14385 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14386 	pktp = SD_GET_PKTP(bp);
14387 	ASSERT(pktp != NULL);
14388 
14389 	SD_TRACE(SD_LOG_IO_CORE, un,
14390 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14391 
14392 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14393 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14394 	ASSERT(uscmd != NULL);
14395 
14396 	/* Save the status and the residual into the uscsi_cmd struct */
14397 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14398 	uscmd->uscsi_resid  = bp->b_resid;
14399 
14400 	/* Transfer scsi_pkt information to uscsi */
14401 	(void) scsi_uscsi_pktfini(pktp, uscmd);
14402 
14403 	/*
14404 	 * If enabled, copy any saved sense data into the area specified
14405 	 * by the uscsi command.
14406 	 */
14407 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14408 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14409 		/*
14410 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
14411 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14412 		 */
14413 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
14414 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
14415 		if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14416 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14417 			    MAX_SENSE_LENGTH);
14418 		} else {
14419 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14420 			    SENSE_LENGTH);
14421 		}
14422 	}
14423 	/*
14424 	 * The following assignments are for SCSI FMA.
14425 	 */
14426 	ASSERT(xp->xb_private != NULL);
14427 	suip = (struct sd_uscsi_info *)xp->xb_private;
14428 	suip->ui_pkt_reason = pktp->pkt_reason;
14429 	suip->ui_pkt_state = pktp->pkt_state;
14430 	suip->ui_pkt_statistics = pktp->pkt_statistics;
14431 	suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
14432 
14433 	/* We are done with the scsi_pkt; free it now */
14434 	ASSERT(SD_GET_PKTP(bp) != NULL);
14435 	scsi_destroy_pkt(SD_GET_PKTP(bp));
14436 
14437 	SD_TRACE(SD_LOG_IO_CORE, un,
14438 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14439 }
14440 
14441 
14442 /*
14443  *    Function: sd_bioclone_alloc
14444  *
14445  * Description: Allocate a buf(9S) and init it as per the given buf
14446  *		and the various arguments.  The associated sd_xbuf
14447  *		struct is (nearly) duplicated.  The struct buf *bp
14448  *		argument is saved in new_xp->xb_private.
14449  *
14450  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14451  *		datalen - size of data area for the shadow bp
14452  *		blkno - starting LBA
14453  *		func - function pointer for b_iodone in the shadow buf. (May
14454  *			be NULL if none.)
14455  *
14456  * Return Code: Pointer to allocates buf(9S) struct
14457  *
14458  *     Context: Can sleep.
14459  */
14460 
14461 static struct buf *
14462 sd_bioclone_alloc(struct buf *bp, size_t datalen, daddr_t blkno,
14463     int (*func)(struct buf *))
14464 {
14465 	struct	sd_lun	*un;
14466 	struct	sd_xbuf	*xp;
14467 	struct	sd_xbuf	*new_xp;
14468 	struct	buf	*new_bp;
14469 
14470 	ASSERT(bp != NULL);
14471 	xp = SD_GET_XBUF(bp);
14472 	ASSERT(xp != NULL);
14473 	un = SD_GET_UN(bp);
14474 	ASSERT(un != NULL);
14475 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14476 
14477 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14478 	    NULL, KM_SLEEP);
14479 
14480 	new_bp->b_lblkno	= blkno;
14481 
14482 	/*
14483 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14484 	 * original xbuf into it.
14485 	 */
14486 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14487 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14488 
14489 	/*
14490 	 * The given bp is automatically saved in the xb_private member
14491 	 * of the new xbuf.  Callers are allowed to depend on this.
14492 	 */
14493 	new_xp->xb_private = bp;
14494 
14495 	new_bp->b_private  = new_xp;
14496 
14497 	return (new_bp);
14498 }
14499 
14500 /*
14501  *    Function: sd_shadow_buf_alloc
14502  *
14503  * Description: Allocate a buf(9S) and init it as per the given buf
14504  *		and the various arguments.  The associated sd_xbuf
14505  *		struct is (nearly) duplicated.  The struct buf *bp
14506  *		argument is saved in new_xp->xb_private.
14507  *
14508  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14509  *		datalen - size of data area for the shadow bp
14510  *		bflags - B_READ or B_WRITE (pseudo flag)
14511  *		blkno - starting LBA
14512  *		func - function pointer for b_iodone in the shadow buf. (May
14513  *			be NULL if none.)
14514  *
14515  * Return Code: Pointer to allocates buf(9S) struct
14516  *
14517  *     Context: Can sleep.
14518  */
14519 
14520 static struct buf *
14521 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14522     daddr_t blkno, int (*func)(struct buf *))
14523 {
14524 	struct	sd_lun	*un;
14525 	struct	sd_xbuf	*xp;
14526 	struct	sd_xbuf	*new_xp;
14527 	struct	buf	*new_bp;
14528 
14529 	ASSERT(bp != NULL);
14530 	xp = SD_GET_XBUF(bp);
14531 	ASSERT(xp != NULL);
14532 	un = SD_GET_UN(bp);
14533 	ASSERT(un != NULL);
14534 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14535 
14536 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14537 		bp_mapin(bp);
14538 	}
14539 
14540 	bflags &= (B_READ | B_WRITE);
14541 #if defined(__i386) || defined(__amd64)
14542 	new_bp = getrbuf(KM_SLEEP);
14543 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14544 	new_bp->b_bcount = datalen;
14545 	new_bp->b_flags = bflags |
14546 	    (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14547 #else
14548 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14549 	    datalen, bflags, SLEEP_FUNC, NULL);
14550 #endif
14551 	new_bp->av_forw	= NULL;
14552 	new_bp->av_back	= NULL;
14553 	new_bp->b_dev	= bp->b_dev;
14554 	new_bp->b_blkno	= blkno;
14555 	new_bp->b_iodone = func;
14556 	new_bp->b_edev	= bp->b_edev;
14557 	new_bp->b_resid	= 0;
14558 
14559 	/* We need to preserve the B_FAILFAST flag */
14560 	if (bp->b_flags & B_FAILFAST) {
14561 		new_bp->b_flags |= B_FAILFAST;
14562 	}
14563 
14564 	/*
14565 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14566 	 * original xbuf into it.
14567 	 */
14568 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14569 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14570 
14571 	/* Need later to copy data between the shadow buf & original buf! */
14572 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14573 
14574 	/*
14575 	 * The given bp is automatically saved in the xb_private member
14576 	 * of the new xbuf.  Callers are allowed to depend on this.
14577 	 */
14578 	new_xp->xb_private = bp;
14579 
14580 	new_bp->b_private  = new_xp;
14581 
14582 	return (new_bp);
14583 }
14584 
14585 /*
14586  *    Function: sd_bioclone_free
14587  *
14588  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14589  *		in the larger than partition operation.
14590  *
14591  *     Context: May be called under interrupt context
14592  */
14593 
14594 static void
14595 sd_bioclone_free(struct buf *bp)
14596 {
14597 	struct sd_xbuf	*xp;
14598 
14599 	ASSERT(bp != NULL);
14600 	xp = SD_GET_XBUF(bp);
14601 	ASSERT(xp != NULL);
14602 
14603 	/*
14604 	 * Call bp_mapout() before freeing the buf,  in case a lower
14605 	 * layer or HBA  had done a bp_mapin().  we must do this here
14606 	 * as we are the "originator" of the shadow buf.
14607 	 */
14608 	bp_mapout(bp);
14609 
14610 	/*
14611 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14612 	 * never gets confused by a stale value in this field. (Just a little
14613 	 * extra defensiveness here.)
14614 	 */
14615 	bp->b_iodone = NULL;
14616 
14617 	freerbuf(bp);
14618 
14619 	kmem_free(xp, sizeof (struct sd_xbuf));
14620 }
14621 
14622 /*
14623  *    Function: sd_shadow_buf_free
14624  *
14625  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14626  *
14627  *     Context: May be called under interrupt context
14628  */
14629 
14630 static void
14631 sd_shadow_buf_free(struct buf *bp)
14632 {
14633 	struct sd_xbuf	*xp;
14634 
14635 	ASSERT(bp != NULL);
14636 	xp = SD_GET_XBUF(bp);
14637 	ASSERT(xp != NULL);
14638 
14639 #if defined(__sparc)
14640 	/*
14641 	 * Call bp_mapout() before freeing the buf,  in case a lower
14642 	 * layer or HBA  had done a bp_mapin().  we must do this here
14643 	 * as we are the "originator" of the shadow buf.
14644 	 */
14645 	bp_mapout(bp);
14646 #endif
14647 
14648 	/*
14649 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14650 	 * never gets confused by a stale value in this field. (Just a little
14651 	 * extra defensiveness here.)
14652 	 */
14653 	bp->b_iodone = NULL;
14654 
14655 #if defined(__i386) || defined(__amd64)
14656 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
14657 	freerbuf(bp);
14658 #else
14659 	scsi_free_consistent_buf(bp);
14660 #endif
14661 
14662 	kmem_free(xp, sizeof (struct sd_xbuf));
14663 }
14664 
14665 
14666 /*
14667  *    Function: sd_print_transport_rejected_message
14668  *
14669  * Description: This implements the ludicrously complex rules for printing
14670  *		a "transport rejected" message.  This is to address the
14671  *		specific problem of having a flood of this error message
14672  *		produced when a failover occurs.
14673  *
14674  *     Context: Any.
14675  */
14676 
14677 static void
14678 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14679     int code)
14680 {
14681 	ASSERT(un != NULL);
14682 	ASSERT(mutex_owned(SD_MUTEX(un)));
14683 	ASSERT(xp != NULL);
14684 
14685 	/*
14686 	 * Print the "transport rejected" message under the following
14687 	 * conditions:
14688 	 *
14689 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14690 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14691 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14692 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14693 	 *   scsi_transport(9F) (which indicates that the target might have
14694 	 *   gone off-line).  This uses the un->un_tran_fatal_count
14695 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14696 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14697 	 *   from scsi_transport().
14698 	 *
14699 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14700 	 * the preceeding cases in order for the message to be printed.
14701 	 */
14702 	if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14703 	    (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14704 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14705 		    (code != TRAN_FATAL_ERROR) ||
14706 		    (un->un_tran_fatal_count == 1)) {
14707 			switch (code) {
14708 			case TRAN_BADPKT:
14709 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14710 				    "transport rejected bad packet\n");
14711 				break;
14712 			case TRAN_FATAL_ERROR:
14713 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14714 				    "transport rejected fatal error\n");
14715 				break;
14716 			default:
14717 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14718 				    "transport rejected (%d)\n", code);
14719 				break;
14720 			}
14721 		}
14722 	}
14723 }
14724 
14725 
14726 /*
14727  *    Function: sd_add_buf_to_waitq
14728  *
14729  * Description: Add the given buf(9S) struct to the wait queue for the
14730  *		instance.  If sorting is enabled, then the buf is added
14731  *		to the queue via an elevator sort algorithm (a la
14732  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14733  *		If sorting is not enabled, then the buf is just added
14734  *		to the end of the wait queue.
14735  *
14736  * Return Code: void
14737  *
14738  *     Context: Does not sleep/block, therefore technically can be called
14739  *		from any context.  However if sorting is enabled then the
14740  *		execution time is indeterminate, and may take long if
14741  *		the wait queue grows large.
14742  */
14743 
14744 static void
14745 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14746 {
14747 	struct buf *ap;
14748 
14749 	ASSERT(bp != NULL);
14750 	ASSERT(un != NULL);
14751 	ASSERT(mutex_owned(SD_MUTEX(un)));
14752 
14753 	/* If the queue is empty, add the buf as the only entry & return. */
14754 	if (un->un_waitq_headp == NULL) {
14755 		ASSERT(un->un_waitq_tailp == NULL);
14756 		un->un_waitq_headp = un->un_waitq_tailp = bp;
14757 		bp->av_forw = NULL;
14758 		return;
14759 	}
14760 
14761 	ASSERT(un->un_waitq_tailp != NULL);
14762 
14763 	/*
14764 	 * If sorting is disabled, just add the buf to the tail end of
14765 	 * the wait queue and return.
14766 	 */
14767 	if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14768 		un->un_waitq_tailp->av_forw = bp;
14769 		un->un_waitq_tailp = bp;
14770 		bp->av_forw = NULL;
14771 		return;
14772 	}
14773 
14774 	/*
14775 	 * Sort thru the list of requests currently on the wait queue
14776 	 * and add the new buf request at the appropriate position.
14777 	 *
14778 	 * The un->un_waitq_headp is an activity chain pointer on which
14779 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14780 	 * first queue holds those requests which are positioned after
14781 	 * the current SD_GET_BLKNO() (in the first request); the second holds
14782 	 * requests which came in after their SD_GET_BLKNO() number was passed.
14783 	 * Thus we implement a one way scan, retracting after reaching
14784 	 * the end of the drive to the first request on the second
14785 	 * queue, at which time it becomes the first queue.
14786 	 * A one-way scan is natural because of the way UNIX read-ahead
14787 	 * blocks are allocated.
14788 	 *
14789 	 * If we lie after the first request, then we must locate the
14790 	 * second request list and add ourselves to it.
14791 	 */
14792 	ap = un->un_waitq_headp;
14793 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14794 		while (ap->av_forw != NULL) {
14795 			/*
14796 			 * Look for an "inversion" in the (normally
14797 			 * ascending) block numbers. This indicates
14798 			 * the start of the second request list.
14799 			 */
14800 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14801 				/*
14802 				 * Search the second request list for the
14803 				 * first request at a larger block number.
14804 				 * We go before that; however if there is
14805 				 * no such request, we go at the end.
14806 				 */
14807 				do {
14808 					if (SD_GET_BLKNO(bp) <
14809 					    SD_GET_BLKNO(ap->av_forw)) {
14810 						goto insert;
14811 					}
14812 					ap = ap->av_forw;
14813 				} while (ap->av_forw != NULL);
14814 				goto insert;		/* after last */
14815 			}
14816 			ap = ap->av_forw;
14817 		}
14818 
14819 		/*
14820 		 * No inversions... we will go after the last, and
14821 		 * be the first request in the second request list.
14822 		 */
14823 		goto insert;
14824 	}
14825 
14826 	/*
14827 	 * Request is at/after the current request...
14828 	 * sort in the first request list.
14829 	 */
14830 	while (ap->av_forw != NULL) {
14831 		/*
14832 		 * We want to go after the current request (1) if
14833 		 * there is an inversion after it (i.e. it is the end
14834 		 * of the first request list), or (2) if the next
14835 		 * request is a larger block no. than our request.
14836 		 */
14837 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14838 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14839 			goto insert;
14840 		}
14841 		ap = ap->av_forw;
14842 	}
14843 
14844 	/*
14845 	 * Neither a second list nor a larger request, therefore
14846 	 * we go at the end of the first list (which is the same
14847 	 * as the end of the whole schebang).
14848 	 */
14849 insert:
14850 	bp->av_forw = ap->av_forw;
14851 	ap->av_forw = bp;
14852 
14853 	/*
14854 	 * If we inserted onto the tail end of the waitq, make sure the
14855 	 * tail pointer is updated.
14856 	 */
14857 	if (ap == un->un_waitq_tailp) {
14858 		un->un_waitq_tailp = bp;
14859 	}
14860 }
14861 
14862 
14863 /*
14864  *    Function: sd_start_cmds
14865  *
14866  * Description: Remove and transport cmds from the driver queues.
14867  *
14868  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14869  *
14870  *		immed_bp - ptr to a buf to be transported immediately. Only
14871  *		the immed_bp is transported; bufs on the waitq are not
14872  *		processed and the un_retry_bp is not checked.  If immed_bp is
14873  *		NULL, then normal queue processing is performed.
14874  *
14875  *     Context: May be called from kernel thread context, interrupt context,
14876  *		or runout callback context. This function may not block or
14877  *		call routines that block.
14878  */
14879 
14880 static void
14881 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14882 {
14883 	struct	sd_xbuf	*xp;
14884 	struct	buf	*bp;
14885 	void	(*statp)(kstat_io_t *);
14886 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14887 	void	(*saved_statp)(kstat_io_t *);
14888 #endif
14889 	int	rval;
14890 	struct sd_fm_internal *sfip = NULL;
14891 
14892 	ASSERT(un != NULL);
14893 	ASSERT(mutex_owned(SD_MUTEX(un)));
14894 	ASSERT(un->un_ncmds_in_transport >= 0);
14895 	ASSERT(un->un_throttle >= 0);
14896 
14897 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14898 
14899 	do {
14900 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14901 		saved_statp = NULL;
14902 #endif
14903 
14904 		/*
14905 		 * If we are syncing or dumping, fail the command to
14906 		 * avoid recursively calling back into scsi_transport().
14907 		 * The dump I/O itself uses a separate code path so this
14908 		 * only prevents non-dump I/O from being sent while dumping.
14909 		 * File system sync takes place before dumping begins.
14910 		 * During panic, filesystem I/O is allowed provided
14911 		 * un_in_callback is <= 1.  This is to prevent recursion
14912 		 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14913 		 * sd_start_cmds and so on.  See panic.c for more information
14914 		 * about the states the system can be in during panic.
14915 		 */
14916 		if ((un->un_state == SD_STATE_DUMPING) ||
14917 		    (ddi_in_panic() && (un->un_in_callback > 1))) {
14918 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14919 			    "sd_start_cmds: panicking\n");
14920 			goto exit;
14921 		}
14922 
14923 		if ((bp = immed_bp) != NULL) {
14924 			/*
14925 			 * We have a bp that must be transported immediately.
14926 			 * It's OK to transport the immed_bp here without doing
14927 			 * the throttle limit check because the immed_bp is
14928 			 * always used in a retry/recovery case. This means
14929 			 * that we know we are not at the throttle limit by
14930 			 * virtue of the fact that to get here we must have
14931 			 * already gotten a command back via sdintr(). This also
14932 			 * relies on (1) the command on un_retry_bp preventing
14933 			 * further commands from the waitq from being issued;
14934 			 * and (2) the code in sd_retry_command checking the
14935 			 * throttle limit before issuing a delayed or immediate
14936 			 * retry. This holds even if the throttle limit is
14937 			 * currently ratcheted down from its maximum value.
14938 			 */
14939 			statp = kstat_runq_enter;
14940 			if (bp == un->un_retry_bp) {
14941 				ASSERT((un->un_retry_statp == NULL) ||
14942 				    (un->un_retry_statp == kstat_waitq_enter) ||
14943 				    (un->un_retry_statp ==
14944 				    kstat_runq_back_to_waitq));
14945 				/*
14946 				 * If the waitq kstat was incremented when
14947 				 * sd_set_retry_bp() queued this bp for a retry,
14948 				 * then we must set up statp so that the waitq
14949 				 * count will get decremented correctly below.
14950 				 * Also we must clear un->un_retry_statp to
14951 				 * ensure that we do not act on a stale value
14952 				 * in this field.
14953 				 */
14954 				if ((un->un_retry_statp == kstat_waitq_enter) ||
14955 				    (un->un_retry_statp ==
14956 				    kstat_runq_back_to_waitq)) {
14957 					statp = kstat_waitq_to_runq;
14958 				}
14959 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14960 				saved_statp = un->un_retry_statp;
14961 #endif
14962 				un->un_retry_statp = NULL;
14963 
14964 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14965 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14966 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
14967 				    un, un->un_retry_bp, un->un_throttle,
14968 				    un->un_ncmds_in_transport);
14969 			} else {
14970 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14971 				    "processing priority bp:0x%p\n", bp);
14972 			}
14973 
14974 		} else if ((bp = un->un_waitq_headp) != NULL) {
14975 			/*
14976 			 * A command on the waitq is ready to go, but do not
14977 			 * send it if:
14978 			 *
14979 			 * (1) the throttle limit has been reached, or
14980 			 * (2) a retry is pending, or
14981 			 * (3) a START_STOP_UNIT callback pending, or
14982 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14983 			 *	command is pending.
14984 			 *
14985 			 * For all of these conditions, IO processing will
14986 			 * restart after the condition is cleared.
14987 			 */
14988 			if (un->un_ncmds_in_transport >= un->un_throttle) {
14989 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14990 				    "sd_start_cmds: exiting, "
14991 				    "throttle limit reached!\n");
14992 				goto exit;
14993 			}
14994 			if (un->un_retry_bp != NULL) {
14995 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14996 				    "sd_start_cmds: exiting, retry pending!\n");
14997 				goto exit;
14998 			}
14999 			if (un->un_startstop_timeid != NULL) {
15000 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15001 				    "sd_start_cmds: exiting, "
15002 				    "START_STOP pending!\n");
15003 				goto exit;
15004 			}
15005 			if (un->un_direct_priority_timeid != NULL) {
15006 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15007 				    "sd_start_cmds: exiting, "
15008 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
15009 				goto exit;
15010 			}
15011 
15012 			/* Dequeue the command */
15013 			un->un_waitq_headp = bp->av_forw;
15014 			if (un->un_waitq_headp == NULL) {
15015 				un->un_waitq_tailp = NULL;
15016 			}
15017 			bp->av_forw = NULL;
15018 			statp = kstat_waitq_to_runq;
15019 			SD_TRACE(SD_LOG_IO_CORE, un,
15020 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
15021 
15022 		} else {
15023 			/* No work to do so bail out now */
15024 			SD_TRACE(SD_LOG_IO_CORE, un,
15025 			    "sd_start_cmds: no more work, exiting!\n");
15026 			goto exit;
15027 		}
15028 
15029 		/*
15030 		 * Reset the state to normal. This is the mechanism by which
15031 		 * the state transitions from either SD_STATE_RWAIT or
15032 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
15033 		 * If state is SD_STATE_PM_CHANGING then this command is
15034 		 * part of the device power control and the state must
15035 		 * not be put back to normal. Doing so would would
15036 		 * allow new commands to proceed when they shouldn't,
15037 		 * the device may be going off.
15038 		 */
15039 		if ((un->un_state != SD_STATE_SUSPENDED) &&
15040 		    (un->un_state != SD_STATE_PM_CHANGING)) {
15041 			New_state(un, SD_STATE_NORMAL);
15042 		}
15043 
15044 		xp = SD_GET_XBUF(bp);
15045 		ASSERT(xp != NULL);
15046 
15047 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15048 		/*
15049 		 * Allocate the scsi_pkt if we need one, or attach DMA
15050 		 * resources if we have a scsi_pkt that needs them. The
15051 		 * latter should only occur for commands that are being
15052 		 * retried.
15053 		 */
15054 		if ((xp->xb_pktp == NULL) ||
15055 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
15056 #else
15057 		if (xp->xb_pktp == NULL) {
15058 #endif
15059 			/*
15060 			 * There is no scsi_pkt allocated for this buf. Call
15061 			 * the initpkt function to allocate & init one.
15062 			 *
15063 			 * The scsi_init_pkt runout callback functionality is
15064 			 * implemented as follows:
15065 			 *
15066 			 * 1) The initpkt function always calls
15067 			 *    scsi_init_pkt(9F) with sdrunout specified as the
15068 			 *    callback routine.
15069 			 * 2) A successful packet allocation is initialized and
15070 			 *    the I/O is transported.
15071 			 * 3) The I/O associated with an allocation resource
15072 			 *    failure is left on its queue to be retried via
15073 			 *    runout or the next I/O.
15074 			 * 4) The I/O associated with a DMA error is removed
15075 			 *    from the queue and failed with EIO. Processing of
15076 			 *    the transport queues is also halted to be
15077 			 *    restarted via runout or the next I/O.
15078 			 * 5) The I/O associated with a CDB size or packet
15079 			 *    size error is removed from the queue and failed
15080 			 *    with EIO. Processing of the transport queues is
15081 			 *    continued.
15082 			 *
15083 			 * Note: there is no interface for canceling a runout
15084 			 * callback. To prevent the driver from detaching or
15085 			 * suspending while a runout is pending the driver
15086 			 * state is set to SD_STATE_RWAIT
15087 			 *
15088 			 * Note: using the scsi_init_pkt callback facility can
15089 			 * result in an I/O request persisting at the head of
15090 			 * the list which cannot be satisfied even after
15091 			 * multiple retries. In the future the driver may
15092 			 * implement some kind of maximum runout count before
15093 			 * failing an I/O.
15094 			 *
15095 			 * Note: the use of funcp below may seem superfluous,
15096 			 * but it helps warlock figure out the correct
15097 			 * initpkt function calls (see [s]sd.wlcmd).
15098 			 */
15099 			struct scsi_pkt	*pktp;
15100 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
15101 
15102 			ASSERT(bp != un->un_rqs_bp);
15103 
15104 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
15105 			switch ((*funcp)(bp, &pktp)) {
15106 			case  SD_PKT_ALLOC_SUCCESS:
15107 				xp->xb_pktp = pktp;
15108 				SD_TRACE(SD_LOG_IO_CORE, un,
15109 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
15110 				    pktp);
15111 				goto got_pkt;
15112 
15113 			case SD_PKT_ALLOC_FAILURE:
15114 				/*
15115 				 * Temporary (hopefully) resource depletion.
15116 				 * Since retries and RQS commands always have a
15117 				 * scsi_pkt allocated, these cases should never
15118 				 * get here. So the only cases this needs to
15119 				 * handle is a bp from the waitq (which we put
15120 				 * back onto the waitq for sdrunout), or a bp
15121 				 * sent as an immed_bp (which we just fail).
15122 				 */
15123 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15124 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
15125 
15126 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15127 
15128 				if (bp == immed_bp) {
15129 					/*
15130 					 * If SD_XB_DMA_FREED is clear, then
15131 					 * this is a failure to allocate a
15132 					 * scsi_pkt, and we must fail the
15133 					 * command.
15134 					 */
15135 					if ((xp->xb_pkt_flags &
15136 					    SD_XB_DMA_FREED) == 0) {
15137 						break;
15138 					}
15139 
15140 					/*
15141 					 * If this immediate command is NOT our
15142 					 * un_retry_bp, then we must fail it.
15143 					 */
15144 					if (bp != un->un_retry_bp) {
15145 						break;
15146 					}
15147 
15148 					/*
15149 					 * We get here if this cmd is our
15150 					 * un_retry_bp that was DMAFREED, but
15151 					 * scsi_init_pkt() failed to reallocate
15152 					 * DMA resources when we attempted to
15153 					 * retry it. This can happen when an
15154 					 * mpxio failover is in progress, but
15155 					 * we don't want to just fail the
15156 					 * command in this case.
15157 					 *
15158 					 * Use timeout(9F) to restart it after
15159 					 * a 100ms delay.  We don't want to
15160 					 * let sdrunout() restart it, because
15161 					 * sdrunout() is just supposed to start
15162 					 * commands that are sitting on the
15163 					 * wait queue.  The un_retry_bp stays
15164 					 * set until the command completes, but
15165 					 * sdrunout can be called many times
15166 					 * before that happens.  Since sdrunout
15167 					 * cannot tell if the un_retry_bp is
15168 					 * already in the transport, it could
15169 					 * end up calling scsi_transport() for
15170 					 * the un_retry_bp multiple times.
15171 					 *
15172 					 * Also: don't schedule the callback
15173 					 * if some other callback is already
15174 					 * pending.
15175 					 */
15176 					if (un->un_retry_statp == NULL) {
15177 						/*
15178 						 * restore the kstat pointer to
15179 						 * keep kstat counts coherent
15180 						 * when we do retry the command.
15181 						 */
15182 						un->un_retry_statp =
15183 						    saved_statp;
15184 					}
15185 
15186 					if ((un->un_startstop_timeid == NULL) &&
15187 					    (un->un_retry_timeid == NULL) &&
15188 					    (un->un_direct_priority_timeid ==
15189 					    NULL)) {
15190 
15191 						un->un_retry_timeid =
15192 						    timeout(
15193 						    sd_start_retry_command,
15194 						    un, SD_RESTART_TIMEOUT);
15195 					}
15196 					goto exit;
15197 				}
15198 
15199 #else
15200 				if (bp == immed_bp) {
15201 					break;	/* Just fail the command */
15202 				}
15203 #endif
15204 
15205 				/* Add the buf back to the head of the waitq */
15206 				bp->av_forw = un->un_waitq_headp;
15207 				un->un_waitq_headp = bp;
15208 				if (un->un_waitq_tailp == NULL) {
15209 					un->un_waitq_tailp = bp;
15210 				}
15211 				goto exit;
15212 
15213 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
15214 				/*
15215 				 * HBA DMA resource failure. Fail the command
15216 				 * and continue processing of the queues.
15217 				 */
15218 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15219 				    "sd_start_cmds: "
15220 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15221 				break;
15222 
15223 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15224 				/*
15225 				 * Note:x86: Partial DMA mapping not supported
15226 				 * for USCSI commands, and all the needed DMA
15227 				 * resources were not allocated.
15228 				 */
15229 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15230 				    "sd_start_cmds: "
15231 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15232 				break;
15233 
15234 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15235 				/*
15236 				 * Note:x86: Request cannot fit into CDB based
15237 				 * on lba and len.
15238 				 */
15239 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15240 				    "sd_start_cmds: "
15241 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15242 				break;
15243 
15244 			default:
15245 				/* Should NEVER get here! */
15246 				panic("scsi_initpkt error");
15247 				/*NOTREACHED*/
15248 			}
15249 
15250 			/*
15251 			 * Fatal error in allocating a scsi_pkt for this buf.
15252 			 * Update kstats & return the buf with an error code.
15253 			 * We must use sd_return_failed_command_no_restart() to
15254 			 * avoid a recursive call back into sd_start_cmds().
15255 			 * However this also means that we must keep processing
15256 			 * the waitq here in order to avoid stalling.
15257 			 */
15258 			if (statp == kstat_waitq_to_runq) {
15259 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15260 			}
15261 			sd_return_failed_command_no_restart(un, bp, EIO);
15262 			if (bp == immed_bp) {
15263 				/* immed_bp is gone by now, so clear this */
15264 				immed_bp = NULL;
15265 			}
15266 			continue;
15267 		}
15268 got_pkt:
15269 		if (bp == immed_bp) {
15270 			/* goto the head of the class.... */
15271 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15272 		}
15273 
15274 		un->un_ncmds_in_transport++;
15275 		SD_UPDATE_KSTATS(un, statp, bp);
15276 
15277 		/*
15278 		 * Call scsi_transport() to send the command to the target.
15279 		 * According to SCSA architecture, we must drop the mutex here
15280 		 * before calling scsi_transport() in order to avoid deadlock.
15281 		 * Note that the scsi_pkt's completion routine can be executed
15282 		 * (from interrupt context) even before the call to
15283 		 * scsi_transport() returns.
15284 		 */
15285 		SD_TRACE(SD_LOG_IO_CORE, un,
15286 		    "sd_start_cmds: calling scsi_transport()\n");
15287 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15288 
15289 		mutex_exit(SD_MUTEX(un));
15290 		rval = scsi_transport(xp->xb_pktp);
15291 		mutex_enter(SD_MUTEX(un));
15292 
15293 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15294 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
15295 
15296 		switch (rval) {
15297 		case TRAN_ACCEPT:
15298 			/* Clear this with every pkt accepted by the HBA */
15299 			un->un_tran_fatal_count = 0;
15300 			break;	/* Success; try the next cmd (if any) */
15301 
15302 		case TRAN_BUSY:
15303 			un->un_ncmds_in_transport--;
15304 			ASSERT(un->un_ncmds_in_transport >= 0);
15305 
15306 			/*
15307 			 * Don't retry request sense, the sense data
15308 			 * is lost when another request is sent.
15309 			 * Free up the rqs buf and retry
15310 			 * the original failed cmd.  Update kstat.
15311 			 */
15312 			if (bp == un->un_rqs_bp) {
15313 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15314 				bp = sd_mark_rqs_idle(un, xp);
15315 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15316 				    NULL, NULL, EIO, un->un_busy_timeout / 500,
15317 				    kstat_waitq_enter);
15318 				goto exit;
15319 			}
15320 
15321 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15322 			/*
15323 			 * Free the DMA resources for the  scsi_pkt. This will
15324 			 * allow mpxio to select another path the next time
15325 			 * we call scsi_transport() with this scsi_pkt.
15326 			 * See sdintr() for the rationalization behind this.
15327 			 */
15328 			if ((un->un_f_is_fibre == TRUE) &&
15329 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15330 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15331 				scsi_dmafree(xp->xb_pktp);
15332 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15333 			}
15334 #endif
15335 
15336 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15337 				/*
15338 				 * Commands that are SD_PATH_DIRECT_PRIORITY
15339 				 * are for error recovery situations. These do
15340 				 * not use the normal command waitq, so if they
15341 				 * get a TRAN_BUSY we cannot put them back onto
15342 				 * the waitq for later retry. One possible
15343 				 * problem is that there could already be some
15344 				 * other command on un_retry_bp that is waiting
15345 				 * for this one to complete, so we would be
15346 				 * deadlocked if we put this command back onto
15347 				 * the waitq for later retry (since un_retry_bp
15348 				 * must complete before the driver gets back to
15349 				 * commands on the waitq).
15350 				 *
15351 				 * To avoid deadlock we must schedule a callback
15352 				 * that will restart this command after a set
15353 				 * interval.  This should keep retrying for as
15354 				 * long as the underlying transport keeps
15355 				 * returning TRAN_BUSY (just like for other
15356 				 * commands).  Use the same timeout interval as
15357 				 * for the ordinary TRAN_BUSY retry.
15358 				 */
15359 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15360 				    "sd_start_cmds: scsi_transport() returned "
15361 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15362 
15363 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15364 				un->un_direct_priority_timeid =
15365 				    timeout(sd_start_direct_priority_command,
15366 				    bp, un->un_busy_timeout / 500);
15367 
15368 				goto exit;
15369 			}
15370 
15371 			/*
15372 			 * For TRAN_BUSY, we want to reduce the throttle value,
15373 			 * unless we are retrying a command.
15374 			 */
15375 			if (bp != un->un_retry_bp) {
15376 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15377 			}
15378 
15379 			/*
15380 			 * Set up the bp to be tried again 10 ms later.
15381 			 * Note:x86: Is there a timeout value in the sd_lun
15382 			 * for this condition?
15383 			 */
15384 			sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15385 			    kstat_runq_back_to_waitq);
15386 			goto exit;
15387 
15388 		case TRAN_FATAL_ERROR:
15389 			un->un_tran_fatal_count++;
15390 			/* FALLTHRU */
15391 
15392 		case TRAN_BADPKT:
15393 		default:
15394 			un->un_ncmds_in_transport--;
15395 			ASSERT(un->un_ncmds_in_transport >= 0);
15396 
15397 			/*
15398 			 * If this is our REQUEST SENSE command with a
15399 			 * transport error, we must get back the pointers
15400 			 * to the original buf, and mark the REQUEST
15401 			 * SENSE command as "available".
15402 			 */
15403 			if (bp == un->un_rqs_bp) {
15404 				bp = sd_mark_rqs_idle(un, xp);
15405 				xp = SD_GET_XBUF(bp);
15406 			} else {
15407 				/*
15408 				 * Legacy behavior: do not update transport
15409 				 * error count for request sense commands.
15410 				 */
15411 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
15412 			}
15413 
15414 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15415 			sd_print_transport_rejected_message(un, xp, rval);
15416 
15417 			/*
15418 			 * This command will be terminated by SD driver due
15419 			 * to a fatal transport error. We should post
15420 			 * ereport.io.scsi.cmd.disk.tran with driver-assessment
15421 			 * of "fail" for any command to indicate this
15422 			 * situation.
15423 			 */
15424 			if (xp->xb_ena > 0) {
15425 				ASSERT(un->un_fm_private != NULL);
15426 				sfip = un->un_fm_private;
15427 				sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15428 				sd_ssc_extract_info(&sfip->fm_ssc, un,
15429 				    xp->xb_pktp, bp, xp);
15430 				sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15431 			}
15432 
15433 			/*
15434 			 * We must use sd_return_failed_command_no_restart() to
15435 			 * avoid a recursive call back into sd_start_cmds().
15436 			 * However this also means that we must keep processing
15437 			 * the waitq here in order to avoid stalling.
15438 			 */
15439 			sd_return_failed_command_no_restart(un, bp, EIO);
15440 
15441 			/*
15442 			 * Notify any threads waiting in sd_ddi_suspend() that
15443 			 * a command completion has occurred.
15444 			 */
15445 			if (un->un_state == SD_STATE_SUSPENDED) {
15446 				cv_broadcast(&un->un_disk_busy_cv);
15447 			}
15448 
15449 			if (bp == immed_bp) {
15450 				/* immed_bp is gone by now, so clear this */
15451 				immed_bp = NULL;
15452 			}
15453 			break;
15454 		}
15455 
15456 	} while (immed_bp == NULL);
15457 
15458 exit:
15459 	ASSERT(mutex_owned(SD_MUTEX(un)));
15460 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15461 }
15462 
15463 
15464 /*
15465  *    Function: sd_return_command
15466  *
15467  * Description: Returns a command to its originator (with or without an
15468  *		error).  Also starts commands waiting to be transported
15469  *		to the target.
15470  *
15471  *     Context: May be called from interrupt, kernel, or timeout context
15472  */
15473 
15474 static void
15475 sd_return_command(struct sd_lun *un, struct buf *bp)
15476 {
15477 	struct sd_xbuf *xp;
15478 	struct scsi_pkt *pktp;
15479 	struct sd_fm_internal *sfip;
15480 
15481 	ASSERT(bp != NULL);
15482 	ASSERT(un != NULL);
15483 	ASSERT(mutex_owned(SD_MUTEX(un)));
15484 	ASSERT(bp != un->un_rqs_bp);
15485 	xp = SD_GET_XBUF(bp);
15486 	ASSERT(xp != NULL);
15487 
15488 	pktp = SD_GET_PKTP(bp);
15489 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15490 	ASSERT(sfip != NULL);
15491 
15492 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15493 
15494 	/*
15495 	 * Note: check for the "sdrestart failed" case.
15496 	 */
15497 	if ((un->un_partial_dma_supported == 1) &&
15498 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15499 	    (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15500 	    (xp->xb_pktp->pkt_resid == 0)) {
15501 
15502 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15503 			/*
15504 			 * Successfully set up next portion of cmd
15505 			 * transfer, try sending it
15506 			 */
15507 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15508 			    NULL, NULL, 0, (clock_t)0, NULL);
15509 			sd_start_cmds(un, NULL);
15510 			return;	/* Note:x86: need a return here? */
15511 		}
15512 	}
15513 
15514 	/*
15515 	 * If this is the failfast bp, clear it from un_failfast_bp. This
15516 	 * can happen if upon being re-tried the failfast bp either
15517 	 * succeeded or encountered another error (possibly even a different
15518 	 * error than the one that precipitated the failfast state, but in
15519 	 * that case it would have had to exhaust retries as well). Regardless,
15520 	 * this should not occur whenever the instance is in the active
15521 	 * failfast state.
15522 	 */
15523 	if (bp == un->un_failfast_bp) {
15524 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15525 		un->un_failfast_bp = NULL;
15526 	}
15527 
15528 	/*
15529 	 * Clear the failfast state upon successful completion of ANY cmd.
15530 	 */
15531 	if (bp->b_error == 0) {
15532 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15533 		/*
15534 		 * If this is a successful command, but used to be retried,
15535 		 * we will take it as a recovered command and post an
15536 		 * ereport with driver-assessment of "recovered".
15537 		 */
15538 		if (xp->xb_ena > 0) {
15539 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15540 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15541 		}
15542 	} else {
15543 		/*
15544 		 * If this is a failed non-USCSI command we will post an
15545 		 * ereport with driver-assessment set accordingly("fail" or
15546 		 * "fatal").
15547 		 */
15548 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15549 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15550 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15551 		}
15552 	}
15553 
15554 	/*
15555 	 * This is used if the command was retried one or more times. Show that
15556 	 * we are done with it, and allow processing of the waitq to resume.
15557 	 */
15558 	if (bp == un->un_retry_bp) {
15559 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15560 		    "sd_return_command: un:0x%p: "
15561 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15562 		un->un_retry_bp = NULL;
15563 		un->un_retry_statp = NULL;
15564 	}
15565 
15566 	SD_UPDATE_RDWR_STATS(un, bp);
15567 	SD_UPDATE_PARTITION_STATS(un, bp);
15568 
15569 	switch (un->un_state) {
15570 	case SD_STATE_SUSPENDED:
15571 		/*
15572 		 * Notify any threads waiting in sd_ddi_suspend() that
15573 		 * a command completion has occurred.
15574 		 */
15575 		cv_broadcast(&un->un_disk_busy_cv);
15576 		break;
15577 	default:
15578 		sd_start_cmds(un, NULL);
15579 		break;
15580 	}
15581 
15582 	/* Return this command up the iodone chain to its originator. */
15583 	mutex_exit(SD_MUTEX(un));
15584 
15585 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15586 	xp->xb_pktp = NULL;
15587 
15588 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15589 
15590 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15591 	mutex_enter(SD_MUTEX(un));
15592 
15593 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15594 }
15595 
15596 
15597 /*
15598  *    Function: sd_return_failed_command
15599  *
15600  * Description: Command completion when an error occurred.
15601  *
15602  *     Context: May be called from interrupt context
15603  */
15604 
15605 static void
15606 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15607 {
15608 	ASSERT(bp != NULL);
15609 	ASSERT(un != NULL);
15610 	ASSERT(mutex_owned(SD_MUTEX(un)));
15611 
15612 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15613 	    "sd_return_failed_command: entry\n");
15614 
15615 	/*
15616 	 * b_resid could already be nonzero due to a partial data
15617 	 * transfer, so do not change it here.
15618 	 */
15619 	SD_BIOERROR(bp, errcode);
15620 
15621 	sd_return_command(un, bp);
15622 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15623 	    "sd_return_failed_command: exit\n");
15624 }
15625 
15626 
15627 /*
15628  *    Function: sd_return_failed_command_no_restart
15629  *
15630  * Description: Same as sd_return_failed_command, but ensures that no
15631  *		call back into sd_start_cmds will be issued.
15632  *
15633  *     Context: May be called from interrupt context
15634  */
15635 
15636 static void
15637 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15638     int errcode)
15639 {
15640 	struct sd_xbuf *xp;
15641 
15642 	ASSERT(bp != NULL);
15643 	ASSERT(un != NULL);
15644 	ASSERT(mutex_owned(SD_MUTEX(un)));
15645 	xp = SD_GET_XBUF(bp);
15646 	ASSERT(xp != NULL);
15647 	ASSERT(errcode != 0);
15648 
15649 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15650 	    "sd_return_failed_command_no_restart: entry\n");
15651 
15652 	/*
15653 	 * b_resid could already be nonzero due to a partial data
15654 	 * transfer, so do not change it here.
15655 	 */
15656 	SD_BIOERROR(bp, errcode);
15657 
15658 	/*
15659 	 * If this is the failfast bp, clear it. This can happen if the
15660 	 * failfast bp encounterd a fatal error when we attempted to
15661 	 * re-try it (such as a scsi_transport(9F) failure).  However
15662 	 * we should NOT be in an active failfast state if the failfast
15663 	 * bp is not NULL.
15664 	 */
15665 	if (bp == un->un_failfast_bp) {
15666 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15667 		un->un_failfast_bp = NULL;
15668 	}
15669 
15670 	if (bp == un->un_retry_bp) {
15671 		/*
15672 		 * This command was retried one or more times. Show that we are
15673 		 * done with it, and allow processing of the waitq to resume.
15674 		 */
15675 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15676 		    "sd_return_failed_command_no_restart: "
15677 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15678 		un->un_retry_bp = NULL;
15679 		un->un_retry_statp = NULL;
15680 	}
15681 
15682 	SD_UPDATE_RDWR_STATS(un, bp);
15683 	SD_UPDATE_PARTITION_STATS(un, bp);
15684 
15685 	mutex_exit(SD_MUTEX(un));
15686 
15687 	if (xp->xb_pktp != NULL) {
15688 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15689 		xp->xb_pktp = NULL;
15690 	}
15691 
15692 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15693 
15694 	mutex_enter(SD_MUTEX(un));
15695 
15696 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15697 	    "sd_return_failed_command_no_restart: exit\n");
15698 }
15699 
15700 
15701 /*
15702  *    Function: sd_retry_command
15703  *
15704  * Description: queue up a command for retry, or (optionally) fail it
15705  *		if retry counts are exhausted.
15706  *
15707  *   Arguments: un - Pointer to the sd_lun struct for the target.
15708  *
15709  *		bp - Pointer to the buf for the command to be retried.
15710  *
15711  *		retry_check_flag - Flag to see which (if any) of the retry
15712  *		   counts should be decremented/checked. If the indicated
15713  *		   retry count is exhausted, then the command will not be
15714  *		   retried; it will be failed instead. This should use a
15715  *		   value equal to one of the following:
15716  *
15717  *			SD_RETRIES_NOCHECK
15718  *			SD_RESD_RETRIES_STANDARD
15719  *			SD_RETRIES_VICTIM
15720  *
15721  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15722  *		   if the check should be made to see of FLAG_ISOLATE is set
15723  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
15724  *		   not retried, it is simply failed.
15725  *
15726  *		user_funcp - Ptr to function to call before dispatching the
15727  *		   command. May be NULL if no action needs to be performed.
15728  *		   (Primarily intended for printing messages.)
15729  *
15730  *		user_arg - Optional argument to be passed along to
15731  *		   the user_funcp call.
15732  *
15733  *		failure_code - errno return code to set in the bp if the
15734  *		   command is going to be failed.
15735  *
15736  *		retry_delay - Retry delay interval in (clock_t) units. May
15737  *		   be zero which indicates that the retry should be retried
15738  *		   immediately (ie, without an intervening delay).
15739  *
15740  *		statp - Ptr to kstat function to be updated if the command
15741  *		   is queued for a delayed retry. May be NULL if no kstat
15742  *		   update is desired.
15743  *
15744  *     Context: May be called from interrupt context.
15745  */
15746 
15747 static void
15748 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15749     void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int code),
15750     void *user_arg, int failure_code, clock_t retry_delay,
15751     void (*statp)(kstat_io_t *))
15752 {
15753 	struct sd_xbuf	*xp;
15754 	struct scsi_pkt	*pktp;
15755 	struct sd_fm_internal *sfip;
15756 
15757 	ASSERT(un != NULL);
15758 	ASSERT(mutex_owned(SD_MUTEX(un)));
15759 	ASSERT(bp != NULL);
15760 	xp = SD_GET_XBUF(bp);
15761 	ASSERT(xp != NULL);
15762 	pktp = SD_GET_PKTP(bp);
15763 	ASSERT(pktp != NULL);
15764 
15765 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15766 	ASSERT(sfip != NULL);
15767 
15768 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15769 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15770 
15771 	/*
15772 	 * If we are syncing or dumping, fail the command to avoid
15773 	 * recursively calling back into scsi_transport().
15774 	 */
15775 	if (ddi_in_panic()) {
15776 		goto fail_command_no_log;
15777 	}
15778 
15779 	/*
15780 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15781 	 * log an error and fail the command.
15782 	 */
15783 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15784 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15785 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
15786 		sd_dump_memory(un, SD_LOG_IO, "CDB",
15787 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15788 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15789 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15790 		goto fail_command;
15791 	}
15792 
15793 	/*
15794 	 * If we are suspended, then put the command onto head of the
15795 	 * wait queue since we don't want to start more commands, and
15796 	 * clear the un_retry_bp. Next time when we are resumed, will
15797 	 * handle the command in the wait queue.
15798 	 */
15799 	switch (un->un_state) {
15800 	case SD_STATE_SUSPENDED:
15801 	case SD_STATE_DUMPING:
15802 		bp->av_forw = un->un_waitq_headp;
15803 		un->un_waitq_headp = bp;
15804 		if (un->un_waitq_tailp == NULL) {
15805 			un->un_waitq_tailp = bp;
15806 		}
15807 		if (bp == un->un_retry_bp) {
15808 			un->un_retry_bp = NULL;
15809 			un->un_retry_statp = NULL;
15810 		}
15811 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15812 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15813 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15814 		return;
15815 	default:
15816 		break;
15817 	}
15818 
15819 	/*
15820 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
15821 	 * is set; if it is then we do not want to retry the command.
15822 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15823 	 */
15824 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15825 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15826 			goto fail_command;
15827 		}
15828 	}
15829 
15830 
15831 	/*
15832 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15833 	 * command timeout or a selection timeout has occurred. This means
15834 	 * that we were unable to establish an kind of communication with
15835 	 * the target, and subsequent retries and/or commands are likely
15836 	 * to encounter similar results and take a long time to complete.
15837 	 *
15838 	 * If this is a failfast error condition, we need to update the
15839 	 * failfast state, even if this bp does not have B_FAILFAST set.
15840 	 */
15841 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
15842 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15843 			ASSERT(un->un_failfast_bp == NULL);
15844 			/*
15845 			 * If we are already in the active failfast state, and
15846 			 * another failfast error condition has been detected,
15847 			 * then fail this command if it has B_FAILFAST set.
15848 			 * If B_FAILFAST is clear, then maintain the legacy
15849 			 * behavior of retrying heroically, even tho this will
15850 			 * take a lot more time to fail the command.
15851 			 */
15852 			if (bp->b_flags & B_FAILFAST) {
15853 				goto fail_command;
15854 			}
15855 		} else {
15856 			/*
15857 			 * We're not in the active failfast state, but we
15858 			 * have a failfast error condition, so we must begin
15859 			 * transition to the next state. We do this regardless
15860 			 * of whether or not this bp has B_FAILFAST set.
15861 			 */
15862 			if (un->un_failfast_bp == NULL) {
15863 				/*
15864 				 * This is the first bp to meet a failfast
15865 				 * condition so save it on un_failfast_bp &
15866 				 * do normal retry processing. Do not enter
15867 				 * active failfast state yet. This marks
15868 				 * entry into the "failfast pending" state.
15869 				 */
15870 				un->un_failfast_bp = bp;
15871 
15872 			} else if (un->un_failfast_bp == bp) {
15873 				/*
15874 				 * This is the second time *this* bp has
15875 				 * encountered a failfast error condition,
15876 				 * so enter active failfast state & flush
15877 				 * queues as appropriate.
15878 				 */
15879 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
15880 				un->un_failfast_bp = NULL;
15881 				sd_failfast_flushq(un);
15882 
15883 				/*
15884 				 * Fail this bp now if B_FAILFAST set;
15885 				 * otherwise continue with retries. (It would
15886 				 * be pretty ironic if this bp succeeded on a
15887 				 * subsequent retry after we just flushed all
15888 				 * the queues).
15889 				 */
15890 				if (bp->b_flags & B_FAILFAST) {
15891 					goto fail_command;
15892 				}
15893 
15894 #if !defined(lint) && !defined(__lint)
15895 			} else {
15896 				/*
15897 				 * If neither of the preceeding conditionals
15898 				 * was true, it means that there is some
15899 				 * *other* bp that has met an inital failfast
15900 				 * condition and is currently either being
15901 				 * retried or is waiting to be retried. In
15902 				 * that case we should perform normal retry
15903 				 * processing on *this* bp, since there is a
15904 				 * chance that the current failfast condition
15905 				 * is transient and recoverable. If that does
15906 				 * not turn out to be the case, then retries
15907 				 * will be cleared when the wait queue is
15908 				 * flushed anyway.
15909 				 */
15910 #endif
15911 			}
15912 		}
15913 	} else {
15914 		/*
15915 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
15916 		 * likely were able to at least establish some level of
15917 		 * communication with the target and subsequent commands
15918 		 * and/or retries are likely to get through to the target,
15919 		 * In this case we want to be aggressive about clearing
15920 		 * the failfast state. Note that this does not affect
15921 		 * the "failfast pending" condition.
15922 		 */
15923 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15924 	}
15925 
15926 
15927 	/*
15928 	 * Check the specified retry count to see if we can still do
15929 	 * any retries with this pkt before we should fail it.
15930 	 */
15931 	switch (retry_check_flag & SD_RETRIES_MASK) {
15932 	case SD_RETRIES_VICTIM:
15933 		/*
15934 		 * Check the victim retry count. If exhausted, then fall
15935 		 * thru & check against the standard retry count.
15936 		 */
15937 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15938 			/* Increment count & proceed with the retry */
15939 			xp->xb_victim_retry_count++;
15940 			break;
15941 		}
15942 		/* Victim retries exhausted, fall back to std. retries... */
15943 		/* FALLTHRU */
15944 
15945 	case SD_RETRIES_STANDARD:
15946 		if (xp->xb_retry_count >= un->un_retry_count) {
15947 			/* Retries exhausted, fail the command */
15948 			SD_TRACE(SD_LOG_IO_CORE, un,
15949 			    "sd_retry_command: retries exhausted!\n");
15950 			/*
15951 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
15952 			 * commands with nonzero pkt_resid.
15953 			 */
15954 			if ((pktp->pkt_reason == CMD_CMPLT) &&
15955 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15956 			    (pktp->pkt_resid != 0)) {
15957 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15958 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15959 					SD_UPDATE_B_RESID(bp, pktp);
15960 				}
15961 			}
15962 			goto fail_command;
15963 		}
15964 		xp->xb_retry_count++;
15965 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15966 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15967 		break;
15968 
15969 	case SD_RETRIES_UA:
15970 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15971 			/* Retries exhausted, fail the command */
15972 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15973 			    "Unit Attention retries exhausted. "
15974 			    "Check the target.\n");
15975 			goto fail_command;
15976 		}
15977 		xp->xb_ua_retry_count++;
15978 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15979 		    "sd_retry_command: retry count:%d\n",
15980 		    xp->xb_ua_retry_count);
15981 		break;
15982 
15983 	case SD_RETRIES_BUSY:
15984 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
15985 			/* Retries exhausted, fail the command */
15986 			SD_TRACE(SD_LOG_IO_CORE, un,
15987 			    "sd_retry_command: retries exhausted!\n");
15988 			goto fail_command;
15989 		}
15990 		xp->xb_retry_count++;
15991 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15992 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15993 		break;
15994 
15995 	case SD_RETRIES_NOCHECK:
15996 	default:
15997 		/* No retry count to check. Just proceed with the retry */
15998 		break;
15999 	}
16000 
16001 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
16002 
16003 	/*
16004 	 * If this is a non-USCSI command being retried
16005 	 * during execution last time, we should post an ereport with
16006 	 * driver-assessment of the value "retry".
16007 	 * For partial DMA, request sense and STATUS_QFULL, there are no
16008 	 * hardware errors, we bypass ereport posting.
16009 	 */
16010 	if (failure_code != 0) {
16011 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
16012 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
16013 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
16014 		}
16015 	}
16016 
16017 	/*
16018 	 * If we were given a zero timeout, we must attempt to retry the
16019 	 * command immediately (ie, without a delay).
16020 	 */
16021 	if (retry_delay == 0) {
16022 		/*
16023 		 * Check some limiting conditions to see if we can actually
16024 		 * do the immediate retry.  If we cannot, then we must
16025 		 * fall back to queueing up a delayed retry.
16026 		 */
16027 		if (un->un_ncmds_in_transport >= un->un_throttle) {
16028 			/*
16029 			 * We are at the throttle limit for the target,
16030 			 * fall back to delayed retry.
16031 			 */
16032 			retry_delay = un->un_busy_timeout;
16033 			statp = kstat_waitq_enter;
16034 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16035 			    "sd_retry_command: immed. retry hit "
16036 			    "throttle!\n");
16037 		} else {
16038 			/*
16039 			 * We're clear to proceed with the immediate retry.
16040 			 * First call the user-provided function (if any)
16041 			 */
16042 			if (user_funcp != NULL) {
16043 				(*user_funcp)(un, bp, user_arg,
16044 				    SD_IMMEDIATE_RETRY_ISSUED);
16045 #ifdef __lock_lint
16046 				sd_print_incomplete_msg(un, bp, user_arg,
16047 				    SD_IMMEDIATE_RETRY_ISSUED);
16048 				sd_print_cmd_incomplete_msg(un, bp, user_arg,
16049 				    SD_IMMEDIATE_RETRY_ISSUED);
16050 				sd_print_sense_failed_msg(un, bp, user_arg,
16051 				    SD_IMMEDIATE_RETRY_ISSUED);
16052 #endif
16053 			}
16054 
16055 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16056 			    "sd_retry_command: issuing immediate retry\n");
16057 
16058 			/*
16059 			 * Call sd_start_cmds() to transport the command to
16060 			 * the target.
16061 			 */
16062 			sd_start_cmds(un, bp);
16063 
16064 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16065 			    "sd_retry_command exit\n");
16066 			return;
16067 		}
16068 	}
16069 
16070 	/*
16071 	 * Set up to retry the command after a delay.
16072 	 * First call the user-provided function (if any)
16073 	 */
16074 	if (user_funcp != NULL) {
16075 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
16076 	}
16077 
16078 	sd_set_retry_bp(un, bp, retry_delay, statp);
16079 
16080 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
16081 	return;
16082 
16083 fail_command:
16084 
16085 	if (user_funcp != NULL) {
16086 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
16087 	}
16088 
16089 fail_command_no_log:
16090 
16091 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16092 	    "sd_retry_command: returning failed command\n");
16093 
16094 	sd_return_failed_command(un, bp, failure_code);
16095 
16096 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
16097 }
16098 
16099 
16100 /*
16101  *    Function: sd_set_retry_bp
16102  *
16103  * Description: Set up the given bp for retry.
16104  *
16105  *   Arguments: un - ptr to associated softstate
16106  *		bp - ptr to buf(9S) for the command
16107  *		retry_delay - time interval before issuing retry (may be 0)
16108  *		statp - optional pointer to kstat function
16109  *
16110  *     Context: May be called under interrupt context
16111  */
16112 
16113 static void
16114 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
16115     void (*statp)(kstat_io_t *))
16116 {
16117 	ASSERT(un != NULL);
16118 	ASSERT(mutex_owned(SD_MUTEX(un)));
16119 	ASSERT(bp != NULL);
16120 
16121 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16122 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
16123 
16124 	/*
16125 	 * Indicate that the command is being retried. This will not allow any
16126 	 * other commands on the wait queue to be transported to the target
16127 	 * until this command has been completed (success or failure). The
16128 	 * "retry command" is not transported to the target until the given
16129 	 * time delay expires, unless the user specified a 0 retry_delay.
16130 	 *
16131 	 * Note: the timeout(9F) callback routine is what actually calls
16132 	 * sd_start_cmds() to transport the command, with the exception of a
16133 	 * zero retry_delay. The only current implementor of a zero retry delay
16134 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
16135 	 */
16136 	if (un->un_retry_bp == NULL) {
16137 		ASSERT(un->un_retry_statp == NULL);
16138 		un->un_retry_bp = bp;
16139 
16140 		/*
16141 		 * If the user has not specified a delay the command should
16142 		 * be queued and no timeout should be scheduled.
16143 		 */
16144 		if (retry_delay == 0) {
16145 			/*
16146 			 * Save the kstat pointer that will be used in the
16147 			 * call to SD_UPDATE_KSTATS() below, so that
16148 			 * sd_start_cmds() can correctly decrement the waitq
16149 			 * count when it is time to transport this command.
16150 			 */
16151 			un->un_retry_statp = statp;
16152 			goto done;
16153 		}
16154 	}
16155 
16156 	if (un->un_retry_bp == bp) {
16157 		/*
16158 		 * Save the kstat pointer that will be used in the call to
16159 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
16160 		 * correctly decrement the waitq count when it is time to
16161 		 * transport this command.
16162 		 */
16163 		un->un_retry_statp = statp;
16164 
16165 		/*
16166 		 * Schedule a timeout if:
16167 		 *   1) The user has specified a delay.
16168 		 *   2) There is not a START_STOP_UNIT callback pending.
16169 		 *
16170 		 * If no delay has been specified, then it is up to the caller
16171 		 * to ensure that IO processing continues without stalling.
16172 		 * Effectively, this means that the caller will issue the
16173 		 * required call to sd_start_cmds(). The START_STOP_UNIT
16174 		 * callback does this after the START STOP UNIT command has
16175 		 * completed. In either of these cases we should not schedule
16176 		 * a timeout callback here.  Also don't schedule the timeout if
16177 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
16178 		 */
16179 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
16180 		    (un->un_direct_priority_timeid == NULL)) {
16181 			un->un_retry_timeid =
16182 			    timeout(sd_start_retry_command, un, retry_delay);
16183 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16184 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
16185 			    " bp:0x%p un_retry_timeid:0x%p\n",
16186 			    un, bp, un->un_retry_timeid);
16187 		}
16188 	} else {
16189 		/*
16190 		 * We only get in here if there is already another command
16191 		 * waiting to be retried.  In this case, we just put the
16192 		 * given command onto the wait queue, so it can be transported
16193 		 * after the current retry command has completed.
16194 		 *
16195 		 * Also we have to make sure that if the command at the head
16196 		 * of the wait queue is the un_failfast_bp, that we do not
16197 		 * put ahead of it any other commands that are to be retried.
16198 		 */
16199 		if ((un->un_failfast_bp != NULL) &&
16200 		    (un->un_failfast_bp == un->un_waitq_headp)) {
16201 			/*
16202 			 * Enqueue this command AFTER the first command on
16203 			 * the wait queue (which is also un_failfast_bp).
16204 			 */
16205 			bp->av_forw = un->un_waitq_headp->av_forw;
16206 			un->un_waitq_headp->av_forw = bp;
16207 			if (un->un_waitq_headp == un->un_waitq_tailp) {
16208 				un->un_waitq_tailp = bp;
16209 			}
16210 		} else {
16211 			/* Enqueue this command at the head of the waitq. */
16212 			bp->av_forw = un->un_waitq_headp;
16213 			un->un_waitq_headp = bp;
16214 			if (un->un_waitq_tailp == NULL) {
16215 				un->un_waitq_tailp = bp;
16216 			}
16217 		}
16218 
16219 		if (statp == NULL) {
16220 			statp = kstat_waitq_enter;
16221 		}
16222 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16223 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
16224 	}
16225 
16226 done:
16227 	if (statp != NULL) {
16228 		SD_UPDATE_KSTATS(un, statp, bp);
16229 	}
16230 
16231 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16232 	    "sd_set_retry_bp: exit un:0x%p\n", un);
16233 }
16234 
16235 
16236 /*
16237  *    Function: sd_start_retry_command
16238  *
16239  * Description: Start the command that has been waiting on the target's
16240  *		retry queue.  Called from timeout(9F) context after the
16241  *		retry delay interval has expired.
16242  *
16243  *   Arguments: arg - pointer to associated softstate for the device.
16244  *
16245  *     Context: timeout(9F) thread context.  May not sleep.
16246  */
16247 
16248 static void
16249 sd_start_retry_command(void *arg)
16250 {
16251 	struct sd_lun *un = arg;
16252 
16253 	ASSERT(un != NULL);
16254 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16255 
16256 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16257 	    "sd_start_retry_command: entry\n");
16258 
16259 	mutex_enter(SD_MUTEX(un));
16260 
16261 	un->un_retry_timeid = NULL;
16262 
16263 	if (un->un_retry_bp != NULL) {
16264 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16265 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16266 		    un, un->un_retry_bp);
16267 		sd_start_cmds(un, un->un_retry_bp);
16268 	}
16269 
16270 	mutex_exit(SD_MUTEX(un));
16271 
16272 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16273 	    "sd_start_retry_command: exit\n");
16274 }
16275 
16276 /*
16277  *    Function: sd_rmw_msg_print_handler
16278  *
16279  * Description: If RMW mode is enabled and warning message is triggered
16280  *              print I/O count during a fixed interval.
16281  *
16282  *   Arguments: arg - pointer to associated softstate for the device.
16283  *
16284  *     Context: timeout(9F) thread context. May not sleep.
16285  */
16286 static void
16287 sd_rmw_msg_print_handler(void *arg)
16288 {
16289 	struct sd_lun *un = arg;
16290 
16291 	ASSERT(un != NULL);
16292 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16293 
16294 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16295 	    "sd_rmw_msg_print_handler: entry\n");
16296 
16297 	mutex_enter(SD_MUTEX(un));
16298 
16299 	if (un->un_rmw_incre_count > 0) {
16300 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16301 		    "%"PRIu64" I/O requests are not aligned with %d disk "
16302 		    "sector size in %ld seconds. They are handled through "
16303 		    "Read Modify Write but the performance is very low!\n",
16304 		    un->un_rmw_incre_count, un->un_tgt_blocksize,
16305 		    drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
16306 		un->un_rmw_incre_count = 0;
16307 		un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
16308 		    un, SD_RMW_MSG_PRINT_TIMEOUT);
16309 	} else {
16310 		un->un_rmw_msg_timeid = NULL;
16311 	}
16312 
16313 	mutex_exit(SD_MUTEX(un));
16314 
16315 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16316 	    "sd_rmw_msg_print_handler: exit\n");
16317 }
16318 
16319 /*
16320  *    Function: sd_start_direct_priority_command
16321  *
16322  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16323  *		received TRAN_BUSY when we called scsi_transport() to send it
16324  *		to the underlying HBA. This function is called from timeout(9F)
16325  *		context after the delay interval has expired.
16326  *
16327  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
16328  *
16329  *     Context: timeout(9F) thread context.  May not sleep.
16330  */
16331 
16332 static void
16333 sd_start_direct_priority_command(void *arg)
16334 {
16335 	struct buf	*priority_bp = arg;
16336 	struct sd_lun	*un;
16337 
16338 	ASSERT(priority_bp != NULL);
16339 	un = SD_GET_UN(priority_bp);
16340 	ASSERT(un != NULL);
16341 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16342 
16343 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16344 	    "sd_start_direct_priority_command: entry\n");
16345 
16346 	mutex_enter(SD_MUTEX(un));
16347 	un->un_direct_priority_timeid = NULL;
16348 	sd_start_cmds(un, priority_bp);
16349 	mutex_exit(SD_MUTEX(un));
16350 
16351 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16352 	    "sd_start_direct_priority_command: exit\n");
16353 }
16354 
16355 
16356 /*
16357  *    Function: sd_send_request_sense_command
16358  *
16359  * Description: Sends a REQUEST SENSE command to the target
16360  *
16361  *     Context: May be called from interrupt context.
16362  */
16363 
16364 static void
16365 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16366     struct scsi_pkt *pktp)
16367 {
16368 	ASSERT(bp != NULL);
16369 	ASSERT(un != NULL);
16370 	ASSERT(mutex_owned(SD_MUTEX(un)));
16371 
16372 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16373 	    "entry: buf:0x%p\n", bp);
16374 
16375 	/*
16376 	 * If we are syncing or dumping, then fail the command to avoid a
16377 	 * recursive callback into scsi_transport(). Also fail the command
16378 	 * if we are suspended (legacy behavior).
16379 	 */
16380 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16381 	    (un->un_state == SD_STATE_DUMPING)) {
16382 		sd_return_failed_command(un, bp, EIO);
16383 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16384 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
16385 		return;
16386 	}
16387 
16388 	/*
16389 	 * Retry the failed command and don't issue the request sense if:
16390 	 *    1) the sense buf is busy
16391 	 *    2) we have 1 or more outstanding commands on the target
16392 	 *    (the sense data will be cleared or invalidated any way)
16393 	 *
16394 	 * Note: There could be an issue with not checking a retry limit here,
16395 	 * the problem is determining which retry limit to check.
16396 	 */
16397 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16398 		/* Don't retry if the command is flagged as non-retryable */
16399 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16400 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16401 			    NULL, NULL, 0, un->un_busy_timeout,
16402 			    kstat_waitq_enter);
16403 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16404 			    "sd_send_request_sense_command: "
16405 			    "at full throttle, retrying exit\n");
16406 		} else {
16407 			sd_return_failed_command(un, bp, EIO);
16408 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16409 			    "sd_send_request_sense_command: "
16410 			    "at full throttle, non-retryable exit\n");
16411 		}
16412 		return;
16413 	}
16414 
16415 	sd_mark_rqs_busy(un, bp);
16416 	sd_start_cmds(un, un->un_rqs_bp);
16417 
16418 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16419 	    "sd_send_request_sense_command: exit\n");
16420 }
16421 
16422 
16423 /*
16424  *    Function: sd_mark_rqs_busy
16425  *
16426  * Description: Indicate that the request sense bp for this instance is
16427  *		in use.
16428  *
16429  *     Context: May be called under interrupt context
16430  */
16431 
16432 static void
16433 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16434 {
16435 	struct sd_xbuf	*sense_xp;
16436 
16437 	ASSERT(un != NULL);
16438 	ASSERT(bp != NULL);
16439 	ASSERT(mutex_owned(SD_MUTEX(un)));
16440 	ASSERT(un->un_sense_isbusy == 0);
16441 
16442 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16443 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16444 
16445 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16446 	ASSERT(sense_xp != NULL);
16447 
16448 	SD_INFO(SD_LOG_IO, un,
16449 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16450 
16451 	ASSERT(sense_xp->xb_pktp != NULL);
16452 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16453 	    == (FLAG_SENSING | FLAG_HEAD));
16454 
16455 	un->un_sense_isbusy = 1;
16456 	un->un_rqs_bp->b_resid = 0;
16457 	sense_xp->xb_pktp->pkt_resid  = 0;
16458 	sense_xp->xb_pktp->pkt_reason = 0;
16459 
16460 	/* So we can get back the bp at interrupt time! */
16461 	sense_xp->xb_sense_bp = bp;
16462 
16463 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16464 
16465 	/*
16466 	 * Mark this buf as awaiting sense data. (This is already set in
16467 	 * the pkt_flags for the RQS packet.)
16468 	 */
16469 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16470 
16471 	/* Request sense down same path */
16472 	if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16473 	    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16474 		sense_xp->xb_pktp->pkt_path_instance =
16475 		    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16476 
16477 	sense_xp->xb_retry_count	= 0;
16478 	sense_xp->xb_victim_retry_count = 0;
16479 	sense_xp->xb_ua_retry_count	= 0;
16480 	sense_xp->xb_nr_retry_count 	= 0;
16481 	sense_xp->xb_dma_resid  = 0;
16482 
16483 	/* Clean up the fields for auto-request sense */
16484 	sense_xp->xb_sense_status = 0;
16485 	sense_xp->xb_sense_state  = 0;
16486 	sense_xp->xb_sense_resid  = 0;
16487 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16488 
16489 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16490 }
16491 
16492 
16493 /*
16494  *    Function: sd_mark_rqs_idle
16495  *
16496  * Description: SD_MUTEX must be held continuously through this routine
16497  *		to prevent reuse of the rqs struct before the caller can
16498  *		complete it's processing.
16499  *
16500  * Return Code: Pointer to the RQS buf
16501  *
16502  *     Context: May be called under interrupt context
16503  */
16504 
16505 static struct buf *
16506 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16507 {
16508 	struct buf *bp;
16509 	ASSERT(un != NULL);
16510 	ASSERT(sense_xp != NULL);
16511 	ASSERT(mutex_owned(SD_MUTEX(un)));
16512 	ASSERT(un->un_sense_isbusy != 0);
16513 
16514 	un->un_sense_isbusy = 0;
16515 	bp = sense_xp->xb_sense_bp;
16516 	sense_xp->xb_sense_bp = NULL;
16517 
16518 	/* This pkt is no longer interested in getting sense data */
16519 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16520 
16521 	return (bp);
16522 }
16523 
16524 
16525 
16526 /*
16527  *    Function: sd_alloc_rqs
16528  *
16529  * Description: Set up the unit to receive auto request sense data
16530  *
16531  * Return Code: DDI_SUCCESS or DDI_FAILURE
16532  *
16533  *     Context: Called under attach(9E) context
16534  */
16535 
16536 static int
16537 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16538 {
16539 	struct sd_xbuf *xp;
16540 
16541 	ASSERT(un != NULL);
16542 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16543 	ASSERT(un->un_rqs_bp == NULL);
16544 	ASSERT(un->un_rqs_pktp == NULL);
16545 
16546 	/*
16547 	 * First allocate the required buf and scsi_pkt structs, then set up
16548 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
16549 	 */
16550 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16551 	    MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16552 	if (un->un_rqs_bp == NULL) {
16553 		return (DDI_FAILURE);
16554 	}
16555 
16556 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16557 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16558 
16559 	if (un->un_rqs_pktp == NULL) {
16560 		sd_free_rqs(un);
16561 		return (DDI_FAILURE);
16562 	}
16563 
16564 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16565 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16566 	    SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16567 
16568 	SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16569 
16570 	/* Set up the other needed members in the ARQ scsi_pkt. */
16571 	un->un_rqs_pktp->pkt_comp   = sdintr;
16572 	un->un_rqs_pktp->pkt_time   = sd_io_time;
16573 	un->un_rqs_pktp->pkt_flags |=
16574 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
16575 
16576 	/*
16577 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16578 	 * provide any intpkt, destroypkt routines as we take care of
16579 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16580 	 */
16581 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16582 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16583 	xp->xb_pktp = un->un_rqs_pktp;
16584 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
16585 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16586 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16587 
16588 	/*
16589 	 * Save the pointer to the request sense private bp so it can
16590 	 * be retrieved in sdintr.
16591 	 */
16592 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16593 	ASSERT(un->un_rqs_bp->b_private == xp);
16594 
16595 	/*
16596 	 * See if the HBA supports auto-request sense for the specified
16597 	 * target/lun. If it does, then try to enable it (if not already
16598 	 * enabled).
16599 	 *
16600 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16601 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16602 	 * return success.  However, in both of these cases ARQ is always
16603 	 * enabled and scsi_ifgetcap will always return true. The best approach
16604 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16605 	 *
16606 	 * The 3rd case is the HBA (adp) always return enabled on
16607 	 * scsi_ifgetgetcap even when it's not enable, the best approach
16608 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16609 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16610 	 */
16611 
16612 	if (un->un_f_is_fibre == TRUE) {
16613 		un->un_f_arq_enabled = TRUE;
16614 	} else {
16615 #if defined(__i386) || defined(__amd64)
16616 		/*
16617 		 * Circumvent the Adaptec bug, remove this code when
16618 		 * the bug is fixed
16619 		 */
16620 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16621 #endif
16622 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16623 		case 0:
16624 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16625 			    "sd_alloc_rqs: HBA supports ARQ\n");
16626 			/*
16627 			 * ARQ is supported by this HBA but currently is not
16628 			 * enabled. Attempt to enable it and if successful then
16629 			 * mark this instance as ARQ enabled.
16630 			 */
16631 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16632 			    == 1) {
16633 				/* Successfully enabled ARQ in the HBA */
16634 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16635 				    "sd_alloc_rqs: ARQ enabled\n");
16636 				un->un_f_arq_enabled = TRUE;
16637 			} else {
16638 				/* Could not enable ARQ in the HBA */
16639 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16640 				    "sd_alloc_rqs: failed ARQ enable\n");
16641 				un->un_f_arq_enabled = FALSE;
16642 			}
16643 			break;
16644 		case 1:
16645 			/*
16646 			 * ARQ is supported by this HBA and is already enabled.
16647 			 * Just mark ARQ as enabled for this instance.
16648 			 */
16649 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16650 			    "sd_alloc_rqs: ARQ already enabled\n");
16651 			un->un_f_arq_enabled = TRUE;
16652 			break;
16653 		default:
16654 			/*
16655 			 * ARQ is not supported by this HBA; disable it for this
16656 			 * instance.
16657 			 */
16658 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16659 			    "sd_alloc_rqs: HBA does not support ARQ\n");
16660 			un->un_f_arq_enabled = FALSE;
16661 			break;
16662 		}
16663 	}
16664 
16665 	return (DDI_SUCCESS);
16666 }
16667 
16668 
16669 /*
16670  *    Function: sd_free_rqs
16671  *
16672  * Description: Cleanup for the pre-instance RQS command.
16673  *
16674  *     Context: Kernel thread context
16675  */
16676 
16677 static void
16678 sd_free_rqs(struct sd_lun *un)
16679 {
16680 	ASSERT(un != NULL);
16681 
16682 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16683 
16684 	/*
16685 	 * If consistent memory is bound to a scsi_pkt, the pkt
16686 	 * has to be destroyed *before* freeing the consistent memory.
16687 	 * Don't change the sequence of this operations.
16688 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
16689 	 * after it was freed in scsi_free_consistent_buf().
16690 	 */
16691 	if (un->un_rqs_pktp != NULL) {
16692 		scsi_destroy_pkt(un->un_rqs_pktp);
16693 		un->un_rqs_pktp = NULL;
16694 	}
16695 
16696 	if (un->un_rqs_bp != NULL) {
16697 		struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16698 		if (xp != NULL) {
16699 			kmem_free(xp, sizeof (struct sd_xbuf));
16700 		}
16701 		scsi_free_consistent_buf(un->un_rqs_bp);
16702 		un->un_rqs_bp = NULL;
16703 	}
16704 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16705 }
16706 
16707 
16708 
16709 /*
16710  *    Function: sd_reduce_throttle
16711  *
16712  * Description: Reduces the maximum # of outstanding commands on a
16713  *		target to the current number of outstanding commands.
16714  *		Queues a tiemout(9F) callback to restore the limit
16715  *		after a specified interval has elapsed.
16716  *		Typically used when we get a TRAN_BUSY return code
16717  *		back from scsi_transport().
16718  *
16719  *   Arguments: un - ptr to the sd_lun softstate struct
16720  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16721  *
16722  *     Context: May be called from interrupt context
16723  */
16724 
16725 static void
16726 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16727 {
16728 	ASSERT(un != NULL);
16729 	ASSERT(mutex_owned(SD_MUTEX(un)));
16730 	ASSERT(un->un_ncmds_in_transport >= 0);
16731 
16732 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16733 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16734 	    un, un->un_throttle, un->un_ncmds_in_transport);
16735 
16736 	if (un->un_throttle > 1) {
16737 		if (un->un_f_use_adaptive_throttle == TRUE) {
16738 			switch (throttle_type) {
16739 			case SD_THROTTLE_TRAN_BUSY:
16740 				if (un->un_busy_throttle == 0) {
16741 					un->un_busy_throttle = un->un_throttle;
16742 				}
16743 				break;
16744 			case SD_THROTTLE_QFULL:
16745 				un->un_busy_throttle = 0;
16746 				break;
16747 			default:
16748 				ASSERT(FALSE);
16749 			}
16750 
16751 			if (un->un_ncmds_in_transport > 0) {
16752 				un->un_throttle = un->un_ncmds_in_transport;
16753 			}
16754 
16755 		} else {
16756 			if (un->un_ncmds_in_transport == 0) {
16757 				un->un_throttle = 1;
16758 			} else {
16759 				un->un_throttle = un->un_ncmds_in_transport;
16760 			}
16761 		}
16762 	}
16763 
16764 	/* Reschedule the timeout if none is currently active */
16765 	if (un->un_reset_throttle_timeid == NULL) {
16766 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16767 		    un, SD_THROTTLE_RESET_INTERVAL);
16768 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16769 		    "sd_reduce_throttle: timeout scheduled!\n");
16770 	}
16771 
16772 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16773 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16774 }
16775 
16776 
16777 
16778 /*
16779  *    Function: sd_restore_throttle
16780  *
16781  * Description: Callback function for timeout(9F).  Resets the current
16782  *		value of un->un_throttle to its default.
16783  *
16784  *   Arguments: arg - pointer to associated softstate for the device.
16785  *
16786  *     Context: May be called from interrupt context
16787  */
16788 
16789 static void
16790 sd_restore_throttle(void *arg)
16791 {
16792 	struct sd_lun	*un = arg;
16793 
16794 	ASSERT(un != NULL);
16795 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16796 
16797 	mutex_enter(SD_MUTEX(un));
16798 
16799 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16800 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16801 
16802 	un->un_reset_throttle_timeid = NULL;
16803 
16804 	if (un->un_f_use_adaptive_throttle == TRUE) {
16805 		/*
16806 		 * If un_busy_throttle is nonzero, then it contains the
16807 		 * value that un_throttle was when we got a TRAN_BUSY back
16808 		 * from scsi_transport(). We want to revert back to this
16809 		 * value.
16810 		 *
16811 		 * In the QFULL case, the throttle limit will incrementally
16812 		 * increase until it reaches max throttle.
16813 		 */
16814 		if (un->un_busy_throttle > 0) {
16815 			un->un_throttle = un->un_busy_throttle;
16816 			un->un_busy_throttle = 0;
16817 		} else {
16818 			/*
16819 			 * increase throttle by 10% open gate slowly, schedule
16820 			 * another restore if saved throttle has not been
16821 			 * reached
16822 			 */
16823 			short throttle;
16824 			if (sd_qfull_throttle_enable) {
16825 				throttle = un->un_throttle +
16826 				    max((un->un_throttle / 10), 1);
16827 				un->un_throttle =
16828 				    (throttle < un->un_saved_throttle) ?
16829 				    throttle : un->un_saved_throttle;
16830 				if (un->un_throttle < un->un_saved_throttle) {
16831 					un->un_reset_throttle_timeid =
16832 					    timeout(sd_restore_throttle,
16833 					    un,
16834 					    SD_QFULL_THROTTLE_RESET_INTERVAL);
16835 				}
16836 			}
16837 		}
16838 
16839 		/*
16840 		 * If un_throttle has fallen below the low-water mark, we
16841 		 * restore the maximum value here (and allow it to ratchet
16842 		 * down again if necessary).
16843 		 */
16844 		if (un->un_throttle < un->un_min_throttle) {
16845 			un->un_throttle = un->un_saved_throttle;
16846 		}
16847 	} else {
16848 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16849 		    "restoring limit from 0x%x to 0x%x\n",
16850 		    un->un_throttle, un->un_saved_throttle);
16851 		un->un_throttle = un->un_saved_throttle;
16852 	}
16853 
16854 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16855 	    "sd_restore_throttle: calling sd_start_cmds!\n");
16856 
16857 	sd_start_cmds(un, NULL);
16858 
16859 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16860 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16861 	    un, un->un_throttle);
16862 
16863 	mutex_exit(SD_MUTEX(un));
16864 
16865 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16866 }
16867 
16868 /*
16869  *    Function: sdrunout
16870  *
16871  * Description: Callback routine for scsi_init_pkt when a resource allocation
16872  *		fails.
16873  *
16874  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16875  *		soft state instance.
16876  *
16877  * Return Code: The scsi_init_pkt routine allows for the callback function to
16878  *		return a 0 indicating the callback should be rescheduled or a 1
16879  *		indicating not to reschedule. This routine always returns 1
16880  *		because the driver always provides a callback function to
16881  *		scsi_init_pkt. This results in a callback always being scheduled
16882  *		(via the scsi_init_pkt callback implementation) if a resource
16883  *		failure occurs.
16884  *
16885  *     Context: This callback function may not block or call routines that block
16886  *
16887  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16888  *		request persisting at the head of the list which cannot be
16889  *		satisfied even after multiple retries. In the future the driver
16890  *		may implement some time of maximum runout count before failing
16891  *		an I/O.
16892  */
16893 
16894 static int
16895 sdrunout(caddr_t arg)
16896 {
16897 	struct sd_lun	*un = (struct sd_lun *)arg;
16898 
16899 	ASSERT(un != NULL);
16900 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16901 
16902 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16903 
16904 	mutex_enter(SD_MUTEX(un));
16905 	sd_start_cmds(un, NULL);
16906 	mutex_exit(SD_MUTEX(un));
16907 	/*
16908 	 * This callback routine always returns 1 (i.e. do not reschedule)
16909 	 * because we always specify sdrunout as the callback handler for
16910 	 * scsi_init_pkt inside the call to sd_start_cmds.
16911 	 */
16912 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16913 	return (1);
16914 }
16915 
16916 
16917 /*
16918  *    Function: sdintr
16919  *
16920  * Description: Completion callback routine for scsi_pkt(9S) structs
16921  *		sent to the HBA driver via scsi_transport(9F).
16922  *
16923  *     Context: Interrupt context
16924  */
16925 
16926 static void
16927 sdintr(struct scsi_pkt *pktp)
16928 {
16929 	struct buf	*bp;
16930 	struct sd_xbuf	*xp;
16931 	struct sd_lun	*un;
16932 	size_t		actual_len;
16933 	sd_ssc_t	*sscp;
16934 
16935 	ASSERT(pktp != NULL);
16936 	bp = (struct buf *)pktp->pkt_private;
16937 	ASSERT(bp != NULL);
16938 	xp = SD_GET_XBUF(bp);
16939 	ASSERT(xp != NULL);
16940 	ASSERT(xp->xb_pktp != NULL);
16941 	un = SD_GET_UN(bp);
16942 	ASSERT(un != NULL);
16943 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16944 
16945 #ifdef SD_FAULT_INJECTION
16946 
16947 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16948 	/* SD FaultInjection */
16949 	sd_faultinjection(pktp);
16950 
16951 #endif /* SD_FAULT_INJECTION */
16952 
16953 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16954 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
16955 
16956 	mutex_enter(SD_MUTEX(un));
16957 
16958 	ASSERT(un->un_fm_private != NULL);
16959 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16960 	ASSERT(sscp != NULL);
16961 
16962 	/* Reduce the count of the #commands currently in transport */
16963 	un->un_ncmds_in_transport--;
16964 	ASSERT(un->un_ncmds_in_transport >= 0);
16965 
16966 	/* Increment counter to indicate that the callback routine is active */
16967 	un->un_in_callback++;
16968 
16969 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16970 
16971 #ifdef	SDDEBUG
16972 	if (bp == un->un_retry_bp) {
16973 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16974 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16975 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
16976 	}
16977 #endif
16978 
16979 	/*
16980 	 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16981 	 * state if needed.
16982 	 */
16983 	if (pktp->pkt_reason == CMD_DEV_GONE) {
16984 		/* Prevent multiple console messages for the same failure. */
16985 		if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16986 			un->un_last_pkt_reason = CMD_DEV_GONE;
16987 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16988 			    "Command failed to complete...Device is gone\n");
16989 		}
16990 		if (un->un_mediastate != DKIO_DEV_GONE) {
16991 			un->un_mediastate = DKIO_DEV_GONE;
16992 			cv_broadcast(&un->un_state_cv);
16993 		}
16994 		/*
16995 		 * If the command happens to be the REQUEST SENSE command,
16996 		 * free up the rqs buf and fail the original command.
16997 		 */
16998 		if (bp == un->un_rqs_bp) {
16999 			bp = sd_mark_rqs_idle(un, xp);
17000 		}
17001 		sd_return_failed_command(un, bp, EIO);
17002 		goto exit;
17003 	}
17004 
17005 	if (pktp->pkt_state & STATE_XARQ_DONE) {
17006 		SD_TRACE(SD_LOG_COMMON, un,
17007 		    "sdintr: extra sense data received. pkt=%p\n", pktp);
17008 	}
17009 
17010 	/*
17011 	 * First see if the pkt has auto-request sense data with it....
17012 	 * Look at the packet state first so we don't take a performance
17013 	 * hit looking at the arq enabled flag unless absolutely necessary.
17014 	 */
17015 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
17016 	    (un->un_f_arq_enabled == TRUE)) {
17017 		/*
17018 		 * The HBA did an auto request sense for this command so check
17019 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17020 		 * driver command that should not be retried.
17021 		 */
17022 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17023 			/*
17024 			 * Save the relevant sense info into the xp for the
17025 			 * original cmd.
17026 			 */
17027 			struct scsi_arq_status *asp;
17028 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17029 			xp->xb_sense_status =
17030 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
17031 			xp->xb_sense_state  = asp->sts_rqpkt_state;
17032 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17033 			if (pktp->pkt_state & STATE_XARQ_DONE) {
17034 				actual_len = MAX_SENSE_LENGTH -
17035 				    xp->xb_sense_resid;
17036 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17037 				    MAX_SENSE_LENGTH);
17038 			} else {
17039 				if (xp->xb_sense_resid > SENSE_LENGTH) {
17040 					actual_len = MAX_SENSE_LENGTH -
17041 					    xp->xb_sense_resid;
17042 				} else {
17043 					actual_len = SENSE_LENGTH -
17044 					    xp->xb_sense_resid;
17045 				}
17046 				if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17047 					if ((((struct uscsi_cmd *)
17048 					    (xp->xb_pktinfo))->uscsi_rqlen) >
17049 					    actual_len) {
17050 						xp->xb_sense_resid =
17051 						    (((struct uscsi_cmd *)
17052 						    (xp->xb_pktinfo))->
17053 						    uscsi_rqlen) - actual_len;
17054 					} else {
17055 						xp->xb_sense_resid = 0;
17056 					}
17057 				}
17058 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17059 				    SENSE_LENGTH);
17060 			}
17061 
17062 			/* fail the command */
17063 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17064 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
17065 			sd_return_failed_command(un, bp, EIO);
17066 			goto exit;
17067 		}
17068 
17069 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
17070 		/*
17071 		 * We want to either retry or fail this command, so free
17072 		 * the DMA resources here.  If we retry the command then
17073 		 * the DMA resources will be reallocated in sd_start_cmds().
17074 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
17075 		 * causes the *entire* transfer to start over again from the
17076 		 * beginning of the request, even for PARTIAL chunks that
17077 		 * have already transferred successfully.
17078 		 */
17079 		if ((un->un_f_is_fibre == TRUE) &&
17080 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17081 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
17082 			scsi_dmafree(pktp);
17083 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17084 		}
17085 #endif
17086 
17087 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17088 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
17089 
17090 		sd_handle_auto_request_sense(un, bp, xp, pktp);
17091 		goto exit;
17092 	}
17093 
17094 	/* Next see if this is the REQUEST SENSE pkt for the instance */
17095 	if (pktp->pkt_flags & FLAG_SENSING)  {
17096 		/* This pktp is from the unit's REQUEST_SENSE command */
17097 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17098 		    "sdintr: sd_handle_request_sense\n");
17099 		sd_handle_request_sense(un, bp, xp, pktp);
17100 		goto exit;
17101 	}
17102 
17103 	/*
17104 	 * Check to see if the command successfully completed as requested;
17105 	 * this is the most common case (and also the hot performance path).
17106 	 *
17107 	 * Requirements for successful completion are:
17108 	 * pkt_reason is CMD_CMPLT and packet status is status good.
17109 	 * In addition:
17110 	 * - A residual of zero indicates successful completion no matter what
17111 	 *   the command is.
17112 	 * - If the residual is not zero and the command is not a read or
17113 	 *   write, then it's still defined as successful completion. In other
17114 	 *   words, if the command is a read or write the residual must be
17115 	 *   zero for successful completion.
17116 	 * - If the residual is not zero and the command is a read or
17117 	 *   write, and it's a USCSICMD, then it's still defined as
17118 	 *   successful completion.
17119 	 */
17120 	if ((pktp->pkt_reason == CMD_CMPLT) &&
17121 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
17122 
17123 		/*
17124 		 * Since this command is returned with a good status, we
17125 		 * can reset the count for Sonoma failover.
17126 		 */
17127 		un->un_sonoma_failure_count = 0;
17128 
17129 		/*
17130 		 * Return all USCSI commands on good status
17131 		 */
17132 		if (pktp->pkt_resid == 0) {
17133 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17134 			    "sdintr: returning command for resid == 0\n");
17135 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
17136 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
17137 			SD_UPDATE_B_RESID(bp, pktp);
17138 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17139 			    "sdintr: returning command for resid != 0\n");
17140 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17141 			SD_UPDATE_B_RESID(bp, pktp);
17142 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17143 			    "sdintr: returning uscsi command\n");
17144 		} else {
17145 			goto not_successful;
17146 		}
17147 		sd_return_command(un, bp);
17148 
17149 		/*
17150 		 * Decrement counter to indicate that the callback routine
17151 		 * is done.
17152 		 */
17153 		un->un_in_callback--;
17154 		ASSERT(un->un_in_callback >= 0);
17155 		mutex_exit(SD_MUTEX(un));
17156 
17157 		return;
17158 	}
17159 
17160 not_successful:
17161 
17162 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
17163 	/*
17164 	 * The following is based upon knowledge of the underlying transport
17165 	 * and its use of DMA resources.  This code should be removed when
17166 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
17167 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
17168 	 * and sd_start_cmds().
17169 	 *
17170 	 * Free any DMA resources associated with this command if there
17171 	 * is a chance it could be retried or enqueued for later retry.
17172 	 * If we keep the DMA binding then mpxio cannot reissue the
17173 	 * command on another path whenever a path failure occurs.
17174 	 *
17175 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
17176 	 * causes the *entire* transfer to start over again from the
17177 	 * beginning of the request, even for PARTIAL chunks that
17178 	 * have already transferred successfully.
17179 	 *
17180 	 * This is only done for non-uscsi commands (and also skipped for the
17181 	 * driver's internal RQS command). Also just do this for Fibre Channel
17182 	 * devices as these are the only ones that support mpxio.
17183 	 */
17184 	if ((un->un_f_is_fibre == TRUE) &&
17185 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17186 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
17187 		scsi_dmafree(pktp);
17188 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17189 	}
17190 #endif
17191 
17192 	/*
17193 	 * The command did not successfully complete as requested so check
17194 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17195 	 * driver command that should not be retried so just return. If
17196 	 * FLAG_DIAGNOSE is not set the error will be processed below.
17197 	 */
17198 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17199 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17200 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
17201 		/*
17202 		 * Issue a request sense if a check condition caused the error
17203 		 * (we handle the auto request sense case above), otherwise
17204 		 * just fail the command.
17205 		 */
17206 		if ((pktp->pkt_reason == CMD_CMPLT) &&
17207 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
17208 			sd_send_request_sense_command(un, bp, pktp);
17209 		} else {
17210 			sd_return_failed_command(un, bp, EIO);
17211 		}
17212 		goto exit;
17213 	}
17214 
17215 	/*
17216 	 * The command did not successfully complete as requested so process
17217 	 * the error, retry, and/or attempt recovery.
17218 	 */
17219 	switch (pktp->pkt_reason) {
17220 	case CMD_CMPLT:
17221 		switch (SD_GET_PKT_STATUS(pktp)) {
17222 		case STATUS_GOOD:
17223 			/*
17224 			 * The command completed successfully with a non-zero
17225 			 * residual
17226 			 */
17227 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17228 			    "sdintr: STATUS_GOOD \n");
17229 			sd_pkt_status_good(un, bp, xp, pktp);
17230 			break;
17231 
17232 		case STATUS_CHECK:
17233 		case STATUS_TERMINATED:
17234 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17235 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17236 			sd_pkt_status_check_condition(un, bp, xp, pktp);
17237 			break;
17238 
17239 		case STATUS_BUSY:
17240 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17241 			    "sdintr: STATUS_BUSY\n");
17242 			sd_pkt_status_busy(un, bp, xp, pktp);
17243 			break;
17244 
17245 		case STATUS_RESERVATION_CONFLICT:
17246 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17247 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
17248 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17249 			break;
17250 
17251 		case STATUS_QFULL:
17252 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17253 			    "sdintr: STATUS_QFULL\n");
17254 			sd_pkt_status_qfull(un, bp, xp, pktp);
17255 			break;
17256 
17257 		case STATUS_MET:
17258 		case STATUS_INTERMEDIATE:
17259 		case STATUS_SCSI2:
17260 		case STATUS_INTERMEDIATE_MET:
17261 		case STATUS_ACA_ACTIVE:
17262 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17263 			    "Unexpected SCSI status received: 0x%x\n",
17264 			    SD_GET_PKT_STATUS(pktp));
17265 			/*
17266 			 * Mark the ssc_flags when detected invalid status
17267 			 * code for non-USCSI command.
17268 			 */
17269 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17270 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17271 				    0, "stat-code");
17272 			}
17273 			sd_return_failed_command(un, bp, EIO);
17274 			break;
17275 
17276 		default:
17277 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17278 			    "Invalid SCSI status received: 0x%x\n",
17279 			    SD_GET_PKT_STATUS(pktp));
17280 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17281 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17282 				    0, "stat-code");
17283 			}
17284 			sd_return_failed_command(un, bp, EIO);
17285 			break;
17286 
17287 		}
17288 		break;
17289 
17290 	case CMD_INCOMPLETE:
17291 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17292 		    "sdintr:  CMD_INCOMPLETE\n");
17293 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17294 		break;
17295 	case CMD_TRAN_ERR:
17296 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17297 		    "sdintr: CMD_TRAN_ERR\n");
17298 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17299 		break;
17300 	case CMD_RESET:
17301 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17302 		    "sdintr: CMD_RESET \n");
17303 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17304 		break;
17305 	case CMD_ABORTED:
17306 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17307 		    "sdintr: CMD_ABORTED \n");
17308 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17309 		break;
17310 	case CMD_TIMEOUT:
17311 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17312 		    "sdintr: CMD_TIMEOUT\n");
17313 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17314 		break;
17315 	case CMD_UNX_BUS_FREE:
17316 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17317 		    "sdintr: CMD_UNX_BUS_FREE \n");
17318 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17319 		break;
17320 	case CMD_TAG_REJECT:
17321 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17322 		    "sdintr: CMD_TAG_REJECT\n");
17323 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17324 		break;
17325 	default:
17326 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17327 		    "sdintr: default\n");
17328 		/*
17329 		 * Mark the ssc_flags for detecting invliad pkt_reason.
17330 		 */
17331 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17332 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17333 			    0, "pkt-reason");
17334 		}
17335 		sd_pkt_reason_default(un, bp, xp, pktp);
17336 		break;
17337 	}
17338 
17339 exit:
17340 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17341 
17342 	/* Decrement counter to indicate that the callback routine is done. */
17343 	un->un_in_callback--;
17344 	ASSERT(un->un_in_callback >= 0);
17345 
17346 	/*
17347 	 * At this point, the pkt has been dispatched, ie, it is either
17348 	 * being re-tried or has been returned to its caller and should
17349 	 * not be referenced.
17350 	 */
17351 
17352 	mutex_exit(SD_MUTEX(un));
17353 }
17354 
17355 
17356 /*
17357  *    Function: sd_print_incomplete_msg
17358  *
17359  * Description: Prints the error message for a CMD_INCOMPLETE error.
17360  *
17361  *   Arguments: un - ptr to associated softstate for the device.
17362  *		bp - ptr to the buf(9S) for the command.
17363  *		arg - message string ptr
17364  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17365  *			or SD_NO_RETRY_ISSUED.
17366  *
17367  *     Context: May be called under interrupt context
17368  */
17369 
17370 static void
17371 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17372 {
17373 	struct scsi_pkt	*pktp;
17374 	char	*msgp;
17375 	char	*cmdp = arg;
17376 
17377 	ASSERT(un != NULL);
17378 	ASSERT(mutex_owned(SD_MUTEX(un)));
17379 	ASSERT(bp != NULL);
17380 	ASSERT(arg != NULL);
17381 	pktp = SD_GET_PKTP(bp);
17382 	ASSERT(pktp != NULL);
17383 
17384 	switch (code) {
17385 	case SD_DELAYED_RETRY_ISSUED:
17386 	case SD_IMMEDIATE_RETRY_ISSUED:
17387 		msgp = "retrying";
17388 		break;
17389 	case SD_NO_RETRY_ISSUED:
17390 	default:
17391 		msgp = "giving up";
17392 		break;
17393 	}
17394 
17395 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17396 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17397 		    "incomplete %s- %s\n", cmdp, msgp);
17398 	}
17399 }
17400 
17401 
17402 
17403 /*
17404  *    Function: sd_pkt_status_good
17405  *
17406  * Description: Processing for a STATUS_GOOD code in pkt_status.
17407  *
17408  *     Context: May be called under interrupt context
17409  */
17410 
17411 static void
17412 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17413     struct sd_xbuf *xp, struct scsi_pkt *pktp)
17414 {
17415 	char	*cmdp;
17416 
17417 	ASSERT(un != NULL);
17418 	ASSERT(mutex_owned(SD_MUTEX(un)));
17419 	ASSERT(bp != NULL);
17420 	ASSERT(xp != NULL);
17421 	ASSERT(pktp != NULL);
17422 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
17423 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17424 	ASSERT(pktp->pkt_resid != 0);
17425 
17426 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17427 
17428 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17429 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17430 	case SCMD_READ:
17431 		cmdp = "read";
17432 		break;
17433 	case SCMD_WRITE:
17434 		cmdp = "write";
17435 		break;
17436 	default:
17437 		SD_UPDATE_B_RESID(bp, pktp);
17438 		sd_return_command(un, bp);
17439 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17440 		return;
17441 	}
17442 
17443 	/*
17444 	 * See if we can retry the read/write, preferrably immediately.
17445 	 * If retries are exhaused, then sd_retry_command() will update
17446 	 * the b_resid count.
17447 	 */
17448 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17449 	    cmdp, EIO, (clock_t)0, NULL);
17450 
17451 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17452 }
17453 
17454 
17455 
17456 
17457 
17458 /*
17459  *    Function: sd_handle_request_sense
17460  *
17461  * Description: Processing for non-auto Request Sense command.
17462  *
17463  *   Arguments: un - ptr to associated softstate
17464  *		sense_bp - ptr to buf(9S) for the RQS command
17465  *		sense_xp - ptr to the sd_xbuf for the RQS command
17466  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17467  *
17468  *     Context: May be called under interrupt context
17469  */
17470 
17471 static void
17472 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17473     struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17474 {
17475 	struct buf	*cmd_bp;	/* buf for the original command */
17476 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
17477 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
17478 	size_t		actual_len;	/* actual sense data length */
17479 
17480 	ASSERT(un != NULL);
17481 	ASSERT(mutex_owned(SD_MUTEX(un)));
17482 	ASSERT(sense_bp != NULL);
17483 	ASSERT(sense_xp != NULL);
17484 	ASSERT(sense_pktp != NULL);
17485 
17486 	/*
17487 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
17488 	 * RQS command and not the original command.
17489 	 */
17490 	ASSERT(sense_pktp == un->un_rqs_pktp);
17491 	ASSERT(sense_bp   == un->un_rqs_bp);
17492 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17493 	    (FLAG_SENSING | FLAG_HEAD));
17494 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17495 	    FLAG_SENSING) == FLAG_SENSING);
17496 
17497 	/* These are the bp, xp, and pktp for the original command */
17498 	cmd_bp = sense_xp->xb_sense_bp;
17499 	cmd_xp = SD_GET_XBUF(cmd_bp);
17500 	cmd_pktp = SD_GET_PKTP(cmd_bp);
17501 
17502 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
17503 		/*
17504 		 * The REQUEST SENSE command failed.  Release the REQUEST
17505 		 * SENSE command for re-use, get back the bp for the original
17506 		 * command, and attempt to re-try the original command if
17507 		 * FLAG_DIAGNOSE is not set in the original packet.
17508 		 */
17509 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17510 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17511 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17512 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17513 			    NULL, NULL, EIO, (clock_t)0, NULL);
17514 			return;
17515 		}
17516 	}
17517 
17518 	/*
17519 	 * Save the relevant sense info into the xp for the original cmd.
17520 	 *
17521 	 * Note: if the request sense failed the state info will be zero
17522 	 * as set in sd_mark_rqs_busy()
17523 	 */
17524 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17525 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17526 	actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17527 	if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17528 	    (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17529 	    SENSE_LENGTH)) {
17530 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17531 		    MAX_SENSE_LENGTH);
17532 		cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17533 	} else {
17534 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17535 		    SENSE_LENGTH);
17536 		if (actual_len < SENSE_LENGTH) {
17537 			cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17538 		} else {
17539 			cmd_xp->xb_sense_resid = 0;
17540 		}
17541 	}
17542 
17543 	/*
17544 	 *  Free up the RQS command....
17545 	 *  NOTE:
17546 	 *	Must do this BEFORE calling sd_validate_sense_data!
17547 	 *	sd_validate_sense_data may return the original command in
17548 	 *	which case the pkt will be freed and the flags can no
17549 	 *	longer be touched.
17550 	 *	SD_MUTEX is held through this process until the command
17551 	 *	is dispatched based upon the sense data, so there are
17552 	 *	no race conditions.
17553 	 */
17554 	(void) sd_mark_rqs_idle(un, sense_xp);
17555 
17556 	/*
17557 	 * For a retryable command see if we have valid sense data, if so then
17558 	 * turn it over to sd_decode_sense() to figure out the right course of
17559 	 * action. Just fail a non-retryable command.
17560 	 */
17561 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17562 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17563 		    SD_SENSE_DATA_IS_VALID) {
17564 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17565 		}
17566 	} else {
17567 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17568 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17569 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17570 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17571 		sd_return_failed_command(un, cmd_bp, EIO);
17572 	}
17573 }
17574 
17575 
17576 
17577 
17578 /*
17579  *    Function: sd_handle_auto_request_sense
17580  *
17581  * Description: Processing for auto-request sense information.
17582  *
17583  *   Arguments: un - ptr to associated softstate
17584  *		bp - ptr to buf(9S) for the command
17585  *		xp - ptr to the sd_xbuf for the command
17586  *		pktp - ptr to the scsi_pkt(9S) for the command
17587  *
17588  *     Context: May be called under interrupt context
17589  */
17590 
17591 static void
17592 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17593     struct sd_xbuf *xp, struct scsi_pkt *pktp)
17594 {
17595 	struct scsi_arq_status *asp;
17596 	size_t actual_len;
17597 
17598 	ASSERT(un != NULL);
17599 	ASSERT(mutex_owned(SD_MUTEX(un)));
17600 	ASSERT(bp != NULL);
17601 	ASSERT(xp != NULL);
17602 	ASSERT(pktp != NULL);
17603 	ASSERT(pktp != un->un_rqs_pktp);
17604 	ASSERT(bp   != un->un_rqs_bp);
17605 
17606 	/*
17607 	 * For auto-request sense, we get a scsi_arq_status back from
17608 	 * the HBA, with the sense data in the sts_sensedata member.
17609 	 * The pkt_scbp of the packet points to this scsi_arq_status.
17610 	 */
17611 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17612 
17613 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17614 		/*
17615 		 * The auto REQUEST SENSE failed; see if we can re-try
17616 		 * the original command.
17617 		 */
17618 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17619 		    "auto request sense failed (reason=%s)\n",
17620 		    scsi_rname(asp->sts_rqpkt_reason));
17621 
17622 		sd_reset_target(un, pktp);
17623 
17624 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17625 		    NULL, NULL, EIO, (clock_t)0, NULL);
17626 		return;
17627 	}
17628 
17629 	/* Save the relevant sense info into the xp for the original cmd. */
17630 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17631 	xp->xb_sense_state  = asp->sts_rqpkt_state;
17632 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17633 	if (xp->xb_sense_state & STATE_XARQ_DONE) {
17634 		actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17635 		bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17636 		    MAX_SENSE_LENGTH);
17637 	} else {
17638 		if (xp->xb_sense_resid > SENSE_LENGTH) {
17639 			actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17640 		} else {
17641 			actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17642 		}
17643 		if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17644 			if ((((struct uscsi_cmd *)
17645 			    (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17646 				xp->xb_sense_resid = (((struct uscsi_cmd *)
17647 				    (xp->xb_pktinfo))->uscsi_rqlen) -
17648 				    actual_len;
17649 			} else {
17650 				xp->xb_sense_resid = 0;
17651 			}
17652 		}
17653 		bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17654 	}
17655 
17656 	/*
17657 	 * See if we have valid sense data, if so then turn it over to
17658 	 * sd_decode_sense() to figure out the right course of action.
17659 	 */
17660 	if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17661 	    SD_SENSE_DATA_IS_VALID) {
17662 		sd_decode_sense(un, bp, xp, pktp);
17663 	}
17664 }
17665 
17666 
17667 /*
17668  *    Function: sd_print_sense_failed_msg
17669  *
17670  * Description: Print log message when RQS has failed.
17671  *
17672  *   Arguments: un - ptr to associated softstate
17673  *		bp - ptr to buf(9S) for the command
17674  *		arg - generic message string ptr
17675  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17676  *			or SD_NO_RETRY_ISSUED
17677  *
17678  *     Context: May be called from interrupt context
17679  */
17680 
17681 static void
17682 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17683     int code)
17684 {
17685 	char	*msgp = arg;
17686 
17687 	ASSERT(un != NULL);
17688 	ASSERT(mutex_owned(SD_MUTEX(un)));
17689 	ASSERT(bp != NULL);
17690 
17691 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17692 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17693 	}
17694 }
17695 
17696 
17697 /*
17698  *    Function: sd_validate_sense_data
17699  *
17700  * Description: Check the given sense data for validity.
17701  *		If the sense data is not valid, the command will
17702  *		be either failed or retried!
17703  *
17704  * Return Code: SD_SENSE_DATA_IS_INVALID
17705  *		SD_SENSE_DATA_IS_VALID
17706  *
17707  *     Context: May be called from interrupt context
17708  */
17709 
17710 static int
17711 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17712     size_t actual_len)
17713 {
17714 	struct scsi_extended_sense *esp;
17715 	struct	scsi_pkt *pktp;
17716 	char	*msgp = NULL;
17717 	sd_ssc_t *sscp;
17718 
17719 	ASSERT(un != NULL);
17720 	ASSERT(mutex_owned(SD_MUTEX(un)));
17721 	ASSERT(bp != NULL);
17722 	ASSERT(bp != un->un_rqs_bp);
17723 	ASSERT(xp != NULL);
17724 	ASSERT(un->un_fm_private != NULL);
17725 
17726 	pktp = SD_GET_PKTP(bp);
17727 	ASSERT(pktp != NULL);
17728 
17729 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17730 	ASSERT(sscp != NULL);
17731 
17732 	/*
17733 	 * Check the status of the RQS command (auto or manual).
17734 	 */
17735 	switch (xp->xb_sense_status & STATUS_MASK) {
17736 	case STATUS_GOOD:
17737 		break;
17738 
17739 	case STATUS_RESERVATION_CONFLICT:
17740 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17741 		return (SD_SENSE_DATA_IS_INVALID);
17742 
17743 	case STATUS_BUSY:
17744 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17745 		    "Busy Status on REQUEST SENSE\n");
17746 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17747 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17748 		return (SD_SENSE_DATA_IS_INVALID);
17749 
17750 	case STATUS_QFULL:
17751 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17752 		    "QFULL Status on REQUEST SENSE\n");
17753 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17754 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17755 		return (SD_SENSE_DATA_IS_INVALID);
17756 
17757 	case STATUS_CHECK:
17758 	case STATUS_TERMINATED:
17759 		msgp = "Check Condition on REQUEST SENSE\n";
17760 		goto sense_failed;
17761 
17762 	default:
17763 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17764 		goto sense_failed;
17765 	}
17766 
17767 	/*
17768 	 * See if we got the minimum required amount of sense data.
17769 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17770 	 * or less.
17771 	 */
17772 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17773 	    (actual_len == 0)) {
17774 		msgp = "Request Sense couldn't get sense data\n";
17775 		goto sense_failed;
17776 	}
17777 
17778 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
17779 		msgp = "Not enough sense information\n";
17780 		/* Mark the ssc_flags for detecting invalid sense data */
17781 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17782 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17783 			    "sense-data");
17784 		}
17785 		goto sense_failed;
17786 	}
17787 
17788 	/*
17789 	 * We require the extended sense data
17790 	 */
17791 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17792 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
17793 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17794 			static char tmp[8];
17795 			static char buf[148];
17796 			char *p = (char *)(xp->xb_sense_data);
17797 			int i;
17798 
17799 			mutex_enter(&sd_sense_mutex);
17800 			(void) strcpy(buf, "undecodable sense information:");
17801 			for (i = 0; i < actual_len; i++) {
17802 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17803 				(void) strcpy(&buf[strlen(buf)], tmp);
17804 			}
17805 			i = strlen(buf);
17806 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
17807 
17808 			if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17809 				scsi_log(SD_DEVINFO(un), sd_label,
17810 				    CE_WARN, buf);
17811 			}
17812 			mutex_exit(&sd_sense_mutex);
17813 		}
17814 
17815 		/* Mark the ssc_flags for detecting invalid sense data */
17816 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17817 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17818 			    "sense-data");
17819 		}
17820 
17821 		/* Note: Legacy behavior, fail the command with no retry */
17822 		sd_return_failed_command(un, bp, EIO);
17823 		return (SD_SENSE_DATA_IS_INVALID);
17824 	}
17825 
17826 	/*
17827 	 * Check that es_code is valid (es_class concatenated with es_code
17828 	 * make up the "response code" field.  es_class will always be 7, so
17829 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17830 	 * format.
17831 	 */
17832 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17833 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17834 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17835 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17836 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17837 		/* Mark the ssc_flags for detecting invalid sense data */
17838 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17839 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17840 			    "sense-data");
17841 		}
17842 		goto sense_failed;
17843 	}
17844 
17845 	return (SD_SENSE_DATA_IS_VALID);
17846 
17847 sense_failed:
17848 	/*
17849 	 * If the request sense failed (for whatever reason), attempt
17850 	 * to retry the original command.
17851 	 */
17852 #if defined(__i386) || defined(__amd64)
17853 	/*
17854 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17855 	 * sddef.h for Sparc platform, and x86 uses 1 binary
17856 	 * for both SCSI/FC.
17857 	 * The SD_RETRY_DELAY value need to be adjusted here
17858 	 * when SD_RETRY_DELAY change in sddef.h
17859 	 */
17860 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17861 	    sd_print_sense_failed_msg, msgp, EIO,
17862 	    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17863 #else
17864 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17865 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17866 #endif
17867 
17868 	return (SD_SENSE_DATA_IS_INVALID);
17869 }
17870 
17871 /*
17872  *    Function: sd_decode_sense
17873  *
17874  * Description: Take recovery action(s) when SCSI Sense Data is received.
17875  *
17876  *     Context: Interrupt context.
17877  */
17878 
17879 static void
17880 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17881     struct scsi_pkt *pktp)
17882 {
17883 	uint8_t sense_key;
17884 
17885 	ASSERT(un != NULL);
17886 	ASSERT(mutex_owned(SD_MUTEX(un)));
17887 	ASSERT(bp != NULL);
17888 	ASSERT(bp != un->un_rqs_bp);
17889 	ASSERT(xp != NULL);
17890 	ASSERT(pktp != NULL);
17891 
17892 	sense_key = scsi_sense_key(xp->xb_sense_data);
17893 
17894 	switch (sense_key) {
17895 	case KEY_NO_SENSE:
17896 		sd_sense_key_no_sense(un, bp, xp, pktp);
17897 		break;
17898 	case KEY_RECOVERABLE_ERROR:
17899 		sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17900 		    bp, xp, pktp);
17901 		break;
17902 	case KEY_NOT_READY:
17903 		sd_sense_key_not_ready(un, xp->xb_sense_data,
17904 		    bp, xp, pktp);
17905 		break;
17906 	case KEY_MEDIUM_ERROR:
17907 	case KEY_HARDWARE_ERROR:
17908 		sd_sense_key_medium_or_hardware_error(un,
17909 		    xp->xb_sense_data, bp, xp, pktp);
17910 		break;
17911 	case KEY_ILLEGAL_REQUEST:
17912 		sd_sense_key_illegal_request(un, bp, xp, pktp);
17913 		break;
17914 	case KEY_UNIT_ATTENTION:
17915 		sd_sense_key_unit_attention(un, xp->xb_sense_data,
17916 		    bp, xp, pktp);
17917 		break;
17918 	case KEY_WRITE_PROTECT:
17919 	case KEY_VOLUME_OVERFLOW:
17920 	case KEY_MISCOMPARE:
17921 		sd_sense_key_fail_command(un, bp, xp, pktp);
17922 		break;
17923 	case KEY_BLANK_CHECK:
17924 		sd_sense_key_blank_check(un, bp, xp, pktp);
17925 		break;
17926 	case KEY_ABORTED_COMMAND:
17927 		sd_sense_key_aborted_command(un, bp, xp, pktp);
17928 		break;
17929 	case KEY_VENDOR_UNIQUE:
17930 	case KEY_COPY_ABORTED:
17931 	case KEY_EQUAL:
17932 	case KEY_RESERVED:
17933 	default:
17934 		sd_sense_key_default(un, xp->xb_sense_data,
17935 		    bp, xp, pktp);
17936 		break;
17937 	}
17938 }
17939 
17940 
17941 /*
17942  *    Function: sd_dump_memory
17943  *
17944  * Description: Debug logging routine to print the contents of a user provided
17945  *		buffer. The output of the buffer is broken up into 256 byte
17946  *		segments due to a size constraint of the scsi_log.
17947  *		implementation.
17948  *
17949  *   Arguments: un - ptr to softstate
17950  *		comp - component mask
17951  *		title - "title" string to preceed data when printed
17952  *		data - ptr to data block to be printed
17953  *		len - size of data block to be printed
17954  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17955  *
17956  *     Context: May be called from interrupt context
17957  */
17958 
17959 #define	SD_DUMP_MEMORY_BUF_SIZE	256
17960 
17961 static char *sd_dump_format_string[] = {
17962 		" 0x%02x",
17963 		" %c"
17964 };
17965 
17966 static void
17967 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17968     int len, int fmt)
17969 {
17970 	int	i, j;
17971 	int	avail_count;
17972 	int	start_offset;
17973 	int	end_offset;
17974 	size_t	entry_len;
17975 	char	*bufp;
17976 	char	*local_buf;
17977 	char	*format_string;
17978 
17979 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17980 
17981 	/*
17982 	 * In the debug version of the driver, this function is called from a
17983 	 * number of places which are NOPs in the release driver.
17984 	 * The debug driver therefore has additional methods of filtering
17985 	 * debug output.
17986 	 */
17987 #ifdef SDDEBUG
17988 	/*
17989 	 * In the debug version of the driver we can reduce the amount of debug
17990 	 * messages by setting sd_error_level to something other than
17991 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17992 	 * sd_component_mask.
17993 	 */
17994 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17995 	    (sd_error_level != SCSI_ERR_ALL)) {
17996 		return;
17997 	}
17998 	if (((sd_component_mask & comp) == 0) ||
17999 	    (sd_error_level != SCSI_ERR_ALL)) {
18000 		return;
18001 	}
18002 #else
18003 	if (sd_error_level != SCSI_ERR_ALL) {
18004 		return;
18005 	}
18006 #endif
18007 
18008 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
18009 	bufp = local_buf;
18010 	/*
18011 	 * Available length is the length of local_buf[], minus the
18012 	 * length of the title string, minus one for the ":", minus
18013 	 * one for the newline, minus one for the NULL terminator.
18014 	 * This gives the #bytes available for holding the printed
18015 	 * values from the given data buffer.
18016 	 */
18017 	if (fmt == SD_LOG_HEX) {
18018 		format_string = sd_dump_format_string[0];
18019 	} else /* SD_LOG_CHAR */ {
18020 		format_string = sd_dump_format_string[1];
18021 	}
18022 	/*
18023 	 * Available count is the number of elements from the given
18024 	 * data buffer that we can fit into the available length.
18025 	 * This is based upon the size of the format string used.
18026 	 * Make one entry and find it's size.
18027 	 */
18028 	(void) sprintf(bufp, format_string, data[0]);
18029 	entry_len = strlen(bufp);
18030 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
18031 
18032 	j = 0;
18033 	while (j < len) {
18034 		bufp = local_buf;
18035 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
18036 		start_offset = j;
18037 
18038 		end_offset = start_offset + avail_count;
18039 
18040 		(void) sprintf(bufp, "%s:", title);
18041 		bufp += strlen(bufp);
18042 		for (i = start_offset; ((i < end_offset) && (j < len));
18043 		    i++, j++) {
18044 			(void) sprintf(bufp, format_string, data[i]);
18045 			bufp += entry_len;
18046 		}
18047 		(void) sprintf(bufp, "\n");
18048 
18049 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
18050 	}
18051 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
18052 }
18053 
18054 /*
18055  *    Function: sd_print_sense_msg
18056  *
18057  * Description: Log a message based upon the given sense data.
18058  *
18059  *   Arguments: un - ptr to associated softstate
18060  *		bp - ptr to buf(9S) for the command
18061  *		arg - ptr to associate sd_sense_info struct
18062  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18063  *			or SD_NO_RETRY_ISSUED
18064  *
18065  *     Context: May be called from interrupt context
18066  */
18067 
18068 static void
18069 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
18070 {
18071 	struct sd_xbuf	*xp;
18072 	struct scsi_pkt	*pktp;
18073 	uint8_t *sensep;
18074 	daddr_t request_blkno;
18075 	diskaddr_t err_blkno;
18076 	int severity;
18077 	int pfa_flag;
18078 	extern struct scsi_key_strings scsi_cmds[];
18079 
18080 	ASSERT(un != NULL);
18081 	ASSERT(mutex_owned(SD_MUTEX(un)));
18082 	ASSERT(bp != NULL);
18083 	xp = SD_GET_XBUF(bp);
18084 	ASSERT(xp != NULL);
18085 	pktp = SD_GET_PKTP(bp);
18086 	ASSERT(pktp != NULL);
18087 	ASSERT(arg != NULL);
18088 
18089 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
18090 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
18091 
18092 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
18093 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
18094 		severity = SCSI_ERR_RETRYABLE;
18095 	}
18096 
18097 	/* Use absolute block number for the request block number */
18098 	request_blkno = xp->xb_blkno;
18099 
18100 	/*
18101 	 * Now try to get the error block number from the sense data
18102 	 */
18103 	sensep = xp->xb_sense_data;
18104 
18105 	if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
18106 	    (uint64_t *)&err_blkno)) {
18107 		/*
18108 		 * We retrieved the error block number from the information
18109 		 * portion of the sense data.
18110 		 *
18111 		 * For USCSI commands we are better off using the error
18112 		 * block no. as the requested block no. (This is the best
18113 		 * we can estimate.)
18114 		 */
18115 		if ((SD_IS_BUFIO(xp) == FALSE) &&
18116 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
18117 			request_blkno = err_blkno;
18118 		}
18119 	} else {
18120 		/*
18121 		 * Without the es_valid bit set (for fixed format) or an
18122 		 * information descriptor (for descriptor format) we cannot
18123 		 * be certain of the error blkno, so just use the
18124 		 * request_blkno.
18125 		 */
18126 		err_blkno = (diskaddr_t)request_blkno;
18127 	}
18128 
18129 	/*
18130 	 * The following will log the buffer contents for the release driver
18131 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
18132 	 * level is set to verbose.
18133 	 */
18134 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
18135 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
18136 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
18137 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
18138 
18139 	if (pfa_flag == FALSE) {
18140 		/* This is normally only set for USCSI */
18141 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
18142 			return;
18143 		}
18144 
18145 		if ((SD_IS_BUFIO(xp) == TRUE) &&
18146 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
18147 		    (severity < sd_error_level))) {
18148 			return;
18149 		}
18150 	}
18151 	/*
18152 	 * Check for Sonoma Failover and keep a count of how many failed I/O's
18153 	 */
18154 	if ((SD_IS_LSI(un)) &&
18155 	    (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
18156 	    (scsi_sense_asc(sensep) == 0x94) &&
18157 	    (scsi_sense_ascq(sensep) == 0x01)) {
18158 		un->un_sonoma_failure_count++;
18159 		if (un->un_sonoma_failure_count > 1) {
18160 			return;
18161 		}
18162 	}
18163 
18164 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
18165 	    ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
18166 	    (pktp->pkt_resid == 0))) {
18167 		scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
18168 		    request_blkno, err_blkno, scsi_cmds,
18169 		    (struct scsi_extended_sense *)sensep,
18170 		    un->un_additional_codes, NULL);
18171 	}
18172 }
18173 
18174 /*
18175  *    Function: sd_sense_key_no_sense
18176  *
18177  * Description: Recovery action when sense data was not received.
18178  *
18179  *     Context: May be called from interrupt context
18180  */
18181 
18182 static void
18183 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18184     struct scsi_pkt *pktp)
18185 {
18186 	struct sd_sense_info	si;
18187 
18188 	ASSERT(un != NULL);
18189 	ASSERT(mutex_owned(SD_MUTEX(un)));
18190 	ASSERT(bp != NULL);
18191 	ASSERT(xp != NULL);
18192 	ASSERT(pktp != NULL);
18193 
18194 	si.ssi_severity = SCSI_ERR_FATAL;
18195 	si.ssi_pfa_flag = FALSE;
18196 
18197 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
18198 
18199 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18200 	    &si, EIO, (clock_t)0, NULL);
18201 }
18202 
18203 
18204 /*
18205  *    Function: sd_sense_key_recoverable_error
18206  *
18207  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
18208  *
18209  *     Context: May be called from interrupt context
18210  */
18211 
18212 static void
18213 sd_sense_key_recoverable_error(struct sd_lun *un, uint8_t *sense_datap,
18214     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18215 {
18216 	struct sd_sense_info	si;
18217 	uint8_t asc = scsi_sense_asc(sense_datap);
18218 	uint8_t ascq = scsi_sense_ascq(sense_datap);
18219 
18220 	ASSERT(un != NULL);
18221 	ASSERT(mutex_owned(SD_MUTEX(un)));
18222 	ASSERT(bp != NULL);
18223 	ASSERT(xp != NULL);
18224 	ASSERT(pktp != NULL);
18225 
18226 	/*
18227 	 * 0x00, 0x1D: ATA PASSTHROUGH INFORMATION AVAILABLE
18228 	 */
18229 	if (asc == 0x00 && ascq == 0x1D) {
18230 		sd_return_command(un, bp);
18231 		return;
18232 	}
18233 
18234 	/*
18235 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
18236 	 */
18237 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
18238 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18239 		si.ssi_severity = SCSI_ERR_INFO;
18240 		si.ssi_pfa_flag = TRUE;
18241 	} else {
18242 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
18243 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18244 		si.ssi_severity = SCSI_ERR_RECOVERED;
18245 		si.ssi_pfa_flag = FALSE;
18246 	}
18247 
18248 	if (pktp->pkt_resid == 0) {
18249 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18250 		sd_return_command(un, bp);
18251 		return;
18252 	}
18253 
18254 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18255 	    &si, EIO, (clock_t)0, NULL);
18256 }
18257 
18258 
18259 
18260 
18261 /*
18262  *    Function: sd_sense_key_not_ready
18263  *
18264  * Description: Recovery actions for a SCSI "Not Ready" sense key.
18265  *
18266  *     Context: May be called from interrupt context
18267  */
18268 
18269 static void
18270 sd_sense_key_not_ready(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
18271     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18272 {
18273 	struct sd_sense_info	si;
18274 	uint8_t asc = scsi_sense_asc(sense_datap);
18275 	uint8_t ascq = scsi_sense_ascq(sense_datap);
18276 
18277 	ASSERT(un != NULL);
18278 	ASSERT(mutex_owned(SD_MUTEX(un)));
18279 	ASSERT(bp != NULL);
18280 	ASSERT(xp != NULL);
18281 	ASSERT(pktp != NULL);
18282 
18283 	si.ssi_severity = SCSI_ERR_FATAL;
18284 	si.ssi_pfa_flag = FALSE;
18285 
18286 	/*
18287 	 * Update error stats after first NOT READY error. Disks may have
18288 	 * been powered down and may need to be restarted.  For CDROMs,
18289 	 * report NOT READY errors only if media is present.
18290 	 */
18291 	if ((ISCD(un) && (asc == 0x3A)) ||
18292 	    (xp->xb_nr_retry_count > 0)) {
18293 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18294 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18295 	}
18296 
18297 	/*
18298 	 * Just fail if the "not ready" retry limit has been reached.
18299 	 */
18300 	if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18301 		/* Special check for error message printing for removables. */
18302 		if (un->un_f_has_removable_media && (asc == 0x04) &&
18303 		    (ascq >= 0x04)) {
18304 			si.ssi_severity = SCSI_ERR_ALL;
18305 		}
18306 		goto fail_command;
18307 	}
18308 
18309 	/*
18310 	 * Check the ASC and ASCQ in the sense data as needed, to determine
18311 	 * what to do.
18312 	 */
18313 	switch (asc) {
18314 	case 0x04:	/* LOGICAL UNIT NOT READY */
18315 		/*
18316 		 * disk drives that don't spin up result in a very long delay
18317 		 * in format without warning messages. We will log a message
18318 		 * if the error level is set to verbose.
18319 		 */
18320 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18321 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18322 			    "logical unit not ready, resetting disk\n");
18323 		}
18324 
18325 		/*
18326 		 * There are different requirements for CDROMs and disks for
18327 		 * the number of retries.  If a CD-ROM is giving this, it is
18328 		 * probably reading TOC and is in the process of getting
18329 		 * ready, so we should keep on trying for a long time to make
18330 		 * sure that all types of media are taken in account (for
18331 		 * some media the drive takes a long time to read TOC).  For
18332 		 * disks we do not want to retry this too many times as this
18333 		 * can cause a long hang in format when the drive refuses to
18334 		 * spin up (a very common failure).
18335 		 */
18336 		switch (ascq) {
18337 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
18338 			/*
18339 			 * Disk drives frequently refuse to spin up which
18340 			 * results in a very long hang in format without
18341 			 * warning messages.
18342 			 *
18343 			 * Note: This code preserves the legacy behavior of
18344 			 * comparing xb_nr_retry_count against zero for fibre
18345 			 * channel targets instead of comparing against the
18346 			 * un_reset_retry_count value.  The reason for this
18347 			 * discrepancy has been so utterly lost beneath the
18348 			 * Sands of Time that even Indiana Jones could not
18349 			 * find it.
18350 			 */
18351 			if (un->un_f_is_fibre == TRUE) {
18352 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18353 				    (xp->xb_nr_retry_count > 0)) &&
18354 				    (un->un_startstop_timeid == NULL)) {
18355 					scsi_log(SD_DEVINFO(un), sd_label,
18356 					    CE_WARN, "logical unit not ready, "
18357 					    "resetting disk\n");
18358 					sd_reset_target(un, pktp);
18359 				}
18360 			} else {
18361 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18362 				    (xp->xb_nr_retry_count >
18363 				    un->un_reset_retry_count)) &&
18364 				    (un->un_startstop_timeid == NULL)) {
18365 					scsi_log(SD_DEVINFO(un), sd_label,
18366 					    CE_WARN, "logical unit not ready, "
18367 					    "resetting disk\n");
18368 					sd_reset_target(un, pktp);
18369 				}
18370 			}
18371 			break;
18372 
18373 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
18374 			/*
18375 			 * If the target is in the process of becoming
18376 			 * ready, just proceed with the retry. This can
18377 			 * happen with CD-ROMs that take a long time to
18378 			 * read TOC after a power cycle or reset.
18379 			 */
18380 			goto do_retry;
18381 
18382 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18383 			break;
18384 
18385 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18386 			/*
18387 			 * Retries cannot help here so just fail right away.
18388 			 */
18389 			goto fail_command;
18390 
18391 		case 0x88:
18392 			/*
18393 			 * Vendor-unique code for T3/T4: it indicates a
18394 			 * path problem in a mutipathed config, but as far as
18395 			 * the target driver is concerned it equates to a fatal
18396 			 * error, so we should just fail the command right away
18397 			 * (without printing anything to the console). If this
18398 			 * is not a T3/T4, fall thru to the default recovery
18399 			 * action.
18400 			 * T3/T4 is FC only, don't need to check is_fibre
18401 			 */
18402 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
18403 				sd_return_failed_command(un, bp, EIO);
18404 				return;
18405 			}
18406 			/* FALLTHRU */
18407 
18408 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
18409 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
18410 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
18411 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
18412 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
18413 		default:    /* Possible future codes in SCSI spec? */
18414 			/*
18415 			 * For removable-media devices, do not retry if
18416 			 * ASCQ > 2 as these result mostly from USCSI commands
18417 			 * on MMC devices issued to check status of an
18418 			 * operation initiated in immediate mode.  Also for
18419 			 * ASCQ >= 4 do not print console messages as these
18420 			 * mainly represent a user-initiated operation
18421 			 * instead of a system failure.
18422 			 */
18423 			if (un->un_f_has_removable_media) {
18424 				si.ssi_severity = SCSI_ERR_ALL;
18425 				goto fail_command;
18426 			}
18427 			break;
18428 		}
18429 
18430 		/*
18431 		 * As part of our recovery attempt for the NOT READY
18432 		 * condition, we issue a START STOP UNIT command. However
18433 		 * we want to wait for a short delay before attempting this
18434 		 * as there may still be more commands coming back from the
18435 		 * target with the check condition. To do this we use
18436 		 * timeout(9F) to call sd_start_stop_unit_callback() after
18437 		 * the delay interval expires. (sd_start_stop_unit_callback()
18438 		 * dispatches sd_start_stop_unit_task(), which will issue
18439 		 * the actual START STOP UNIT command. The delay interval
18440 		 * is one-half of the delay that we will use to retry the
18441 		 * command that generated the NOT READY condition.
18442 		 *
18443 		 * Note that we could just dispatch sd_start_stop_unit_task()
18444 		 * from here and allow it to sleep for the delay interval,
18445 		 * but then we would be tying up the taskq thread
18446 		 * uncesessarily for the duration of the delay.
18447 		 *
18448 		 * Do not issue the START STOP UNIT if the current command
18449 		 * is already a START STOP UNIT.
18450 		 */
18451 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18452 			break;
18453 		}
18454 
18455 		/*
18456 		 * Do not schedule the timeout if one is already pending.
18457 		 */
18458 		if (un->un_startstop_timeid != NULL) {
18459 			SD_INFO(SD_LOG_ERROR, un,
18460 			    "sd_sense_key_not_ready: restart already issued to"
18461 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18462 			    ddi_get_instance(SD_DEVINFO(un)));
18463 			break;
18464 		}
18465 
18466 		/*
18467 		 * Schedule the START STOP UNIT command, then queue the command
18468 		 * for a retry.
18469 		 *
18470 		 * Note: A timeout is not scheduled for this retry because we
18471 		 * want the retry to be serial with the START_STOP_UNIT. The
18472 		 * retry will be started when the START_STOP_UNIT is completed
18473 		 * in sd_start_stop_unit_task.
18474 		 */
18475 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18476 		    un, un->un_busy_timeout / 2);
18477 		xp->xb_nr_retry_count++;
18478 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18479 		return;
18480 
18481 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18482 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18483 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18484 			    "unit does not respond to selection\n");
18485 		}
18486 		break;
18487 
18488 	case 0x3A:	/* MEDIUM NOT PRESENT */
18489 		if (sd_error_level >= SCSI_ERR_FATAL) {
18490 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18491 			    "Caddy not inserted in drive\n");
18492 		}
18493 
18494 		sr_ejected(un);
18495 		un->un_mediastate = DKIO_EJECTED;
18496 		/* The state has changed, inform the media watch routines */
18497 		cv_broadcast(&un->un_state_cv);
18498 		/* Just fail if no media is present in the drive. */
18499 		goto fail_command;
18500 
18501 	default:
18502 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18503 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18504 			    "Unit not Ready. Additional sense code 0x%x\n",
18505 			    asc);
18506 		}
18507 		break;
18508 	}
18509 
18510 do_retry:
18511 
18512 	/*
18513 	 * Retry the command, as some targets may report NOT READY for
18514 	 * several seconds after being reset.
18515 	 */
18516 	xp->xb_nr_retry_count++;
18517 	si.ssi_severity = SCSI_ERR_RETRYABLE;
18518 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18519 	    &si, EIO, un->un_busy_timeout, NULL);
18520 
18521 	return;
18522 
18523 fail_command:
18524 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18525 	sd_return_failed_command(un, bp, EIO);
18526 }
18527 
18528 
18529 
18530 /*
18531  *    Function: sd_sense_key_medium_or_hardware_error
18532  *
18533  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18534  *		sense key.
18535  *
18536  *     Context: May be called from interrupt context
18537  */
18538 
18539 static void
18540 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, uint8_t *sense_datap,
18541     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18542 {
18543 	struct sd_sense_info	si;
18544 	uint8_t sense_key = scsi_sense_key(sense_datap);
18545 	uint8_t asc = scsi_sense_asc(sense_datap);
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 	si.ssi_severity = SCSI_ERR_FATAL;
18554 	si.ssi_pfa_flag = FALSE;
18555 
18556 	if (sense_key == KEY_MEDIUM_ERROR) {
18557 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18558 	}
18559 
18560 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18561 
18562 	if ((un->un_reset_retry_count != 0) &&
18563 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
18564 		mutex_exit(SD_MUTEX(un));
18565 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18566 		if (un->un_f_allow_bus_device_reset == TRUE) {
18567 
18568 			boolean_t try_resetting_target = B_TRUE;
18569 
18570 			/*
18571 			 * We need to be able to handle specific ASC when we are
18572 			 * handling a KEY_HARDWARE_ERROR. In particular
18573 			 * taking the default action of resetting the target may
18574 			 * not be the appropriate way to attempt recovery.
18575 			 * Resetting a target because of a single LUN failure
18576 			 * victimizes all LUNs on that target.
18577 			 *
18578 			 * This is true for the LSI arrays, if an LSI
18579 			 * array controller returns an ASC of 0x84 (LUN Dead) we
18580 			 * should trust it.
18581 			 */
18582 
18583 			if (sense_key == KEY_HARDWARE_ERROR) {
18584 				switch (asc) {
18585 				case 0x84:
18586 					if (SD_IS_LSI(un)) {
18587 						try_resetting_target = B_FALSE;
18588 					}
18589 					break;
18590 				default:
18591 					break;
18592 				}
18593 			}
18594 
18595 			if (try_resetting_target == B_TRUE) {
18596 				int reset_retval = 0;
18597 				if (un->un_f_lun_reset_enabled == TRUE) {
18598 					SD_TRACE(SD_LOG_IO_CORE, un,
18599 					    "sd_sense_key_medium_or_hardware_"
18600 					    "error: issuing RESET_LUN\n");
18601 					reset_retval =
18602 					    scsi_reset(SD_ADDRESS(un),
18603 					    RESET_LUN);
18604 				}
18605 				if (reset_retval == 0) {
18606 					SD_TRACE(SD_LOG_IO_CORE, un,
18607 					    "sd_sense_key_medium_or_hardware_"
18608 					    "error: issuing RESET_TARGET\n");
18609 					(void) scsi_reset(SD_ADDRESS(un),
18610 					    RESET_TARGET);
18611 				}
18612 			}
18613 		}
18614 		mutex_enter(SD_MUTEX(un));
18615 	}
18616 
18617 	/*
18618 	 * This really ought to be a fatal error, but we will retry anyway
18619 	 * as some drives report this as a spurious error.
18620 	 */
18621 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18622 	    &si, EIO, (clock_t)0, NULL);
18623 }
18624 
18625 
18626 
18627 /*
18628  *    Function: sd_sense_key_illegal_request
18629  *
18630  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18631  *
18632  *     Context: May be called from interrupt context
18633  */
18634 
18635 static void
18636 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18637     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18638 {
18639 	struct sd_sense_info	si;
18640 
18641 	ASSERT(un != NULL);
18642 	ASSERT(mutex_owned(SD_MUTEX(un)));
18643 	ASSERT(bp != NULL);
18644 	ASSERT(xp != NULL);
18645 	ASSERT(pktp != NULL);
18646 
18647 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18648 
18649 	si.ssi_severity = SCSI_ERR_INFO;
18650 	si.ssi_pfa_flag = FALSE;
18651 
18652 	/* Pointless to retry if the target thinks it's an illegal request */
18653 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18654 	sd_return_failed_command(un, bp, EIO);
18655 }
18656 
18657 
18658 
18659 
18660 /*
18661  *    Function: sd_sense_key_unit_attention
18662  *
18663  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18664  *
18665  *     Context: May be called from interrupt context
18666  */
18667 
18668 static void
18669 sd_sense_key_unit_attention(struct sd_lun *un, 	uint8_t *sense_datap,
18670     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18671 {
18672 	/*
18673 	 * For UNIT ATTENTION we allow retries for one minute. Devices
18674 	 * like Sonoma can return UNIT ATTENTION close to a minute
18675 	 * under certain conditions.
18676 	 */
18677 	int	retry_check_flag = SD_RETRIES_UA;
18678 	boolean_t	kstat_updated = B_FALSE;
18679 	struct	sd_sense_info		si;
18680 	uint8_t asc = scsi_sense_asc(sense_datap);
18681 	uint8_t	ascq = scsi_sense_ascq(sense_datap);
18682 
18683 	ASSERT(un != NULL);
18684 	ASSERT(mutex_owned(SD_MUTEX(un)));
18685 	ASSERT(bp != NULL);
18686 	ASSERT(xp != NULL);
18687 	ASSERT(pktp != NULL);
18688 
18689 	si.ssi_severity = SCSI_ERR_INFO;
18690 	si.ssi_pfa_flag = FALSE;
18691 
18692 
18693 	switch (asc) {
18694 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18695 		if (sd_report_pfa != 0) {
18696 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18697 			si.ssi_pfa_flag = TRUE;
18698 			retry_check_flag = SD_RETRIES_STANDARD;
18699 			goto do_retry;
18700 		}
18701 
18702 		break;
18703 
18704 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18705 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18706 			un->un_resvd_status |=
18707 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
18708 		}
18709 #ifdef _LP64
18710 		if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18711 			if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18712 			    un, KM_NOSLEEP) == 0) {
18713 				/*
18714 				 * If we can't dispatch the task we'll just
18715 				 * live without descriptor sense.  We can
18716 				 * try again on the next "unit attention"
18717 				 */
18718 				SD_ERROR(SD_LOG_ERROR, un,
18719 				    "sd_sense_key_unit_attention: "
18720 				    "Could not dispatch "
18721 				    "sd_reenable_dsense_task\n");
18722 			}
18723 		}
18724 #endif /* _LP64 */
18725 		/* FALLTHRU */
18726 
18727 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18728 		if (!un->un_f_has_removable_media) {
18729 			break;
18730 		}
18731 
18732 		/*
18733 		 * When we get a unit attention from a removable-media device,
18734 		 * it may be in a state that will take a long time to recover
18735 		 * (e.g., from a reset).  Since we are executing in interrupt
18736 		 * context here, we cannot wait around for the device to come
18737 		 * back. So hand this command off to sd_media_change_task()
18738 		 * for deferred processing under taskq thread context. (Note
18739 		 * that the command still may be failed if a problem is
18740 		 * encountered at a later time.)
18741 		 */
18742 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18743 		    KM_NOSLEEP) == 0) {
18744 			/*
18745 			 * Cannot dispatch the request so fail the command.
18746 			 */
18747 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
18748 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18749 			si.ssi_severity = SCSI_ERR_FATAL;
18750 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18751 			sd_return_failed_command(un, bp, EIO);
18752 		}
18753 
18754 		/*
18755 		 * If failed to dispatch sd_media_change_task(), we already
18756 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
18757 		 * we should update kstat later if it encounters an error. So,
18758 		 * we update kstat_updated flag here.
18759 		 */
18760 		kstat_updated = B_TRUE;
18761 
18762 		/*
18763 		 * Either the command has been successfully dispatched to a
18764 		 * task Q for retrying, or the dispatch failed. In either case
18765 		 * do NOT retry again by calling sd_retry_command. This sets up
18766 		 * two retries of the same command and when one completes and
18767 		 * frees the resources the other will access freed memory,
18768 		 * a bad thing.
18769 		 */
18770 		return;
18771 
18772 	default:
18773 		break;
18774 	}
18775 
18776 	/*
18777 	 * ASC  ASCQ
18778 	 *  2A   09	Capacity data has changed
18779 	 *  2A   01	Mode parameters changed
18780 	 *  3F   0E	Reported luns data has changed
18781 	 * Arrays that support logical unit expansion should report
18782 	 * capacity changes(2Ah/09). Mode parameters changed and
18783 	 * reported luns data has changed are the approximation.
18784 	 */
18785 	if (((asc == 0x2a) && (ascq == 0x09)) ||
18786 	    ((asc == 0x2a) && (ascq == 0x01)) ||
18787 	    ((asc == 0x3f) && (ascq == 0x0e))) {
18788 		if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18789 		    KM_NOSLEEP) == 0) {
18790 			SD_ERROR(SD_LOG_ERROR, un,
18791 			    "sd_sense_key_unit_attention: "
18792 			    "Could not dispatch sd_target_change_task\n");
18793 		}
18794 	}
18795 
18796 	/*
18797 	 * Update kstat if we haven't done that.
18798 	 */
18799 	if (!kstat_updated) {
18800 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18801 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18802 	}
18803 
18804 do_retry:
18805 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18806 	    EIO, SD_UA_RETRY_DELAY, NULL);
18807 }
18808 
18809 
18810 
18811 /*
18812  *    Function: sd_sense_key_fail_command
18813  *
18814  * Description: Use to fail a command when we don't like the sense key that
18815  *		was returned.
18816  *
18817  *     Context: May be called from interrupt context
18818  */
18819 
18820 static void
18821 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18822     struct scsi_pkt *pktp)
18823 {
18824 	struct sd_sense_info	si;
18825 
18826 	ASSERT(un != NULL);
18827 	ASSERT(mutex_owned(SD_MUTEX(un)));
18828 	ASSERT(bp != NULL);
18829 	ASSERT(xp != NULL);
18830 	ASSERT(pktp != NULL);
18831 
18832 	si.ssi_severity = SCSI_ERR_FATAL;
18833 	si.ssi_pfa_flag = FALSE;
18834 
18835 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18836 	sd_return_failed_command(un, bp, EIO);
18837 }
18838 
18839 
18840 
18841 /*
18842  *    Function: sd_sense_key_blank_check
18843  *
18844  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18845  *		Has no monetary connotation.
18846  *
18847  *     Context: May be called from interrupt context
18848  */
18849 
18850 static void
18851 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18852     struct scsi_pkt *pktp)
18853 {
18854 	struct sd_sense_info	si;
18855 
18856 	ASSERT(un != NULL);
18857 	ASSERT(mutex_owned(SD_MUTEX(un)));
18858 	ASSERT(bp != NULL);
18859 	ASSERT(xp != NULL);
18860 	ASSERT(pktp != NULL);
18861 
18862 	/*
18863 	 * Blank check is not fatal for removable devices, therefore
18864 	 * it does not require a console message.
18865 	 */
18866 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18867 	    SCSI_ERR_FATAL;
18868 	si.ssi_pfa_flag = FALSE;
18869 
18870 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18871 	sd_return_failed_command(un, bp, EIO);
18872 }
18873 
18874 
18875 
18876 
18877 /*
18878  *    Function: sd_sense_key_aborted_command
18879  *
18880  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18881  *
18882  *     Context: May be called from interrupt context
18883  */
18884 
18885 static void
18886 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18887     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18888 {
18889 	struct sd_sense_info	si;
18890 
18891 	ASSERT(un != NULL);
18892 	ASSERT(mutex_owned(SD_MUTEX(un)));
18893 	ASSERT(bp != NULL);
18894 	ASSERT(xp != NULL);
18895 	ASSERT(pktp != NULL);
18896 
18897 	si.ssi_severity = SCSI_ERR_FATAL;
18898 	si.ssi_pfa_flag = FALSE;
18899 
18900 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18901 
18902 	/*
18903 	 * This really ought to be a fatal error, but we will retry anyway
18904 	 * as some drives report this as a spurious error.
18905 	 */
18906 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18907 	    &si, EIO, drv_usectohz(100000), NULL);
18908 }
18909 
18910 
18911 
18912 /*
18913  *    Function: sd_sense_key_default
18914  *
18915  * Description: Default recovery action for several SCSI sense keys (basically
18916  *		attempts a retry).
18917  *
18918  *     Context: May be called from interrupt context
18919  */
18920 
18921 static void
18922 sd_sense_key_default(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
18923     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18924 {
18925 	struct sd_sense_info	si;
18926 	uint8_t sense_key = scsi_sense_key(sense_datap);
18927 
18928 	ASSERT(un != NULL);
18929 	ASSERT(mutex_owned(SD_MUTEX(un)));
18930 	ASSERT(bp != NULL);
18931 	ASSERT(xp != NULL);
18932 	ASSERT(pktp != NULL);
18933 
18934 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18935 
18936 	/*
18937 	 * Undecoded sense key.	Attempt retries and hope that will fix
18938 	 * the problem.  Otherwise, we're dead.
18939 	 */
18940 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18941 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18942 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18943 	}
18944 
18945 	si.ssi_severity = SCSI_ERR_FATAL;
18946 	si.ssi_pfa_flag = FALSE;
18947 
18948 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18949 	    &si, EIO, (clock_t)0, NULL);
18950 }
18951 
18952 
18953 
18954 /*
18955  *    Function: sd_print_retry_msg
18956  *
18957  * Description: Print a message indicating the retry action being taken.
18958  *
18959  *   Arguments: un - ptr to associated softstate
18960  *		bp - ptr to buf(9S) for the command
18961  *		arg - not used.
18962  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18963  *			or SD_NO_RETRY_ISSUED
18964  *
18965  *     Context: May be called from interrupt context
18966  */
18967 /* ARGSUSED */
18968 static void
18969 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18970 {
18971 	struct sd_xbuf	*xp;
18972 	struct scsi_pkt *pktp;
18973 	char *reasonp;
18974 	char *msgp;
18975 
18976 	ASSERT(un != NULL);
18977 	ASSERT(mutex_owned(SD_MUTEX(un)));
18978 	ASSERT(bp != NULL);
18979 	pktp = SD_GET_PKTP(bp);
18980 	ASSERT(pktp != NULL);
18981 	xp = SD_GET_XBUF(bp);
18982 	ASSERT(xp != NULL);
18983 
18984 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18985 	mutex_enter(&un->un_pm_mutex);
18986 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18987 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18988 	    (pktp->pkt_flags & FLAG_SILENT)) {
18989 		mutex_exit(&un->un_pm_mutex);
18990 		goto update_pkt_reason;
18991 	}
18992 	mutex_exit(&un->un_pm_mutex);
18993 
18994 	/*
18995 	 * Suppress messages if they are all the same pkt_reason; with
18996 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18997 	 * If we are in panic, then suppress the retry messages.
18998 	 */
18999 	switch (flag) {
19000 	case SD_NO_RETRY_ISSUED:
19001 		msgp = "giving up";
19002 		break;
19003 	case SD_IMMEDIATE_RETRY_ISSUED:
19004 	case SD_DELAYED_RETRY_ISSUED:
19005 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
19006 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
19007 		    (sd_error_level != SCSI_ERR_ALL))) {
19008 			return;
19009 		}
19010 		msgp = "retrying command";
19011 		break;
19012 	default:
19013 		goto update_pkt_reason;
19014 	}
19015 
19016 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
19017 	    scsi_rname(pktp->pkt_reason));
19018 
19019 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
19020 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19021 		    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
19022 	}
19023 
19024 update_pkt_reason:
19025 	/*
19026 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
19027 	 * This is to prevent multiple console messages for the same failure
19028 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
19029 	 * when the command is retried successfully because there still may be
19030 	 * more commands coming back with the same value of pktp->pkt_reason.
19031 	 */
19032 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
19033 		un->un_last_pkt_reason = pktp->pkt_reason;
19034 	}
19035 }
19036 
19037 
19038 /*
19039  *    Function: sd_print_cmd_incomplete_msg
19040  *
19041  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
19042  *
19043  *   Arguments: un - ptr to associated softstate
19044  *		bp - ptr to buf(9S) for the command
19045  *		arg - passed to sd_print_retry_msg()
19046  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
19047  *			or SD_NO_RETRY_ISSUED
19048  *
19049  *     Context: May be called from interrupt context
19050  */
19051 
19052 static void
19053 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
19054     int code)
19055 {
19056 	dev_info_t	*dip;
19057 
19058 	ASSERT(un != NULL);
19059 	ASSERT(mutex_owned(SD_MUTEX(un)));
19060 	ASSERT(bp != NULL);
19061 
19062 	switch (code) {
19063 	case SD_NO_RETRY_ISSUED:
19064 		/* Command was failed. Someone turned off this target? */
19065 		if (un->un_state != SD_STATE_OFFLINE) {
19066 			/*
19067 			 * Suppress message if we are detaching and
19068 			 * device has been disconnected
19069 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
19070 			 * private interface and not part of the DDI
19071 			 */
19072 			dip = un->un_sd->sd_dev;
19073 			if (!(DEVI_IS_DETACHING(dip) &&
19074 			    DEVI_IS_DEVICE_REMOVED(dip))) {
19075 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19076 				"disk not responding to selection\n");
19077 			}
19078 			New_state(un, SD_STATE_OFFLINE);
19079 		}
19080 		break;
19081 
19082 	case SD_DELAYED_RETRY_ISSUED:
19083 	case SD_IMMEDIATE_RETRY_ISSUED:
19084 	default:
19085 		/* Command was successfully queued for retry */
19086 		sd_print_retry_msg(un, bp, arg, code);
19087 		break;
19088 	}
19089 }
19090 
19091 
19092 /*
19093  *    Function: sd_pkt_reason_cmd_incomplete
19094  *
19095  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
19096  *
19097  *     Context: May be called from interrupt context
19098  */
19099 
19100 static void
19101 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
19102     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19103 {
19104 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
19105 
19106 	ASSERT(un != NULL);
19107 	ASSERT(mutex_owned(SD_MUTEX(un)));
19108 	ASSERT(bp != NULL);
19109 	ASSERT(xp != NULL);
19110 	ASSERT(pktp != NULL);
19111 
19112 	/* Do not do a reset if selection did not complete */
19113 	/* Note: Should this not just check the bit? */
19114 	if (pktp->pkt_state != STATE_GOT_BUS) {
19115 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
19116 		sd_reset_target(un, pktp);
19117 	}
19118 
19119 	/*
19120 	 * If the target was not successfully selected, then set
19121 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
19122 	 * with the target, and further retries and/or commands are
19123 	 * likely to take a long time.
19124 	 */
19125 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
19126 		flag |= SD_RETRIES_FAILFAST;
19127 	}
19128 
19129 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19130 
19131 	sd_retry_command(un, bp, flag,
19132 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19133 }
19134 
19135 
19136 
19137 /*
19138  *    Function: sd_pkt_reason_cmd_tran_err
19139  *
19140  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
19141  *
19142  *     Context: May be called from interrupt context
19143  */
19144 
19145 static void
19146 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
19147     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19148 {
19149 	ASSERT(un != NULL);
19150 	ASSERT(mutex_owned(SD_MUTEX(un)));
19151 	ASSERT(bp != NULL);
19152 	ASSERT(xp != NULL);
19153 	ASSERT(pktp != NULL);
19154 
19155 	/*
19156 	 * Do not reset if we got a parity error, or if
19157 	 * selection did not complete.
19158 	 */
19159 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19160 	/* Note: Should this not just check the bit for pkt_state? */
19161 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
19162 	    (pktp->pkt_state != STATE_GOT_BUS)) {
19163 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
19164 		sd_reset_target(un, pktp);
19165 	}
19166 
19167 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19168 
19169 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19170 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19171 }
19172 
19173 
19174 
19175 /*
19176  *    Function: sd_pkt_reason_cmd_reset
19177  *
19178  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
19179  *
19180  *     Context: May be called from interrupt context
19181  */
19182 
19183 static void
19184 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19185     struct scsi_pkt *pktp)
19186 {
19187 	ASSERT(un != NULL);
19188 	ASSERT(mutex_owned(SD_MUTEX(un)));
19189 	ASSERT(bp != NULL);
19190 	ASSERT(xp != NULL);
19191 	ASSERT(pktp != NULL);
19192 
19193 	/* The target may still be running the command, so try to reset. */
19194 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19195 	sd_reset_target(un, pktp);
19196 
19197 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19198 
19199 	/*
19200 	 * If pkt_reason is CMD_RESET chances are that this pkt got
19201 	 * reset because another target on this bus caused it. The target
19202 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19203 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19204 	 */
19205 
19206 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19207 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19208 }
19209 
19210 
19211 
19212 
19213 /*
19214  *    Function: sd_pkt_reason_cmd_aborted
19215  *
19216  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
19217  *
19218  *     Context: May be called from interrupt context
19219  */
19220 
19221 static void
19222 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19223     struct scsi_pkt *pktp)
19224 {
19225 	ASSERT(un != NULL);
19226 	ASSERT(mutex_owned(SD_MUTEX(un)));
19227 	ASSERT(bp != NULL);
19228 	ASSERT(xp != NULL);
19229 	ASSERT(pktp != NULL);
19230 
19231 	/* The target may still be running the command, so try to reset. */
19232 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19233 	sd_reset_target(un, pktp);
19234 
19235 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19236 
19237 	/*
19238 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
19239 	 * aborted because another target on this bus caused it. The target
19240 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19241 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19242 	 */
19243 
19244 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19245 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19246 }
19247 
19248 
19249 
19250 /*
19251  *    Function: sd_pkt_reason_cmd_timeout
19252  *
19253  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19254  *
19255  *     Context: May be called from interrupt context
19256  */
19257 
19258 static void
19259 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19260     struct scsi_pkt *pktp)
19261 {
19262 	ASSERT(un != NULL);
19263 	ASSERT(mutex_owned(SD_MUTEX(un)));
19264 	ASSERT(bp != NULL);
19265 	ASSERT(xp != NULL);
19266 	ASSERT(pktp != NULL);
19267 
19268 
19269 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19270 	sd_reset_target(un, pktp);
19271 
19272 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19273 
19274 	/*
19275 	 * A command timeout indicates that we could not establish
19276 	 * communication with the target, so set SD_RETRIES_FAILFAST
19277 	 * as further retries/commands are likely to take a long time.
19278 	 */
19279 	sd_retry_command(un, bp,
19280 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19281 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19282 }
19283 
19284 
19285 
19286 /*
19287  *    Function: sd_pkt_reason_cmd_unx_bus_free
19288  *
19289  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19290  *
19291  *     Context: May be called from interrupt context
19292  */
19293 
19294 static void
19295 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19296     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19297 {
19298 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19299 
19300 	ASSERT(un != NULL);
19301 	ASSERT(mutex_owned(SD_MUTEX(un)));
19302 	ASSERT(bp != NULL);
19303 	ASSERT(xp != NULL);
19304 	ASSERT(pktp != NULL);
19305 
19306 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19307 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19308 
19309 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19310 	    sd_print_retry_msg : NULL;
19311 
19312 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19313 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19314 }
19315 
19316 
19317 /*
19318  *    Function: sd_pkt_reason_cmd_tag_reject
19319  *
19320  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19321  *
19322  *     Context: May be called from interrupt context
19323  */
19324 
19325 static void
19326 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19327     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19328 {
19329 	ASSERT(un != NULL);
19330 	ASSERT(mutex_owned(SD_MUTEX(un)));
19331 	ASSERT(bp != NULL);
19332 	ASSERT(xp != NULL);
19333 	ASSERT(pktp != NULL);
19334 
19335 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19336 	pktp->pkt_flags = 0;
19337 	un->un_tagflags = 0;
19338 	if (un->un_f_opt_queueing == TRUE) {
19339 		un->un_throttle = min(un->un_throttle, 3);
19340 	} else {
19341 		un->un_throttle = 1;
19342 	}
19343 	mutex_exit(SD_MUTEX(un));
19344 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19345 	mutex_enter(SD_MUTEX(un));
19346 
19347 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19348 
19349 	/* Legacy behavior not to check retry counts here. */
19350 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19351 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19352 }
19353 
19354 
19355 /*
19356  *    Function: sd_pkt_reason_default
19357  *
19358  * Description: Default recovery actions for SCSA pkt_reason values that
19359  *		do not have more explicit recovery actions.
19360  *
19361  *     Context: May be called from interrupt context
19362  */
19363 
19364 static void
19365 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19366     struct scsi_pkt *pktp)
19367 {
19368 	ASSERT(un != NULL);
19369 	ASSERT(mutex_owned(SD_MUTEX(un)));
19370 	ASSERT(bp != NULL);
19371 	ASSERT(xp != NULL);
19372 	ASSERT(pktp != NULL);
19373 
19374 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19375 	sd_reset_target(un, pktp);
19376 
19377 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19378 
19379 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19380 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19381 }
19382 
19383 
19384 
19385 /*
19386  *    Function: sd_pkt_status_check_condition
19387  *
19388  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19389  *
19390  *     Context: May be called from interrupt context
19391  */
19392 
19393 static void
19394 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19395     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19396 {
19397 	ASSERT(un != NULL);
19398 	ASSERT(mutex_owned(SD_MUTEX(un)));
19399 	ASSERT(bp != NULL);
19400 	ASSERT(xp != NULL);
19401 	ASSERT(pktp != NULL);
19402 
19403 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19404 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
19405 
19406 	/*
19407 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19408 	 * command will be retried after the request sense). Otherwise, retry
19409 	 * the command. Note: we are issuing the request sense even though the
19410 	 * retry limit may have been reached for the failed command.
19411 	 */
19412 	if (un->un_f_arq_enabled == FALSE) {
19413 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19414 		    "no ARQ, sending request sense command\n");
19415 		sd_send_request_sense_command(un, bp, pktp);
19416 	} else {
19417 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19418 		    "ARQ,retrying request sense command\n");
19419 #if defined(__i386) || defined(__amd64)
19420 		/*
19421 		 * The SD_RETRY_DELAY value need to be adjusted here
19422 		 * when SD_RETRY_DELAY change in sddef.h
19423 		 */
19424 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19425 		    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19426 		    NULL);
19427 #else
19428 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19429 		    EIO, SD_RETRY_DELAY, NULL);
19430 #endif
19431 	}
19432 
19433 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19434 }
19435 
19436 
19437 /*
19438  *    Function: sd_pkt_status_busy
19439  *
19440  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19441  *
19442  *     Context: May be called from interrupt context
19443  */
19444 
19445 static void
19446 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19447     struct scsi_pkt *pktp)
19448 {
19449 	ASSERT(un != NULL);
19450 	ASSERT(mutex_owned(SD_MUTEX(un)));
19451 	ASSERT(bp != NULL);
19452 	ASSERT(xp != NULL);
19453 	ASSERT(pktp != NULL);
19454 
19455 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19456 	    "sd_pkt_status_busy: entry\n");
19457 
19458 	/* If retries are exhausted, just fail the command. */
19459 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
19460 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19461 		    "device busy too long\n");
19462 		sd_return_failed_command(un, bp, EIO);
19463 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19464 		    "sd_pkt_status_busy: exit\n");
19465 		return;
19466 	}
19467 	xp->xb_retry_count++;
19468 
19469 	/*
19470 	 * Try to reset the target. However, we do not want to perform
19471 	 * more than one reset if the device continues to fail. The reset
19472 	 * will be performed when the retry count reaches the reset
19473 	 * threshold.  This threshold should be set such that at least
19474 	 * one retry is issued before the reset is performed.
19475 	 */
19476 	if (xp->xb_retry_count ==
19477 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19478 		int rval = 0;
19479 		mutex_exit(SD_MUTEX(un));
19480 		if (un->un_f_allow_bus_device_reset == TRUE) {
19481 			/*
19482 			 * First try to reset the LUN; if we cannot then
19483 			 * try to reset the target.
19484 			 */
19485 			if (un->un_f_lun_reset_enabled == TRUE) {
19486 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19487 				    "sd_pkt_status_busy: RESET_LUN\n");
19488 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19489 			}
19490 			if (rval == 0) {
19491 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19492 				    "sd_pkt_status_busy: RESET_TARGET\n");
19493 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19494 			}
19495 		}
19496 		if (rval == 0) {
19497 			/*
19498 			 * If the RESET_LUN and/or RESET_TARGET failed,
19499 			 * try RESET_ALL
19500 			 */
19501 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19502 			    "sd_pkt_status_busy: RESET_ALL\n");
19503 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19504 		}
19505 		mutex_enter(SD_MUTEX(un));
19506 		if (rval == 0) {
19507 			/*
19508 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19509 			 * At this point we give up & fail the command.
19510 			 */
19511 			sd_return_failed_command(un, bp, EIO);
19512 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19513 			    "sd_pkt_status_busy: exit (failed cmd)\n");
19514 			return;
19515 		}
19516 	}
19517 
19518 	/*
19519 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19520 	 * we have already checked the retry counts above.
19521 	 */
19522 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19523 	    EIO, un->un_busy_timeout, NULL);
19524 
19525 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19526 	    "sd_pkt_status_busy: exit\n");
19527 }
19528 
19529 
19530 /*
19531  *    Function: sd_pkt_status_reservation_conflict
19532  *
19533  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19534  *		command status.
19535  *
19536  *     Context: May be called from interrupt context
19537  */
19538 
19539 static void
19540 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19541     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19542 {
19543 	ASSERT(un != NULL);
19544 	ASSERT(mutex_owned(SD_MUTEX(un)));
19545 	ASSERT(bp != NULL);
19546 	ASSERT(xp != NULL);
19547 	ASSERT(pktp != NULL);
19548 
19549 	/*
19550 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19551 	 * conflict could be due to various reasons like incorrect keys, not
19552 	 * registered or not reserved etc. So, we return EACCES to the caller.
19553 	 */
19554 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19555 		int cmd = SD_GET_PKT_OPCODE(pktp);
19556 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19557 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19558 			sd_return_failed_command(un, bp, EACCES);
19559 			return;
19560 		}
19561 	}
19562 
19563 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19564 
19565 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19566 		if (sd_failfast_enable != 0) {
19567 			/* By definition, we must panic here.... */
19568 			sd_panic_for_res_conflict(un);
19569 			/*NOTREACHED*/
19570 		}
19571 		SD_ERROR(SD_LOG_IO, un,
19572 		    "sd_handle_resv_conflict: Disk Reserved\n");
19573 		sd_return_failed_command(un, bp, EACCES);
19574 		return;
19575 	}
19576 
19577 	/*
19578 	 * 1147670: retry only if sd_retry_on_reservation_conflict
19579 	 * property is set (default is 1). Retries will not succeed
19580 	 * on a disk reserved by another initiator. HA systems
19581 	 * may reset this via sd.conf to avoid these retries.
19582 	 *
19583 	 * Note: The legacy return code for this failure is EIO, however EACCES
19584 	 * seems more appropriate for a reservation conflict.
19585 	 */
19586 	if (sd_retry_on_reservation_conflict == 0) {
19587 		SD_ERROR(SD_LOG_IO, un,
19588 		    "sd_handle_resv_conflict: Device Reserved\n");
19589 		sd_return_failed_command(un, bp, EIO);
19590 		return;
19591 	}
19592 
19593 	/*
19594 	 * Retry the command if we can.
19595 	 *
19596 	 * Note: The legacy return code for this failure is EIO, however EACCES
19597 	 * seems more appropriate for a reservation conflict.
19598 	 */
19599 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19600 	    (clock_t)2, NULL);
19601 }
19602 
19603 
19604 
19605 /*
19606  *    Function: sd_pkt_status_qfull
19607  *
19608  * Description: Handle a QUEUE FULL condition from the target.  This can
19609  *		occur if the HBA does not handle the queue full condition.
19610  *		(Basically this means third-party HBAs as Sun HBAs will
19611  *		handle the queue full condition.)  Note that if there are
19612  *		some commands already in the transport, then the queue full
19613  *		has occurred because the queue for this nexus is actually
19614  *		full. If there are no commands in the transport, then the
19615  *		queue full is resulting from some other initiator or lun
19616  *		consuming all the resources at the target.
19617  *
19618  *     Context: May be called from interrupt context
19619  */
19620 
19621 static void
19622 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19623     struct scsi_pkt *pktp)
19624 {
19625 	ASSERT(un != NULL);
19626 	ASSERT(mutex_owned(SD_MUTEX(un)));
19627 	ASSERT(bp != NULL);
19628 	ASSERT(xp != NULL);
19629 	ASSERT(pktp != NULL);
19630 
19631 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19632 	    "sd_pkt_status_qfull: entry\n");
19633 
19634 	/*
19635 	 * Just lower the QFULL throttle and retry the command.  Note that
19636 	 * we do not limit the number of retries here.
19637 	 */
19638 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19639 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19640 	    SD_RESTART_TIMEOUT, NULL);
19641 
19642 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19643 	    "sd_pkt_status_qfull: exit\n");
19644 }
19645 
19646 
19647 /*
19648  *    Function: sd_reset_target
19649  *
19650  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19651  *		RESET_TARGET, or RESET_ALL.
19652  *
19653  *     Context: May be called under interrupt context.
19654  */
19655 
19656 static void
19657 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19658 {
19659 	int rval = 0;
19660 
19661 	ASSERT(un != NULL);
19662 	ASSERT(mutex_owned(SD_MUTEX(un)));
19663 	ASSERT(pktp != NULL);
19664 
19665 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19666 
19667 	/*
19668 	 * No need to reset if the transport layer has already done so.
19669 	 */
19670 	if ((pktp->pkt_statistics &
19671 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19672 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19673 		    "sd_reset_target: no reset\n");
19674 		return;
19675 	}
19676 
19677 	mutex_exit(SD_MUTEX(un));
19678 
19679 	if (un->un_f_allow_bus_device_reset == TRUE) {
19680 		if (un->un_f_lun_reset_enabled == TRUE) {
19681 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19682 			    "sd_reset_target: RESET_LUN\n");
19683 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19684 		}
19685 		if (rval == 0) {
19686 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19687 			    "sd_reset_target: RESET_TARGET\n");
19688 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19689 		}
19690 	}
19691 
19692 	if (rval == 0) {
19693 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19694 		    "sd_reset_target: RESET_ALL\n");
19695 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19696 	}
19697 
19698 	mutex_enter(SD_MUTEX(un));
19699 
19700 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19701 }
19702 
19703 /*
19704  *    Function: sd_target_change_task
19705  *
19706  * Description: Handle dynamic target change
19707  *
19708  *     Context: Executes in a taskq() thread context
19709  */
19710 static void
19711 sd_target_change_task(void *arg)
19712 {
19713 	struct sd_lun		*un = arg;
19714 	uint64_t		capacity;
19715 	diskaddr_t		label_cap;
19716 	uint_t			lbasize;
19717 	sd_ssc_t		*ssc;
19718 
19719 	ASSERT(un != NULL);
19720 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19721 
19722 	if ((un->un_f_blockcount_is_valid == FALSE) ||
19723 	    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19724 		return;
19725 	}
19726 
19727 	ssc = sd_ssc_init(un);
19728 
19729 	if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19730 	    &lbasize, SD_PATH_DIRECT) != 0) {
19731 		SD_ERROR(SD_LOG_ERROR, un,
19732 		    "sd_target_change_task: fail to read capacity\n");
19733 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19734 		goto task_exit;
19735 	}
19736 
19737 	mutex_enter(SD_MUTEX(un));
19738 	if (capacity <= un->un_blockcount) {
19739 		mutex_exit(SD_MUTEX(un));
19740 		goto task_exit;
19741 	}
19742 
19743 	sd_update_block_info(un, lbasize, capacity);
19744 	mutex_exit(SD_MUTEX(un));
19745 
19746 	/*
19747 	 * If lun is EFI labeled and lun capacity is greater than the
19748 	 * capacity contained in the label, log a sys event.
19749 	 */
19750 	if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19751 	    (void*)SD_PATH_DIRECT) == 0) {
19752 		mutex_enter(SD_MUTEX(un));
19753 		if (un->un_f_blockcount_is_valid &&
19754 		    un->un_blockcount > label_cap) {
19755 			mutex_exit(SD_MUTEX(un));
19756 			sd_log_lun_expansion_event(un, KM_SLEEP);
19757 		} else {
19758 			mutex_exit(SD_MUTEX(un));
19759 		}
19760 	}
19761 
19762 task_exit:
19763 	sd_ssc_fini(ssc);
19764 }
19765 
19766 
19767 /*
19768  *    Function: sd_log_dev_status_event
19769  *
19770  * Description: Log EC_dev_status sysevent
19771  *
19772  *     Context: Never called from interrupt context
19773  */
19774 static void
19775 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19776 {
19777 	int err;
19778 	char			*path;
19779 	nvlist_t		*attr_list;
19780 
19781 	/* Allocate and build sysevent attribute list */
19782 	err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19783 	if (err != 0) {
19784 		SD_ERROR(SD_LOG_ERROR, un,
19785 		    "sd_log_dev_status_event: fail to allocate space\n");
19786 		return;
19787 	}
19788 
19789 	path = kmem_alloc(MAXPATHLEN, km_flag);
19790 	if (path == NULL) {
19791 		nvlist_free(attr_list);
19792 		SD_ERROR(SD_LOG_ERROR, un,
19793 		    "sd_log_dev_status_event: fail to allocate space\n");
19794 		return;
19795 	}
19796 	/*
19797 	 * Add path attribute to identify the lun.
19798 	 * We are using minor node 'a' as the sysevent attribute.
19799 	 */
19800 	(void) snprintf(path, MAXPATHLEN, "/devices");
19801 	(void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19802 	(void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19803 	    ":a");
19804 
19805 	err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19806 	if (err != 0) {
19807 		nvlist_free(attr_list);
19808 		kmem_free(path, MAXPATHLEN);
19809 		SD_ERROR(SD_LOG_ERROR, un,
19810 		    "sd_log_dev_status_event: fail to add attribute\n");
19811 		return;
19812 	}
19813 
19814 	/* Log dynamic lun expansion sysevent */
19815 	err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19816 	    esc, attr_list, NULL, km_flag);
19817 	if (err != DDI_SUCCESS) {
19818 		SD_ERROR(SD_LOG_ERROR, un,
19819 		    "sd_log_dev_status_event: fail to log sysevent\n");
19820 	}
19821 
19822 	nvlist_free(attr_list);
19823 	kmem_free(path, MAXPATHLEN);
19824 }
19825 
19826 
19827 /*
19828  *    Function: sd_log_lun_expansion_event
19829  *
19830  * Description: Log lun expansion sys event
19831  *
19832  *     Context: Never called from interrupt context
19833  */
19834 static void
19835 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19836 {
19837 	sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19838 }
19839 
19840 
19841 /*
19842  *    Function: sd_log_eject_request_event
19843  *
19844  * Description: Log eject request sysevent
19845  *
19846  *     Context: Never called from interrupt context
19847  */
19848 static void
19849 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19850 {
19851 	sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19852 }
19853 
19854 
19855 /*
19856  *    Function: sd_media_change_task
19857  *
19858  * Description: Recovery action for CDROM to become available.
19859  *
19860  *     Context: Executes in a taskq() thread context
19861  */
19862 
19863 static void
19864 sd_media_change_task(void *arg)
19865 {
19866 	struct	scsi_pkt	*pktp = arg;
19867 	struct	sd_lun		*un;
19868 	struct	buf		*bp;
19869 	struct	sd_xbuf		*xp;
19870 	int	err		= 0;
19871 	int	retry_count	= 0;
19872 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
19873 	struct	sd_sense_info	si;
19874 
19875 	ASSERT(pktp != NULL);
19876 	bp = (struct buf *)pktp->pkt_private;
19877 	ASSERT(bp != NULL);
19878 	xp = SD_GET_XBUF(bp);
19879 	ASSERT(xp != NULL);
19880 	un = SD_GET_UN(bp);
19881 	ASSERT(un != NULL);
19882 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19883 	ASSERT(un->un_f_monitor_media_state);
19884 
19885 	si.ssi_severity = SCSI_ERR_INFO;
19886 	si.ssi_pfa_flag = FALSE;
19887 
19888 	/*
19889 	 * When a reset is issued on a CDROM, it takes a long time to
19890 	 * recover. First few attempts to read capacity and other things
19891 	 * related to handling unit attention fail (with a ASC 0x4 and
19892 	 * ASCQ 0x1). In that case we want to do enough retries and we want
19893 	 * to limit the retries in other cases of genuine failures like
19894 	 * no media in drive.
19895 	 */
19896 	while (retry_count++ < retry_limit) {
19897 		if ((err = sd_handle_mchange(un)) == 0) {
19898 			break;
19899 		}
19900 		if (err == EAGAIN) {
19901 			retry_limit = SD_UNIT_ATTENTION_RETRY;
19902 		}
19903 		/* Sleep for 0.5 sec. & try again */
19904 		delay(drv_usectohz(500000));
19905 	}
19906 
19907 	/*
19908 	 * Dispatch (retry or fail) the original command here,
19909 	 * along with appropriate console messages....
19910 	 *
19911 	 * Must grab the mutex before calling sd_retry_command,
19912 	 * sd_print_sense_msg and sd_return_failed_command.
19913 	 */
19914 	mutex_enter(SD_MUTEX(un));
19915 	if (err != SD_CMD_SUCCESS) {
19916 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
19917 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19918 		si.ssi_severity = SCSI_ERR_FATAL;
19919 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19920 		sd_return_failed_command(un, bp, EIO);
19921 	} else {
19922 		sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19923 		    &si, EIO, (clock_t)0, NULL);
19924 	}
19925 	mutex_exit(SD_MUTEX(un));
19926 }
19927 
19928 
19929 
19930 /*
19931  *    Function: sd_handle_mchange
19932  *
19933  * Description: Perform geometry validation & other recovery when CDROM
19934  *		has been removed from drive.
19935  *
19936  * Return Code: 0 for success
19937  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
19938  *		sd_send_scsi_READ_CAPACITY()
19939  *
19940  *     Context: Executes in a taskq() thread context
19941  */
19942 
19943 static int
19944 sd_handle_mchange(struct sd_lun *un)
19945 {
19946 	uint64_t	capacity;
19947 	uint32_t	lbasize;
19948 	int		rval;
19949 	sd_ssc_t	*ssc;
19950 
19951 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19952 	ASSERT(un->un_f_monitor_media_state);
19953 
19954 	ssc = sd_ssc_init(un);
19955 	rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19956 	    SD_PATH_DIRECT_PRIORITY);
19957 
19958 	if (rval != 0)
19959 		goto failed;
19960 
19961 	mutex_enter(SD_MUTEX(un));
19962 	sd_update_block_info(un, lbasize, capacity);
19963 
19964 	if (un->un_errstats != NULL) {
19965 		struct	sd_errstats *stp =
19966 		    (struct sd_errstats *)un->un_errstats->ks_data;
19967 		stp->sd_capacity.value.ui64 = (uint64_t)
19968 		    ((uint64_t)un->un_blockcount *
19969 		    (uint64_t)un->un_tgt_blocksize);
19970 	}
19971 
19972 	/*
19973 	 * Check if the media in the device is writable or not
19974 	 */
19975 	if (ISCD(un)) {
19976 		sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19977 	}
19978 
19979 	/*
19980 	 * Note: Maybe let the strategy/partitioning chain worry about getting
19981 	 * valid geometry.
19982 	 */
19983 	mutex_exit(SD_MUTEX(un));
19984 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19985 
19986 
19987 	if (cmlb_validate(un->un_cmlbhandle, 0,
19988 	    (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19989 		sd_ssc_fini(ssc);
19990 		return (EIO);
19991 	} else {
19992 		if (un->un_f_pkstats_enabled) {
19993 			sd_set_pstats(un);
19994 			SD_TRACE(SD_LOG_IO_PARTITION, un,
19995 			    "sd_handle_mchange: un:0x%p pstats created and "
19996 			    "set\n", un);
19997 		}
19998 	}
19999 
20000 	/*
20001 	 * Try to lock the door
20002 	 */
20003 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
20004 	    SD_PATH_DIRECT_PRIORITY);
20005 failed:
20006 	if (rval != 0)
20007 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20008 	sd_ssc_fini(ssc);
20009 	return (rval);
20010 }
20011 
20012 
20013 /*
20014  *    Function: sd_send_scsi_DOORLOCK
20015  *
20016  * Description: Issue the scsi DOOR LOCK command
20017  *
20018  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20019  *                      structure for this target.
20020  *		flag  - SD_REMOVAL_ALLOW
20021  *			SD_REMOVAL_PREVENT
20022  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20023  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20024  *			to use the USCSI "direct" chain and bypass the normal
20025  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20026  *			command is issued as part of an error recovery action.
20027  *
20028  * Return Code: 0   - Success
20029  *		errno return code from sd_ssc_send()
20030  *
20031  *     Context: Can sleep.
20032  */
20033 
20034 static int
20035 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
20036 {
20037 	struct scsi_extended_sense	sense_buf;
20038 	union scsi_cdb		cdb;
20039 	struct uscsi_cmd	ucmd_buf;
20040 	int			status;
20041 	struct sd_lun		*un;
20042 
20043 	ASSERT(ssc != NULL);
20044 	un = ssc->ssc_un;
20045 	ASSERT(un != NULL);
20046 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20047 
20048 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
20049 
20050 	/* already determined doorlock is not supported, fake success */
20051 	if (un->un_f_doorlock_supported == FALSE) {
20052 		return (0);
20053 	}
20054 
20055 	/*
20056 	 * If we are ejecting and see an SD_REMOVAL_PREVENT
20057 	 * ignore the command so we can complete the eject
20058 	 * operation.
20059 	 */
20060 	if (flag == SD_REMOVAL_PREVENT) {
20061 		mutex_enter(SD_MUTEX(un));
20062 		if (un->un_f_ejecting == TRUE) {
20063 			mutex_exit(SD_MUTEX(un));
20064 			return (EAGAIN);
20065 		}
20066 		mutex_exit(SD_MUTEX(un));
20067 	}
20068 
20069 	bzero(&cdb, sizeof (cdb));
20070 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20071 
20072 	cdb.scc_cmd = SCMD_DOORLOCK;
20073 	cdb.cdb_opaque[4] = (uchar_t)flag;
20074 
20075 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20076 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20077 	ucmd_buf.uscsi_bufaddr	= NULL;
20078 	ucmd_buf.uscsi_buflen	= 0;
20079 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20080 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20081 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20082 	ucmd_buf.uscsi_timeout	= 15;
20083 
20084 	SD_TRACE(SD_LOG_IO, un,
20085 	    "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
20086 
20087 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20088 	    UIO_SYSSPACE, path_flag);
20089 
20090 	if (status == 0)
20091 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20092 
20093 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
20094 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20095 	    (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
20096 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20097 
20098 		/* fake success and skip subsequent doorlock commands */
20099 		un->un_f_doorlock_supported = FALSE;
20100 		return (0);
20101 	}
20102 
20103 	return (status);
20104 }
20105 
20106 /*
20107  *    Function: sd_send_scsi_READ_CAPACITY
20108  *
20109  * Description: This routine uses the scsi READ CAPACITY command to determine
20110  *		the device capacity in number of blocks and the device native
20111  *		block size. If this function returns a failure, then the
20112  *		values in *capp and *lbap are undefined.  If the capacity
20113  *		returned is 0xffffffff then the lun is too large for a
20114  *		normal READ CAPACITY command and the results of a
20115  *		READ CAPACITY 16 will be used instead.
20116  *
20117  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20118  *		capp - ptr to unsigned 64-bit variable to receive the
20119  *			capacity value from the command.
20120  *		lbap - ptr to unsigned 32-bit varaible to receive the
20121  *			block size value from the command
20122  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20123  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20124  *			to use the USCSI "direct" chain and bypass the normal
20125  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20126  *			command is issued as part of an error recovery action.
20127  *
20128  * Return Code: 0   - Success
20129  *		EIO - IO error
20130  *		EACCES - Reservation conflict detected
20131  *		EAGAIN - Device is becoming ready
20132  *		errno return code from sd_ssc_send()
20133  *
20134  *     Context: Can sleep.  Blocks until command completes.
20135  */
20136 
20137 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
20138 
20139 static int
20140 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20141     int path_flag)
20142 {
20143 	struct	scsi_extended_sense	sense_buf;
20144 	struct	uscsi_cmd	ucmd_buf;
20145 	union	scsi_cdb	cdb;
20146 	uint32_t		*capacity_buf;
20147 	uint64_t		capacity;
20148 	uint32_t		lbasize;
20149 	uint32_t		pbsize;
20150 	int			status;
20151 	struct sd_lun		*un;
20152 
20153 	ASSERT(ssc != NULL);
20154 
20155 	un = ssc->ssc_un;
20156 	ASSERT(un != NULL);
20157 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20158 	ASSERT(capp != NULL);
20159 	ASSERT(lbap != NULL);
20160 
20161 	SD_TRACE(SD_LOG_IO, un,
20162 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20163 
20164 	/*
20165 	 * First send a READ_CAPACITY command to the target.
20166 	 * (This command is mandatory under SCSI-2.)
20167 	 *
20168 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
20169 	 * Medium Indicator bit is cleared.  The address field must be
20170 	 * zero if the PMI bit is zero.
20171 	 */
20172 	bzero(&cdb, sizeof (cdb));
20173 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20174 
20175 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
20176 
20177 	cdb.scc_cmd = SCMD_READ_CAPACITY;
20178 
20179 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20180 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20181 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
20182 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
20183 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20184 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20185 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20186 	ucmd_buf.uscsi_timeout	= 60;
20187 
20188 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20189 	    UIO_SYSSPACE, path_flag);
20190 
20191 	switch (status) {
20192 	case 0:
20193 		/* Return failure if we did not get valid capacity data. */
20194 		if (ucmd_buf.uscsi_resid != 0) {
20195 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20196 			    "sd_send_scsi_READ_CAPACITY received invalid "
20197 			    "capacity data");
20198 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20199 			return (EIO);
20200 		}
20201 		/*
20202 		 * Read capacity and block size from the READ CAPACITY 10 data.
20203 		 * This data may be adjusted later due to device specific
20204 		 * issues.
20205 		 *
20206 		 * According to the SCSI spec, the READ CAPACITY 10
20207 		 * command returns the following:
20208 		 *
20209 		 *  bytes 0-3: Maximum logical block address available.
20210 		 *		(MSB in byte:0 & LSB in byte:3)
20211 		 *
20212 		 *  bytes 4-7: Block length in bytes
20213 		 *		(MSB in byte:4 & LSB in byte:7)
20214 		 *
20215 		 */
20216 		capacity = BE_32(capacity_buf[0]);
20217 		lbasize = BE_32(capacity_buf[1]);
20218 
20219 		/*
20220 		 * Done with capacity_buf
20221 		 */
20222 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20223 
20224 		/*
20225 		 * if the reported capacity is set to all 0xf's, then
20226 		 * this disk is too large and requires SBC-2 commands.
20227 		 * Reissue the request using READ CAPACITY 16.
20228 		 */
20229 		if (capacity == 0xffffffff) {
20230 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20231 			status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
20232 			    &lbasize, &pbsize, path_flag);
20233 			if (status != 0) {
20234 				return (status);
20235 			} else {
20236 				goto rc16_done;
20237 			}
20238 		}
20239 		break;	/* Success! */
20240 	case EIO:
20241 		switch (ucmd_buf.uscsi_status) {
20242 		case STATUS_RESERVATION_CONFLICT:
20243 			status = EACCES;
20244 			break;
20245 		case STATUS_CHECK:
20246 			/*
20247 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20248 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20249 			 */
20250 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20251 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20252 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20253 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20254 				return (EAGAIN);
20255 			}
20256 			break;
20257 		default:
20258 			break;
20259 		}
20260 		/* FALLTHRU */
20261 	default:
20262 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20263 		return (status);
20264 	}
20265 
20266 	/*
20267 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20268 	 * (2352 and 0 are common) so for these devices always force the value
20269 	 * to 2048 as required by the ATAPI specs.
20270 	 */
20271 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20272 		lbasize = 2048;
20273 	}
20274 
20275 	/*
20276 	 * Get the maximum LBA value from the READ CAPACITY data.
20277 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20278 	 * was cleared when issuing the command. This means that the LBA
20279 	 * returned from the device is the LBA of the last logical block
20280 	 * on the logical unit.  The actual logical block count will be
20281 	 * this value plus one.
20282 	 */
20283 	capacity += 1;
20284 
20285 	/*
20286 	 * Currently, for removable media, the capacity is saved in terms
20287 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20288 	 */
20289 	if (un->un_f_has_removable_media)
20290 		capacity *= (lbasize / un->un_sys_blocksize);
20291 
20292 rc16_done:
20293 
20294 	/*
20295 	 * Copy the values from the READ CAPACITY command into the space
20296 	 * provided by the caller.
20297 	 */
20298 	*capp = capacity;
20299 	*lbap = lbasize;
20300 
20301 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20302 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
20303 
20304 	/*
20305 	 * Both the lbasize and capacity from the device must be nonzero,
20306 	 * otherwise we assume that the values are not valid and return
20307 	 * failure to the caller. (4203735)
20308 	 */
20309 	if ((capacity == 0) || (lbasize == 0)) {
20310 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20311 		    "sd_send_scsi_READ_CAPACITY received invalid value "
20312 		    "capacity %llu lbasize %d", capacity, lbasize);
20313 		return (EIO);
20314 	}
20315 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20316 	return (0);
20317 }
20318 
20319 /*
20320  *    Function: sd_send_scsi_READ_CAPACITY_16
20321  *
20322  * Description: This routine uses the scsi READ CAPACITY 16 command to
20323  *		determine the device capacity in number of blocks and the
20324  *		device native block size.  If this function returns a failure,
20325  *		then the values in *capp and *lbap are undefined.
20326  *		This routine should be called by sd_send_scsi_READ_CAPACITY
20327  *              which will apply any device specific adjustments to capacity
20328  *              and lbasize. One exception is it is also called by
20329  *              sd_get_media_info_ext. In that function, there is no need to
20330  *              adjust the capacity and lbasize.
20331  *
20332  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20333  *		capp - ptr to unsigned 64-bit variable to receive the
20334  *			capacity value from the command.
20335  *		lbap - ptr to unsigned 32-bit varaible to receive the
20336  *			block size value from the command
20337  *              psp  - ptr to unsigned 32-bit variable to receive the
20338  *                      physical block size value from the command
20339  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20340  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20341  *			to use the USCSI "direct" chain and bypass the normal
20342  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
20343  *			this command is issued as part of an error recovery
20344  *			action.
20345  *
20346  * Return Code: 0   - Success
20347  *		EIO - IO error
20348  *		EACCES - Reservation conflict detected
20349  *		EAGAIN - Device is becoming ready
20350  *		errno return code from sd_ssc_send()
20351  *
20352  *     Context: Can sleep.  Blocks until command completes.
20353  */
20354 
20355 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
20356 
20357 static int
20358 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20359     uint32_t *psp, int path_flag)
20360 {
20361 	struct	scsi_extended_sense	sense_buf;
20362 	struct	uscsi_cmd	ucmd_buf;
20363 	union	scsi_cdb	cdb;
20364 	uint64_t		*capacity16_buf;
20365 	uint64_t		capacity;
20366 	uint32_t		lbasize;
20367 	uint32_t		pbsize;
20368 	uint32_t		lbpb_exp;
20369 	int			status;
20370 	struct sd_lun		*un;
20371 
20372 	ASSERT(ssc != NULL);
20373 
20374 	un = ssc->ssc_un;
20375 	ASSERT(un != NULL);
20376 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20377 	ASSERT(capp != NULL);
20378 	ASSERT(lbap != NULL);
20379 
20380 	SD_TRACE(SD_LOG_IO, un,
20381 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20382 
20383 	/*
20384 	 * First send a READ_CAPACITY_16 command to the target.
20385 	 *
20386 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
20387 	 * Medium Indicator bit is cleared.  The address field must be
20388 	 * zero if the PMI bit is zero.
20389 	 */
20390 	bzero(&cdb, sizeof (cdb));
20391 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20392 
20393 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20394 
20395 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20396 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
20397 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
20398 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
20399 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20400 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20401 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20402 	ucmd_buf.uscsi_timeout	= 60;
20403 
20404 	/*
20405 	 * Read Capacity (16) is a Service Action In command.  One
20406 	 * command byte (0x9E) is overloaded for multiple operations,
20407 	 * with the second CDB byte specifying the desired operation
20408 	 */
20409 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20410 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20411 
20412 	/*
20413 	 * Fill in allocation length field
20414 	 */
20415 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20416 
20417 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20418 	    UIO_SYSSPACE, path_flag);
20419 
20420 	switch (status) {
20421 	case 0:
20422 		/* Return failure if we did not get valid capacity data. */
20423 		if (ucmd_buf.uscsi_resid > 20) {
20424 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20425 			    "sd_send_scsi_READ_CAPACITY_16 received invalid "
20426 			    "capacity data");
20427 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20428 			return (EIO);
20429 		}
20430 
20431 		/*
20432 		 * Read capacity and block size from the READ CAPACITY 16 data.
20433 		 * This data may be adjusted later due to device specific
20434 		 * issues.
20435 		 *
20436 		 * According to the SCSI spec, the READ CAPACITY 16
20437 		 * command returns the following:
20438 		 *
20439 		 *  bytes 0-7: Maximum logical block address available.
20440 		 *		(MSB in byte:0 & LSB in byte:7)
20441 		 *
20442 		 *  bytes 8-11: Block length in bytes
20443 		 *		(MSB in byte:8 & LSB in byte:11)
20444 		 *
20445 		 *  byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20446 		 *
20447 		 *  byte 14:
20448 		 *	bit 7: Thin-Provisioning Enabled
20449 		 *	bit 6: Thin-Provisioning Read Zeros
20450 		 */
20451 		capacity = BE_64(capacity16_buf[0]);
20452 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20453 		lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20454 
20455 		un->un_thin_flags = 0;
20456 		if (((uint8_t *)capacity16_buf)[14] & (1 << 7))
20457 			un->un_thin_flags |= SD_THIN_PROV_ENABLED;
20458 		if (((uint8_t *)capacity16_buf)[14] & (1 << 6))
20459 			un->un_thin_flags |= SD_THIN_PROV_READ_ZEROS;
20460 
20461 		pbsize = lbasize << lbpb_exp;
20462 
20463 		/*
20464 		 * Done with capacity16_buf
20465 		 */
20466 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20467 
20468 		/*
20469 		 * if the reported capacity is set to all 0xf's, then
20470 		 * this disk is too large.  This could only happen with
20471 		 * a device that supports LBAs larger than 64 bits which
20472 		 * are not defined by any current T10 standards.
20473 		 */
20474 		if (capacity == 0xffffffffffffffff) {
20475 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20476 			    "disk is too large");
20477 			return (EIO);
20478 		}
20479 		break;	/* Success! */
20480 	case EIO:
20481 		switch (ucmd_buf.uscsi_status) {
20482 		case STATUS_RESERVATION_CONFLICT:
20483 			status = EACCES;
20484 			break;
20485 		case STATUS_CHECK:
20486 			/*
20487 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20488 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20489 			 */
20490 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20491 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20492 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20493 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20494 				return (EAGAIN);
20495 			}
20496 			break;
20497 		default:
20498 			break;
20499 		}
20500 		/* FALLTHRU */
20501 	default:
20502 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20503 		return (status);
20504 	}
20505 
20506 	/*
20507 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20508 	 * (2352 and 0 are common) so for these devices always force the value
20509 	 * to 2048 as required by the ATAPI specs.
20510 	 */
20511 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20512 		lbasize = 2048;
20513 	}
20514 
20515 	/*
20516 	 * Get the maximum LBA value from the READ CAPACITY 16 data.
20517 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20518 	 * was cleared when issuing the command. This means that the LBA
20519 	 * returned from the device is the LBA of the last logical block
20520 	 * on the logical unit.  The actual logical block count will be
20521 	 * this value plus one.
20522 	 */
20523 	capacity += 1;
20524 
20525 	/*
20526 	 * Currently, for removable media, the capacity is saved in terms
20527 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20528 	 */
20529 	if (un->un_f_has_removable_media)
20530 		capacity *= (lbasize / un->un_sys_blocksize);
20531 
20532 	*capp = capacity;
20533 	*lbap = lbasize;
20534 	*psp = pbsize;
20535 
20536 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20537 	    "capacity:0x%llx  lbasize:0x%x, pbsize: 0x%x\n",
20538 	    capacity, lbasize, pbsize);
20539 
20540 	if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
20541 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20542 		    "sd_send_scsi_READ_CAPACITY_16 received invalid value "
20543 		    "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
20544 		return (EIO);
20545 	}
20546 
20547 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20548 	return (0);
20549 }
20550 
20551 
20552 /*
20553  *    Function: sd_send_scsi_START_STOP_UNIT
20554  *
20555  * Description: Issue a scsi START STOP UNIT command to the target.
20556  *
20557  *   Arguments: ssc    - ssc contatins pointer to driver soft state (unit)
20558  *                       structure for this target.
20559  *      pc_flag - SD_POWER_CONDITION
20560  *                SD_START_STOP
20561  *		flag  - SD_TARGET_START
20562  *			SD_TARGET_STOP
20563  *			SD_TARGET_EJECT
20564  *			SD_TARGET_CLOSE
20565  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20566  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20567  *			to use the USCSI "direct" chain and bypass the normal
20568  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20569  *			command is issued as part of an error recovery action.
20570  *
20571  * Return Code: 0   - Success
20572  *		EIO - IO error
20573  *		EACCES - Reservation conflict detected
20574  *		ENXIO  - Not Ready, medium not present
20575  *		errno return code from sd_ssc_send()
20576  *
20577  *     Context: Can sleep.
20578  */
20579 
20580 static int
20581 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20582     int path_flag)
20583 {
20584 	struct	scsi_extended_sense	sense_buf;
20585 	union scsi_cdb		cdb;
20586 	struct uscsi_cmd	ucmd_buf;
20587 	int			status;
20588 	struct sd_lun		*un;
20589 
20590 	ASSERT(ssc != NULL);
20591 	un = ssc->ssc_un;
20592 	ASSERT(un != NULL);
20593 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20594 
20595 	SD_TRACE(SD_LOG_IO, un,
20596 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20597 
20598 	if (un->un_f_check_start_stop &&
20599 	    (pc_flag == SD_START_STOP) &&
20600 	    ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20601 	    (un->un_f_start_stop_supported != TRUE)) {
20602 		return (0);
20603 	}
20604 
20605 	/*
20606 	 * If we are performing an eject operation and
20607 	 * we receive any command other than SD_TARGET_EJECT
20608 	 * we should immediately return.
20609 	 */
20610 	if (flag != SD_TARGET_EJECT) {
20611 		mutex_enter(SD_MUTEX(un));
20612 		if (un->un_f_ejecting == TRUE) {
20613 			mutex_exit(SD_MUTEX(un));
20614 			return (EAGAIN);
20615 		}
20616 		mutex_exit(SD_MUTEX(un));
20617 	}
20618 
20619 	bzero(&cdb, sizeof (cdb));
20620 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20621 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20622 
20623 	cdb.scc_cmd = SCMD_START_STOP;
20624 	cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20625 	    (uchar_t)(flag << 4) : (uchar_t)flag;
20626 
20627 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20628 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20629 	ucmd_buf.uscsi_bufaddr	= NULL;
20630 	ucmd_buf.uscsi_buflen	= 0;
20631 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20632 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20633 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20634 	ucmd_buf.uscsi_timeout	= 200;
20635 
20636 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20637 	    UIO_SYSSPACE, path_flag);
20638 
20639 	switch (status) {
20640 	case 0:
20641 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20642 		break;	/* Success! */
20643 	case EIO:
20644 		switch (ucmd_buf.uscsi_status) {
20645 		case STATUS_RESERVATION_CONFLICT:
20646 			status = EACCES;
20647 			break;
20648 		case STATUS_CHECK:
20649 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20650 				switch (scsi_sense_key(
20651 				    (uint8_t *)&sense_buf)) {
20652 				case KEY_ILLEGAL_REQUEST:
20653 					status = ENOTSUP;
20654 					break;
20655 				case KEY_NOT_READY:
20656 					if (scsi_sense_asc(
20657 					    (uint8_t *)&sense_buf)
20658 					    == 0x3A) {
20659 						status = ENXIO;
20660 					}
20661 					break;
20662 				default:
20663 					break;
20664 				}
20665 			}
20666 			break;
20667 		default:
20668 			break;
20669 		}
20670 		break;
20671 	default:
20672 		break;
20673 	}
20674 
20675 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20676 
20677 	return (status);
20678 }
20679 
20680 
20681 /*
20682  *    Function: sd_start_stop_unit_callback
20683  *
20684  * Description: timeout(9F) callback to begin recovery process for a
20685  *		device that has spun down.
20686  *
20687  *   Arguments: arg - pointer to associated softstate struct.
20688  *
20689  *     Context: Executes in a timeout(9F) thread context
20690  */
20691 
20692 static void
20693 sd_start_stop_unit_callback(void *arg)
20694 {
20695 	struct sd_lun	*un = arg;
20696 	ASSERT(un != NULL);
20697 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20698 
20699 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20700 
20701 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20702 }
20703 
20704 
20705 /*
20706  *    Function: sd_start_stop_unit_task
20707  *
20708  * Description: Recovery procedure when a drive is spun down.
20709  *
20710  *   Arguments: arg - pointer to associated softstate struct.
20711  *
20712  *     Context: Executes in a taskq() thread context
20713  */
20714 
20715 static void
20716 sd_start_stop_unit_task(void *arg)
20717 {
20718 	struct sd_lun	*un = arg;
20719 	sd_ssc_t	*ssc;
20720 	int		power_level;
20721 	int		rval;
20722 
20723 	ASSERT(un != NULL);
20724 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20725 
20726 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20727 
20728 	/*
20729 	 * Some unformatted drives report not ready error, no need to
20730 	 * restart if format has been initiated.
20731 	 */
20732 	mutex_enter(SD_MUTEX(un));
20733 	if (un->un_f_format_in_progress == TRUE) {
20734 		mutex_exit(SD_MUTEX(un));
20735 		return;
20736 	}
20737 	mutex_exit(SD_MUTEX(un));
20738 
20739 	ssc = sd_ssc_init(un);
20740 	/*
20741 	 * When a START STOP command is issued from here, it is part of a
20742 	 * failure recovery operation and must be issued before any other
20743 	 * commands, including any pending retries. Thus it must be sent
20744 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20745 	 * succeeds or not, we will start I/O after the attempt.
20746 	 * If power condition is supported and the current power level
20747 	 * is capable of performing I/O, we should set the power condition
20748 	 * to that level. Otherwise, set the power condition to ACTIVE.
20749 	 */
20750 	if (un->un_f_power_condition_supported) {
20751 		mutex_enter(SD_MUTEX(un));
20752 		ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20753 		power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20754 		    > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20755 		mutex_exit(SD_MUTEX(un));
20756 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20757 		    sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20758 	} else {
20759 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20760 		    SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20761 	}
20762 
20763 	if (rval != 0)
20764 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20765 	sd_ssc_fini(ssc);
20766 	/*
20767 	 * The above call blocks until the START_STOP_UNIT command completes.
20768 	 * Now that it has completed, we must re-try the original IO that
20769 	 * received the NOT READY condition in the first place. There are
20770 	 * three possible conditions here:
20771 	 *
20772 	 *  (1) The original IO is on un_retry_bp.
20773 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
20774 	 *	is NULL.
20775 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
20776 	 *	points to some other, unrelated bp.
20777 	 *
20778 	 * For each case, we must call sd_start_cmds() with un_retry_bp
20779 	 * as the argument. If un_retry_bp is NULL, this will initiate
20780 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
20781 	 * then this will process the bp on un_retry_bp. That may or may not
20782 	 * be the original IO, but that does not matter: the important thing
20783 	 * is to keep the IO processing going at this point.
20784 	 *
20785 	 * Note: This is a very specific error recovery sequence associated
20786 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20787 	 * serialize the I/O with completion of the spin-up.
20788 	 */
20789 	mutex_enter(SD_MUTEX(un));
20790 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20791 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20792 	    un, un->un_retry_bp);
20793 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
20794 	sd_start_cmds(un, un->un_retry_bp);
20795 	mutex_exit(SD_MUTEX(un));
20796 
20797 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20798 }
20799 
20800 
20801 /*
20802  *    Function: sd_send_scsi_INQUIRY
20803  *
20804  * Description: Issue the scsi INQUIRY command.
20805  *
20806  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20807  *                      structure for this target.
20808  *		bufaddr
20809  *		buflen
20810  *		evpd
20811  *		page_code
20812  *		page_length
20813  *
20814  * Return Code: 0   - Success
20815  *		errno return code from sd_ssc_send()
20816  *
20817  *     Context: Can sleep. Does not return until command is completed.
20818  */
20819 
20820 static int
20821 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20822     uchar_t evpd, uchar_t page_code, size_t *residp)
20823 {
20824 	union scsi_cdb		cdb;
20825 	struct uscsi_cmd	ucmd_buf;
20826 	int			status;
20827 	struct sd_lun		*un;
20828 
20829 	ASSERT(ssc != NULL);
20830 	un = ssc->ssc_un;
20831 	ASSERT(un != NULL);
20832 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20833 	ASSERT(bufaddr != NULL);
20834 
20835 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20836 
20837 	bzero(&cdb, sizeof (cdb));
20838 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20839 	bzero(bufaddr, buflen);
20840 
20841 	cdb.scc_cmd = SCMD_INQUIRY;
20842 	cdb.cdb_opaque[1] = evpd;
20843 	cdb.cdb_opaque[2] = page_code;
20844 	FORMG0COUNT(&cdb, buflen);
20845 
20846 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20847 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20848 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20849 	ucmd_buf.uscsi_buflen	= buflen;
20850 	ucmd_buf.uscsi_rqbuf	= NULL;
20851 	ucmd_buf.uscsi_rqlen	= 0;
20852 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
20853 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
20854 
20855 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20856 	    UIO_SYSSPACE, SD_PATH_DIRECT);
20857 
20858 	/*
20859 	 * Only handle status == 0, the upper-level caller
20860 	 * will put different assessment based on the context.
20861 	 */
20862 	if (status == 0)
20863 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20864 
20865 	if ((status == 0) && (residp != NULL)) {
20866 		*residp = ucmd_buf.uscsi_resid;
20867 	}
20868 
20869 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20870 
20871 	return (status);
20872 }
20873 
20874 
20875 /*
20876  *    Function: sd_send_scsi_TEST_UNIT_READY
20877  *
20878  * Description: Issue the scsi TEST UNIT READY command.
20879  *		This routine can be told to set the flag USCSI_DIAGNOSE to
20880  *		prevent retrying failed commands. Use this when the intent
20881  *		is either to check for device readiness, to clear a Unit
20882  *		Attention, or to clear any outstanding sense data.
20883  *		However under specific conditions the expected behavior
20884  *		is for retries to bring a device ready, so use the flag
20885  *		with caution.
20886  *
20887  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20888  *                      structure for this target.
20889  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20890  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20891  *			0: dont check for media present, do retries on cmd.
20892  *
20893  * Return Code: 0   - Success
20894  *		EIO - IO error
20895  *		EACCES - Reservation conflict detected
20896  *		ENXIO  - Not Ready, medium not present
20897  *		errno return code from sd_ssc_send()
20898  *
20899  *     Context: Can sleep. Does not return until command is completed.
20900  */
20901 
20902 static int
20903 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20904 {
20905 	struct	scsi_extended_sense	sense_buf;
20906 	union scsi_cdb		cdb;
20907 	struct uscsi_cmd	ucmd_buf;
20908 	int			status;
20909 	struct sd_lun		*un;
20910 
20911 	ASSERT(ssc != NULL);
20912 	un = ssc->ssc_un;
20913 	ASSERT(un != NULL);
20914 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20915 
20916 	SD_TRACE(SD_LOG_IO, un,
20917 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20918 
20919 	/*
20920 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20921 	 * timeouts when they receive a TUR and the queue is not empty. Check
20922 	 * the configuration flag set during attach (indicating the drive has
20923 	 * this firmware bug) and un_ncmds_in_transport before issuing the
20924 	 * TUR. If there are
20925 	 * pending commands return success, this is a bit arbitrary but is ok
20926 	 * for non-removables (i.e. the eliteI disks) and non-clustering
20927 	 * configurations.
20928 	 */
20929 	if (un->un_f_cfg_tur_check == TRUE) {
20930 		mutex_enter(SD_MUTEX(un));
20931 		if (un->un_ncmds_in_transport != 0) {
20932 			mutex_exit(SD_MUTEX(un));
20933 			return (0);
20934 		}
20935 		mutex_exit(SD_MUTEX(un));
20936 	}
20937 
20938 	bzero(&cdb, sizeof (cdb));
20939 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20940 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20941 
20942 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20943 
20944 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20945 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20946 	ucmd_buf.uscsi_bufaddr	= NULL;
20947 	ucmd_buf.uscsi_buflen	= 0;
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_SILENT;
20951 
20952 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20953 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
20954 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20955 	}
20956 	ucmd_buf.uscsi_timeout	= 60;
20957 
20958 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20959 	    UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20960 	    SD_PATH_STANDARD));
20961 
20962 	switch (status) {
20963 	case 0:
20964 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20965 		break;	/* Success! */
20966 	case EIO:
20967 		switch (ucmd_buf.uscsi_status) {
20968 		case STATUS_RESERVATION_CONFLICT:
20969 			status = EACCES;
20970 			break;
20971 		case STATUS_CHECK:
20972 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20973 				break;
20974 			}
20975 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20976 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20977 			    KEY_NOT_READY) &&
20978 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20979 				status = ENXIO;
20980 			}
20981 			break;
20982 		default:
20983 			break;
20984 		}
20985 		break;
20986 	default:
20987 		break;
20988 	}
20989 
20990 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20991 
20992 	return (status);
20993 }
20994 
20995 /*
20996  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20997  *
20998  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20999  *
21000  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21001  *                      structure for this target.
21002  *
21003  * Return Code: 0   - Success
21004  *		EACCES
21005  *		ENOTSUP
21006  *		errno return code from sd_ssc_send()
21007  *
21008  *     Context: Can sleep. Does not return until command is completed.
21009  */
21010 
21011 static int
21012 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd,
21013     uint16_t data_len, uchar_t *data_bufp)
21014 {
21015 	struct scsi_extended_sense	sense_buf;
21016 	union scsi_cdb		cdb;
21017 	struct uscsi_cmd	ucmd_buf;
21018 	int			status;
21019 	int			no_caller_buf = FALSE;
21020 	struct sd_lun		*un;
21021 
21022 	ASSERT(ssc != NULL);
21023 	un = ssc->ssc_un;
21024 	ASSERT(un != NULL);
21025 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21026 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
21027 
21028 	SD_TRACE(SD_LOG_IO, un,
21029 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
21030 
21031 	bzero(&cdb, sizeof (cdb));
21032 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21033 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21034 	if (data_bufp == NULL) {
21035 		/* Allocate a default buf if the caller did not give one */
21036 		ASSERT(data_len == 0);
21037 		data_len  = MHIOC_RESV_KEY_SIZE;
21038 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
21039 		no_caller_buf = TRUE;
21040 	}
21041 
21042 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
21043 	cdb.cdb_opaque[1] = usr_cmd;
21044 	FORMG1COUNT(&cdb, data_len);
21045 
21046 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21047 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21048 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
21049 	ucmd_buf.uscsi_buflen	= data_len;
21050 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21051 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21052 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21053 	ucmd_buf.uscsi_timeout	= 60;
21054 
21055 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21056 	    UIO_SYSSPACE, SD_PATH_STANDARD);
21057 
21058 	switch (status) {
21059 	case 0:
21060 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21061 
21062 		break;	/* Success! */
21063 	case EIO:
21064 		switch (ucmd_buf.uscsi_status) {
21065 		case STATUS_RESERVATION_CONFLICT:
21066 			status = EACCES;
21067 			break;
21068 		case STATUS_CHECK:
21069 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21070 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
21071 			    KEY_ILLEGAL_REQUEST)) {
21072 				status = ENOTSUP;
21073 			}
21074 			break;
21075 		default:
21076 			break;
21077 		}
21078 		break;
21079 	default:
21080 		break;
21081 	}
21082 
21083 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
21084 
21085 	if (no_caller_buf == TRUE) {
21086 		kmem_free(data_bufp, data_len);
21087 	}
21088 
21089 	return (status);
21090 }
21091 
21092 
21093 /*
21094  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
21095  *
21096  * Description: This routine is the driver entry point for handling CD-ROM
21097  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
21098  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
21099  *		device.
21100  *
21101  *   Arguments: ssc  -  ssc contains un - pointer to soft state struct
21102  *                      for the target.
21103  *		usr_cmd SCSI-3 reservation facility command (one of
21104  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
21105  *			SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
21106  *		usr_bufp - user provided pointer register, reserve descriptor or
21107  *			preempt and abort structure (mhioc_register_t,
21108  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
21109  *
21110  * Return Code: 0   - Success
21111  *		EACCES
21112  *		ENOTSUP
21113  *		errno return code from sd_ssc_send()
21114  *
21115  *     Context: Can sleep. Does not return until command is completed.
21116  */
21117 
21118 static int
21119 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
21120     uchar_t *usr_bufp)
21121 {
21122 	struct scsi_extended_sense	sense_buf;
21123 	union scsi_cdb		cdb;
21124 	struct uscsi_cmd	ucmd_buf;
21125 	int			status;
21126 	uchar_t			data_len = sizeof (sd_prout_t);
21127 	sd_prout_t		*prp;
21128 	struct sd_lun		*un;
21129 
21130 	ASSERT(ssc != NULL);
21131 	un = ssc->ssc_un;
21132 	ASSERT(un != NULL);
21133 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21134 	ASSERT(data_len == 24);	/* required by scsi spec */
21135 
21136 	SD_TRACE(SD_LOG_IO, un,
21137 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
21138 
21139 	if (usr_bufp == NULL) {
21140 		return (EINVAL);
21141 	}
21142 
21143 	bzero(&cdb, sizeof (cdb));
21144 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21145 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21146 	prp = kmem_zalloc(data_len, KM_SLEEP);
21147 
21148 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
21149 	cdb.cdb_opaque[1] = usr_cmd;
21150 	FORMG1COUNT(&cdb, data_len);
21151 
21152 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21153 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21154 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
21155 	ucmd_buf.uscsi_buflen	= data_len;
21156 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21157 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21158 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21159 	ucmd_buf.uscsi_timeout	= 60;
21160 
21161 	switch (usr_cmd) {
21162 	case SD_SCSI3_REGISTER: {
21163 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
21164 
21165 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21166 		bcopy(ptr->newkey.key, prp->service_key,
21167 		    MHIOC_RESV_KEY_SIZE);
21168 		prp->aptpl = ptr->aptpl;
21169 		break;
21170 	}
21171 	case SD_SCSI3_CLEAR: {
21172 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21173 
21174 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21175 		break;
21176 	}
21177 	case SD_SCSI3_RESERVE:
21178 	case SD_SCSI3_RELEASE: {
21179 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21180 
21181 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21182 		prp->scope_address = BE_32(ptr->scope_specific_addr);
21183 		cdb.cdb_opaque[2] = ptr->type;
21184 		break;
21185 	}
21186 	case SD_SCSI3_PREEMPTANDABORT: {
21187 		mhioc_preemptandabort_t *ptr =
21188 		    (mhioc_preemptandabort_t *)usr_bufp;
21189 
21190 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21191 		bcopy(ptr->victim_key.key, prp->service_key,
21192 		    MHIOC_RESV_KEY_SIZE);
21193 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
21194 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
21195 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
21196 		break;
21197 	}
21198 	case SD_SCSI3_REGISTERANDIGNOREKEY:
21199 	{
21200 		mhioc_registerandignorekey_t *ptr;
21201 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
21202 		bcopy(ptr->newkey.key,
21203 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
21204 		prp->aptpl = ptr->aptpl;
21205 		break;
21206 	}
21207 	default:
21208 		ASSERT(FALSE);
21209 		break;
21210 	}
21211 
21212 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21213 	    UIO_SYSSPACE, SD_PATH_STANDARD);
21214 
21215 	switch (status) {
21216 	case 0:
21217 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21218 		break;	/* Success! */
21219 	case EIO:
21220 		switch (ucmd_buf.uscsi_status) {
21221 		case STATUS_RESERVATION_CONFLICT:
21222 			status = EACCES;
21223 			break;
21224 		case STATUS_CHECK:
21225 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21226 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
21227 			    KEY_ILLEGAL_REQUEST)) {
21228 				status = ENOTSUP;
21229 			}
21230 			break;
21231 		default:
21232 			break;
21233 		}
21234 		break;
21235 	default:
21236 		break;
21237 	}
21238 
21239 	kmem_free(prp, data_len);
21240 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
21241 	return (status);
21242 }
21243 
21244 
21245 /*
21246  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
21247  *
21248  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
21249  *
21250  *   Arguments: un - pointer to the target's soft state struct
21251  *              dkc - pointer to the callback structure
21252  *
21253  * Return Code: 0 - success
21254  *		errno-type error code
21255  *
21256  *     Context: kernel thread context only.
21257  *
21258  *  _______________________________________________________________
21259  * | dkc_flag &   | dkc_callback | DKIOCFLUSHWRITECACHE            |
21260  * |FLUSH_VOLATILE|              | operation                       |
21261  * |______________|______________|_________________________________|
21262  * | 0            | NULL         | Synchronous flush on both       |
21263  * |              |              | volatile and non-volatile cache |
21264  * |______________|______________|_________________________________|
21265  * | 1            | NULL         | Synchronous flush on volatile   |
21266  * |              |              | cache; disk drivers may suppress|
21267  * |              |              | flush if disk table indicates   |
21268  * |              |              | non-volatile cache              |
21269  * |______________|______________|_________________________________|
21270  * | 0            | !NULL        | Asynchronous flush on both      |
21271  * |              |              | volatile and non-volatile cache;|
21272  * |______________|______________|_________________________________|
21273  * | 1            | !NULL        | Asynchronous flush on volatile  |
21274  * |              |              | cache; disk drivers may suppress|
21275  * |              |              | flush if disk table indicates   |
21276  * |              |              | non-volatile cache              |
21277  * |______________|______________|_________________________________|
21278  *
21279  */
21280 
21281 static int
21282 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
21283 {
21284 	struct sd_uscsi_info	*uip;
21285 	struct uscsi_cmd	*uscmd;
21286 	union scsi_cdb		*cdb;
21287 	struct buf		*bp;
21288 	int			rval = 0;
21289 	int			is_async;
21290 
21291 	SD_TRACE(SD_LOG_IO, un,
21292 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
21293 
21294 	ASSERT(un != NULL);
21295 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21296 
21297 	if (dkc == NULL || dkc->dkc_callback == NULL) {
21298 		is_async = FALSE;
21299 	} else {
21300 		is_async = TRUE;
21301 	}
21302 
21303 	mutex_enter(SD_MUTEX(un));
21304 	/* check whether cache flush should be suppressed */
21305 	if (un->un_f_suppress_cache_flush == TRUE) {
21306 		mutex_exit(SD_MUTEX(un));
21307 		/*
21308 		 * suppress the cache flush if the device is told to do
21309 		 * so by sd.conf or disk table
21310 		 */
21311 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
21312 		    skip the cache flush since suppress_cache_flush is %d!\n",
21313 		    un->un_f_suppress_cache_flush);
21314 
21315 		if (is_async == TRUE) {
21316 			/* invoke callback for asynchronous flush */
21317 			(*dkc->dkc_callback)(dkc->dkc_cookie, 0);
21318 		}
21319 		return (rval);
21320 	}
21321 	mutex_exit(SD_MUTEX(un));
21322 
21323 	/*
21324 	 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
21325 	 * set properly
21326 	 */
21327 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
21328 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
21329 
21330 	mutex_enter(SD_MUTEX(un));
21331 	if (dkc != NULL && un->un_f_sync_nv_supported &&
21332 	    (dkc->dkc_flag & FLUSH_VOLATILE)) {
21333 		/*
21334 		 * if the device supports SYNC_NV bit, turn on
21335 		 * the SYNC_NV bit to only flush volatile cache
21336 		 */
21337 		cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21338 	}
21339 	mutex_exit(SD_MUTEX(un));
21340 
21341 	/*
21342 	 * First get some memory for the uscsi_cmd struct and cdb
21343 	 * and initialize for SYNCHRONIZE_CACHE cmd.
21344 	 */
21345 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21346 	uscmd->uscsi_cdblen = CDB_GROUP1;
21347 	uscmd->uscsi_cdb = (caddr_t)cdb;
21348 	uscmd->uscsi_bufaddr = NULL;
21349 	uscmd->uscsi_buflen = 0;
21350 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21351 	uscmd->uscsi_rqlen = SENSE_LENGTH;
21352 	uscmd->uscsi_rqresid = SENSE_LENGTH;
21353 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21354 	uscmd->uscsi_timeout = sd_io_time;
21355 
21356 	/*
21357 	 * Allocate an sd_uscsi_info struct and fill it with the info
21358 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
21359 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
21360 	 * since we allocate the buf here in this function, we do not
21361 	 * need to preserve the prior contents of b_private.
21362 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21363 	 */
21364 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21365 	uip->ui_flags = SD_PATH_DIRECT;
21366 	uip->ui_cmdp  = uscmd;
21367 
21368 	bp = getrbuf(KM_SLEEP);
21369 	bp->b_private = uip;
21370 
21371 	/*
21372 	 * Setup buffer to carry uscsi request.
21373 	 */
21374 	bp->b_flags  = B_BUSY;
21375 	bp->b_bcount = 0;
21376 	bp->b_blkno  = 0;
21377 
21378 	if (is_async == TRUE) {
21379 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21380 		uip->ui_dkc = *dkc;
21381 	}
21382 
21383 	bp->b_edev = SD_GET_DEV(un);
21384 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
21385 
21386 	/*
21387 	 * Unset un_f_sync_cache_required flag
21388 	 */
21389 	mutex_enter(SD_MUTEX(un));
21390 	un->un_f_sync_cache_required = FALSE;
21391 	mutex_exit(SD_MUTEX(un));
21392 
21393 	(void) sd_uscsi_strategy(bp);
21394 
21395 	/*
21396 	 * If synchronous request, wait for completion
21397 	 * If async just return and let b_iodone callback
21398 	 * cleanup.
21399 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
21400 	 * but it was also incremented in sd_uscsi_strategy(), so
21401 	 * we should be ok.
21402 	 */
21403 	if (is_async == FALSE) {
21404 		(void) biowait(bp);
21405 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21406 	}
21407 
21408 	return (rval);
21409 }
21410 
21411 
21412 static int
21413 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21414 {
21415 	struct sd_uscsi_info *uip;
21416 	struct uscsi_cmd *uscmd;
21417 	uint8_t *sense_buf;
21418 	struct sd_lun *un;
21419 	int status;
21420 	union scsi_cdb *cdb;
21421 
21422 	uip = (struct sd_uscsi_info *)(bp->b_private);
21423 	ASSERT(uip != NULL);
21424 
21425 	uscmd = uip->ui_cmdp;
21426 	ASSERT(uscmd != NULL);
21427 
21428 	sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21429 	ASSERT(sense_buf != NULL);
21430 
21431 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21432 	ASSERT(un != NULL);
21433 
21434 	cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21435 
21436 	status = geterror(bp);
21437 	switch (status) {
21438 	case 0:
21439 		break;	/* Success! */
21440 	case EIO:
21441 		switch (uscmd->uscsi_status) {
21442 		case STATUS_RESERVATION_CONFLICT:
21443 			/* Ignore reservation conflict */
21444 			status = 0;
21445 			goto done;
21446 
21447 		case STATUS_CHECK:
21448 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21449 			    (scsi_sense_key(sense_buf) ==
21450 			    KEY_ILLEGAL_REQUEST)) {
21451 				/* Ignore Illegal Request error */
21452 				if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21453 					mutex_enter(SD_MUTEX(un));
21454 					un->un_f_sync_nv_supported = FALSE;
21455 					mutex_exit(SD_MUTEX(un));
21456 					status = 0;
21457 					SD_TRACE(SD_LOG_IO, un,
21458 					    "un_f_sync_nv_supported \
21459 					    is set to false.\n");
21460 					goto done;
21461 				}
21462 
21463 				mutex_enter(SD_MUTEX(un));
21464 				un->un_f_sync_cache_supported = FALSE;
21465 				mutex_exit(SD_MUTEX(un));
21466 				SD_TRACE(SD_LOG_IO, un,
21467 				    "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21468 				    un_f_sync_cache_supported set to false \
21469 				    with asc = %x, ascq = %x\n",
21470 				    scsi_sense_asc(sense_buf),
21471 				    scsi_sense_ascq(sense_buf));
21472 				status = ENOTSUP;
21473 				goto done;
21474 			}
21475 			break;
21476 		default:
21477 			break;
21478 		}
21479 		/* FALLTHRU */
21480 	default:
21481 		/*
21482 		 * Turn on the un_f_sync_cache_required flag
21483 		 * since the SYNC CACHE command failed
21484 		 */
21485 		mutex_enter(SD_MUTEX(un));
21486 		un->un_f_sync_cache_required = TRUE;
21487 		mutex_exit(SD_MUTEX(un));
21488 
21489 		/*
21490 		 * Don't log an error message if this device
21491 		 * has removable media.
21492 		 */
21493 		if (!un->un_f_has_removable_media) {
21494 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21495 			    "SYNCHRONIZE CACHE command failed (%d)\n", status);
21496 		}
21497 		break;
21498 	}
21499 
21500 done:
21501 	if (uip->ui_dkc.dkc_callback != NULL) {
21502 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21503 	}
21504 
21505 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
21506 	freerbuf(bp);
21507 	kmem_free(uip, sizeof (struct sd_uscsi_info));
21508 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21509 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21510 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
21511 
21512 	return (status);
21513 }
21514 
21515 /*
21516  * Issues a single SCSI UNMAP command with a prepared UNMAP parameter list.
21517  * Returns zero on success, or the non-zero command error code on failure.
21518  */
21519 static int
21520 sd_send_scsi_UNMAP_issue_one(sd_ssc_t *ssc, unmap_param_hdr_t *uph,
21521     uint64_t num_descr, uint64_t bytes)
21522 {
21523 	struct sd_lun		*un = ssc->ssc_un;
21524 	struct scsi_extended_sense	sense_buf;
21525 	union scsi_cdb		cdb;
21526 	struct uscsi_cmd	ucmd_buf;
21527 	int			status;
21528 	const uint64_t		param_size = sizeof (unmap_param_hdr_t) +
21529 	    num_descr * sizeof (unmap_blk_descr_t);
21530 
21531 	ASSERT3U(param_size - 2, <=, UINT16_MAX);
21532 	uph->uph_data_len = BE_16(param_size - 2);
21533 	uph->uph_descr_data_len = BE_16(param_size - 8);
21534 
21535 	bzero(&cdb, sizeof (cdb));
21536 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21537 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21538 
21539 	cdb.scc_cmd = SCMD_UNMAP;
21540 	FORMG1COUNT(&cdb, param_size);
21541 
21542 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21543 	ucmd_buf.uscsi_cdblen	= (uchar_t)CDB_GROUP1;
21544 	ucmd_buf.uscsi_bufaddr	= (caddr_t)uph;
21545 	ucmd_buf.uscsi_buflen	= param_size;
21546 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21547 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21548 	ucmd_buf.uscsi_flags	= USCSI_WRITE | USCSI_RQENABLE | USCSI_SILENT;
21549 	ucmd_buf.uscsi_timeout	= un->un_cmd_timeout;
21550 
21551 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, UIO_SYSSPACE,
21552 	    SD_PATH_STANDARD);
21553 
21554 	switch (status) {
21555 	case 0:
21556 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21557 
21558 		if (un->un_unmapstats) {
21559 			atomic_inc_64(&un->un_unmapstats->us_cmds.value.ui64);
21560 			atomic_add_64(&un->un_unmapstats->us_extents.value.ui64,
21561 			    num_descr);
21562 			atomic_add_64(&un->un_unmapstats->us_bytes.value.ui64,
21563 			    bytes);
21564 		}
21565 		break;	/* Success! */
21566 	case EIO:
21567 		if (un->un_unmapstats)
21568 			atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
21569 		switch (ucmd_buf.uscsi_status) {
21570 		case STATUS_RESERVATION_CONFLICT:
21571 			status = EACCES;
21572 			break;
21573 		default:
21574 			break;
21575 		}
21576 		break;
21577 	default:
21578 		if (un->un_unmapstats)
21579 			atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
21580 		break;
21581 	}
21582 
21583 	return (status);
21584 }
21585 
21586 /*
21587  * Returns a pointer to the i'th block descriptor inside an UNMAP param list.
21588  */
21589 static inline unmap_blk_descr_t *
21590 UNMAP_blk_descr_i(void *buf, size_t i)
21591 {
21592 	return ((unmap_blk_descr_t *)((uintptr_t)buf +
21593 	    sizeof (unmap_param_hdr_t) + (i * sizeof (unmap_blk_descr_t))));
21594 }
21595 
21596 /*
21597  * Takes the list of extents from sd_send_scsi_UNMAP, chops it up, prepares
21598  * UNMAP block descriptors and issues individual SCSI UNMAP commands. While
21599  * doing so we consult the block limits to determine at most how many
21600  * extents and LBAs we can UNMAP in one command.
21601  * If a command fails for whatever, reason, extent list processing is aborted
21602  * and the failed command's status is returned. Otherwise returns 0 on
21603  * success.
21604  */
21605 static int
21606 sd_send_scsi_UNMAP_issue(dev_t dev, sd_ssc_t *ssc, const dkioc_free_list_t *dfl)
21607 {
21608 	struct sd_lun		*un = ssc->ssc_un;
21609 	unmap_param_hdr_t	*uph;
21610 	sd_blk_limits_t		*lim = &un->un_blk_lim;
21611 	int			rval = 0;
21612 	int			partition;
21613 	/* partition offset & length in system blocks */
21614 	diskaddr_t		part_off_sysblks = 0, part_len_sysblks = 0;
21615 	uint64_t		part_off, part_len;
21616 	uint64_t		descr_cnt_lim, byte_cnt_lim;
21617 	uint64_t		descr_issued = 0, bytes_issued = 0;
21618 
21619 	uph = kmem_zalloc(SD_UNMAP_PARAM_LIST_MAXSZ, KM_SLEEP);
21620 
21621 	partition = SDPART(dev);
21622 	rval = cmlb_partinfo(un->un_cmlbhandle, partition, &part_len_sysblks,
21623 	    &part_off_sysblks, NULL, NULL, (void *)SD_PATH_DIRECT);
21624 	if (rval != 0)
21625 		goto out;
21626 	part_off = SD_SYSBLOCKS2BYTES(part_off_sysblks);
21627 	part_len = SD_SYSBLOCKS2BYTES(part_len_sysblks);
21628 
21629 	ASSERT(un->un_blk_lim.lim_max_unmap_lba_cnt != 0);
21630 	ASSERT(un->un_blk_lim.lim_max_unmap_descr_cnt != 0);
21631 	/* Spec says 0xffffffff are special values, so compute maximums. */
21632 	byte_cnt_lim = lim->lim_max_unmap_lba_cnt < UINT32_MAX ?
21633 	    (uint64_t)lim->lim_max_unmap_lba_cnt * un->un_tgt_blocksize :
21634 	    UINT64_MAX;
21635 	descr_cnt_lim = MIN(lim->lim_max_unmap_descr_cnt, SD_UNMAP_MAX_DESCR);
21636 
21637 	if (dfl->dfl_offset >= part_len) {
21638 		rval = SET_ERROR(EINVAL);
21639 		goto out;
21640 	}
21641 
21642 	for (size_t i = 0; i < dfl->dfl_num_exts; i++) {
21643 		const dkioc_free_list_ext_t *ext = &dfl->dfl_exts[i];
21644 		uint64_t ext_start = ext->dfle_start;
21645 		uint64_t ext_length = ext->dfle_length;
21646 
21647 		while (ext_length > 0) {
21648 			unmap_blk_descr_t *ubd;
21649 			/* Respect device limit on LBA count per command */
21650 			uint64_t len = MIN(MIN(ext_length, byte_cnt_lim -
21651 			    bytes_issued), SD_TGTBLOCKS2BYTES(un, UINT32_MAX));
21652 
21653 			/* check partition limits */
21654 			if (ext_start >= part_len ||
21655 			    ext_start + len < ext_start ||
21656 			    dfl->dfl_offset + ext_start + len <
21657 			    dfl->dfl_offset ||
21658 			    dfl->dfl_offset + ext_start + len > part_len) {
21659 				rval = SET_ERROR(EINVAL);
21660 				goto out;
21661 			}
21662 
21663 			ASSERT3U(descr_issued, <, descr_cnt_lim);
21664 			ASSERT3U(bytes_issued, <, byte_cnt_lim);
21665 			ubd = UNMAP_blk_descr_i(uph, descr_issued);
21666 
21667 			/* adjust in-partition addresses to be device-global */
21668 			ubd->ubd_lba = BE_64(SD_BYTES2TGTBLOCKS(un,
21669 			    dfl->dfl_offset + ext_start + part_off));
21670 			ubd->ubd_lba_cnt = BE_32(SD_BYTES2TGTBLOCKS(un, len));
21671 
21672 			descr_issued++;
21673 			bytes_issued += len;
21674 
21675 			/* Issue command when device limits reached */
21676 			if (descr_issued == descr_cnt_lim ||
21677 			    bytes_issued == byte_cnt_lim) {
21678 				rval = sd_send_scsi_UNMAP_issue_one(ssc, uph,
21679 				    descr_issued, bytes_issued);
21680 				if (rval != 0)
21681 					goto out;
21682 				descr_issued = 0;
21683 				bytes_issued = 0;
21684 			}
21685 
21686 			ext_start += len;
21687 			ext_length -= len;
21688 		}
21689 	}
21690 
21691 	if (descr_issued > 0) {
21692 		/* issue last command */
21693 		rval = sd_send_scsi_UNMAP_issue_one(ssc, uph, descr_issued,
21694 		    bytes_issued);
21695 	}
21696 
21697 out:
21698 	kmem_free(uph, SD_UNMAP_PARAM_LIST_MAXSZ);
21699 	return (rval);
21700 }
21701 
21702 /*
21703  * Issues one or several UNMAP commands based on a list of extents to be
21704  * unmapped. The internal multi-command processing is hidden, as the exact
21705  * number of commands and extents per command is limited by both SCSI
21706  * command syntax and device limits (as expressed in the SCSI Block Limits
21707  * VPD page and un_blk_lim in struct sd_lun).
21708  * Returns zero on success, or the error code of the first failed SCSI UNMAP
21709  * command.
21710  */
21711 static int
21712 sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl, int flag)
21713 {
21714 	struct sd_lun		*un = ssc->ssc_un;
21715 	int			rval = 0;
21716 
21717 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21718 	ASSERT(dfl != NULL);
21719 
21720 	/* Per spec, any of these conditions signals lack of UNMAP support. */
21721 	if (!(un->un_thin_flags & SD_THIN_PROV_ENABLED) ||
21722 	    un->un_blk_lim.lim_max_unmap_descr_cnt == 0 ||
21723 	    un->un_blk_lim.lim_max_unmap_lba_cnt == 0) {
21724 		return (SET_ERROR(ENOTSUP));
21725 	}
21726 
21727 	/* For userspace calls we must copy in. */
21728 	if (!(flag & FKIOCTL)) {
21729 		int err = dfl_copyin(dfl, &dfl, flag, KM_SLEEP);
21730 		if (err != 0)
21731 			return (err);
21732 	} else if (dfl->dfl_num_exts > DFL_COPYIN_MAX_EXTS) {
21733 		ASSERT3U(dfl->dfl_num_exts, <=, DFL_COPYIN_MAX_EXTS);
21734 		return (SET_ERROR(EINVAL));
21735 	}
21736 
21737 	rval = sd_send_scsi_UNMAP_issue(dev, ssc, dfl);
21738 
21739 	if (!(flag & FKIOCTL)) {
21740 		dfl_free(dfl);
21741 		dfl = NULL;
21742 	}
21743 
21744 	return (rval);
21745 }
21746 
21747 /*
21748  *    Function: sd_send_scsi_GET_CONFIGURATION
21749  *
21750  * Description: Issues the get configuration command to the device.
21751  *		Called from sd_check_for_writable_cd & sd_get_media_info
21752  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21753  *   Arguments: ssc
21754  *		ucmdbuf
21755  *		rqbuf
21756  *		rqbuflen
21757  *		bufaddr
21758  *		buflen
21759  *		path_flag
21760  *
21761  * Return Code: 0   - Success
21762  *		errno return code from sd_ssc_send()
21763  *
21764  *     Context: Can sleep. Does not return until command is completed.
21765  *
21766  */
21767 
21768 static int
21769 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21770     uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21771     int path_flag)
21772 {
21773 	char	cdb[CDB_GROUP1];
21774 	int	status;
21775 	struct sd_lun	*un;
21776 
21777 	ASSERT(ssc != NULL);
21778 	un = ssc->ssc_un;
21779 	ASSERT(un != NULL);
21780 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21781 	ASSERT(bufaddr != NULL);
21782 	ASSERT(ucmdbuf != NULL);
21783 	ASSERT(rqbuf != NULL);
21784 
21785 	SD_TRACE(SD_LOG_IO, un,
21786 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21787 
21788 	bzero(cdb, sizeof (cdb));
21789 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21790 	bzero(rqbuf, rqbuflen);
21791 	bzero(bufaddr, buflen);
21792 
21793 	/*
21794 	 * Set up cdb field for the get configuration command.
21795 	 */
21796 	cdb[0] = SCMD_GET_CONFIGURATION;
21797 	cdb[1] = 0x02;  /* Requested Type */
21798 	cdb[8] = SD_PROFILE_HEADER_LEN;
21799 	ucmdbuf->uscsi_cdb = cdb;
21800 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21801 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21802 	ucmdbuf->uscsi_buflen = buflen;
21803 	ucmdbuf->uscsi_timeout = sd_io_time;
21804 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21805 	ucmdbuf->uscsi_rqlen = rqbuflen;
21806 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21807 
21808 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21809 	    UIO_SYSSPACE, path_flag);
21810 
21811 	switch (status) {
21812 	case 0:
21813 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21814 		break;  /* Success! */
21815 	case EIO:
21816 		switch (ucmdbuf->uscsi_status) {
21817 		case STATUS_RESERVATION_CONFLICT:
21818 			status = EACCES;
21819 			break;
21820 		default:
21821 			break;
21822 		}
21823 		break;
21824 	default:
21825 		break;
21826 	}
21827 
21828 	if (status == 0) {
21829 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21830 		    "sd_send_scsi_GET_CONFIGURATION: data",
21831 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21832 	}
21833 
21834 	SD_TRACE(SD_LOG_IO, un,
21835 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
21836 
21837 	return (status);
21838 }
21839 
21840 /*
21841  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
21842  *
21843  * Description: Issues the get configuration command to the device to
21844  *              retrieve a specific feature. Called from
21845  *		sd_check_for_writable_cd & sd_set_mmc_caps.
21846  *   Arguments: ssc
21847  *              ucmdbuf
21848  *              rqbuf
21849  *              rqbuflen
21850  *              bufaddr
21851  *              buflen
21852  *		feature
21853  *
21854  * Return Code: 0   - Success
21855  *              errno return code from sd_ssc_send()
21856  *
21857  *     Context: Can sleep. Does not return until command is completed.
21858  *
21859  */
21860 static int
21861 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21862     uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21863     char feature, int path_flag)
21864 {
21865 	char    cdb[CDB_GROUP1];
21866 	int	status;
21867 	struct sd_lun	*un;
21868 
21869 	ASSERT(ssc != NULL);
21870 	un = ssc->ssc_un;
21871 	ASSERT(un != NULL);
21872 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21873 	ASSERT(bufaddr != NULL);
21874 	ASSERT(ucmdbuf != NULL);
21875 	ASSERT(rqbuf != NULL);
21876 
21877 	SD_TRACE(SD_LOG_IO, un,
21878 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21879 
21880 	bzero(cdb, sizeof (cdb));
21881 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21882 	bzero(rqbuf, rqbuflen);
21883 	bzero(bufaddr, buflen);
21884 
21885 	/*
21886 	 * Set up cdb field for the get configuration command.
21887 	 */
21888 	cdb[0] = SCMD_GET_CONFIGURATION;
21889 	cdb[1] = 0x02;  /* Requested Type */
21890 	cdb[3] = feature;
21891 	cdb[8] = buflen;
21892 	ucmdbuf->uscsi_cdb = cdb;
21893 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21894 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21895 	ucmdbuf->uscsi_buflen = buflen;
21896 	ucmdbuf->uscsi_timeout = sd_io_time;
21897 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21898 	ucmdbuf->uscsi_rqlen = rqbuflen;
21899 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21900 
21901 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21902 	    UIO_SYSSPACE, path_flag);
21903 
21904 	switch (status) {
21905 	case 0:
21906 
21907 		break;  /* Success! */
21908 	case EIO:
21909 		switch (ucmdbuf->uscsi_status) {
21910 		case STATUS_RESERVATION_CONFLICT:
21911 			status = EACCES;
21912 			break;
21913 		default:
21914 			break;
21915 		}
21916 		break;
21917 	default:
21918 		break;
21919 	}
21920 
21921 	if (status == 0) {
21922 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21923 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
21924 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21925 	}
21926 
21927 	SD_TRACE(SD_LOG_IO, un,
21928 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21929 
21930 	return (status);
21931 }
21932 
21933 
21934 /*
21935  *    Function: sd_send_scsi_MODE_SENSE
21936  *
21937  * Description: Utility function for issuing a scsi MODE SENSE command.
21938  *		Note: This routine uses a consistent implementation for Group0,
21939  *		Group1, and Group2 commands across all platforms. ATAPI devices
21940  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21941  *
21942  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21943  *                      structure for this target.
21944  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21945  *			  CDB_GROUP[1|2] (10 byte).
21946  *		bufaddr - buffer for page data retrieved from the target.
21947  *		buflen - size of page to be retrieved.
21948  *		page_code - page code of data to be retrieved from the target.
21949  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21950  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21951  *			to use the USCSI "direct" chain and bypass the normal
21952  *			command waitq.
21953  *
21954  * Return Code: 0   - Success
21955  *		errno return code from sd_ssc_send()
21956  *
21957  *     Context: Can sleep. Does not return until command is completed.
21958  */
21959 
21960 static int
21961 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21962     size_t buflen,  uchar_t page_code, int path_flag)
21963 {
21964 	struct	scsi_extended_sense	sense_buf;
21965 	union scsi_cdb		cdb;
21966 	struct uscsi_cmd	ucmd_buf;
21967 	int			status;
21968 	int			headlen;
21969 	struct sd_lun		*un;
21970 
21971 	ASSERT(ssc != NULL);
21972 	un = ssc->ssc_un;
21973 	ASSERT(un != NULL);
21974 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21975 	ASSERT(bufaddr != NULL);
21976 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21977 	    (cdbsize == CDB_GROUP2));
21978 
21979 	SD_TRACE(SD_LOG_IO, un,
21980 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21981 
21982 	bzero(&cdb, sizeof (cdb));
21983 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21984 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21985 	bzero(bufaddr, buflen);
21986 
21987 	if (cdbsize == CDB_GROUP0) {
21988 		cdb.scc_cmd = SCMD_MODE_SENSE;
21989 		cdb.cdb_opaque[2] = page_code;
21990 		FORMG0COUNT(&cdb, buflen);
21991 		headlen = MODE_HEADER_LENGTH;
21992 	} else {
21993 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21994 		cdb.cdb_opaque[2] = page_code;
21995 		FORMG1COUNT(&cdb, buflen);
21996 		headlen = MODE_HEADER_LENGTH_GRP2;
21997 	}
21998 
21999 	ASSERT(headlen <= buflen);
22000 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
22001 
22002 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22003 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
22004 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
22005 	ucmd_buf.uscsi_buflen	= buflen;
22006 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
22007 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
22008 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
22009 	ucmd_buf.uscsi_timeout	= 60;
22010 
22011 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22012 	    UIO_SYSSPACE, path_flag);
22013 
22014 	switch (status) {
22015 	case 0:
22016 		/*
22017 		 * sr_check_wp() uses 0x3f page code and check the header of
22018 		 * mode page to determine if target device is write-protected.
22019 		 * But some USB devices return 0 bytes for 0x3f page code. For
22020 		 * this case, make sure that mode page header is returned at
22021 		 * least.
22022 		 */
22023 		if (buflen - ucmd_buf.uscsi_resid <  headlen) {
22024 			status = EIO;
22025 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
22026 			    "mode page header is not returned");
22027 		}
22028 		break;	/* Success! */
22029 	case EIO:
22030 		switch (ucmd_buf.uscsi_status) {
22031 		case STATUS_RESERVATION_CONFLICT:
22032 			status = EACCES;
22033 			break;
22034 		default:
22035 			break;
22036 		}
22037 		break;
22038 	default:
22039 		break;
22040 	}
22041 
22042 	if (status == 0) {
22043 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
22044 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22045 	}
22046 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
22047 
22048 	return (status);
22049 }
22050 
22051 
22052 /*
22053  *    Function: sd_send_scsi_MODE_SELECT
22054  *
22055  * Description: Utility function for issuing a scsi MODE SELECT command.
22056  *		Note: This routine uses a consistent implementation for Group0,
22057  *		Group1, and Group2 commands across all platforms. ATAPI devices
22058  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
22059  *
22060  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22061  *                      structure for this target.
22062  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
22063  *			  CDB_GROUP[1|2] (10 byte).
22064  *		bufaddr - buffer for page data retrieved from the target.
22065  *		buflen - size of page to be retrieved.
22066  *		save_page - boolean to determin if SP bit should be set.
22067  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
22068  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
22069  *			to use the USCSI "direct" chain and bypass the normal
22070  *			command waitq.
22071  *
22072  * Return Code: 0   - Success
22073  *		errno return code from sd_ssc_send()
22074  *
22075  *     Context: Can sleep. Does not return until command is completed.
22076  */
22077 
22078 static int
22079 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
22080     size_t buflen,  uchar_t save_page, int path_flag)
22081 {
22082 	struct	scsi_extended_sense	sense_buf;
22083 	union scsi_cdb		cdb;
22084 	struct uscsi_cmd	ucmd_buf;
22085 	int			status;
22086 	struct sd_lun		*un;
22087 
22088 	ASSERT(ssc != NULL);
22089 	un = ssc->ssc_un;
22090 	ASSERT(un != NULL);
22091 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22092 	ASSERT(bufaddr != NULL);
22093 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
22094 	    (cdbsize == CDB_GROUP2));
22095 
22096 	SD_TRACE(SD_LOG_IO, un,
22097 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
22098 
22099 	bzero(&cdb, sizeof (cdb));
22100 	bzero(&ucmd_buf, sizeof (ucmd_buf));
22101 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
22102 
22103 	/* Set the PF bit for many third party drives */
22104 	cdb.cdb_opaque[1] = 0x10;
22105 
22106 	/* Set the savepage(SP) bit if given */
22107 	if (save_page == SD_SAVE_PAGE) {
22108 		cdb.cdb_opaque[1] |= 0x01;
22109 	}
22110 
22111 	if (cdbsize == CDB_GROUP0) {
22112 		cdb.scc_cmd = SCMD_MODE_SELECT;
22113 		FORMG0COUNT(&cdb, buflen);
22114 	} else {
22115 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
22116 		FORMG1COUNT(&cdb, buflen);
22117 	}
22118 
22119 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
22120 
22121 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22122 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
22123 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
22124 	ucmd_buf.uscsi_buflen	= buflen;
22125 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
22126 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
22127 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
22128 	ucmd_buf.uscsi_timeout	= 60;
22129 
22130 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22131 	    UIO_SYSSPACE, path_flag);
22132 
22133 	switch (status) {
22134 	case 0:
22135 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22136 		break;	/* Success! */
22137 	case EIO:
22138 		switch (ucmd_buf.uscsi_status) {
22139 		case STATUS_RESERVATION_CONFLICT:
22140 			status = EACCES;
22141 			break;
22142 		default:
22143 			break;
22144 		}
22145 		break;
22146 	default:
22147 		break;
22148 	}
22149 
22150 	if (status == 0) {
22151 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
22152 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22153 	}
22154 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
22155 
22156 	return (status);
22157 }
22158 
22159 
22160 /*
22161  *    Function: sd_send_scsi_RDWR
22162  *
22163  * Description: Issue a scsi READ or WRITE command with the given parameters.
22164  *
22165  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22166  *                      structure for this target.
22167  *		cmd:	 SCMD_READ or SCMD_WRITE
22168  *		bufaddr: Address of caller's buffer to receive the RDWR data
22169  *		buflen:  Length of caller's buffer receive the RDWR data.
22170  *		start_block: Block number for the start of the RDWR operation.
22171  *			 (Assumes target-native block size.)
22172  *		residp:  Pointer to variable to receive the redisual of the
22173  *			 RDWR operation (may be NULL of no residual requested).
22174  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
22175  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
22176  *			to use the USCSI "direct" chain and bypass the normal
22177  *			command waitq.
22178  *
22179  * Return Code: 0   - Success
22180  *		errno return code from sd_ssc_send()
22181  *
22182  *     Context: Can sleep. Does not return until command is completed.
22183  */
22184 
22185 static int
22186 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
22187     size_t buflen, daddr_t start_block, int path_flag)
22188 {
22189 	struct	scsi_extended_sense	sense_buf;
22190 	union scsi_cdb		cdb;
22191 	struct uscsi_cmd	ucmd_buf;
22192 	uint32_t		block_count;
22193 	int			status;
22194 	int			cdbsize;
22195 	uchar_t			flag;
22196 	struct sd_lun		*un;
22197 
22198 	ASSERT(ssc != NULL);
22199 	un = ssc->ssc_un;
22200 	ASSERT(un != NULL);
22201 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22202 	ASSERT(bufaddr != NULL);
22203 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
22204 
22205 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
22206 
22207 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
22208 		return (EINVAL);
22209 	}
22210 
22211 	mutex_enter(SD_MUTEX(un));
22212 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
22213 	mutex_exit(SD_MUTEX(un));
22214 
22215 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
22216 
22217 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
22218 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
22219 	    bufaddr, buflen, start_block, block_count);
22220 
22221 	bzero(&cdb, sizeof (cdb));
22222 	bzero(&ucmd_buf, sizeof (ucmd_buf));
22223 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
22224 
22225 	/* Compute CDB size to use */
22226 	if (start_block > 0xffffffff)
22227 		cdbsize = CDB_GROUP4;
22228 	else if ((start_block & 0xFFE00000) ||
22229 	    (un->un_f_cfg_is_atapi == TRUE))
22230 		cdbsize = CDB_GROUP1;
22231 	else
22232 		cdbsize = CDB_GROUP0;
22233 
22234 	switch (cdbsize) {
22235 	case CDB_GROUP0:	/* 6-byte CDBs */
22236 		cdb.scc_cmd = cmd;
22237 		FORMG0ADDR(&cdb, start_block);
22238 		FORMG0COUNT(&cdb, block_count);
22239 		break;
22240 	case CDB_GROUP1:	/* 10-byte CDBs */
22241 		cdb.scc_cmd = cmd | SCMD_GROUP1;
22242 		FORMG1ADDR(&cdb, start_block);
22243 		FORMG1COUNT(&cdb, block_count);
22244 		break;
22245 	case CDB_GROUP4:	/* 16-byte CDBs */
22246 		cdb.scc_cmd = cmd | SCMD_GROUP4;
22247 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
22248 		FORMG4COUNT(&cdb, block_count);
22249 		break;
22250 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
22251 	default:
22252 		/* All others reserved */
22253 		return (EINVAL);
22254 	}
22255 
22256 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
22257 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
22258 
22259 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22260 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
22261 	ucmd_buf.uscsi_bufaddr	= bufaddr;
22262 	ucmd_buf.uscsi_buflen	= buflen;
22263 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
22264 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
22265 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
22266 	ucmd_buf.uscsi_timeout	= 60;
22267 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22268 	    UIO_SYSSPACE, path_flag);
22269 
22270 	switch (status) {
22271 	case 0:
22272 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22273 		break;	/* Success! */
22274 	case EIO:
22275 		switch (ucmd_buf.uscsi_status) {
22276 		case STATUS_RESERVATION_CONFLICT:
22277 			status = EACCES;
22278 			break;
22279 		default:
22280 			break;
22281 		}
22282 		break;
22283 	default:
22284 		break;
22285 	}
22286 
22287 	if (status == 0) {
22288 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
22289 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22290 	}
22291 
22292 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
22293 
22294 	return (status);
22295 }
22296 
22297 
22298 /*
22299  *    Function: sd_send_scsi_LOG_SENSE
22300  *
22301  * Description: Issue a scsi LOG_SENSE command with the given parameters.
22302  *
22303  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22304  *                      structure for this target.
22305  *
22306  * Return Code: 0   - Success
22307  *		errno return code from sd_ssc_send()
22308  *
22309  *     Context: Can sleep. Does not return until command is completed.
22310  */
22311 
22312 static int
22313 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
22314     uchar_t page_code, uchar_t page_control, uint16_t param_ptr, int path_flag)
22315 {
22316 	struct scsi_extended_sense	sense_buf;
22317 	union scsi_cdb		cdb;
22318 	struct uscsi_cmd	ucmd_buf;
22319 	int			status;
22320 	struct sd_lun		*un;
22321 
22322 	ASSERT(ssc != NULL);
22323 	un = ssc->ssc_un;
22324 	ASSERT(un != NULL);
22325 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22326 
22327 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
22328 
22329 	bzero(&cdb, sizeof (cdb));
22330 	bzero(&ucmd_buf, sizeof (ucmd_buf));
22331 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
22332 
22333 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
22334 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
22335 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
22336 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
22337 	FORMG1COUNT(&cdb, buflen);
22338 
22339 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22340 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
22341 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
22342 	ucmd_buf.uscsi_buflen	= buflen;
22343 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
22344 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
22345 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
22346 	ucmd_buf.uscsi_timeout	= 60;
22347 
22348 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22349 	    UIO_SYSSPACE, path_flag);
22350 
22351 	switch (status) {
22352 	case 0:
22353 		break;
22354 	case EIO:
22355 		switch (ucmd_buf.uscsi_status) {
22356 		case STATUS_RESERVATION_CONFLICT:
22357 			status = EACCES;
22358 			break;
22359 		case STATUS_CHECK:
22360 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
22361 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
22362 			    KEY_ILLEGAL_REQUEST) &&
22363 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
22364 				/*
22365 				 * ASC 0x24: INVALID FIELD IN CDB
22366 				 */
22367 				switch (page_code) {
22368 				case START_STOP_CYCLE_PAGE:
22369 					/*
22370 					 * The start stop cycle counter is
22371 					 * implemented as page 0x31 in earlier
22372 					 * generation disks. In new generation
22373 					 * disks the start stop cycle counter is
22374 					 * implemented as page 0xE. To properly
22375 					 * handle this case if an attempt for
22376 					 * log page 0xE is made and fails we
22377 					 * will try again using page 0x31.
22378 					 *
22379 					 * Network storage BU committed to
22380 					 * maintain the page 0x31 for this
22381 					 * purpose and will not have any other
22382 					 * page implemented with page code 0x31
22383 					 * until all disks transition to the
22384 					 * standard page.
22385 					 */
22386 					mutex_enter(SD_MUTEX(un));
22387 					un->un_start_stop_cycle_page =
22388 					    START_STOP_CYCLE_VU_PAGE;
22389 					cdb.cdb_opaque[2] =
22390 					    (char)(page_control << 6) |
22391 					    un->un_start_stop_cycle_page;
22392 					mutex_exit(SD_MUTEX(un));
22393 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22394 					status = sd_ssc_send(
22395 					    ssc, &ucmd_buf, FKIOCTL,
22396 					    UIO_SYSSPACE, path_flag);
22397 
22398 					break;
22399 				case TEMPERATURE_PAGE:
22400 					status = ENOTTY;
22401 					break;
22402 				default:
22403 					break;
22404 				}
22405 			}
22406 			break;
22407 		default:
22408 			break;
22409 		}
22410 		break;
22411 	default:
22412 		break;
22413 	}
22414 
22415 	if (status == 0) {
22416 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22417 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
22418 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22419 	}
22420 
22421 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
22422 
22423 	return (status);
22424 }
22425 
22426 
22427 /*
22428  *    Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
22429  *
22430  * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
22431  *
22432  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22433  *                      structure for this target.
22434  *		bufaddr
22435  *		buflen
22436  *		class_req
22437  *
22438  * Return Code: 0   - Success
22439  *		errno return code from sd_ssc_send()
22440  *
22441  *     Context: Can sleep. Does not return until command is completed.
22442  */
22443 
22444 static int
22445 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
22446     size_t buflen, uchar_t class_req)
22447 {
22448 	union scsi_cdb		cdb;
22449 	struct uscsi_cmd	ucmd_buf;
22450 	int			status;
22451 	struct sd_lun		*un;
22452 
22453 	ASSERT(ssc != NULL);
22454 	un = ssc->ssc_un;
22455 	ASSERT(un != NULL);
22456 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22457 	ASSERT(bufaddr != NULL);
22458 
22459 	SD_TRACE(SD_LOG_IO, un,
22460 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22461 
22462 	bzero(&cdb, sizeof (cdb));
22463 	bzero(&ucmd_buf, sizeof (ucmd_buf));
22464 	bzero(bufaddr, buflen);
22465 
22466 	cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22467 	cdb.cdb_opaque[1] = 1; /* polled */
22468 	cdb.cdb_opaque[4] = class_req;
22469 	FORMG1COUNT(&cdb, buflen);
22470 
22471 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22472 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
22473 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
22474 	ucmd_buf.uscsi_buflen	= buflen;
22475 	ucmd_buf.uscsi_rqbuf	= NULL;
22476 	ucmd_buf.uscsi_rqlen	= 0;
22477 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
22478 	ucmd_buf.uscsi_timeout	= 60;
22479 
22480 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22481 	    UIO_SYSSPACE, SD_PATH_DIRECT);
22482 
22483 	/*
22484 	 * Only handle status == 0, the upper-level caller
22485 	 * will put different assessment based on the context.
22486 	 */
22487 	if (status == 0) {
22488 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22489 
22490 		if (ucmd_buf.uscsi_resid != 0) {
22491 			status = EIO;
22492 		}
22493 	}
22494 
22495 	SD_TRACE(SD_LOG_IO, un,
22496 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22497 
22498 	return (status);
22499 }
22500 
22501 
22502 static boolean_t
22503 sd_gesn_media_data_valid(uchar_t *data)
22504 {
22505 	uint16_t			len;
22506 
22507 	len = (data[1] << 8) | data[0];
22508 	return ((len >= 6) &&
22509 	    ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
22510 	    ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
22511 	    ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
22512 }
22513 
22514 
22515 /*
22516  *    Function: sdioctl
22517  *
22518  * Description: Driver's ioctl(9e) entry point function.
22519  *
22520  *   Arguments: dev     - device number
22521  *		cmd     - ioctl operation to be performed
22522  *		arg     - user argument, contains data to be set or reference
22523  *			  parameter for get
22524  *		flag    - bit flag, indicating open settings, 32/64 bit type
22525  *		cred_p  - user credential pointer
22526  *		rval_p  - calling process return value (OPT)
22527  *
22528  * Return Code: EINVAL
22529  *		ENOTTY
22530  *		ENXIO
22531  *		EIO
22532  *		EFAULT
22533  *		ENOTSUP
22534  *		EPERM
22535  *
22536  *     Context: Called from the device switch at normal priority.
22537  */
22538 
22539 static int
22540 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22541 {
22542 	struct sd_lun	*un = NULL;
22543 	int		err = 0;
22544 	int		i = 0;
22545 	cred_t		*cr;
22546 	int		tmprval = EINVAL;
22547 	boolean_t	is_valid;
22548 	sd_ssc_t	*ssc;
22549 
22550 	/*
22551 	 * All device accesses go thru sdstrategy where we check on suspend
22552 	 * status
22553 	 */
22554 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22555 		return (ENXIO);
22556 	}
22557 
22558 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22559 
22560 	/* Initialize sd_ssc_t for internal uscsi commands */
22561 	ssc = sd_ssc_init(un);
22562 
22563 	is_valid = SD_IS_VALID_LABEL(un);
22564 
22565 	/*
22566 	 * Moved this wait from sd_uscsi_strategy to here for
22567 	 * reasons of deadlock prevention. Internal driver commands,
22568 	 * specifically those to change a devices power level, result
22569 	 * in a call to sd_uscsi_strategy.
22570 	 */
22571 	mutex_enter(SD_MUTEX(un));
22572 	while ((un->un_state == SD_STATE_SUSPENDED) ||
22573 	    (un->un_state == SD_STATE_PM_CHANGING)) {
22574 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22575 	}
22576 	/*
22577 	 * Twiddling the counter here protects commands from now
22578 	 * through to the top of sd_uscsi_strategy. Without the
22579 	 * counter inc. a power down, for example, could get in
22580 	 * after the above check for state is made and before
22581 	 * execution gets to the top of sd_uscsi_strategy.
22582 	 * That would cause problems.
22583 	 */
22584 	un->un_ncmds_in_driver++;
22585 
22586 	if (!is_valid &&
22587 	    (flag & (FNDELAY | FNONBLOCK))) {
22588 		switch (cmd) {
22589 		case DKIOCGGEOM:	/* SD_PATH_DIRECT */
22590 		case DKIOCGVTOC:
22591 		case DKIOCGEXTVTOC:
22592 		case DKIOCGAPART:
22593 		case DKIOCPARTINFO:
22594 		case DKIOCEXTPARTINFO:
22595 		case DKIOCSGEOM:
22596 		case DKIOCSAPART:
22597 		case DKIOCGETEFI:
22598 		case DKIOCPARTITION:
22599 		case DKIOCSVTOC:
22600 		case DKIOCSEXTVTOC:
22601 		case DKIOCSETEFI:
22602 		case DKIOCGMBOOT:
22603 		case DKIOCSMBOOT:
22604 		case DKIOCG_PHYGEOM:
22605 		case DKIOCG_VIRTGEOM:
22606 #if defined(__i386) || defined(__amd64)
22607 		case DKIOCSETEXTPART:
22608 #endif
22609 			/* let cmlb handle it */
22610 			goto skip_ready_valid;
22611 
22612 		case CDROMPAUSE:
22613 		case CDROMRESUME:
22614 		case CDROMPLAYMSF:
22615 		case CDROMPLAYTRKIND:
22616 		case CDROMREADTOCHDR:
22617 		case CDROMREADTOCENTRY:
22618 		case CDROMSTOP:
22619 		case CDROMSTART:
22620 		case CDROMVOLCTRL:
22621 		case CDROMSUBCHNL:
22622 		case CDROMREADMODE2:
22623 		case CDROMREADMODE1:
22624 		case CDROMREADOFFSET:
22625 		case CDROMSBLKMODE:
22626 		case CDROMGBLKMODE:
22627 		case CDROMGDRVSPEED:
22628 		case CDROMSDRVSPEED:
22629 		case CDROMCDDA:
22630 		case CDROMCDXA:
22631 		case CDROMSUBCODE:
22632 			if (!ISCD(un)) {
22633 				un->un_ncmds_in_driver--;
22634 				ASSERT(un->un_ncmds_in_driver >= 0);
22635 				mutex_exit(SD_MUTEX(un));
22636 				err = ENOTTY;
22637 				goto done_without_assess;
22638 			}
22639 			break;
22640 		case FDEJECT:
22641 		case DKIOCEJECT:
22642 		case CDROMEJECT:
22643 			if (!un->un_f_eject_media_supported) {
22644 				un->un_ncmds_in_driver--;
22645 				ASSERT(un->un_ncmds_in_driver >= 0);
22646 				mutex_exit(SD_MUTEX(un));
22647 				err = ENOTTY;
22648 				goto done_without_assess;
22649 			}
22650 			break;
22651 		case DKIOCFLUSHWRITECACHE:
22652 			mutex_exit(SD_MUTEX(un));
22653 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22654 			if (err != 0) {
22655 				mutex_enter(SD_MUTEX(un));
22656 				un->un_ncmds_in_driver--;
22657 				ASSERT(un->un_ncmds_in_driver >= 0);
22658 				mutex_exit(SD_MUTEX(un));
22659 				err = EIO;
22660 				goto done_quick_assess;
22661 			}
22662 			mutex_enter(SD_MUTEX(un));
22663 			/* FALLTHROUGH */
22664 		case DKIOCREMOVABLE:
22665 		case DKIOCHOTPLUGGABLE:
22666 		case DKIOCINFO:
22667 		case DKIOCGMEDIAINFO:
22668 		case DKIOCGMEDIAINFOEXT:
22669 		case DKIOCSOLIDSTATE:
22670 		case MHIOCENFAILFAST:
22671 		case MHIOCSTATUS:
22672 		case MHIOCTKOWN:
22673 		case MHIOCRELEASE:
22674 		case MHIOCGRP_INKEYS:
22675 		case MHIOCGRP_INRESV:
22676 		case MHIOCGRP_REGISTER:
22677 		case MHIOCGRP_CLEAR:
22678 		case MHIOCGRP_RESERVE:
22679 		case MHIOCGRP_PREEMPTANDABORT:
22680 		case MHIOCGRP_REGISTERANDIGNOREKEY:
22681 		case CDROMCLOSETRAY:
22682 		case USCSICMD:
22683 			goto skip_ready_valid;
22684 		default:
22685 			break;
22686 		}
22687 
22688 		mutex_exit(SD_MUTEX(un));
22689 		err = sd_ready_and_valid(ssc, SDPART(dev));
22690 		mutex_enter(SD_MUTEX(un));
22691 
22692 		if (err != SD_READY_VALID) {
22693 			switch (cmd) {
22694 			case DKIOCSTATE:
22695 			case CDROMGDRVSPEED:
22696 			case CDROMSDRVSPEED:
22697 			case FDEJECT:	/* for eject command */
22698 			case DKIOCEJECT:
22699 			case CDROMEJECT:
22700 			case DKIOCREMOVABLE:
22701 			case DKIOCHOTPLUGGABLE:
22702 				break;
22703 			default:
22704 				if (un->un_f_has_removable_media) {
22705 					err = ENXIO;
22706 				} else {
22707 				/* Do not map SD_RESERVED_BY_OTHERS to EIO */
22708 					if (err == SD_RESERVED_BY_OTHERS) {
22709 						err = EACCES;
22710 					} else {
22711 						err = EIO;
22712 					}
22713 				}
22714 				un->un_ncmds_in_driver--;
22715 				ASSERT(un->un_ncmds_in_driver >= 0);
22716 				mutex_exit(SD_MUTEX(un));
22717 
22718 				goto done_without_assess;
22719 			}
22720 		}
22721 	}
22722 
22723 skip_ready_valid:
22724 	mutex_exit(SD_MUTEX(un));
22725 
22726 	switch (cmd) {
22727 	case DKIOCINFO:
22728 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22729 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22730 		break;
22731 
22732 	case DKIOCGMEDIAINFO:
22733 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22734 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
22735 		break;
22736 
22737 	case DKIOCGMEDIAINFOEXT:
22738 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22739 		err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22740 		break;
22741 
22742 	case DKIOCGGEOM:
22743 	case DKIOCGVTOC:
22744 	case DKIOCGEXTVTOC:
22745 	case DKIOCGAPART:
22746 	case DKIOCPARTINFO:
22747 	case DKIOCEXTPARTINFO:
22748 	case DKIOCSGEOM:
22749 	case DKIOCSAPART:
22750 	case DKIOCGETEFI:
22751 	case DKIOCPARTITION:
22752 	case DKIOCSVTOC:
22753 	case DKIOCSEXTVTOC:
22754 	case DKIOCSETEFI:
22755 	case DKIOCGMBOOT:
22756 	case DKIOCSMBOOT:
22757 	case DKIOCG_PHYGEOM:
22758 	case DKIOCG_VIRTGEOM:
22759 #if defined(__i386) || defined(__amd64)
22760 	case DKIOCSETEXTPART:
22761 #endif
22762 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22763 
22764 		/* TUR should spin up */
22765 
22766 		if (un->un_f_has_removable_media)
22767 			err = sd_send_scsi_TEST_UNIT_READY(ssc,
22768 			    SD_CHECK_FOR_MEDIA);
22769 
22770 		else
22771 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22772 
22773 		if (err != 0)
22774 			goto done_with_assess;
22775 
22776 		err = cmlb_ioctl(un->un_cmlbhandle, dev,
22777 		    cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22778 
22779 		if ((err == 0) &&
22780 		    ((cmd == DKIOCSETEFI) ||
22781 		    ((un->un_f_pkstats_enabled) &&
22782 		    (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22783 		    cmd == DKIOCSEXTVTOC)))) {
22784 
22785 			tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22786 			    (void *)SD_PATH_DIRECT);
22787 			if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22788 				sd_set_pstats(un);
22789 				SD_TRACE(SD_LOG_IO_PARTITION, un,
22790 				    "sd_ioctl: un:0x%p pstats created and "
22791 				    "set\n", un);
22792 			}
22793 		}
22794 
22795 		if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22796 		    ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22797 
22798 			mutex_enter(SD_MUTEX(un));
22799 			if (un->un_f_devid_supported &&
22800 			    (un->un_f_opt_fab_devid == TRUE)) {
22801 				if (un->un_devid == NULL) {
22802 					sd_register_devid(ssc, SD_DEVINFO(un),
22803 					    SD_TARGET_IS_UNRESERVED);
22804 				} else {
22805 					/*
22806 					 * The device id for this disk
22807 					 * has been fabricated. The
22808 					 * device id must be preserved
22809 					 * by writing it back out to
22810 					 * disk.
22811 					 */
22812 					if (sd_write_deviceid(ssc) != 0) {
22813 						ddi_devid_free(un->un_devid);
22814 						un->un_devid = NULL;
22815 					}
22816 				}
22817 			}
22818 			mutex_exit(SD_MUTEX(un));
22819 		}
22820 
22821 		break;
22822 
22823 	case DKIOCLOCK:
22824 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22825 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22826 		    SD_PATH_STANDARD);
22827 		goto done_with_assess;
22828 
22829 	case DKIOCUNLOCK:
22830 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22831 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22832 		    SD_PATH_STANDARD);
22833 		goto done_with_assess;
22834 
22835 	case DKIOCSTATE: {
22836 		enum dkio_state		state;
22837 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22838 
22839 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22840 			err = EFAULT;
22841 		} else {
22842 			err = sd_check_media(dev, state);
22843 			if (err == 0) {
22844 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
22845 				    sizeof (int), flag) != 0)
22846 					err = EFAULT;
22847 			}
22848 		}
22849 		break;
22850 	}
22851 
22852 	case DKIOCREMOVABLE:
22853 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22854 		i = un->un_f_has_removable_media ? 1 : 0;
22855 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22856 			err = EFAULT;
22857 		} else {
22858 			err = 0;
22859 		}
22860 		break;
22861 
22862 	case DKIOCSOLIDSTATE:
22863 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSOLIDSTATE\n");
22864 		i = un->un_f_is_solid_state ? 1 : 0;
22865 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22866 			err = EFAULT;
22867 		} else {
22868 			err = 0;
22869 		}
22870 		break;
22871 
22872 	case DKIOCHOTPLUGGABLE:
22873 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22874 		i = un->un_f_is_hotpluggable ? 1 : 0;
22875 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22876 			err = EFAULT;
22877 		} else {
22878 			err = 0;
22879 		}
22880 		break;
22881 
22882 	case DKIOCREADONLY:
22883 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
22884 		i = 0;
22885 		if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
22886 		    (sr_check_wp(dev) != 0)) {
22887 			i = 1;
22888 		}
22889 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22890 			err = EFAULT;
22891 		} else {
22892 			err = 0;
22893 		}
22894 		break;
22895 
22896 	case DKIOCGTEMPERATURE:
22897 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22898 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22899 		break;
22900 
22901 	case MHIOCENFAILFAST:
22902 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22903 		if ((err = drv_priv(cred_p)) == 0) {
22904 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22905 		}
22906 		break;
22907 
22908 	case MHIOCTKOWN:
22909 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22910 		if ((err = drv_priv(cred_p)) == 0) {
22911 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22912 		}
22913 		break;
22914 
22915 	case MHIOCRELEASE:
22916 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22917 		if ((err = drv_priv(cred_p)) == 0) {
22918 			err = sd_mhdioc_release(dev);
22919 		}
22920 		break;
22921 
22922 	case MHIOCSTATUS:
22923 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22924 		if ((err = drv_priv(cred_p)) == 0) {
22925 			switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22926 			case 0:
22927 				err = 0;
22928 				break;
22929 			case EACCES:
22930 				*rval_p = 1;
22931 				err = 0;
22932 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22933 				break;
22934 			default:
22935 				err = EIO;
22936 				goto done_with_assess;
22937 			}
22938 		}
22939 		break;
22940 
22941 	case MHIOCQRESERVE:
22942 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22943 		if ((err = drv_priv(cred_p)) == 0) {
22944 			err = sd_reserve_release(dev, SD_RESERVE);
22945 		}
22946 		break;
22947 
22948 	case MHIOCREREGISTERDEVID:
22949 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22950 		if (drv_priv(cred_p) == EPERM) {
22951 			err = EPERM;
22952 		} else if (!un->un_f_devid_supported) {
22953 			err = ENOTTY;
22954 		} else {
22955 			err = sd_mhdioc_register_devid(dev);
22956 		}
22957 		break;
22958 
22959 	case MHIOCGRP_INKEYS:
22960 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22961 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22962 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22963 				err = ENOTSUP;
22964 			} else {
22965 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22966 				    flag);
22967 			}
22968 		}
22969 		break;
22970 
22971 	case MHIOCGRP_INRESV:
22972 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22973 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22974 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22975 				err = ENOTSUP;
22976 			} else {
22977 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22978 			}
22979 		}
22980 		break;
22981 
22982 	case MHIOCGRP_REGISTER:
22983 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22984 		if ((err = drv_priv(cred_p)) != EPERM) {
22985 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22986 				err = ENOTSUP;
22987 			} else if (arg != NULL) {
22988 				mhioc_register_t reg;
22989 				if (ddi_copyin((void *)arg, &reg,
22990 				    sizeof (mhioc_register_t), flag) != 0) {
22991 					err = EFAULT;
22992 				} else {
22993 					err =
22994 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22995 					    ssc, SD_SCSI3_REGISTER,
22996 					    (uchar_t *)&reg);
22997 					if (err != 0)
22998 						goto done_with_assess;
22999 				}
23000 			}
23001 		}
23002 		break;
23003 
23004 	case MHIOCGRP_CLEAR:
23005 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
23006 		if ((err = drv_priv(cred_p)) != EPERM) {
23007 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
23008 				err = ENOTSUP;
23009 			} else if (arg != NULL) {
23010 				mhioc_register_t reg;
23011 				if (ddi_copyin((void *)arg, &reg,
23012 				    sizeof (mhioc_register_t), flag) != 0) {
23013 					err = EFAULT;
23014 				} else {
23015 					err =
23016 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
23017 					    ssc, SD_SCSI3_CLEAR,
23018 					    (uchar_t *)&reg);
23019 					if (err != 0)
23020 						goto done_with_assess;
23021 				}
23022 			}
23023 		}
23024 		break;
23025 
23026 	case MHIOCGRP_RESERVE:
23027 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
23028 		if ((err = drv_priv(cred_p)) != EPERM) {
23029 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
23030 				err = ENOTSUP;
23031 			} else if (arg != NULL) {
23032 				mhioc_resv_desc_t resv_desc;
23033 				if (ddi_copyin((void *)arg, &resv_desc,
23034 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
23035 					err = EFAULT;
23036 				} else {
23037 					err =
23038 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
23039 					    ssc, SD_SCSI3_RESERVE,
23040 					    (uchar_t *)&resv_desc);
23041 					if (err != 0)
23042 						goto done_with_assess;
23043 				}
23044 			}
23045 		}
23046 		break;
23047 
23048 	case MHIOCGRP_PREEMPTANDABORT:
23049 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
23050 		if ((err = drv_priv(cred_p)) != EPERM) {
23051 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
23052 				err = ENOTSUP;
23053 			} else if (arg != NULL) {
23054 				mhioc_preemptandabort_t preempt_abort;
23055 				if (ddi_copyin((void *)arg, &preempt_abort,
23056 				    sizeof (mhioc_preemptandabort_t),
23057 				    flag) != 0) {
23058 					err = EFAULT;
23059 				} else {
23060 					err =
23061 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
23062 					    ssc, SD_SCSI3_PREEMPTANDABORT,
23063 					    (uchar_t *)&preempt_abort);
23064 					if (err != 0)
23065 						goto done_with_assess;
23066 				}
23067 			}
23068 		}
23069 		break;
23070 
23071 	case MHIOCGRP_REGISTERANDIGNOREKEY:
23072 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
23073 		if ((err = drv_priv(cred_p)) != EPERM) {
23074 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
23075 				err = ENOTSUP;
23076 			} else if (arg != NULL) {
23077 				mhioc_registerandignorekey_t r_and_i;
23078 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
23079 				    sizeof (mhioc_registerandignorekey_t),
23080 				    flag) != 0) {
23081 					err = EFAULT;
23082 				} else {
23083 					err =
23084 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
23085 					    ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
23086 					    (uchar_t *)&r_and_i);
23087 					if (err != 0)
23088 						goto done_with_assess;
23089 				}
23090 			}
23091 		}
23092 		break;
23093 
23094 	case USCSICMD:
23095 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
23096 		cr = ddi_get_cred();
23097 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
23098 			err = EPERM;
23099 		} else {
23100 			enum uio_seg	uioseg;
23101 
23102 			uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
23103 			    UIO_USERSPACE;
23104 			if (un->un_f_format_in_progress == TRUE) {
23105 				err = EAGAIN;
23106 				break;
23107 			}
23108 
23109 			err = sd_ssc_send(ssc,
23110 			    (struct uscsi_cmd *)arg,
23111 			    flag, uioseg, SD_PATH_STANDARD);
23112 			if (err != 0)
23113 				goto done_with_assess;
23114 			else
23115 				sd_ssc_assessment(ssc, SD_FMT_STANDARD);
23116 		}
23117 		break;
23118 
23119 	case CDROMPAUSE:
23120 	case CDROMRESUME:
23121 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
23122 		if (!ISCD(un)) {
23123 			err = ENOTTY;
23124 		} else {
23125 			err = sr_pause_resume(dev, cmd);
23126 		}
23127 		break;
23128 
23129 	case CDROMPLAYMSF:
23130 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
23131 		if (!ISCD(un)) {
23132 			err = ENOTTY;
23133 		} else {
23134 			err = sr_play_msf(dev, (caddr_t)arg, flag);
23135 		}
23136 		break;
23137 
23138 	case CDROMPLAYTRKIND:
23139 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
23140 #if defined(__i386) || defined(__amd64)
23141 		/*
23142 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
23143 		 */
23144 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
23145 #else
23146 		if (!ISCD(un)) {
23147 #endif
23148 			err = ENOTTY;
23149 		} else {
23150 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
23151 		}
23152 		break;
23153 
23154 	case CDROMREADTOCHDR:
23155 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
23156 		if (!ISCD(un)) {
23157 			err = ENOTTY;
23158 		} else {
23159 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
23160 		}
23161 		break;
23162 
23163 	case CDROMREADTOCENTRY:
23164 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
23165 		if (!ISCD(un)) {
23166 			err = ENOTTY;
23167 		} else {
23168 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
23169 		}
23170 		break;
23171 
23172 	case CDROMSTOP:
23173 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
23174 		if (!ISCD(un)) {
23175 			err = ENOTTY;
23176 		} else {
23177 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
23178 			    SD_TARGET_STOP, SD_PATH_STANDARD);
23179 			goto done_with_assess;
23180 		}
23181 		break;
23182 
23183 	case CDROMSTART:
23184 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
23185 		if (!ISCD(un)) {
23186 			err = ENOTTY;
23187 		} else {
23188 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
23189 			    SD_TARGET_START, SD_PATH_STANDARD);
23190 			goto done_with_assess;
23191 		}
23192 		break;
23193 
23194 	case CDROMCLOSETRAY:
23195 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
23196 		if (!ISCD(un)) {
23197 			err = ENOTTY;
23198 		} else {
23199 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
23200 			    SD_TARGET_CLOSE, SD_PATH_STANDARD);
23201 			goto done_with_assess;
23202 		}
23203 		break;
23204 
23205 	case FDEJECT:	/* for eject command */
23206 	case DKIOCEJECT:
23207 	case CDROMEJECT:
23208 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
23209 		if (!un->un_f_eject_media_supported) {
23210 			err = ENOTTY;
23211 		} else {
23212 			err = sr_eject(dev);
23213 		}
23214 		break;
23215 
23216 	case CDROMVOLCTRL:
23217 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
23218 		if (!ISCD(un)) {
23219 			err = ENOTTY;
23220 		} else {
23221 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
23222 		}
23223 		break;
23224 
23225 	case CDROMSUBCHNL:
23226 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
23227 		if (!ISCD(un)) {
23228 			err = ENOTTY;
23229 		} else {
23230 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
23231 		}
23232 		break;
23233 
23234 	case CDROMREADMODE2:
23235 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
23236 		if (!ISCD(un)) {
23237 			err = ENOTTY;
23238 		} else if (un->un_f_cfg_is_atapi == TRUE) {
23239 			/*
23240 			 * If the drive supports READ CD, use that instead of
23241 			 * switching the LBA size via a MODE SELECT
23242 			 * Block Descriptor
23243 			 */
23244 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
23245 		} else {
23246 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
23247 		}
23248 		break;
23249 
23250 	case CDROMREADMODE1:
23251 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
23252 		if (!ISCD(un)) {
23253 			err = ENOTTY;
23254 		} else {
23255 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
23256 		}
23257 		break;
23258 
23259 	case CDROMREADOFFSET:
23260 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
23261 		if (!ISCD(un)) {
23262 			err = ENOTTY;
23263 		} else {
23264 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
23265 			    flag);
23266 		}
23267 		break;
23268 
23269 	case CDROMSBLKMODE:
23270 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
23271 		/*
23272 		 * There is no means of changing block size in case of atapi
23273 		 * drives, thus return ENOTTY if drive type is atapi
23274 		 */
23275 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
23276 			err = ENOTTY;
23277 		} else if (un->un_f_mmc_cap == TRUE) {
23278 
23279 			/*
23280 			 * MMC Devices do not support changing the
23281 			 * logical block size
23282 			 *
23283 			 * Note: EINVAL is being returned instead of ENOTTY to
23284 			 * maintain consistancy with the original mmc
23285 			 * driver update.
23286 			 */
23287 			err = EINVAL;
23288 		} else {
23289 			mutex_enter(SD_MUTEX(un));
23290 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
23291 			    (un->un_ncmds_in_transport > 0)) {
23292 				mutex_exit(SD_MUTEX(un));
23293 				err = EINVAL;
23294 			} else {
23295 				mutex_exit(SD_MUTEX(un));
23296 				err = sr_change_blkmode(dev, cmd, arg, flag);
23297 			}
23298 		}
23299 		break;
23300 
23301 	case CDROMGBLKMODE:
23302 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
23303 		if (!ISCD(un)) {
23304 			err = ENOTTY;
23305 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
23306 		    (un->un_f_blockcount_is_valid != FALSE)) {
23307 			/*
23308 			 * Drive is an ATAPI drive so return target block
23309 			 * size for ATAPI drives since we cannot change the
23310 			 * blocksize on ATAPI drives. Used primarily to detect
23311 			 * if an ATAPI cdrom is present.
23312 			 */
23313 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
23314 			    sizeof (int), flag) != 0) {
23315 				err = EFAULT;
23316 			} else {
23317 				err = 0;
23318 			}
23319 
23320 		} else {
23321 			/*
23322 			 * Drive supports changing block sizes via a Mode
23323 			 * Select.
23324 			 */
23325 			err = sr_change_blkmode(dev, cmd, arg, flag);
23326 		}
23327 		break;
23328 
23329 	case CDROMGDRVSPEED:
23330 	case CDROMSDRVSPEED:
23331 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
23332 		if (!ISCD(un)) {
23333 			err = ENOTTY;
23334 		} else if (un->un_f_mmc_cap == TRUE) {
23335 			/*
23336 			 * Note: In the future the driver implementation
23337 			 * for getting and
23338 			 * setting cd speed should entail:
23339 			 * 1) If non-mmc try the Toshiba mode page
23340 			 *    (sr_change_speed)
23341 			 * 2) If mmc but no support for Real Time Streaming try
23342 			 *    the SET CD SPEED (0xBB) command
23343 			 *   (sr_atapi_change_speed)
23344 			 * 3) If mmc and support for Real Time Streaming
23345 			 *    try the GET PERFORMANCE and SET STREAMING
23346 			 *    commands (not yet implemented, 4380808)
23347 			 */
23348 			/*
23349 			 * As per recent MMC spec, CD-ROM speed is variable
23350 			 * and changes with LBA. Since there is no such
23351 			 * things as drive speed now, fail this ioctl.
23352 			 *
23353 			 * Note: EINVAL is returned for consistancy of original
23354 			 * implementation which included support for getting
23355 			 * the drive speed of mmc devices but not setting
23356 			 * the drive speed. Thus EINVAL would be returned
23357 			 * if a set request was made for an mmc device.
23358 			 * We no longer support get or set speed for
23359 			 * mmc but need to remain consistent with regard
23360 			 * to the error code returned.
23361 			 */
23362 			err = EINVAL;
23363 		} else if (un->un_f_cfg_is_atapi == TRUE) {
23364 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
23365 		} else {
23366 			err = sr_change_speed(dev, cmd, arg, flag);
23367 		}
23368 		break;
23369 
23370 	case CDROMCDDA:
23371 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
23372 		if (!ISCD(un)) {
23373 			err = ENOTTY;
23374 		} else {
23375 			err = sr_read_cdda(dev, (void *)arg, flag);
23376 		}
23377 		break;
23378 
23379 	case CDROMCDXA:
23380 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
23381 		if (!ISCD(un)) {
23382 			err = ENOTTY;
23383 		} else {
23384 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
23385 		}
23386 		break;
23387 
23388 	case CDROMSUBCODE:
23389 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
23390 		if (!ISCD(un)) {
23391 			err = ENOTTY;
23392 		} else {
23393 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
23394 		}
23395 		break;
23396 
23397 
23398 #ifdef SDDEBUG
23399 /* RESET/ABORTS testing ioctls */
23400 	case DKIOCRESET: {
23401 		int	reset_level;
23402 
23403 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
23404 			err = EFAULT;
23405 		} else {
23406 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
23407 			    "reset_level = 0x%lx\n", reset_level);
23408 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
23409 				err = 0;
23410 			} else {
23411 				err = EIO;
23412 			}
23413 		}
23414 		break;
23415 	}
23416 
23417 	case DKIOCABORT:
23418 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23419 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
23420 			err = 0;
23421 		} else {
23422 			err = EIO;
23423 		}
23424 		break;
23425 #endif
23426 
23427 #ifdef SD_FAULT_INJECTION
23428 /* SDIOC FaultInjection testing ioctls */
23429 	case SDIOCSTART:
23430 	case SDIOCSTOP:
23431 	case SDIOCINSERTPKT:
23432 	case SDIOCINSERTXB:
23433 	case SDIOCINSERTUN:
23434 	case SDIOCINSERTARQ:
23435 	case SDIOCPUSH:
23436 	case SDIOCRETRIEVE:
23437 	case SDIOCRUN:
23438 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23439 		    "SDIOC detected cmd:0x%X:\n", cmd);
23440 		/* call error generator */
23441 		sd_faultinjection_ioctl(cmd, arg, un);
23442 		err = 0;
23443 		break;
23444 
23445 #endif /* SD_FAULT_INJECTION */
23446 
23447 	case DKIOCFLUSHWRITECACHE:
23448 		{
23449 			struct dk_callback *dkc = (struct dk_callback *)arg;
23450 
23451 			mutex_enter(SD_MUTEX(un));
23452 			if (!un->un_f_sync_cache_supported ||
23453 			    !un->un_f_write_cache_enabled) {
23454 				err = un->un_f_sync_cache_supported ?
23455 				    0 : ENOTSUP;
23456 				mutex_exit(SD_MUTEX(un));
23457 				if ((flag & FKIOCTL) && dkc != NULL &&
23458 				    dkc->dkc_callback != NULL) {
23459 					(*dkc->dkc_callback)(dkc->dkc_cookie,
23460 					    err);
23461 					/*
23462 					 * Did callback and reported error.
23463 					 * Since we did a callback, ioctl
23464 					 * should return 0.
23465 					 */
23466 					err = 0;
23467 				}
23468 				break;
23469 			}
23470 			mutex_exit(SD_MUTEX(un));
23471 
23472 			if ((flag & FKIOCTL) && dkc != NULL &&
23473 			    dkc->dkc_callback != NULL) {
23474 				/* async SYNC CACHE request */
23475 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23476 			} else {
23477 				/* synchronous SYNC CACHE request */
23478 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23479 			}
23480 		}
23481 		break;
23482 
23483 	case DKIOCFREE:
23484 		{
23485 			dkioc_free_list_t *dfl = (dkioc_free_list_t *)arg;
23486 
23487 			/* bad ioctls shouldn't panic */
23488 			if (dfl == NULL) {
23489 				/* check kernel callers strictly in debug */
23490 				ASSERT0(flag & FKIOCTL);
23491 				err = SET_ERROR(EINVAL);
23492 				break;
23493 			}
23494 			/* synchronous UNMAP request */
23495 			err = sd_send_scsi_UNMAP(dev, ssc, dfl, flag);
23496 		}
23497 		break;
23498 
23499 	case DKIOCGETWCE: {
23500 
23501 		int wce;
23502 
23503 		if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23504 			break;
23505 		}
23506 
23507 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23508 			err = EFAULT;
23509 		}
23510 		break;
23511 	}
23512 
23513 	case DKIOCSETWCE: {
23514 
23515 		int wce, sync_supported;
23516 		int cur_wce = 0;
23517 
23518 		if (!un->un_f_cache_mode_changeable) {
23519 			err = EINVAL;
23520 			break;
23521 		}
23522 
23523 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
23524 			err = EFAULT;
23525 			break;
23526 		}
23527 
23528 		/*
23529 		 * Synchronize multiple threads trying to enable
23530 		 * or disable the cache via the un_f_wcc_cv
23531 		 * condition variable.
23532 		 */
23533 		mutex_enter(SD_MUTEX(un));
23534 
23535 		/*
23536 		 * Don't allow the cache to be enabled if the
23537 		 * config file has it disabled.
23538 		 */
23539 		if (un->un_f_opt_disable_cache && wce) {
23540 			mutex_exit(SD_MUTEX(un));
23541 			err = EINVAL;
23542 			break;
23543 		}
23544 
23545 		/*
23546 		 * Wait for write cache change in progress
23547 		 * bit to be clear before proceeding.
23548 		 */
23549 		while (un->un_f_wcc_inprog)
23550 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
23551 
23552 		un->un_f_wcc_inprog = 1;
23553 
23554 		mutex_exit(SD_MUTEX(un));
23555 
23556 		/*
23557 		 * Get the current write cache state
23558 		 */
23559 		if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
23560 			mutex_enter(SD_MUTEX(un));
23561 			un->un_f_wcc_inprog = 0;
23562 			cv_broadcast(&un->un_wcc_cv);
23563 			mutex_exit(SD_MUTEX(un));
23564 			break;
23565 		}
23566 
23567 		mutex_enter(SD_MUTEX(un));
23568 		un->un_f_write_cache_enabled = (cur_wce != 0);
23569 
23570 		if (un->un_f_write_cache_enabled && wce == 0) {
23571 			/*
23572 			 * Disable the write cache.  Don't clear
23573 			 * un_f_write_cache_enabled until after
23574 			 * the mode select and flush are complete.
23575 			 */
23576 			sync_supported = un->un_f_sync_cache_supported;
23577 
23578 			/*
23579 			 * If cache flush is suppressed, we assume that the
23580 			 * controller firmware will take care of managing the
23581 			 * write cache for us: no need to explicitly
23582 			 * disable it.
23583 			 */
23584 			if (!un->un_f_suppress_cache_flush) {
23585 				mutex_exit(SD_MUTEX(un));
23586 				if ((err = sd_cache_control(ssc,
23587 				    SD_CACHE_NOCHANGE,
23588 				    SD_CACHE_DISABLE)) == 0 &&
23589 				    sync_supported) {
23590 					err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
23591 					    NULL);
23592 				}
23593 			} else {
23594 				mutex_exit(SD_MUTEX(un));
23595 			}
23596 
23597 			mutex_enter(SD_MUTEX(un));
23598 			if (err == 0) {
23599 				un->un_f_write_cache_enabled = 0;
23600 			}
23601 
23602 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
23603 			/*
23604 			 * Set un_f_write_cache_enabled first, so there is
23605 			 * no window where the cache is enabled, but the
23606 			 * bit says it isn't.
23607 			 */
23608 			un->un_f_write_cache_enabled = 1;
23609 
23610 			/*
23611 			 * If cache flush is suppressed, we assume that the
23612 			 * controller firmware will take care of managing the
23613 			 * write cache for us: no need to explicitly
23614 			 * enable it.
23615 			 */
23616 			if (!un->un_f_suppress_cache_flush) {
23617 				mutex_exit(SD_MUTEX(un));
23618 				err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23619 				    SD_CACHE_ENABLE);
23620 			} else {
23621 				mutex_exit(SD_MUTEX(un));
23622 			}
23623 
23624 			mutex_enter(SD_MUTEX(un));
23625 
23626 			if (err) {
23627 				un->un_f_write_cache_enabled = 0;
23628 			}
23629 		}
23630 
23631 		un->un_f_wcc_inprog = 0;
23632 		cv_broadcast(&un->un_wcc_cv);
23633 		mutex_exit(SD_MUTEX(un));
23634 		break;
23635 	}
23636 
23637 	default:
23638 		err = ENOTTY;
23639 		break;
23640 	}
23641 	mutex_enter(SD_MUTEX(un));
23642 	un->un_ncmds_in_driver--;
23643 	ASSERT(un->un_ncmds_in_driver >= 0);
23644 	mutex_exit(SD_MUTEX(un));
23645 
23646 
23647 done_without_assess:
23648 	sd_ssc_fini(ssc);
23649 
23650 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23651 	return (err);
23652 
23653 done_with_assess:
23654 	mutex_enter(SD_MUTEX(un));
23655 	un->un_ncmds_in_driver--;
23656 	ASSERT(un->un_ncmds_in_driver >= 0);
23657 	mutex_exit(SD_MUTEX(un));
23658 
23659 done_quick_assess:
23660 	if (err != 0)
23661 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23662 	/* Uninitialize sd_ssc_t pointer */
23663 	sd_ssc_fini(ssc);
23664 
23665 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23666 	return (err);
23667 }
23668 
23669 
23670 /*
23671  *    Function: sd_dkio_ctrl_info
23672  *
23673  * Description: This routine is the driver entry point for handling controller
23674  *		information ioctl requests (DKIOCINFO).
23675  *
23676  *   Arguments: dev  - the device number
23677  *		arg  - pointer to user provided dk_cinfo structure
23678  *		       specifying the controller type and attributes.
23679  *		flag - this argument is a pass through to ddi_copyxxx()
23680  *		       directly from the mode argument of ioctl().
23681  *
23682  * Return Code: 0
23683  *		EFAULT
23684  *		ENXIO
23685  */
23686 
23687 static int
23688 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23689 {
23690 	struct sd_lun	*un = NULL;
23691 	struct dk_cinfo	*info;
23692 	dev_info_t	*pdip;
23693 	int		lun, tgt;
23694 
23695 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23696 		return (ENXIO);
23697 	}
23698 
23699 	info = (struct dk_cinfo *)
23700 	    kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23701 
23702 	switch (un->un_ctype) {
23703 	case CTYPE_CDROM:
23704 		info->dki_ctype = DKC_CDROM;
23705 		break;
23706 	default:
23707 		info->dki_ctype = DKC_SCSI_CCS;
23708 		break;
23709 	}
23710 	pdip = ddi_get_parent(SD_DEVINFO(un));
23711 	info->dki_cnum = ddi_get_instance(pdip);
23712 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23713 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
23714 	} else {
23715 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
23716 		    DK_DEVLEN - 1);
23717 	}
23718 
23719 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23720 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23721 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23722 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23723 
23724 	/* Unit Information */
23725 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23726 	info->dki_slave = ((tgt << 3) | lun);
23727 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23728 	    DK_DEVLEN - 1);
23729 	info->dki_flags = DKI_FMTVOL;
23730 	info->dki_partition = SDPART(dev);
23731 
23732 	/* Max Transfer size of this device in blocks */
23733 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23734 	info->dki_addr = 0;
23735 	info->dki_space = 0;
23736 	info->dki_prio = 0;
23737 	info->dki_vec = 0;
23738 
23739 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23740 		kmem_free(info, sizeof (struct dk_cinfo));
23741 		return (EFAULT);
23742 	} else {
23743 		kmem_free(info, sizeof (struct dk_cinfo));
23744 		return (0);
23745 	}
23746 }
23747 
23748 /*
23749  *    Function: sd_get_media_info_com
23750  *
23751  * Description: This routine returns the information required to populate
23752  *		the fields for the dk_minfo/dk_minfo_ext structures.
23753  *
23754  *   Arguments: dev		- the device number
23755  *		dki_media_type	- media_type
23756  *		dki_lbsize	- logical block size
23757  *		dki_capacity	- capacity in blocks
23758  *		dki_pbsize	- physical block size (if requested)
23759  *
23760  * Return Code: 0
23761  *		EACCESS
23762  *		EFAULT
23763  *		ENXIO
23764  *		EIO
23765  */
23766 static int
23767 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23768     diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23769 {
23770 	struct sd_lun		*un = NULL;
23771 	struct uscsi_cmd	com;
23772 	struct scsi_inquiry	*sinq;
23773 	u_longlong_t		media_capacity;
23774 	uint64_t		capacity;
23775 	uint_t			lbasize;
23776 	uint_t			pbsize;
23777 	uchar_t			*out_data;
23778 	uchar_t			*rqbuf;
23779 	int			rval = 0;
23780 	int			rtn;
23781 	sd_ssc_t		*ssc;
23782 
23783 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23784 	    (un->un_state == SD_STATE_OFFLINE)) {
23785 		return (ENXIO);
23786 	}
23787 
23788 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
23789 
23790 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23791 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23792 	ssc = sd_ssc_init(un);
23793 
23794 	/* Issue a TUR to determine if the drive is ready with media present */
23795 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23796 	if (rval == ENXIO) {
23797 		goto done;
23798 	} else if (rval != 0) {
23799 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23800 	}
23801 
23802 	/* Now get configuration data */
23803 	if (ISCD(un)) {
23804 		*dki_media_type = DK_CDROM;
23805 
23806 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23807 		if (un->un_f_mmc_cap == TRUE) {
23808 			rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23809 			    SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23810 			    SD_PATH_STANDARD);
23811 
23812 			if (rtn) {
23813 				/*
23814 				 * We ignore all failures for CD and need to
23815 				 * put the assessment before processing code
23816 				 * to avoid missing assessment for FMA.
23817 				 */
23818 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23819 				/*
23820 				 * Failed for other than an illegal request
23821 				 * or command not supported
23822 				 */
23823 				if ((com.uscsi_status == STATUS_CHECK) &&
23824 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
23825 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23826 					    (rqbuf[12] != 0x20)) {
23827 						rval = EIO;
23828 						goto no_assessment;
23829 					}
23830 				}
23831 			} else {
23832 				/*
23833 				 * The GET CONFIGURATION command succeeded
23834 				 * so set the media type according to the
23835 				 * returned data
23836 				 */
23837 				*dki_media_type = out_data[6];
23838 				*dki_media_type <<= 8;
23839 				*dki_media_type |= out_data[7];
23840 			}
23841 		}
23842 	} else {
23843 		/*
23844 		 * The profile list is not available, so we attempt to identify
23845 		 * the media type based on the inquiry data
23846 		 */
23847 		sinq = un->un_sd->sd_inq;
23848 		if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23849 		    (sinq->inq_dtype == DTYPE_OPTICAL)) {
23850 			/* This is a direct access device  or optical disk */
23851 			*dki_media_type = DK_FIXED_DISK;
23852 
23853 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23854 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23855 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23856 					*dki_media_type = DK_ZIP;
23857 				} else if (
23858 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23859 					*dki_media_type = DK_JAZ;
23860 				}
23861 			}
23862 		} else {
23863 			/*
23864 			 * Not a CD, direct access or optical disk so return
23865 			 * unknown media
23866 			 */
23867 			*dki_media_type = DK_UNKNOWN;
23868 		}
23869 	}
23870 
23871 	/*
23872 	 * Now read the capacity so we can provide the lbasize,
23873 	 * pbsize and capacity.
23874 	 */
23875 	if (dki_pbsize && un->un_f_descr_format_supported) {
23876 		rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23877 		    &pbsize, SD_PATH_DIRECT);
23878 
23879 		/*
23880 		 * Override the physical blocksize if the instance already
23881 		 * has a larger value.
23882 		 */
23883 		pbsize = MAX(pbsize, un->un_phy_blocksize);
23884 	}
23885 
23886 	if (dki_pbsize == NULL || rval != 0 ||
23887 	    !un->un_f_descr_format_supported) {
23888 		rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23889 		    SD_PATH_DIRECT);
23890 
23891 		switch (rval) {
23892 		case 0:
23893 			if (un->un_f_enable_rmw &&
23894 			    un->un_phy_blocksize != 0) {
23895 				pbsize = un->un_phy_blocksize;
23896 			} else {
23897 				pbsize = lbasize;
23898 			}
23899 			media_capacity = capacity;
23900 
23901 			/*
23902 			 * sd_send_scsi_READ_CAPACITY() reports capacity in
23903 			 * un->un_sys_blocksize chunks. So we need to convert
23904 			 * it into cap.lbsize chunks.
23905 			 */
23906 			if (un->un_f_has_removable_media) {
23907 				media_capacity *= un->un_sys_blocksize;
23908 				media_capacity /= lbasize;
23909 			}
23910 			break;
23911 		case EACCES:
23912 			rval = EACCES;
23913 			goto done;
23914 		default:
23915 			rval = EIO;
23916 			goto done;
23917 		}
23918 	} else {
23919 		if (un->un_f_enable_rmw &&
23920 		    !ISP2(pbsize % DEV_BSIZE)) {
23921 			pbsize = SSD_SECSIZE;
23922 		} else if (!ISP2(lbasize % DEV_BSIZE) ||
23923 		    !ISP2(pbsize % DEV_BSIZE)) {
23924 			pbsize = lbasize = DEV_BSIZE;
23925 		}
23926 		media_capacity = capacity;
23927 	}
23928 
23929 	/*
23930 	 * If lun is expanded dynamically, update the un structure.
23931 	 */
23932 	mutex_enter(SD_MUTEX(un));
23933 	if ((un->un_f_blockcount_is_valid == TRUE) &&
23934 	    (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23935 	    (capacity > un->un_blockcount)) {
23936 		un->un_f_expnevent = B_FALSE;
23937 		sd_update_block_info(un, lbasize, capacity);
23938 	}
23939 	mutex_exit(SD_MUTEX(un));
23940 
23941 	*dki_lbsize = lbasize;
23942 	*dki_capacity = media_capacity;
23943 	if (dki_pbsize)
23944 		*dki_pbsize = pbsize;
23945 
23946 done:
23947 	if (rval != 0) {
23948 		if (rval == EIO)
23949 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23950 		else
23951 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23952 	}
23953 no_assessment:
23954 	sd_ssc_fini(ssc);
23955 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23956 	kmem_free(rqbuf, SENSE_LENGTH);
23957 	return (rval);
23958 }
23959 
23960 /*
23961  *    Function: sd_get_media_info
23962  *
23963  * Description: This routine is the driver entry point for handling ioctl
23964  *		requests for the media type or command set profile used by the
23965  *		drive to operate on the media (DKIOCGMEDIAINFO).
23966  *
23967  *   Arguments: dev	- the device number
23968  *		arg	- pointer to user provided dk_minfo structure
23969  *			  specifying the media type, logical block size and
23970  *			  drive capacity.
23971  *		flag	- this argument is a pass through to ddi_copyxxx()
23972  *			  directly from the mode argument of ioctl().
23973  *
23974  * Return Code: returns the value from sd_get_media_info_com
23975  */
23976 static int
23977 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23978 {
23979 	struct dk_minfo		mi;
23980 	int			rval;
23981 
23982 	rval = sd_get_media_info_com(dev, &mi.dki_media_type,
23983 	    &mi.dki_lbsize, &mi.dki_capacity, NULL);
23984 
23985 	if (rval)
23986 		return (rval);
23987 	if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
23988 		rval = EFAULT;
23989 	return (rval);
23990 }
23991 
23992 /*
23993  *    Function: sd_get_media_info_ext
23994  *
23995  * Description: This routine is the driver entry point for handling ioctl
23996  *		requests for the media type or command set profile used by the
23997  *		drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23998  *		difference this ioctl and DKIOCGMEDIAINFO is the return value
23999  *		of this ioctl contains both logical block size and physical
24000  *		block size.
24001  *
24002  *
24003  *   Arguments: dev	- the device number
24004  *		arg	- pointer to user provided dk_minfo_ext structure
24005  *			  specifying the media type, logical block size,
24006  *			  physical block size and disk capacity.
24007  *		flag	- this argument is a pass through to ddi_copyxxx()
24008  *			  directly from the mode argument of ioctl().
24009  *
24010  * Return Code: returns the value from sd_get_media_info_com
24011  */
24012 static int
24013 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
24014 {
24015 	struct dk_minfo_ext	mie;
24016 	int			rval = 0;
24017 
24018 	rval = sd_get_media_info_com(dev, &mie.dki_media_type,
24019 	    &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize);
24020 
24021 	if (rval)
24022 		return (rval);
24023 	if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag))
24024 		rval = EFAULT;
24025 	return (rval);
24026 
24027 }
24028 
24029 /*
24030  *    Function: sd_watch_request_submit
24031  *
24032  * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
24033  *		depending on which is supported by device.
24034  */
24035 static opaque_t
24036 sd_watch_request_submit(struct sd_lun *un)
24037 {
24038 	dev_t			dev;
24039 
24040 	/* All submissions are unified to use same device number */
24041 	dev = sd_make_device(SD_DEVINFO(un));
24042 
24043 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
24044 		return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
24045 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
24046 		    (caddr_t)dev));
24047 	} else {
24048 		return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
24049 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
24050 		    (caddr_t)dev));
24051 	}
24052 }
24053 
24054 
24055 /*
24056  *    Function: sd_check_media
24057  *
24058  * Description: This utility routine implements the functionality for the
24059  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
24060  *		driver state changes from that specified by the user
24061  *		(inserted or ejected). For example, if the user specifies
24062  *		DKIO_EJECTED and the current media state is inserted this
24063  *		routine will immediately return DKIO_INSERTED. However, if the
24064  *		current media state is not inserted the user thread will be
24065  *		blocked until the drive state changes. If DKIO_NONE is specified
24066  *		the user thread will block until a drive state change occurs.
24067  *
24068  *   Arguments: dev  - the device number
24069  *		state  - user pointer to a dkio_state, updated with the current
24070  *			drive state at return.
24071  *
24072  * Return Code: ENXIO
24073  *		EIO
24074  *		EAGAIN
24075  *		EINTR
24076  */
24077 
24078 static int
24079 sd_check_media(dev_t dev, enum dkio_state state)
24080 {
24081 	struct sd_lun		*un = NULL;
24082 	enum dkio_state		prev_state;
24083 	opaque_t		token = NULL;
24084 	int			rval = 0;
24085 	sd_ssc_t		*ssc;
24086 
24087 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24088 		return (ENXIO);
24089 	}
24090 
24091 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
24092 
24093 	ssc = sd_ssc_init(un);
24094 
24095 	mutex_enter(SD_MUTEX(un));
24096 
24097 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
24098 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
24099 
24100 	prev_state = un->un_mediastate;
24101 
24102 	/* is there anything to do? */
24103 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
24104 		/*
24105 		 * submit the request to the scsi_watch service;
24106 		 * scsi_media_watch_cb() does the real work
24107 		 */
24108 		mutex_exit(SD_MUTEX(un));
24109 
24110 		/*
24111 		 * This change handles the case where a scsi watch request is
24112 		 * added to a device that is powered down. To accomplish this
24113 		 * we power up the device before adding the scsi watch request,
24114 		 * since the scsi watch sends a TUR directly to the device
24115 		 * which the device cannot handle if it is powered down.
24116 		 */
24117 		if (sd_pm_entry(un) != DDI_SUCCESS) {
24118 			mutex_enter(SD_MUTEX(un));
24119 			goto done;
24120 		}
24121 
24122 		token = sd_watch_request_submit(un);
24123 
24124 		sd_pm_exit(un);
24125 
24126 		mutex_enter(SD_MUTEX(un));
24127 		if (token == NULL) {
24128 			rval = EAGAIN;
24129 			goto done;
24130 		}
24131 
24132 		/*
24133 		 * This is a special case IOCTL that doesn't return
24134 		 * until the media state changes. Routine sdpower
24135 		 * knows about and handles this so don't count it
24136 		 * as an active cmd in the driver, which would
24137 		 * keep the device busy to the pm framework.
24138 		 * If the count isn't decremented the device can't
24139 		 * be powered down.
24140 		 */
24141 		un->un_ncmds_in_driver--;
24142 		ASSERT(un->un_ncmds_in_driver >= 0);
24143 
24144 		/*
24145 		 * if a prior request had been made, this will be the same
24146 		 * token, as scsi_watch was designed that way.
24147 		 */
24148 		un->un_swr_token = token;
24149 		un->un_specified_mediastate = state;
24150 
24151 		/*
24152 		 * now wait for media change
24153 		 * we will not be signalled unless mediastate == state but it is
24154 		 * still better to test for this condition, since there is a
24155 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
24156 		 */
24157 		SD_TRACE(SD_LOG_COMMON, un,
24158 		    "sd_check_media: waiting for media state change\n");
24159 		while (un->un_mediastate == state) {
24160 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
24161 				SD_TRACE(SD_LOG_COMMON, un,
24162 				    "sd_check_media: waiting for media state "
24163 				    "was interrupted\n");
24164 				un->un_ncmds_in_driver++;
24165 				rval = EINTR;
24166 				goto done;
24167 			}
24168 			SD_TRACE(SD_LOG_COMMON, un,
24169 			    "sd_check_media: received signal, state=%x\n",
24170 			    un->un_mediastate);
24171 		}
24172 		/*
24173 		 * Inc the counter to indicate the device once again
24174 		 * has an active outstanding cmd.
24175 		 */
24176 		un->un_ncmds_in_driver++;
24177 	}
24178 
24179 	/* invalidate geometry */
24180 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
24181 		sr_ejected(un);
24182 	}
24183 
24184 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
24185 		uint64_t	capacity;
24186 		uint_t		lbasize;
24187 
24188 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
24189 		mutex_exit(SD_MUTEX(un));
24190 		/*
24191 		 * Since the following routines use SD_PATH_DIRECT, we must
24192 		 * call PM directly before the upcoming disk accesses. This
24193 		 * may cause the disk to be power/spin up.
24194 		 */
24195 
24196 		if (sd_pm_entry(un) == DDI_SUCCESS) {
24197 			rval = sd_send_scsi_READ_CAPACITY(ssc,
24198 			    &capacity, &lbasize, SD_PATH_DIRECT);
24199 			if (rval != 0) {
24200 				sd_pm_exit(un);
24201 				if (rval == EIO)
24202 					sd_ssc_assessment(ssc,
24203 					    SD_FMT_STATUS_CHECK);
24204 				else
24205 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24206 				mutex_enter(SD_MUTEX(un));
24207 				goto done;
24208 			}
24209 		} else {
24210 			rval = EIO;
24211 			mutex_enter(SD_MUTEX(un));
24212 			goto done;
24213 		}
24214 		mutex_enter(SD_MUTEX(un));
24215 
24216 		sd_update_block_info(un, lbasize, capacity);
24217 
24218 		/*
24219 		 *  Check if the media in the device is writable or not
24220 		 */
24221 		if (ISCD(un)) {
24222 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
24223 		}
24224 
24225 		mutex_exit(SD_MUTEX(un));
24226 		cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
24227 		if ((cmlb_validate(un->un_cmlbhandle, 0,
24228 		    (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
24229 			sd_set_pstats(un);
24230 			SD_TRACE(SD_LOG_IO_PARTITION, un,
24231 			    "sd_check_media: un:0x%p pstats created and "
24232 			    "set\n", un);
24233 		}
24234 
24235 		rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
24236 		    SD_PATH_DIRECT);
24237 
24238 		sd_pm_exit(un);
24239 
24240 		if (rval != 0) {
24241 			if (rval == EIO)
24242 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24243 			else
24244 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24245 		}
24246 
24247 		mutex_enter(SD_MUTEX(un));
24248 	}
24249 done:
24250 	sd_ssc_fini(ssc);
24251 	un->un_f_watcht_stopped = FALSE;
24252 	if (token != NULL && un->un_swr_token != NULL) {
24253 		/*
24254 		 * Use of this local token and the mutex ensures that we avoid
24255 		 * some race conditions associated with terminating the
24256 		 * scsi watch.
24257 		 */
24258 		token = un->un_swr_token;
24259 		mutex_exit(SD_MUTEX(un));
24260 		(void) scsi_watch_request_terminate(token,
24261 		    SCSI_WATCH_TERMINATE_WAIT);
24262 		if (scsi_watch_get_ref_count(token) == 0) {
24263 			mutex_enter(SD_MUTEX(un));
24264 			un->un_swr_token = (opaque_t)NULL;
24265 		} else {
24266 			mutex_enter(SD_MUTEX(un));
24267 		}
24268 	}
24269 
24270 	/*
24271 	 * Update the capacity kstat value, if no media previously
24272 	 * (capacity kstat is 0) and a media has been inserted
24273 	 * (un_f_blockcount_is_valid == TRUE)
24274 	 */
24275 	if (un->un_errstats) {
24276 		struct sd_errstats	*stp = NULL;
24277 
24278 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
24279 		if ((stp->sd_capacity.value.ui64 == 0) &&
24280 		    (un->un_f_blockcount_is_valid == TRUE)) {
24281 			stp->sd_capacity.value.ui64 =
24282 			    (uint64_t)((uint64_t)un->un_blockcount *
24283 			    un->un_sys_blocksize);
24284 		}
24285 	}
24286 	mutex_exit(SD_MUTEX(un));
24287 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
24288 	return (rval);
24289 }
24290 
24291 
24292 /*
24293  *    Function: sd_delayed_cv_broadcast
24294  *
24295  * Description: Delayed cv_broadcast to allow for target to recover from media
24296  *		insertion.
24297  *
24298  *   Arguments: arg - driver soft state (unit) structure
24299  */
24300 
24301 static void
24302 sd_delayed_cv_broadcast(void *arg)
24303 {
24304 	struct sd_lun *un = arg;
24305 
24306 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
24307 
24308 	mutex_enter(SD_MUTEX(un));
24309 	un->un_dcvb_timeid = NULL;
24310 	cv_broadcast(&un->un_state_cv);
24311 	mutex_exit(SD_MUTEX(un));
24312 }
24313 
24314 
24315 /*
24316  *    Function: sd_media_watch_cb
24317  *
24318  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
24319  *		routine processes the TUR sense data and updates the driver
24320  *		state if a transition has occurred. The user thread
24321  *		(sd_check_media) is then signalled.
24322  *
24323  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24324  *			among multiple watches that share this callback function
24325  *		resultp - scsi watch facility result packet containing scsi
24326  *			  packet, status byte and sense data
24327  *
24328  * Return Code: 0 for success, -1 for failure
24329  */
24330 
24331 static int
24332 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24333 {
24334 	struct sd_lun			*un;
24335 	struct scsi_status		*statusp = resultp->statusp;
24336 	uint8_t				*sensep = (uint8_t *)resultp->sensep;
24337 	enum dkio_state			state = DKIO_NONE;
24338 	dev_t				dev = (dev_t)arg;
24339 	uchar_t				actual_sense_length;
24340 	uint8_t				skey, asc, ascq;
24341 
24342 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24343 		return (-1);
24344 	}
24345 	actual_sense_length = resultp->actual_sense_length;
24346 
24347 	mutex_enter(SD_MUTEX(un));
24348 	SD_TRACE(SD_LOG_COMMON, un,
24349 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
24350 	    *((char *)statusp), (void *)sensep, actual_sense_length);
24351 
24352 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
24353 		un->un_mediastate = DKIO_DEV_GONE;
24354 		cv_broadcast(&un->un_state_cv);
24355 		mutex_exit(SD_MUTEX(un));
24356 
24357 		return (0);
24358 	}
24359 
24360 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
24361 		if (sd_gesn_media_data_valid(resultp->mmc_data)) {
24362 			if ((resultp->mmc_data[5] &
24363 			    SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
24364 				state = DKIO_INSERTED;
24365 			} else {
24366 				state = DKIO_EJECTED;
24367 			}
24368 			if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
24369 			    SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
24370 				sd_log_eject_request_event(un, KM_NOSLEEP);
24371 			}
24372 		}
24373 	} else if (sensep != NULL) {
24374 		/*
24375 		 * If there was a check condition then sensep points to valid
24376 		 * sense data. If status was not a check condition but a
24377 		 * reservation or busy status then the new state is DKIO_NONE.
24378 		 */
24379 		skey = scsi_sense_key(sensep);
24380 		asc = scsi_sense_asc(sensep);
24381 		ascq = scsi_sense_ascq(sensep);
24382 
24383 		SD_INFO(SD_LOG_COMMON, un,
24384 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
24385 		    skey, asc, ascq);
24386 		/* This routine only uses up to 13 bytes of sense data. */
24387 		if (actual_sense_length >= 13) {
24388 			if (skey == KEY_UNIT_ATTENTION) {
24389 				if (asc == 0x28) {
24390 					state = DKIO_INSERTED;
24391 				}
24392 			} else if (skey == KEY_NOT_READY) {
24393 				/*
24394 				 * Sense data of 02/06/00 means that the
24395 				 * drive could not read the media (No
24396 				 * reference position found). In this case
24397 				 * to prevent a hang on the DKIOCSTATE IOCTL
24398 				 * we set the media state to DKIO_INSERTED.
24399 				 */
24400 				if (asc == 0x06 && ascq == 0x00)
24401 					state = DKIO_INSERTED;
24402 
24403 				/*
24404 				 * if 02/04/02  means that the host
24405 				 * should send start command. Explicitly
24406 				 * leave the media state as is
24407 				 * (inserted) as the media is inserted
24408 				 * and host has stopped device for PM
24409 				 * reasons. Upon next true read/write
24410 				 * to this media will bring the
24411 				 * device to the right state good for
24412 				 * media access.
24413 				 */
24414 				if (asc == 0x3a) {
24415 					state = DKIO_EJECTED;
24416 				} else {
24417 					/*
24418 					 * If the drive is busy with an
24419 					 * operation or long write, keep the
24420 					 * media in an inserted state.
24421 					 */
24422 
24423 					if ((asc == 0x04) &&
24424 					    ((ascq == 0x02) ||
24425 					    (ascq == 0x07) ||
24426 					    (ascq == 0x08))) {
24427 						state = DKIO_INSERTED;
24428 					}
24429 				}
24430 			} else if (skey == KEY_NO_SENSE) {
24431 				if ((asc == 0x00) && (ascq == 0x00)) {
24432 					/*
24433 					 * Sense Data 00/00/00 does not provide
24434 					 * any information about the state of
24435 					 * the media. Ignore it.
24436 					 */
24437 					mutex_exit(SD_MUTEX(un));
24438 					return (0);
24439 				}
24440 			}
24441 		}
24442 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
24443 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24444 		state = DKIO_INSERTED;
24445 	}
24446 
24447 	SD_TRACE(SD_LOG_COMMON, un,
24448 	    "sd_media_watch_cb: state=%x, specified=%x\n",
24449 	    state, un->un_specified_mediastate);
24450 
24451 	/*
24452 	 * now signal the waiting thread if this is *not* the specified state;
24453 	 * delay the signal if the state is DKIO_INSERTED to allow the target
24454 	 * to recover
24455 	 */
24456 	if (state != un->un_specified_mediastate) {
24457 		un->un_mediastate = state;
24458 		if (state == DKIO_INSERTED) {
24459 			/*
24460 			 * delay the signal to give the drive a chance
24461 			 * to do what it apparently needs to do
24462 			 */
24463 			SD_TRACE(SD_LOG_COMMON, un,
24464 			    "sd_media_watch_cb: delayed cv_broadcast\n");
24465 			if (un->un_dcvb_timeid == NULL) {
24466 				un->un_dcvb_timeid =
24467 				    timeout(sd_delayed_cv_broadcast, un,
24468 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24469 			}
24470 		} else {
24471 			SD_TRACE(SD_LOG_COMMON, un,
24472 			    "sd_media_watch_cb: immediate cv_broadcast\n");
24473 			cv_broadcast(&un->un_state_cv);
24474 		}
24475 	}
24476 	mutex_exit(SD_MUTEX(un));
24477 	return (0);
24478 }
24479 
24480 
24481 /*
24482  *    Function: sd_dkio_get_temp
24483  *
24484  * Description: This routine is the driver entry point for handling ioctl
24485  *		requests to get the disk temperature.
24486  *
24487  *   Arguments: dev  - the device number
24488  *		arg  - pointer to user provided dk_temperature structure.
24489  *		flag - this argument is a pass through to ddi_copyxxx()
24490  *		       directly from the mode argument of ioctl().
24491  *
24492  * Return Code: 0
24493  *		EFAULT
24494  *		ENXIO
24495  *		EAGAIN
24496  */
24497 
24498 static int
24499 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24500 {
24501 	struct sd_lun		*un = NULL;
24502 	struct dk_temperature	*dktemp = NULL;
24503 	uchar_t			*temperature_page;
24504 	int			rval = 0;
24505 	int			path_flag = SD_PATH_STANDARD;
24506 	sd_ssc_t		*ssc;
24507 
24508 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24509 		return (ENXIO);
24510 	}
24511 
24512 	ssc = sd_ssc_init(un);
24513 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24514 
24515 	/* copyin the disk temp argument to get the user flags */
24516 	if (ddi_copyin((void *)arg, dktemp,
24517 	    sizeof (struct dk_temperature), flag) != 0) {
24518 		rval = EFAULT;
24519 		goto done;
24520 	}
24521 
24522 	/* Initialize the temperature to invalid. */
24523 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24524 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24525 
24526 	/*
24527 	 * Note: Investigate removing the "bypass pm" semantic.
24528 	 * Can we just bypass PM always?
24529 	 */
24530 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24531 		path_flag = SD_PATH_DIRECT;
24532 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24533 		mutex_enter(&un->un_pm_mutex);
24534 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24535 			/*
24536 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24537 			 * in low power mode, we can not wake it up, Need to
24538 			 * return EAGAIN.
24539 			 */
24540 			mutex_exit(&un->un_pm_mutex);
24541 			rval = EAGAIN;
24542 			goto done;
24543 		} else {
24544 			/*
24545 			 * Indicate to PM the device is busy. This is required
24546 			 * to avoid a race - i.e. the ioctl is issuing a
24547 			 * command and the pm framework brings down the device
24548 			 * to low power mode (possible power cut-off on some
24549 			 * platforms).
24550 			 */
24551 			mutex_exit(&un->un_pm_mutex);
24552 			if (sd_pm_entry(un) != DDI_SUCCESS) {
24553 				rval = EAGAIN;
24554 				goto done;
24555 			}
24556 		}
24557 	}
24558 
24559 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24560 
24561 	rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24562 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24563 	if (rval != 0)
24564 		goto done2;
24565 
24566 	/*
24567 	 * For the current temperature verify that the parameter length is 0x02
24568 	 * and the parameter code is 0x00
24569 	 */
24570 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24571 	    (temperature_page[5] == 0x00)) {
24572 		if (temperature_page[9] == 0xFF) {
24573 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24574 		} else {
24575 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24576 		}
24577 	}
24578 
24579 	/*
24580 	 * For the reference temperature verify that the parameter
24581 	 * length is 0x02 and the parameter code is 0x01
24582 	 */
24583 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24584 	    (temperature_page[11] == 0x01)) {
24585 		if (temperature_page[15] == 0xFF) {
24586 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24587 		} else {
24588 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24589 		}
24590 	}
24591 
24592 	/* Do the copyout regardless of the temperature commands status. */
24593 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24594 	    flag) != 0) {
24595 		rval = EFAULT;
24596 		goto done1;
24597 	}
24598 
24599 done2:
24600 	if (rval != 0) {
24601 		if (rval == EIO)
24602 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24603 		else
24604 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24605 	}
24606 done1:
24607 	if (path_flag == SD_PATH_DIRECT) {
24608 		sd_pm_exit(un);
24609 	}
24610 
24611 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24612 done:
24613 	sd_ssc_fini(ssc);
24614 	if (dktemp != NULL) {
24615 		kmem_free(dktemp, sizeof (struct dk_temperature));
24616 	}
24617 
24618 	return (rval);
24619 }
24620 
24621 
24622 /*
24623  *    Function: sd_log_page_supported
24624  *
24625  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24626  *		supported log pages.
24627  *
24628  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
24629  *                      structure for this target.
24630  *		log_page -
24631  *
24632  * Return Code: -1 - on error (log sense is optional and may not be supported).
24633  *		0  - log page not found.
24634  *  		1  - log page found.
24635  */
24636 
24637 static int
24638 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24639 {
24640 	uchar_t *log_page_data;
24641 	int	i;
24642 	int	match = 0;
24643 	int	log_size;
24644 	int	status = 0;
24645 	struct sd_lun	*un;
24646 
24647 	ASSERT(ssc != NULL);
24648 	un = ssc->ssc_un;
24649 	ASSERT(un != NULL);
24650 
24651 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24652 
24653 	status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24654 	    SD_PATH_DIRECT);
24655 
24656 	if (status != 0) {
24657 		if (status == EIO) {
24658 			/*
24659 			 * Some disks do not support log sense, we
24660 			 * should ignore this kind of error(sense key is
24661 			 * 0x5 - illegal request).
24662 			 */
24663 			uint8_t *sensep;
24664 			int senlen;
24665 
24666 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24667 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24668 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
24669 
24670 			if (senlen > 0 &&
24671 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24672 				sd_ssc_assessment(ssc,
24673 				    SD_FMT_IGNORE_COMPROMISE);
24674 			} else {
24675 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24676 			}
24677 		} else {
24678 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24679 		}
24680 
24681 		SD_ERROR(SD_LOG_COMMON, un,
24682 		    "sd_log_page_supported: failed log page retrieval\n");
24683 		kmem_free(log_page_data, 0xFF);
24684 		return (-1);
24685 	}
24686 
24687 	log_size = log_page_data[3];
24688 
24689 	/*
24690 	 * The list of supported log pages start from the fourth byte. Check
24691 	 * until we run out of log pages or a match is found.
24692 	 */
24693 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
24694 		if (log_page_data[i] == log_page) {
24695 			match++;
24696 		}
24697 	}
24698 	kmem_free(log_page_data, 0xFF);
24699 	return (match);
24700 }
24701 
24702 
24703 /*
24704  *    Function: sd_mhdioc_failfast
24705  *
24706  * Description: This routine is the driver entry point for handling ioctl
24707  *		requests to enable/disable the multihost failfast option.
24708  *		(MHIOCENFAILFAST)
24709  *
24710  *   Arguments: dev	- the device number
24711  *		arg	- user specified probing interval.
24712  *		flag	- this argument is a pass through to ddi_copyxxx()
24713  *			  directly from the mode argument of ioctl().
24714  *
24715  * Return Code: 0
24716  *		EFAULT
24717  *		ENXIO
24718  */
24719 
24720 static int
24721 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24722 {
24723 	struct sd_lun	*un = NULL;
24724 	int		mh_time;
24725 	int		rval = 0;
24726 
24727 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24728 		return (ENXIO);
24729 	}
24730 
24731 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24732 		return (EFAULT);
24733 
24734 	if (mh_time) {
24735 		mutex_enter(SD_MUTEX(un));
24736 		un->un_resvd_status |= SD_FAILFAST;
24737 		mutex_exit(SD_MUTEX(un));
24738 		/*
24739 		 * If mh_time is INT_MAX, then this ioctl is being used for
24740 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24741 		 */
24742 		if (mh_time != INT_MAX) {
24743 			rval = sd_check_mhd(dev, mh_time);
24744 		}
24745 	} else {
24746 		(void) sd_check_mhd(dev, 0);
24747 		mutex_enter(SD_MUTEX(un));
24748 		un->un_resvd_status &= ~SD_FAILFAST;
24749 		mutex_exit(SD_MUTEX(un));
24750 	}
24751 	return (rval);
24752 }
24753 
24754 
24755 /*
24756  *    Function: sd_mhdioc_takeown
24757  *
24758  * Description: This routine is the driver entry point for handling ioctl
24759  *		requests to forcefully acquire exclusive access rights to the
24760  *		multihost disk (MHIOCTKOWN).
24761  *
24762  *   Arguments: dev	- the device number
24763  *		arg	- user provided structure specifying the delay
24764  *			  parameters in milliseconds
24765  *		flag	- this argument is a pass through to ddi_copyxxx()
24766  *			  directly from the mode argument of ioctl().
24767  *
24768  * Return Code: 0
24769  *		EFAULT
24770  *		ENXIO
24771  */
24772 
24773 static int
24774 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24775 {
24776 	struct sd_lun		*un = NULL;
24777 	struct mhioctkown	*tkown = NULL;
24778 	int			rval = 0;
24779 
24780 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24781 		return (ENXIO);
24782 	}
24783 
24784 	if (arg != NULL) {
24785 		tkown = (struct mhioctkown *)
24786 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24787 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24788 		if (rval != 0) {
24789 			rval = EFAULT;
24790 			goto error;
24791 		}
24792 	}
24793 
24794 	rval = sd_take_ownership(dev, tkown);
24795 	mutex_enter(SD_MUTEX(un));
24796 	if (rval == 0) {
24797 		un->un_resvd_status |= SD_RESERVE;
24798 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24799 			sd_reinstate_resv_delay =
24800 			    tkown->reinstate_resv_delay * 1000;
24801 		} else {
24802 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24803 		}
24804 		/*
24805 		 * Give the scsi_watch routine interval set by
24806 		 * the MHIOCENFAILFAST ioctl precedence here.
24807 		 */
24808 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24809 			mutex_exit(SD_MUTEX(un));
24810 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24811 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24812 			    "sd_mhdioc_takeown : %d\n",
24813 			    sd_reinstate_resv_delay);
24814 		} else {
24815 			mutex_exit(SD_MUTEX(un));
24816 		}
24817 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24818 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24819 	} else {
24820 		un->un_resvd_status &= ~SD_RESERVE;
24821 		mutex_exit(SD_MUTEX(un));
24822 	}
24823 
24824 error:
24825 	if (tkown != NULL) {
24826 		kmem_free(tkown, sizeof (struct mhioctkown));
24827 	}
24828 	return (rval);
24829 }
24830 
24831 
24832 /*
24833  *    Function: sd_mhdioc_release
24834  *
24835  * Description: This routine is the driver entry point for handling ioctl
24836  *		requests to release exclusive access rights to the multihost
24837  *		disk (MHIOCRELEASE).
24838  *
24839  *   Arguments: dev	- the device number
24840  *
24841  * Return Code: 0
24842  *		ENXIO
24843  */
24844 
24845 static int
24846 sd_mhdioc_release(dev_t dev)
24847 {
24848 	struct sd_lun		*un = NULL;
24849 	timeout_id_t		resvd_timeid_save;
24850 	int			resvd_status_save;
24851 	int			rval = 0;
24852 
24853 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24854 		return (ENXIO);
24855 	}
24856 
24857 	mutex_enter(SD_MUTEX(un));
24858 	resvd_status_save = un->un_resvd_status;
24859 	un->un_resvd_status &=
24860 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24861 	if (un->un_resvd_timeid) {
24862 		resvd_timeid_save = un->un_resvd_timeid;
24863 		un->un_resvd_timeid = NULL;
24864 		mutex_exit(SD_MUTEX(un));
24865 		(void) untimeout(resvd_timeid_save);
24866 	} else {
24867 		mutex_exit(SD_MUTEX(un));
24868 	}
24869 
24870 	/*
24871 	 * destroy any pending timeout thread that may be attempting to
24872 	 * reinstate reservation on this device.
24873 	 */
24874 	sd_rmv_resv_reclaim_req(dev);
24875 
24876 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24877 		mutex_enter(SD_MUTEX(un));
24878 		if ((un->un_mhd_token) &&
24879 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24880 			mutex_exit(SD_MUTEX(un));
24881 			(void) sd_check_mhd(dev, 0);
24882 		} else {
24883 			mutex_exit(SD_MUTEX(un));
24884 		}
24885 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24886 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24887 	} else {
24888 		/*
24889 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24890 		 */
24891 		mutex_enter(SD_MUTEX(un));
24892 		un->un_resvd_status = resvd_status_save;
24893 		mutex_exit(SD_MUTEX(un));
24894 	}
24895 	return (rval);
24896 }
24897 
24898 
24899 /*
24900  *    Function: sd_mhdioc_register_devid
24901  *
24902  * Description: This routine is the driver entry point for handling ioctl
24903  *		requests to register the device id (MHIOCREREGISTERDEVID).
24904  *
24905  *		Note: The implementation for this ioctl has been updated to
24906  *		be consistent with the original PSARC case (1999/357)
24907  *		(4375899, 4241671, 4220005)
24908  *
24909  *   Arguments: dev	- the device number
24910  *
24911  * Return Code: 0
24912  *		ENXIO
24913  */
24914 
24915 static int
24916 sd_mhdioc_register_devid(dev_t dev)
24917 {
24918 	struct sd_lun	*un = NULL;
24919 	int		rval = 0;
24920 	sd_ssc_t	*ssc;
24921 
24922 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24923 		return (ENXIO);
24924 	}
24925 
24926 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24927 
24928 	mutex_enter(SD_MUTEX(un));
24929 
24930 	/* If a devid already exists, de-register it */
24931 	if (un->un_devid != NULL) {
24932 		ddi_devid_unregister(SD_DEVINFO(un));
24933 		/*
24934 		 * After unregister devid, needs to free devid memory
24935 		 */
24936 		ddi_devid_free(un->un_devid);
24937 		un->un_devid = NULL;
24938 	}
24939 
24940 	/* Check for reservation conflict */
24941 	mutex_exit(SD_MUTEX(un));
24942 	ssc = sd_ssc_init(un);
24943 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24944 	mutex_enter(SD_MUTEX(un));
24945 
24946 	switch (rval) {
24947 	case 0:
24948 		sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24949 		break;
24950 	case EACCES:
24951 		break;
24952 	default:
24953 		rval = EIO;
24954 	}
24955 
24956 	mutex_exit(SD_MUTEX(un));
24957 	if (rval != 0) {
24958 		if (rval == EIO)
24959 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24960 		else
24961 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24962 	}
24963 	sd_ssc_fini(ssc);
24964 	return (rval);
24965 }
24966 
24967 
24968 /*
24969  *    Function: sd_mhdioc_inkeys
24970  *
24971  * Description: This routine is the driver entry point for handling ioctl
24972  *		requests to issue the SCSI-3 Persistent In Read Keys command
24973  *		to the device (MHIOCGRP_INKEYS).
24974  *
24975  *   Arguments: dev	- the device number
24976  *		arg	- user provided in_keys structure
24977  *		flag	- this argument is a pass through to ddi_copyxxx()
24978  *			  directly from the mode argument of ioctl().
24979  *
24980  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24981  *		ENXIO
24982  *		EFAULT
24983  */
24984 
24985 static int
24986 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24987 {
24988 	struct sd_lun		*un;
24989 	mhioc_inkeys_t		inkeys;
24990 	int			rval = 0;
24991 
24992 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24993 		return (ENXIO);
24994 	}
24995 
24996 #ifdef _MULTI_DATAMODEL
24997 	switch (ddi_model_convert_from(flag & FMODELS)) {
24998 	case DDI_MODEL_ILP32: {
24999 		struct mhioc_inkeys32	inkeys32;
25000 
25001 		if (ddi_copyin(arg, &inkeys32,
25002 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
25003 			return (EFAULT);
25004 		}
25005 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
25006 		if ((rval = sd_persistent_reservation_in_read_keys(un,
25007 		    &inkeys, flag)) != 0) {
25008 			return (rval);
25009 		}
25010 		inkeys32.generation = inkeys.generation;
25011 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
25012 		    flag) != 0) {
25013 			return (EFAULT);
25014 		}
25015 		break;
25016 	}
25017 	case DDI_MODEL_NONE:
25018 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
25019 		    flag) != 0) {
25020 			return (EFAULT);
25021 		}
25022 		if ((rval = sd_persistent_reservation_in_read_keys(un,
25023 		    &inkeys, flag)) != 0) {
25024 			return (rval);
25025 		}
25026 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
25027 		    flag) != 0) {
25028 			return (EFAULT);
25029 		}
25030 		break;
25031 	}
25032 
25033 #else /* ! _MULTI_DATAMODEL */
25034 
25035 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
25036 		return (EFAULT);
25037 	}
25038 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
25039 	if (rval != 0) {
25040 		return (rval);
25041 	}
25042 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
25043 		return (EFAULT);
25044 	}
25045 
25046 #endif /* _MULTI_DATAMODEL */
25047 
25048 	return (rval);
25049 }
25050 
25051 
25052 /*
25053  *    Function: sd_mhdioc_inresv
25054  *
25055  * Description: This routine is the driver entry point for handling ioctl
25056  *		requests to issue the SCSI-3 Persistent In Read Reservations
25057  *		command to the device (MHIOCGRP_INKEYS).
25058  *
25059  *   Arguments: dev	- the device number
25060  *		arg	- user provided in_resv structure
25061  *		flag	- this argument is a pass through to ddi_copyxxx()
25062  *			  directly from the mode argument of ioctl().
25063  *
25064  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
25065  *		ENXIO
25066  *		EFAULT
25067  */
25068 
25069 static int
25070 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
25071 {
25072 	struct sd_lun		*un;
25073 	mhioc_inresvs_t		inresvs;
25074 	int			rval = 0;
25075 
25076 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25077 		return (ENXIO);
25078 	}
25079 
25080 #ifdef _MULTI_DATAMODEL
25081 
25082 	switch (ddi_model_convert_from(flag & FMODELS)) {
25083 	case DDI_MODEL_ILP32: {
25084 		struct mhioc_inresvs32	inresvs32;
25085 
25086 		if (ddi_copyin(arg, &inresvs32,
25087 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
25088 			return (EFAULT);
25089 		}
25090 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
25091 		if ((rval = sd_persistent_reservation_in_read_resv(un,
25092 		    &inresvs, flag)) != 0) {
25093 			return (rval);
25094 		}
25095 		inresvs32.generation = inresvs.generation;
25096 		if (ddi_copyout(&inresvs32, arg,
25097 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
25098 			return (EFAULT);
25099 		}
25100 		break;
25101 	}
25102 	case DDI_MODEL_NONE:
25103 		if (ddi_copyin(arg, &inresvs,
25104 		    sizeof (mhioc_inresvs_t), flag) != 0) {
25105 			return (EFAULT);
25106 		}
25107 		if ((rval = sd_persistent_reservation_in_read_resv(un,
25108 		    &inresvs, flag)) != 0) {
25109 			return (rval);
25110 		}
25111 		if (ddi_copyout(&inresvs, arg,
25112 		    sizeof (mhioc_inresvs_t), flag) != 0) {
25113 			return (EFAULT);
25114 		}
25115 		break;
25116 	}
25117 
25118 #else /* ! _MULTI_DATAMODEL */
25119 
25120 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
25121 		return (EFAULT);
25122 	}
25123 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
25124 	if (rval != 0) {
25125 		return (rval);
25126 	}
25127 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
25128 		return (EFAULT);
25129 	}
25130 
25131 #endif /* ! _MULTI_DATAMODEL */
25132 
25133 	return (rval);
25134 }
25135 
25136 
25137 /*
25138  * The following routines support the clustering functionality described below
25139  * and implement lost reservation reclaim functionality.
25140  *
25141  * Clustering
25142  * ----------
25143  * The clustering code uses two different, independent forms of SCSI
25144  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
25145  * Persistent Group Reservations. For any particular disk, it will use either
25146  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
25147  *
25148  * SCSI-2
25149  * The cluster software takes ownership of a multi-hosted disk by issuing the
25150  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
25151  * MHIOCRELEASE ioctl.  Closely related is the MHIOCENFAILFAST ioctl -- a
25152  * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
25153  * then issues the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the
25154  * driver. The meaning of failfast is that if the driver (on this host) ever
25155  * encounters the scsi error return code RESERVATION_CONFLICT from the device,
25156  * it should immediately panic the host. The motivation for this ioctl is that
25157  * if this host does encounter reservation conflict, the underlying cause is
25158  * that some other host of the cluster has decided that this host is no longer
25159  * in the cluster and has seized control of the disks for itself. Since this
25160  * host is no longer in the cluster, it ought to panic itself. The
25161  * MHIOCENFAILFAST ioctl does two things:
25162  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
25163  *      error to panic the host
25164  *      (b) it sets up a periodic timer to test whether this host still has
25165  *      "access" (in that no other host has reserved the device):  if the
25166  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
25167  *      purpose of that periodic timer is to handle scenarios where the host is
25168  *      otherwise temporarily quiescent, temporarily doing no real i/o.
25169  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
25170  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
25171  * the device itself.
25172  *
25173  * SCSI-3 PGR
25174  * A direct semantic implementation of the SCSI-3 Persistent Reservation
25175  * facility is supported through the shared multihost disk ioctls
25176  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
25177  * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR)
25178  *
25179  * Reservation Reclaim:
25180  * --------------------
25181  * To support the lost reservation reclaim operations this driver creates a
25182  * single thread to handle reinstating reservations on all devices that have
25183  * lost reservations sd_resv_reclaim_requests are logged for all devices that
25184  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
25185  * and the reservation reclaim thread loops through the requests to regain the
25186  * lost reservations.
25187  */
25188 
25189 /*
25190  *    Function: sd_check_mhd()
25191  *
25192  * Description: This function sets up and submits a scsi watch request or
25193  *		terminates an existing watch request. This routine is used in
25194  *		support of reservation reclaim.
25195  *
25196  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
25197  *			 among multiple watches that share the callback function
25198  *		interval - the number of microseconds specifying the watch
25199  *			   interval for issuing TEST UNIT READY commands. If
25200  *			   set to 0 the watch should be terminated. If the
25201  *			   interval is set to 0 and if the device is required
25202  *			   to hold reservation while disabling failfast, the
25203  *			   watch is restarted with an interval of
25204  *			   reinstate_resv_delay.
25205  *
25206  * Return Code: 0	   - Successful submit/terminate of scsi watch request
25207  *		ENXIO      - Indicates an invalid device was specified
25208  *		EAGAIN     - Unable to submit the scsi watch request
25209  */
25210 
25211 static int
25212 sd_check_mhd(dev_t dev, int interval)
25213 {
25214 	struct sd_lun	*un;
25215 	opaque_t	token;
25216 
25217 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25218 		return (ENXIO);
25219 	}
25220 
25221 	/* is this a watch termination request? */
25222 	if (interval == 0) {
25223 		mutex_enter(SD_MUTEX(un));
25224 		/* if there is an existing watch task then terminate it */
25225 		if (un->un_mhd_token) {
25226 			token = un->un_mhd_token;
25227 			un->un_mhd_token = NULL;
25228 			mutex_exit(SD_MUTEX(un));
25229 			(void) scsi_watch_request_terminate(token,
25230 			    SCSI_WATCH_TERMINATE_ALL_WAIT);
25231 			mutex_enter(SD_MUTEX(un));
25232 		} else {
25233 			mutex_exit(SD_MUTEX(un));
25234 			/*
25235 			 * Note: If we return here we don't check for the
25236 			 * failfast case. This is the original legacy
25237 			 * implementation but perhaps we should be checking
25238 			 * the failfast case.
25239 			 */
25240 			return (0);
25241 		}
25242 		/*
25243 		 * If the device is required to hold reservation while
25244 		 * disabling failfast, we need to restart the scsi_watch
25245 		 * routine with an interval of reinstate_resv_delay.
25246 		 */
25247 		if (un->un_resvd_status & SD_RESERVE) {
25248 			interval = sd_reinstate_resv_delay/1000;
25249 		} else {
25250 			/* no failfast so bail */
25251 			mutex_exit(SD_MUTEX(un));
25252 			return (0);
25253 		}
25254 		mutex_exit(SD_MUTEX(un));
25255 	}
25256 
25257 	/*
25258 	 * adjust minimum time interval to 1 second,
25259 	 * and convert from msecs to usecs
25260 	 */
25261 	if (interval > 0 && interval < 1000) {
25262 		interval = 1000;
25263 	}
25264 	interval *= 1000;
25265 
25266 	/*
25267 	 * submit the request to the scsi_watch service
25268 	 */
25269 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
25270 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
25271 	if (token == NULL) {
25272 		return (EAGAIN);
25273 	}
25274 
25275 	/*
25276 	 * save token for termination later on
25277 	 */
25278 	mutex_enter(SD_MUTEX(un));
25279 	un->un_mhd_token = token;
25280 	mutex_exit(SD_MUTEX(un));
25281 	return (0);
25282 }
25283 
25284 
25285 /*
25286  *    Function: sd_mhd_watch_cb()
25287  *
25288  * Description: This function is the call back function used by the scsi watch
25289  *		facility. The scsi watch facility sends the "Test Unit Ready"
25290  *		and processes the status. If applicable (i.e. a "Unit Attention"
25291  *		status and automatic "Request Sense" not used) the scsi watch
25292  *		facility will send a "Request Sense" and retrieve the sense data
25293  *		to be passed to this callback function. In either case the
25294  *		automatic "Request Sense" or the facility submitting one, this
25295  *		callback is passed the status and sense data.
25296  *
25297  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25298  *			among multiple watches that share this callback function
25299  *		resultp - scsi watch facility result packet containing scsi
25300  *			  packet, status byte and sense data
25301  *
25302  * Return Code: 0 - continue the watch task
25303  *		non-zero - terminate the watch task
25304  */
25305 
25306 static int
25307 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
25308 {
25309 	struct sd_lun			*un;
25310 	struct scsi_status		*statusp;
25311 	uint8_t				*sensep;
25312 	struct scsi_pkt			*pkt;
25313 	uchar_t				actual_sense_length;
25314 	dev_t  				dev = (dev_t)arg;
25315 
25316 	ASSERT(resultp != NULL);
25317 	statusp			= resultp->statusp;
25318 	sensep			= (uint8_t *)resultp->sensep;
25319 	pkt			= resultp->pkt;
25320 	actual_sense_length	= resultp->actual_sense_length;
25321 
25322 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25323 		return (ENXIO);
25324 	}
25325 
25326 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25327 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
25328 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
25329 
25330 	/* Begin processing of the status and/or sense data */
25331 	if (pkt->pkt_reason != CMD_CMPLT) {
25332 		/* Handle the incomplete packet */
25333 		sd_mhd_watch_incomplete(un, pkt);
25334 		return (0);
25335 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
25336 		if (*((unsigned char *)statusp)
25337 		    == STATUS_RESERVATION_CONFLICT) {
25338 			/*
25339 			 * Handle a reservation conflict by panicking if
25340 			 * configured for failfast or by logging the conflict
25341 			 * and updating the reservation status
25342 			 */
25343 			mutex_enter(SD_MUTEX(un));
25344 			if ((un->un_resvd_status & SD_FAILFAST) &&
25345 			    (sd_failfast_enable)) {
25346 				sd_panic_for_res_conflict(un);
25347 				/*NOTREACHED*/
25348 			}
25349 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25350 			    "sd_mhd_watch_cb: Reservation Conflict\n");
25351 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
25352 			mutex_exit(SD_MUTEX(un));
25353 		}
25354 	}
25355 
25356 	if (sensep != NULL) {
25357 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
25358 			mutex_enter(SD_MUTEX(un));
25359 			if ((scsi_sense_asc(sensep) ==
25360 			    SD_SCSI_RESET_SENSE_CODE) &&
25361 			    (un->un_resvd_status & SD_RESERVE)) {
25362 				/*
25363 				 * The additional sense code indicates a power
25364 				 * on or bus device reset has occurred; update
25365 				 * the reservation status.
25366 				 */
25367 				un->un_resvd_status |=
25368 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25369 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25370 				    "sd_mhd_watch_cb: Lost Reservation\n");
25371 			}
25372 		} else {
25373 			return (0);
25374 		}
25375 	} else {
25376 		mutex_enter(SD_MUTEX(un));
25377 	}
25378 
25379 	if ((un->un_resvd_status & SD_RESERVE) &&
25380 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
25381 		if (un->un_resvd_status & SD_WANT_RESERVE) {
25382 			/*
25383 			 * A reset occurred in between the last probe and this
25384 			 * one so if a timeout is pending cancel it.
25385 			 */
25386 			if (un->un_resvd_timeid) {
25387 				timeout_id_t temp_id = un->un_resvd_timeid;
25388 				un->un_resvd_timeid = NULL;
25389 				mutex_exit(SD_MUTEX(un));
25390 				(void) untimeout(temp_id);
25391 				mutex_enter(SD_MUTEX(un));
25392 			}
25393 			un->un_resvd_status &= ~SD_WANT_RESERVE;
25394 		}
25395 		if (un->un_resvd_timeid == 0) {
25396 			/* Schedule a timeout to handle the lost reservation */
25397 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25398 			    (void *)dev,
25399 			    drv_usectohz(sd_reinstate_resv_delay));
25400 		}
25401 	}
25402 	mutex_exit(SD_MUTEX(un));
25403 	return (0);
25404 }
25405 
25406 
25407 /*
25408  *    Function: sd_mhd_watch_incomplete()
25409  *
25410  * Description: This function is used to find out why a scsi pkt sent by the
25411  *		scsi watch facility was not completed. Under some scenarios this
25412  *		routine will return. Otherwise it will send a bus reset to see
25413  *		if the drive is still online.
25414  *
25415  *   Arguments: un  - driver soft state (unit) structure
25416  *		pkt - incomplete scsi pkt
25417  */
25418 
25419 static void
25420 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25421 {
25422 	int	be_chatty;
25423 	int	perr;
25424 
25425 	ASSERT(pkt != NULL);
25426 	ASSERT(un != NULL);
25427 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
25428 	perr		= (pkt->pkt_statistics & STAT_PERR);
25429 
25430 	mutex_enter(SD_MUTEX(un));
25431 	if (un->un_state == SD_STATE_DUMPING) {
25432 		mutex_exit(SD_MUTEX(un));
25433 		return;
25434 	}
25435 
25436 	switch (pkt->pkt_reason) {
25437 	case CMD_UNX_BUS_FREE:
25438 		/*
25439 		 * If we had a parity error that caused the target to drop BSY*,
25440 		 * don't be chatty about it.
25441 		 */
25442 		if (perr && be_chatty) {
25443 			be_chatty = 0;
25444 		}
25445 		break;
25446 	case CMD_TAG_REJECT:
25447 		/*
25448 		 * The SCSI-2 spec states that a tag reject will be sent by the
25449 		 * target if tagged queuing is not supported. A tag reject may
25450 		 * also be sent during certain initialization periods or to
25451 		 * control internal resources. For the latter case the target
25452 		 * may also return Queue Full.
25453 		 *
25454 		 * If this driver receives a tag reject from a target that is
25455 		 * going through an init period or controlling internal
25456 		 * resources tagged queuing will be disabled. This is a less
25457 		 * than optimal behavior but the driver is unable to determine
25458 		 * the target state and assumes tagged queueing is not supported
25459 		 */
25460 		pkt->pkt_flags = 0;
25461 		un->un_tagflags = 0;
25462 
25463 		if (un->un_f_opt_queueing == TRUE) {
25464 			un->un_throttle = min(un->un_throttle, 3);
25465 		} else {
25466 			un->un_throttle = 1;
25467 		}
25468 		mutex_exit(SD_MUTEX(un));
25469 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25470 		mutex_enter(SD_MUTEX(un));
25471 		break;
25472 	case CMD_INCOMPLETE:
25473 		/*
25474 		 * The transport stopped with an abnormal state, fallthrough and
25475 		 * reset the target and/or bus unless selection did not complete
25476 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
25477 		 * go through a target/bus reset
25478 		 */
25479 		if (pkt->pkt_state == STATE_GOT_BUS) {
25480 			break;
25481 		}
25482 		/*FALLTHROUGH*/
25483 
25484 	case CMD_TIMEOUT:
25485 	default:
25486 		/*
25487 		 * The lun may still be running the command, so a lun reset
25488 		 * should be attempted. If the lun reset fails or cannot be
25489 		 * issued, than try a target reset. Lastly try a bus reset.
25490 		 */
25491 		if ((pkt->pkt_statistics &
25492 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25493 			int reset_retval = 0;
25494 			mutex_exit(SD_MUTEX(un));
25495 			if (un->un_f_allow_bus_device_reset == TRUE) {
25496 				if (un->un_f_lun_reset_enabled == TRUE) {
25497 					reset_retval =
25498 					    scsi_reset(SD_ADDRESS(un),
25499 					    RESET_LUN);
25500 				}
25501 				if (reset_retval == 0) {
25502 					reset_retval =
25503 					    scsi_reset(SD_ADDRESS(un),
25504 					    RESET_TARGET);
25505 				}
25506 			}
25507 			if (reset_retval == 0) {
25508 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25509 			}
25510 			mutex_enter(SD_MUTEX(un));
25511 		}
25512 		break;
25513 	}
25514 
25515 	/* A device/bus reset has occurred; update the reservation status. */
25516 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25517 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25518 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25519 			un->un_resvd_status |=
25520 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25521 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25522 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25523 		}
25524 	}
25525 
25526 	/*
25527 	 * The disk has been turned off; Update the device state.
25528 	 *
25529 	 * Note: Should we be offlining the disk here?
25530 	 */
25531 	if (pkt->pkt_state == STATE_GOT_BUS) {
25532 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25533 		    "Disk not responding to selection\n");
25534 		if (un->un_state != SD_STATE_OFFLINE) {
25535 			New_state(un, SD_STATE_OFFLINE);
25536 		}
25537 	} else if (be_chatty) {
25538 		/*
25539 		 * suppress messages if they are all the same pkt reason;
25540 		 * with TQ, many (up to 256) are returned with the same
25541 		 * pkt_reason
25542 		 */
25543 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25544 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25545 			    "sd_mhd_watch_incomplete: "
25546 			    "SCSI transport failed: reason '%s'\n",
25547 			    scsi_rname(pkt->pkt_reason));
25548 		}
25549 	}
25550 	un->un_last_pkt_reason = pkt->pkt_reason;
25551 	mutex_exit(SD_MUTEX(un));
25552 }
25553 
25554 
25555 /*
25556  *    Function: sd_sname()
25557  *
25558  * Description: This is a simple little routine to return a string containing
25559  *		a printable description of command status byte for use in
25560  *		logging.
25561  *
25562  *   Arguments: status - pointer to a status byte
25563  *
25564  * Return Code: char * - string containing status description.
25565  */
25566 
25567 static char *
25568 sd_sname(uchar_t status)
25569 {
25570 	switch (status & STATUS_MASK) {
25571 	case STATUS_GOOD:
25572 		return ("good status");
25573 	case STATUS_CHECK:
25574 		return ("check condition");
25575 	case STATUS_MET:
25576 		return ("condition met");
25577 	case STATUS_BUSY:
25578 		return ("busy");
25579 	case STATUS_INTERMEDIATE:
25580 		return ("intermediate");
25581 	case STATUS_INTERMEDIATE_MET:
25582 		return ("intermediate - condition met");
25583 	case STATUS_RESERVATION_CONFLICT:
25584 		return ("reservation_conflict");
25585 	case STATUS_TERMINATED:
25586 		return ("command terminated");
25587 	case STATUS_QFULL:
25588 		return ("queue full");
25589 	default:
25590 		return ("<unknown status>");
25591 	}
25592 }
25593 
25594 
25595 /*
25596  *    Function: sd_mhd_resvd_recover()
25597  *
25598  * Description: This function adds a reservation entry to the
25599  *		sd_resv_reclaim_request list and signals the reservation
25600  *		reclaim thread that there is work pending. If the reservation
25601  *		reclaim thread has not been previously created this function
25602  *		will kick it off.
25603  *
25604  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25605  *			among multiple watches that share this callback function
25606  *
25607  *     Context: This routine is called by timeout() and is run in interrupt
25608  *		context. It must not sleep or call other functions which may
25609  *		sleep.
25610  */
25611 
25612 static void
25613 sd_mhd_resvd_recover(void *arg)
25614 {
25615 	dev_t			dev = (dev_t)arg;
25616 	struct sd_lun		*un;
25617 	struct sd_thr_request	*sd_treq = NULL;
25618 	struct sd_thr_request	*sd_cur = NULL;
25619 	struct sd_thr_request	*sd_prev = NULL;
25620 	int			already_there = 0;
25621 
25622 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25623 		return;
25624 	}
25625 
25626 	mutex_enter(SD_MUTEX(un));
25627 	un->un_resvd_timeid = NULL;
25628 	if (un->un_resvd_status & SD_WANT_RESERVE) {
25629 		/*
25630 		 * There was a reset so don't issue the reserve, allow the
25631 		 * sd_mhd_watch_cb callback function to notice this and
25632 		 * reschedule the timeout for reservation.
25633 		 */
25634 		mutex_exit(SD_MUTEX(un));
25635 		return;
25636 	}
25637 	mutex_exit(SD_MUTEX(un));
25638 
25639 	/*
25640 	 * Add this device to the sd_resv_reclaim_request list and the
25641 	 * sd_resv_reclaim_thread should take care of the rest.
25642 	 *
25643 	 * Note: We can't sleep in this context so if the memory allocation
25644 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
25645 	 * reschedule the timeout for reservation.  (4378460)
25646 	 */
25647 	sd_treq = (struct sd_thr_request *)
25648 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25649 	if (sd_treq == NULL) {
25650 		return;
25651 	}
25652 
25653 	sd_treq->sd_thr_req_next = NULL;
25654 	sd_treq->dev = dev;
25655 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25656 	if (sd_tr.srq_thr_req_head == NULL) {
25657 		sd_tr.srq_thr_req_head = sd_treq;
25658 	} else {
25659 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25660 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25661 			if (sd_cur->dev == dev) {
25662 				/*
25663 				 * already in Queue so don't log
25664 				 * another request for the device
25665 				 */
25666 				already_there = 1;
25667 				break;
25668 			}
25669 			sd_prev = sd_cur;
25670 		}
25671 		if (!already_there) {
25672 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25673 			    "logging request for %lx\n", dev);
25674 			sd_prev->sd_thr_req_next = sd_treq;
25675 		} else {
25676 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
25677 		}
25678 	}
25679 
25680 	/*
25681 	 * Create a kernel thread to do the reservation reclaim and free up this
25682 	 * thread. We cannot block this thread while we go away to do the
25683 	 * reservation reclaim
25684 	 */
25685 	if (sd_tr.srq_resv_reclaim_thread == NULL)
25686 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25687 		    sd_resv_reclaim_thread, NULL,
25688 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
25689 
25690 	/* Tell the reservation reclaim thread that it has work to do */
25691 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
25692 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25693 }
25694 
25695 /*
25696  *    Function: sd_resv_reclaim_thread()
25697  *
25698  * Description: This function implements the reservation reclaim operations
25699  *
25700  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25701  *		      among multiple watches that share this callback function
25702  */
25703 
25704 static void
25705 sd_resv_reclaim_thread()
25706 {
25707 	struct sd_lun		*un;
25708 	struct sd_thr_request	*sd_mhreq;
25709 
25710 	/* Wait for work */
25711 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25712 	if (sd_tr.srq_thr_req_head == NULL) {
25713 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
25714 		    &sd_tr.srq_resv_reclaim_mutex);
25715 	}
25716 
25717 	/* Loop while we have work */
25718 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25719 		un = ddi_get_soft_state(sd_state,
25720 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
25721 		if (un == NULL) {
25722 			/*
25723 			 * softstate structure is NULL so just
25724 			 * dequeue the request and continue
25725 			 */
25726 			sd_tr.srq_thr_req_head =
25727 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25728 			kmem_free(sd_tr.srq_thr_cur_req,
25729 			    sizeof (struct sd_thr_request));
25730 			continue;
25731 		}
25732 
25733 		/* dequeue the request */
25734 		sd_mhreq = sd_tr.srq_thr_cur_req;
25735 		sd_tr.srq_thr_req_head =
25736 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25737 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25738 
25739 		/*
25740 		 * Reclaim reservation only if SD_RESERVE is still set. There
25741 		 * may have been a call to MHIOCRELEASE before we got here.
25742 		 */
25743 		mutex_enter(SD_MUTEX(un));
25744 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25745 			/*
25746 			 * Note: The SD_LOST_RESERVE flag is cleared before
25747 			 * reclaiming the reservation. If this is done after the
25748 			 * call to sd_reserve_release a reservation loss in the
25749 			 * window between pkt completion of reserve cmd and
25750 			 * mutex_enter below may not be recognized
25751 			 */
25752 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25753 			mutex_exit(SD_MUTEX(un));
25754 
25755 			if (sd_reserve_release(sd_mhreq->dev,
25756 			    SD_RESERVE) == 0) {
25757 				mutex_enter(SD_MUTEX(un));
25758 				un->un_resvd_status |= SD_RESERVE;
25759 				mutex_exit(SD_MUTEX(un));
25760 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25761 				    "sd_resv_reclaim_thread: "
25762 				    "Reservation Recovered\n");
25763 			} else {
25764 				mutex_enter(SD_MUTEX(un));
25765 				un->un_resvd_status |= SD_LOST_RESERVE;
25766 				mutex_exit(SD_MUTEX(un));
25767 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25768 				    "sd_resv_reclaim_thread: Failed "
25769 				    "Reservation Recovery\n");
25770 			}
25771 		} else {
25772 			mutex_exit(SD_MUTEX(un));
25773 		}
25774 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25775 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25776 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25777 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25778 		/*
25779 		 * wakeup the destroy thread if anyone is waiting on
25780 		 * us to complete.
25781 		 */
25782 		cv_signal(&sd_tr.srq_inprocess_cv);
25783 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25784 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25785 	}
25786 
25787 	/*
25788 	 * cleanup the sd_tr structure now that this thread will not exist
25789 	 */
25790 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25791 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25792 	sd_tr.srq_resv_reclaim_thread = NULL;
25793 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25794 	thread_exit();
25795 }
25796 
25797 
25798 /*
25799  *    Function: sd_rmv_resv_reclaim_req()
25800  *
25801  * Description: This function removes any pending reservation reclaim requests
25802  *		for the specified device.
25803  *
25804  *   Arguments: dev - the device 'dev_t'
25805  */
25806 
25807 static void
25808 sd_rmv_resv_reclaim_req(dev_t dev)
25809 {
25810 	struct sd_thr_request *sd_mhreq;
25811 	struct sd_thr_request *sd_prev;
25812 
25813 	/* Remove a reservation reclaim request from the list */
25814 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25815 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25816 		/*
25817 		 * We are attempting to reinstate reservation for
25818 		 * this device. We wait for sd_reserve_release()
25819 		 * to return before we return.
25820 		 */
25821 		cv_wait(&sd_tr.srq_inprocess_cv,
25822 		    &sd_tr.srq_resv_reclaim_mutex);
25823 	} else {
25824 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25825 		if (sd_mhreq && sd_mhreq->dev == dev) {
25826 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25827 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25828 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25829 			return;
25830 		}
25831 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25832 			if (sd_mhreq && sd_mhreq->dev == dev) {
25833 				break;
25834 			}
25835 			sd_prev = sd_mhreq;
25836 		}
25837 		if (sd_mhreq != NULL) {
25838 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25839 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25840 		}
25841 	}
25842 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25843 }
25844 
25845 
25846 /*
25847  *    Function: sd_mhd_reset_notify_cb()
25848  *
25849  * Description: This is a call back function for scsi_reset_notify. This
25850  *		function updates the softstate reserved status and logs the
25851  *		reset. The driver scsi watch facility callback function
25852  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25853  *		will reclaim the reservation.
25854  *
25855  *   Arguments: arg  - driver soft state (unit) structure
25856  */
25857 
25858 static void
25859 sd_mhd_reset_notify_cb(caddr_t arg)
25860 {
25861 	struct sd_lun *un = (struct sd_lun *)arg;
25862 
25863 	mutex_enter(SD_MUTEX(un));
25864 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25865 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25866 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25867 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25868 	}
25869 	mutex_exit(SD_MUTEX(un));
25870 }
25871 
25872 
25873 /*
25874  *    Function: sd_take_ownership()
25875  *
25876  * Description: This routine implements an algorithm to achieve a stable
25877  *		reservation on disks which don't implement priority reserve,
25878  *		and makes sure that other host lose re-reservation attempts.
25879  *		This algorithm contains of a loop that keeps issuing the RESERVE
25880  *		for some period of time (min_ownership_delay, default 6 seconds)
25881  *		During that loop, it looks to see if there has been a bus device
25882  *		reset or bus reset (both of which cause an existing reservation
25883  *		to be lost). If the reservation is lost issue RESERVE until a
25884  *		period of min_ownership_delay with no resets has gone by, or
25885  *		until max_ownership_delay has expired. This loop ensures that
25886  *		the host really did manage to reserve the device, in spite of
25887  *		resets. The looping for min_ownership_delay (default six
25888  *		seconds) is important to early generation clustering products,
25889  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25890  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25891  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25892  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25893  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25894  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25895  *		no longer "owns" the disk and will have panicked itself.  Thus,
25896  *		the host issuing the MHIOCTKOWN is assured (with timing
25897  *		dependencies) that by the time it actually starts to use the
25898  *		disk for real work, the old owner is no longer accessing it.
25899  *
25900  *		min_ownership_delay is the minimum amount of time for which the
25901  *		disk must be reserved continuously devoid of resets before the
25902  *		MHIOCTKOWN ioctl will return success.
25903  *
25904  *		max_ownership_delay indicates the amount of time by which the
25905  *		take ownership should succeed or timeout with an error.
25906  *
25907  *   Arguments: dev - the device 'dev_t'
25908  *		*p  - struct containing timing info.
25909  *
25910  * Return Code: 0 for success or error code
25911  */
25912 
25913 static int
25914 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25915 {
25916 	struct sd_lun	*un;
25917 	int		rval;
25918 	int		err;
25919 	int		reservation_count   = 0;
25920 	int		min_ownership_delay =  6000000; /* in usec */
25921 	int		max_ownership_delay = 30000000; /* in usec */
25922 	clock_t		start_time;	/* starting time of this algorithm */
25923 	clock_t		end_time;	/* time limit for giving up */
25924 	clock_t		ownership_time;	/* time limit for stable ownership */
25925 	clock_t		current_time;
25926 	clock_t		previous_current_time;
25927 
25928 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25929 		return (ENXIO);
25930 	}
25931 
25932 	/*
25933 	 * Attempt a device reservation. A priority reservation is requested.
25934 	 */
25935 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25936 	    != SD_SUCCESS) {
25937 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25938 		    "sd_take_ownership: return(1)=%d\n", rval);
25939 		return (rval);
25940 	}
25941 
25942 	/* Update the softstate reserved status to indicate the reservation */
25943 	mutex_enter(SD_MUTEX(un));
25944 	un->un_resvd_status |= SD_RESERVE;
25945 	un->un_resvd_status &=
25946 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25947 	mutex_exit(SD_MUTEX(un));
25948 
25949 	if (p != NULL) {
25950 		if (p->min_ownership_delay != 0) {
25951 			min_ownership_delay = p->min_ownership_delay * 1000;
25952 		}
25953 		if (p->max_ownership_delay != 0) {
25954 			max_ownership_delay = p->max_ownership_delay * 1000;
25955 		}
25956 	}
25957 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25958 	    "sd_take_ownership: min, max delays: %d, %d\n",
25959 	    min_ownership_delay, max_ownership_delay);
25960 
25961 	start_time = ddi_get_lbolt();
25962 	current_time	= start_time;
25963 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
25964 	end_time	= start_time + drv_usectohz(max_ownership_delay);
25965 
25966 	while (current_time - end_time < 0) {
25967 		delay(drv_usectohz(500000));
25968 
25969 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25970 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25971 				mutex_enter(SD_MUTEX(un));
25972 				rval = (un->un_resvd_status &
25973 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25974 				mutex_exit(SD_MUTEX(un));
25975 				break;
25976 			}
25977 		}
25978 		previous_current_time = current_time;
25979 		current_time = ddi_get_lbolt();
25980 		mutex_enter(SD_MUTEX(un));
25981 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25982 			ownership_time = ddi_get_lbolt() +
25983 			    drv_usectohz(min_ownership_delay);
25984 			reservation_count = 0;
25985 		} else {
25986 			reservation_count++;
25987 		}
25988 		un->un_resvd_status |= SD_RESERVE;
25989 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25990 		mutex_exit(SD_MUTEX(un));
25991 
25992 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25993 		    "sd_take_ownership: ticks for loop iteration=%ld, "
25994 		    "reservation=%s\n", (current_time - previous_current_time),
25995 		    reservation_count ? "ok" : "reclaimed");
25996 
25997 		if (current_time - ownership_time >= 0 &&
25998 		    reservation_count >= 4) {
25999 			rval = 0; /* Achieved a stable ownership */
26000 			break;
26001 		}
26002 		if (current_time - end_time >= 0) {
26003 			rval = EACCES; /* No ownership in max possible time */
26004 			break;
26005 		}
26006 	}
26007 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
26008 	    "sd_take_ownership: return(2)=%d\n", rval);
26009 	return (rval);
26010 }
26011 
26012 
26013 /*
26014  *    Function: sd_reserve_release()
26015  *
26016  * Description: This function builds and sends scsi RESERVE, RELEASE, and
26017  *		PRIORITY RESERVE commands based on a user specified command type
26018  *
26019  *   Arguments: dev - the device 'dev_t'
26020  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
26021  *		      SD_RESERVE, SD_RELEASE
26022  *
26023  * Return Code: 0 or Error Code
26024  */
26025 
26026 static int
26027 sd_reserve_release(dev_t dev, int cmd)
26028 {
26029 	struct uscsi_cmd	*com = NULL;
26030 	struct sd_lun		*un = NULL;
26031 	char			cdb[CDB_GROUP0];
26032 	int			rval;
26033 
26034 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
26035 	    (cmd == SD_PRIORITY_RESERVE));
26036 
26037 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26038 		return (ENXIO);
26039 	}
26040 
26041 	/* instantiate and initialize the command and cdb */
26042 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
26043 	bzero(cdb, CDB_GROUP0);
26044 	com->uscsi_flags   = USCSI_SILENT;
26045 	com->uscsi_timeout = un->un_reserve_release_time;
26046 	com->uscsi_cdblen  = CDB_GROUP0;
26047 	com->uscsi_cdb	   = cdb;
26048 	if (cmd == SD_RELEASE) {
26049 		cdb[0] = SCMD_RELEASE;
26050 	} else {
26051 		cdb[0] = SCMD_RESERVE;
26052 	}
26053 
26054 	/* Send the command. */
26055 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
26056 	    SD_PATH_STANDARD);
26057 
26058 	/*
26059 	 * "break" a reservation that is held by another host, by issuing a
26060 	 * reset if priority reserve is desired, and we could not get the
26061 	 * device.
26062 	 */
26063 	if ((cmd == SD_PRIORITY_RESERVE) &&
26064 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
26065 		/*
26066 		 * First try to reset the LUN. If we cannot, then try a target
26067 		 * reset, followed by a bus reset if the target reset fails.
26068 		 */
26069 		int reset_retval = 0;
26070 		if (un->un_f_lun_reset_enabled == TRUE) {
26071 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
26072 		}
26073 		if (reset_retval == 0) {
26074 			/* The LUN reset either failed or was not issued */
26075 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26076 		}
26077 		if ((reset_retval == 0) &&
26078 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
26079 			rval = EIO;
26080 			kmem_free(com, sizeof (*com));
26081 			return (rval);
26082 		}
26083 
26084 		bzero(com, sizeof (struct uscsi_cmd));
26085 		com->uscsi_flags   = USCSI_SILENT;
26086 		com->uscsi_cdb	   = cdb;
26087 		com->uscsi_cdblen  = CDB_GROUP0;
26088 		com->uscsi_timeout = 5;
26089 
26090 		/*
26091 		 * Reissue the last reserve command, this time without request
26092 		 * sense.  Assume that it is just a regular reserve command.
26093 		 */
26094 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
26095 		    SD_PATH_STANDARD);
26096 	}
26097 
26098 	/* Return an error if still getting a reservation conflict. */
26099 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
26100 		rval = EACCES;
26101 	}
26102 
26103 	kmem_free(com, sizeof (*com));
26104 	return (rval);
26105 }
26106 
26107 
26108 #define	SD_NDUMP_RETRIES	12
26109 /*
26110  *	System Crash Dump routine
26111  */
26112 
26113 static int
26114 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
26115 {
26116 	int		instance;
26117 	int		partition;
26118 	int		i;
26119 	int		err;
26120 	struct sd_lun	*un;
26121 	struct scsi_pkt *wr_pktp;
26122 	struct buf	*wr_bp;
26123 	struct buf	wr_buf;
26124 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
26125 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
26126 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
26127 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
26128 	size_t		io_start_offset;
26129 	int		doing_rmw = FALSE;
26130 	int		rval;
26131 	ssize_t		dma_resid;
26132 	daddr_t		oblkno;
26133 	diskaddr_t	nblks = 0;
26134 	diskaddr_t	start_block;
26135 
26136 	instance = SDUNIT(dev);
26137 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
26138 	    !SD_IS_VALID_LABEL(un) || ISCD(un)) {
26139 		return (ENXIO);
26140 	}
26141 
26142 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
26143 
26144 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
26145 
26146 	partition = SDPART(dev);
26147 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
26148 
26149 	if (!(NOT_DEVBSIZE(un))) {
26150 		int secmask = 0;
26151 		int blknomask = 0;
26152 
26153 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
26154 		secmask = un->un_tgt_blocksize - 1;
26155 
26156 		if (blkno & blknomask) {
26157 			SD_TRACE(SD_LOG_DUMP, un,
26158 			    "sddump: dump start block not modulo %d\n",
26159 			    un->un_tgt_blocksize);
26160 			return (EINVAL);
26161 		}
26162 
26163 		if ((nblk * DEV_BSIZE) & secmask) {
26164 			SD_TRACE(SD_LOG_DUMP, un,
26165 			    "sddump: dump length not modulo %d\n",
26166 			    un->un_tgt_blocksize);
26167 			return (EINVAL);
26168 		}
26169 
26170 	}
26171 
26172 	/* Validate blocks to dump at against partition size. */
26173 
26174 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
26175 	    &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
26176 
26177 	if (NOT_DEVBSIZE(un)) {
26178 		if ((blkno + nblk) > nblks) {
26179 			SD_TRACE(SD_LOG_DUMP, un,
26180 			    "sddump: dump range larger than partition: "
26181 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
26182 			    blkno, nblk, nblks);
26183 			return (EINVAL);
26184 		}
26185 	} else {
26186 		if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
26187 		    (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
26188 			SD_TRACE(SD_LOG_DUMP, un,
26189 			    "sddump: dump range larger than partition: "
26190 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
26191 			    blkno, nblk, nblks);
26192 			return (EINVAL);
26193 		}
26194 	}
26195 
26196 	mutex_enter(&un->un_pm_mutex);
26197 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
26198 		struct scsi_pkt *start_pktp;
26199 
26200 		mutex_exit(&un->un_pm_mutex);
26201 
26202 		/*
26203 		 * use pm framework to power on HBA 1st
26204 		 */
26205 		(void) pm_raise_power(SD_DEVINFO(un), 0,
26206 		    SD_PM_STATE_ACTIVE(un));
26207 
26208 		/*
26209 		 * Dump no long uses sdpower to power on a device, it's
26210 		 * in-line here so it can be done in polled mode.
26211 		 */
26212 
26213 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
26214 
26215 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
26216 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
26217 
26218 		if (start_pktp == NULL) {
26219 			/* We were not given a SCSI packet, fail. */
26220 			return (EIO);
26221 		}
26222 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
26223 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
26224 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
26225 		start_pktp->pkt_flags = FLAG_NOINTR;
26226 
26227 		mutex_enter(SD_MUTEX(un));
26228 		SD_FILL_SCSI1_LUN(un, start_pktp);
26229 		mutex_exit(SD_MUTEX(un));
26230 		/*
26231 		 * Scsi_poll returns 0 (success) if the command completes and
26232 		 * the status block is STATUS_GOOD.
26233 		 */
26234 		if (sd_scsi_poll(un, start_pktp) != 0) {
26235 			scsi_destroy_pkt(start_pktp);
26236 			return (EIO);
26237 		}
26238 		scsi_destroy_pkt(start_pktp);
26239 		(void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
26240 		    SD_PM_STATE_CHANGE);
26241 	} else {
26242 		mutex_exit(&un->un_pm_mutex);
26243 	}
26244 
26245 	mutex_enter(SD_MUTEX(un));
26246 	un->un_throttle = 0;
26247 
26248 	/*
26249 	 * The first time through, reset the specific target device.
26250 	 * However, when cpr calls sddump we know that sd is in a
26251 	 * a good state so no bus reset is required.
26252 	 * Clear sense data via Request Sense cmd.
26253 	 * In sddump we don't care about allow_bus_device_reset anymore
26254 	 */
26255 
26256 	if ((un->un_state != SD_STATE_SUSPENDED) &&
26257 	    (un->un_state != SD_STATE_DUMPING)) {
26258 
26259 		New_state(un, SD_STATE_DUMPING);
26260 
26261 		if (un->un_f_is_fibre == FALSE) {
26262 			mutex_exit(SD_MUTEX(un));
26263 			/*
26264 			 * Attempt a bus reset for parallel scsi.
26265 			 *
26266 			 * Note: A bus reset is required because on some host
26267 			 * systems (i.e. E420R) a bus device reset is
26268 			 * insufficient to reset the state of the target.
26269 			 *
26270 			 * Note: Don't issue the reset for fibre-channel,
26271 			 * because this tends to hang the bus (loop) for
26272 			 * too long while everyone is logging out and in
26273 			 * and the deadman timer for dumping will fire
26274 			 * before the dump is complete.
26275 			 */
26276 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
26277 				mutex_enter(SD_MUTEX(un));
26278 				Restore_state(un);
26279 				mutex_exit(SD_MUTEX(un));
26280 				return (EIO);
26281 			}
26282 
26283 			/* Delay to give the device some recovery time. */
26284 			drv_usecwait(10000);
26285 
26286 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
26287 				SD_INFO(SD_LOG_DUMP, un,
26288 				    "sddump: sd_send_polled_RQS failed\n");
26289 			}
26290 			mutex_enter(SD_MUTEX(un));
26291 		}
26292 	}
26293 
26294 	/*
26295 	 * Convert the partition-relative block number to a
26296 	 * disk physical block number.
26297 	 */
26298 	if (NOT_DEVBSIZE(un)) {
26299 		blkno += start_block;
26300 	} else {
26301 		blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
26302 		blkno += start_block;
26303 	}
26304 
26305 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
26306 
26307 
26308 	/*
26309 	 * Check if the device has a non-512 block size.
26310 	 */
26311 	wr_bp = NULL;
26312 	if (NOT_DEVBSIZE(un)) {
26313 		tgt_byte_offset = blkno * un->un_sys_blocksize;
26314 		tgt_byte_count = nblk * un->un_sys_blocksize;
26315 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
26316 		    (tgt_byte_count % un->un_tgt_blocksize)) {
26317 			doing_rmw = TRUE;
26318 			/*
26319 			 * Calculate the block number and number of block
26320 			 * in terms of the media block size.
26321 			 */
26322 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26323 			tgt_nblk =
26324 			    ((tgt_byte_offset + tgt_byte_count +
26325 			    (un->un_tgt_blocksize - 1)) /
26326 			    un->un_tgt_blocksize) - tgt_blkno;
26327 
26328 			/*
26329 			 * Invoke the routine which is going to do read part
26330 			 * of read-modify-write.
26331 			 * Note that this routine returns a pointer to
26332 			 * a valid bp in wr_bp.
26333 			 */
26334 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
26335 			    &wr_bp);
26336 			if (err) {
26337 				mutex_exit(SD_MUTEX(un));
26338 				return (err);
26339 			}
26340 			/*
26341 			 * Offset is being calculated as -
26342 			 * (original block # * system block size) -
26343 			 * (new block # * target block size)
26344 			 */
26345 			io_start_offset =
26346 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
26347 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
26348 
26349 			ASSERT(io_start_offset < un->un_tgt_blocksize);
26350 			/*
26351 			 * Do the modify portion of read modify write.
26352 			 */
26353 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
26354 			    (size_t)nblk * un->un_sys_blocksize);
26355 		} else {
26356 			doing_rmw = FALSE;
26357 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26358 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
26359 		}
26360 
26361 		/* Convert blkno and nblk to target blocks */
26362 		blkno = tgt_blkno;
26363 		nblk = tgt_nblk;
26364 	} else {
26365 		wr_bp = &wr_buf;
26366 		bzero(wr_bp, sizeof (struct buf));
26367 		wr_bp->b_flags		= B_BUSY;
26368 		wr_bp->b_un.b_addr	= addr;
26369 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
26370 		wr_bp->b_resid		= 0;
26371 	}
26372 
26373 	mutex_exit(SD_MUTEX(un));
26374 
26375 	/*
26376 	 * Obtain a SCSI packet for the write command.
26377 	 * It should be safe to call the allocator here without
26378 	 * worrying about being locked for DVMA mapping because
26379 	 * the address we're passed is already a DVMA mapping
26380 	 *
26381 	 * We are also not going to worry about semaphore ownership
26382 	 * in the dump buffer. Dumping is single threaded at present.
26383 	 */
26384 
26385 	wr_pktp = NULL;
26386 
26387 	dma_resid = wr_bp->b_bcount;
26388 	oblkno = blkno;
26389 
26390 	if (!(NOT_DEVBSIZE(un))) {
26391 		nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
26392 	}
26393 
26394 	while (dma_resid != 0) {
26395 
26396 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26397 		wr_bp->b_flags &= ~B_ERROR;
26398 
26399 		if (un->un_partial_dma_supported == 1) {
26400 			blkno = oblkno +
26401 			    ((wr_bp->b_bcount - dma_resid) /
26402 			    un->un_tgt_blocksize);
26403 			nblk = dma_resid / un->un_tgt_blocksize;
26404 
26405 			if (wr_pktp) {
26406 				/*
26407 				 * Partial DMA transfers after initial transfer
26408 				 */
26409 				rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26410 				    blkno, nblk);
26411 			} else {
26412 				/* Initial transfer */
26413 				rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26414 				    un->un_pkt_flags, NULL_FUNC, NULL,
26415 				    blkno, nblk);
26416 			}
26417 		} else {
26418 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26419 			    0, NULL_FUNC, NULL, blkno, nblk);
26420 		}
26421 
26422 		if (rval == 0) {
26423 			/* We were given a SCSI packet, continue. */
26424 			break;
26425 		}
26426 
26427 		if (i == 0) {
26428 			if (wr_bp->b_flags & B_ERROR) {
26429 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26430 				    "no resources for dumping; "
26431 				    "error code: 0x%x, retrying",
26432 				    geterror(wr_bp));
26433 			} else {
26434 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26435 				    "no resources for dumping; retrying");
26436 			}
26437 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
26438 			if (wr_bp->b_flags & B_ERROR) {
26439 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26440 				    "no resources for dumping; error code: "
26441 				    "0x%x, retrying\n", geterror(wr_bp));
26442 			}
26443 		} else {
26444 			if (wr_bp->b_flags & B_ERROR) {
26445 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26446 				    "no resources for dumping; "
26447 				    "error code: 0x%x, retries failed, "
26448 				    "giving up.\n", geterror(wr_bp));
26449 			} else {
26450 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26451 				    "no resources for dumping; "
26452 				    "retries failed, giving up.\n");
26453 			}
26454 			mutex_enter(SD_MUTEX(un));
26455 			Restore_state(un);
26456 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26457 				mutex_exit(SD_MUTEX(un));
26458 				scsi_free_consistent_buf(wr_bp);
26459 			} else {
26460 				mutex_exit(SD_MUTEX(un));
26461 			}
26462 			return (EIO);
26463 		}
26464 		drv_usecwait(10000);
26465 	}
26466 
26467 	if (un->un_partial_dma_supported == 1) {
26468 		/*
26469 		 * save the resid from PARTIAL_DMA
26470 		 */
26471 		dma_resid = wr_pktp->pkt_resid;
26472 		if (dma_resid != 0)
26473 			nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26474 		wr_pktp->pkt_resid = 0;
26475 	} else {
26476 		dma_resid = 0;
26477 	}
26478 
26479 	/* SunBug 1222170 */
26480 	wr_pktp->pkt_flags = FLAG_NOINTR;
26481 
26482 	err = EIO;
26483 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26484 
26485 		/*
26486 		 * Scsi_poll returns 0 (success) if the command completes and
26487 		 * the status block is STATUS_GOOD.  We should only check
26488 		 * errors if this condition is not true.  Even then we should
26489 		 * send our own request sense packet only if we have a check
26490 		 * condition and auto request sense has not been performed by
26491 		 * the hba.
26492 		 */
26493 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26494 
26495 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26496 		    (wr_pktp->pkt_resid == 0)) {
26497 			err = SD_SUCCESS;
26498 			break;
26499 		}
26500 
26501 		/*
26502 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
26503 		 */
26504 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26505 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26506 			    "Error while dumping state...Device is gone\n");
26507 			break;
26508 		}
26509 
26510 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26511 			SD_INFO(SD_LOG_DUMP, un,
26512 			    "sddump: write failed with CHECK, try # %d\n", i);
26513 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26514 				(void) sd_send_polled_RQS(un);
26515 			}
26516 
26517 			continue;
26518 		}
26519 
26520 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26521 			int reset_retval = 0;
26522 
26523 			SD_INFO(SD_LOG_DUMP, un,
26524 			    "sddump: write failed with BUSY, try # %d\n", i);
26525 
26526 			if (un->un_f_lun_reset_enabled == TRUE) {
26527 				reset_retval = scsi_reset(SD_ADDRESS(un),
26528 				    RESET_LUN);
26529 			}
26530 			if (reset_retval == 0) {
26531 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26532 			}
26533 			(void) sd_send_polled_RQS(un);
26534 
26535 		} else {
26536 			SD_INFO(SD_LOG_DUMP, un,
26537 			    "sddump: write failed with 0x%x, try # %d\n",
26538 			    SD_GET_PKT_STATUS(wr_pktp), i);
26539 			mutex_enter(SD_MUTEX(un));
26540 			sd_reset_target(un, wr_pktp);
26541 			mutex_exit(SD_MUTEX(un));
26542 		}
26543 
26544 		/*
26545 		 * If we are not getting anywhere with lun/target resets,
26546 		 * let's reset the bus.
26547 		 */
26548 		if (i == SD_NDUMP_RETRIES/2) {
26549 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26550 			(void) sd_send_polled_RQS(un);
26551 		}
26552 	}
26553 	}
26554 
26555 	scsi_destroy_pkt(wr_pktp);
26556 	mutex_enter(SD_MUTEX(un));
26557 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26558 		mutex_exit(SD_MUTEX(un));
26559 		scsi_free_consistent_buf(wr_bp);
26560 	} else {
26561 		mutex_exit(SD_MUTEX(un));
26562 	}
26563 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26564 	return (err);
26565 }
26566 
26567 /*
26568  *    Function: sd_scsi_poll()
26569  *
26570  * Description: This is a wrapper for the scsi_poll call.
26571  *
26572  *   Arguments: sd_lun - The unit structure
26573  *              scsi_pkt - The scsi packet being sent to the device.
26574  *
26575  * Return Code: 0 - Command completed successfully with good status
26576  *             -1 - Command failed.  This could indicate a check condition
26577  *                  or other status value requiring recovery action.
26578  *
26579  * NOTE: This code is only called off sddump().
26580  */
26581 
26582 static int
26583 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26584 {
26585 	int status;
26586 
26587 	ASSERT(un != NULL);
26588 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26589 	ASSERT(pktp != NULL);
26590 
26591 	status = SD_SUCCESS;
26592 
26593 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26594 		pktp->pkt_flags |= un->un_tagflags;
26595 		pktp->pkt_flags &= ~FLAG_NODISCON;
26596 	}
26597 
26598 	status = sd_ddi_scsi_poll(pktp);
26599 	/*
26600 	 * Scsi_poll returns 0 (success) if the command completes and the
26601 	 * status block is STATUS_GOOD.  We should only check errors if this
26602 	 * condition is not true.  Even then we should send our own request
26603 	 * sense packet only if we have a check condition and auto
26604 	 * request sense has not been performed by the hba.
26605 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26606 	 */
26607 	if ((status != SD_SUCCESS) &&
26608 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26609 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26610 	    (pktp->pkt_reason != CMD_DEV_GONE))
26611 		(void) sd_send_polled_RQS(un);
26612 
26613 	return (status);
26614 }
26615 
26616 /*
26617  *    Function: sd_send_polled_RQS()
26618  *
26619  * Description: This sends the request sense command to a device.
26620  *
26621  *   Arguments: sd_lun - The unit structure
26622  *
26623  * Return Code: 0 - Command completed successfully with good status
26624  *             -1 - Command failed.
26625  *
26626  */
26627 
26628 static int
26629 sd_send_polled_RQS(struct sd_lun *un)
26630 {
26631 	int	ret_val;
26632 	struct	scsi_pkt	*rqs_pktp;
26633 	struct	buf		*rqs_bp;
26634 
26635 	ASSERT(un != NULL);
26636 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26637 
26638 	ret_val = SD_SUCCESS;
26639 
26640 	rqs_pktp = un->un_rqs_pktp;
26641 	rqs_bp	 = un->un_rqs_bp;
26642 
26643 	mutex_enter(SD_MUTEX(un));
26644 
26645 	if (un->un_sense_isbusy) {
26646 		ret_val = SD_FAILURE;
26647 		mutex_exit(SD_MUTEX(un));
26648 		return (ret_val);
26649 	}
26650 
26651 	/*
26652 	 * If the request sense buffer (and packet) is not in use,
26653 	 * let's set the un_sense_isbusy and send our packet
26654 	 */
26655 	un->un_sense_isbusy 	= 1;
26656 	rqs_pktp->pkt_resid  	= 0;
26657 	rqs_pktp->pkt_reason 	= 0;
26658 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
26659 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26660 
26661 	mutex_exit(SD_MUTEX(un));
26662 
26663 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26664 	    " 0x%p\n", rqs_bp->b_un.b_addr);
26665 
26666 	/*
26667 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26668 	 * axle - it has a call into us!
26669 	 */
26670 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26671 		SD_INFO(SD_LOG_COMMON, un,
26672 		    "sd_send_polled_RQS: RQS failed\n");
26673 	}
26674 
26675 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26676 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26677 
26678 	mutex_enter(SD_MUTEX(un));
26679 	un->un_sense_isbusy = 0;
26680 	mutex_exit(SD_MUTEX(un));
26681 
26682 	return (ret_val);
26683 }
26684 
26685 /*
26686  * Defines needed for localized version of the scsi_poll routine.
26687  */
26688 #define	CSEC		10000			/* usecs */
26689 #define	SEC_TO_CSEC	(1000000/CSEC)
26690 
26691 /*
26692  *    Function: sd_ddi_scsi_poll()
26693  *
26694  * Description: Localized version of the scsi_poll routine.  The purpose is to
26695  *		send a scsi_pkt to a device as a polled command.  This version
26696  *		is to ensure more robust handling of transport errors.
26697  *		Specifically this routine cures not ready, coming ready
26698  *		transition for power up and reset of sonoma's.  This can take
26699  *		up to 45 seconds for power-on and 20 seconds for reset of a
26700  * 		sonoma lun.
26701  *
26702  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26703  *
26704  * Return Code: 0 - Command completed successfully with good status
26705  *             -1 - Command failed.
26706  *
26707  * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26708  * be fixed (removing this code), we need to determine how to handle the
26709  * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26710  *
26711  * NOTE: This code is only called off sddump().
26712  */
26713 static int
26714 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26715 {
26716 	int			rval = -1;
26717 	int			savef;
26718 	long			savet;
26719 	void			(*savec)();
26720 	int			timeout;
26721 	int			busy_count;
26722 	int			poll_delay;
26723 	int			rc;
26724 	uint8_t			*sensep;
26725 	struct scsi_arq_status	*arqstat;
26726 	extern int		do_polled_io;
26727 
26728 	ASSERT(pkt->pkt_scbp);
26729 
26730 	/*
26731 	 * save old flags..
26732 	 */
26733 	savef = pkt->pkt_flags;
26734 	savec = pkt->pkt_comp;
26735 	savet = pkt->pkt_time;
26736 
26737 	pkt->pkt_flags |= FLAG_NOINTR;
26738 
26739 	/*
26740 	 * XXX there is nothing in the SCSA spec that states that we should not
26741 	 * do a callback for polled cmds; however, removing this will break sd
26742 	 * and probably other target drivers
26743 	 */
26744 	pkt->pkt_comp = NULL;
26745 
26746 	/*
26747 	 * we don't like a polled command without timeout.
26748 	 * 60 seconds seems long enough.
26749 	 */
26750 	if (pkt->pkt_time == 0)
26751 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
26752 
26753 	/*
26754 	 * Send polled cmd.
26755 	 *
26756 	 * We do some error recovery for various errors.  Tran_busy,
26757 	 * queue full, and non-dispatched commands are retried every 10 msec.
26758 	 * as they are typically transient failures.  Busy status and Not
26759 	 * Ready are retried every second as this status takes a while to
26760 	 * change.
26761 	 */
26762 	timeout = pkt->pkt_time * SEC_TO_CSEC;
26763 
26764 	for (busy_count = 0; busy_count < timeout; busy_count++) {
26765 		/*
26766 		 * Initialize pkt status variables.
26767 		 */
26768 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26769 
26770 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26771 			if (rc != TRAN_BUSY) {
26772 				/* Transport failed - give up. */
26773 				break;
26774 			} else {
26775 				/* Transport busy - try again. */
26776 				poll_delay = 1 * CSEC;		/* 10 msec. */
26777 			}
26778 		} else {
26779 			/*
26780 			 * Transport accepted - check pkt status.
26781 			 */
26782 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
26783 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26784 			    (rc == STATUS_CHECK) &&
26785 			    (pkt->pkt_state & STATE_ARQ_DONE)) {
26786 				arqstat =
26787 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26788 				sensep = (uint8_t *)&arqstat->sts_sensedata;
26789 			} else {
26790 				sensep = NULL;
26791 			}
26792 
26793 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26794 			    (rc == STATUS_GOOD)) {
26795 				/* No error - we're done */
26796 				rval = 0;
26797 				break;
26798 
26799 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26800 				/* Lost connection - give up */
26801 				break;
26802 
26803 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26804 			    (pkt->pkt_state == 0)) {
26805 				/* Pkt not dispatched - try again. */
26806 				poll_delay = 1 * CSEC;		/* 10 msec. */
26807 
26808 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26809 			    (rc == STATUS_QFULL)) {
26810 				/* Queue full - try again. */
26811 				poll_delay = 1 * CSEC;		/* 10 msec. */
26812 
26813 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26814 			    (rc == STATUS_BUSY)) {
26815 				/* Busy - try again. */
26816 				poll_delay = 100 * CSEC;	/* 1 sec. */
26817 				busy_count += (SEC_TO_CSEC - 1);
26818 
26819 			} else if ((sensep != NULL) &&
26820 			    (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26821 				/*
26822 				 * Unit Attention - try again.
26823 				 * Pretend it took 1 sec.
26824 				 * NOTE: 'continue' avoids poll_delay
26825 				 */
26826 				busy_count += (SEC_TO_CSEC - 1);
26827 				continue;
26828 
26829 			} else if ((sensep != NULL) &&
26830 			    (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26831 			    (scsi_sense_asc(sensep) == 0x04) &&
26832 			    (scsi_sense_ascq(sensep) == 0x01)) {
26833 				/*
26834 				 * Not ready -> ready - try again.
26835 				 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26836 				 * ...same as STATUS_BUSY
26837 				 */
26838 				poll_delay = 100 * CSEC;	/* 1 sec. */
26839 				busy_count += (SEC_TO_CSEC - 1);
26840 
26841 			} else {
26842 				/* BAD status - give up. */
26843 				break;
26844 			}
26845 		}
26846 
26847 		if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26848 		    !do_polled_io) {
26849 			delay(drv_usectohz(poll_delay));
26850 		} else {
26851 			/* we busy wait during cpr_dump or interrupt threads */
26852 			drv_usecwait(poll_delay);
26853 		}
26854 	}
26855 
26856 	pkt->pkt_flags = savef;
26857 	pkt->pkt_comp = savec;
26858 	pkt->pkt_time = savet;
26859 
26860 	/* return on error */
26861 	if (rval)
26862 		return (rval);
26863 
26864 	/*
26865 	 * This is not a performance critical code path.
26866 	 *
26867 	 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26868 	 * issues associated with looking at DMA memory prior to
26869 	 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26870 	 */
26871 	scsi_sync_pkt(pkt);
26872 	return (0);
26873 }
26874 
26875 
26876 
26877 /*
26878  *    Function: sd_persistent_reservation_in_read_keys
26879  *
26880  * Description: This routine is the driver entry point for handling CD-ROM
26881  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26882  *		by sending the SCSI-3 PRIN commands to the device.
26883  *		Processes the read keys command response by copying the
26884  *		reservation key information into the user provided buffer.
26885  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26886  *
26887  *   Arguments: un   -  Pointer to soft state struct for the target.
26888  *		usrp -	user provided pointer to multihost Persistent In Read
26889  *			Keys structure (mhioc_inkeys_t)
26890  *		flag -	this argument is a pass through to ddi_copyxxx()
26891  *			directly from the mode argument of ioctl().
26892  *
26893  * Return Code: 0   - Success
26894  *		EACCES
26895  *		ENOTSUP
26896  *		errno return code from sd_send_scsi_cmd()
26897  *
26898  *     Context: Can sleep. Does not return until command is completed.
26899  */
26900 
26901 static int
26902 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26903     mhioc_inkeys_t *usrp, int flag)
26904 {
26905 #ifdef _MULTI_DATAMODEL
26906 	struct mhioc_key_list32	li32;
26907 #endif
26908 	sd_prin_readkeys_t	*in;
26909 	mhioc_inkeys_t		*ptr;
26910 	mhioc_key_list_t	li;
26911 	uchar_t			*data_bufp = NULL;
26912 	int 			data_len = 0;
26913 	int			rval = 0;
26914 	size_t			copysz = 0;
26915 	sd_ssc_t		*ssc;
26916 
26917 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26918 		return (EINVAL);
26919 	}
26920 	bzero(&li, sizeof (mhioc_key_list_t));
26921 
26922 	ssc = sd_ssc_init(un);
26923 
26924 	/*
26925 	 * Get the listsize from user
26926 	 */
26927 #ifdef _MULTI_DATAMODEL
26928 	switch (ddi_model_convert_from(flag & FMODELS)) {
26929 	case DDI_MODEL_ILP32:
26930 		copysz = sizeof (struct mhioc_key_list32);
26931 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26932 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26933 			    "sd_persistent_reservation_in_read_keys: "
26934 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26935 			rval = EFAULT;
26936 			goto done;
26937 		}
26938 		li.listsize = li32.listsize;
26939 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26940 		break;
26941 
26942 	case DDI_MODEL_NONE:
26943 		copysz = sizeof (mhioc_key_list_t);
26944 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26945 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26946 			    "sd_persistent_reservation_in_read_keys: "
26947 			    "failed ddi_copyin: mhioc_key_list_t\n");
26948 			rval = EFAULT;
26949 			goto done;
26950 		}
26951 		break;
26952 	}
26953 
26954 #else /* ! _MULTI_DATAMODEL */
26955 	copysz = sizeof (mhioc_key_list_t);
26956 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26957 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26958 		    "sd_persistent_reservation_in_read_keys: "
26959 		    "failed ddi_copyin: mhioc_key_list_t\n");
26960 		rval = EFAULT;
26961 		goto done;
26962 	}
26963 #endif
26964 
26965 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26966 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26967 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26968 
26969 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26970 	    data_len, data_bufp);
26971 	if (rval != 0) {
26972 		if (rval == EIO)
26973 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26974 		else
26975 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26976 		goto done;
26977 	}
26978 	in = (sd_prin_readkeys_t *)data_bufp;
26979 	ptr->generation = BE_32(in->generation);
26980 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26981 
26982 	/*
26983 	 * Return the min(listsize, listlen) keys
26984 	 */
26985 #ifdef _MULTI_DATAMODEL
26986 
26987 	switch (ddi_model_convert_from(flag & FMODELS)) {
26988 	case DDI_MODEL_ILP32:
26989 		li32.listlen = li.listlen;
26990 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26991 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26992 			    "sd_persistent_reservation_in_read_keys: "
26993 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26994 			rval = EFAULT;
26995 			goto done;
26996 		}
26997 		break;
26998 
26999 	case DDI_MODEL_NONE:
27000 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
27001 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27002 			    "sd_persistent_reservation_in_read_keys: "
27003 			    "failed ddi_copyout: mhioc_key_list_t\n");
27004 			rval = EFAULT;
27005 			goto done;
27006 		}
27007 		break;
27008 	}
27009 
27010 #else /* ! _MULTI_DATAMODEL */
27011 
27012 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
27013 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27014 		    "sd_persistent_reservation_in_read_keys: "
27015 		    "failed ddi_copyout: mhioc_key_list_t\n");
27016 		rval = EFAULT;
27017 		goto done;
27018 	}
27019 
27020 #endif /* _MULTI_DATAMODEL */
27021 
27022 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
27023 	    li.listsize * MHIOC_RESV_KEY_SIZE);
27024 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
27025 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27026 		    "sd_persistent_reservation_in_read_keys: "
27027 		    "failed ddi_copyout: keylist\n");
27028 		rval = EFAULT;
27029 	}
27030 done:
27031 	sd_ssc_fini(ssc);
27032 	kmem_free(data_bufp, data_len);
27033 	return (rval);
27034 }
27035 
27036 
27037 /*
27038  *    Function: sd_persistent_reservation_in_read_resv
27039  *
27040  * Description: This routine is the driver entry point for handling CD-ROM
27041  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
27042  *		by sending the SCSI-3 PRIN commands to the device.
27043  *		Process the read persistent reservations command response by
27044  *		copying the reservation information into the user provided
27045  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
27046  *
27047  *   Arguments: un   -  Pointer to soft state struct for the target.
27048  *		usrp -	user provided pointer to multihost Persistent In Read
27049  *			Keys structure (mhioc_inkeys_t)
27050  *		flag -	this argument is a pass through to ddi_copyxxx()
27051  *			directly from the mode argument of ioctl().
27052  *
27053  * Return Code: 0   - Success
27054  *		EACCES
27055  *		ENOTSUP
27056  *		errno return code from sd_send_scsi_cmd()
27057  *
27058  *     Context: Can sleep. Does not return until command is completed.
27059  */
27060 
27061 static int
27062 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
27063     mhioc_inresvs_t *usrp, int flag)
27064 {
27065 #ifdef _MULTI_DATAMODEL
27066 	struct mhioc_resv_desc_list32 resvlist32;
27067 #endif
27068 	sd_prin_readresv_t	*in;
27069 	mhioc_inresvs_t		*ptr;
27070 	sd_readresv_desc_t	*readresv_ptr;
27071 	mhioc_resv_desc_list_t	resvlist;
27072 	mhioc_resv_desc_t 	resvdesc;
27073 	uchar_t			*data_bufp = NULL;
27074 	int 			data_len;
27075 	int			rval = 0;
27076 	int			i;
27077 	size_t			copysz = 0;
27078 	mhioc_resv_desc_t	*bufp;
27079 	sd_ssc_t		*ssc;
27080 
27081 	if ((ptr = usrp) == NULL) {
27082 		return (EINVAL);
27083 	}
27084 
27085 	ssc = sd_ssc_init(un);
27086 
27087 	/*
27088 	 * Get the listsize from user
27089 	 */
27090 #ifdef _MULTI_DATAMODEL
27091 	switch (ddi_model_convert_from(flag & FMODELS)) {
27092 	case DDI_MODEL_ILP32:
27093 		copysz = sizeof (struct mhioc_resv_desc_list32);
27094 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
27095 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27096 			    "sd_persistent_reservation_in_read_resv: "
27097 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
27098 			rval = EFAULT;
27099 			goto done;
27100 		}
27101 		resvlist.listsize = resvlist32.listsize;
27102 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
27103 		break;
27104 
27105 	case DDI_MODEL_NONE:
27106 		copysz = sizeof (mhioc_resv_desc_list_t);
27107 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
27108 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27109 			    "sd_persistent_reservation_in_read_resv: "
27110 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
27111 			rval = EFAULT;
27112 			goto done;
27113 		}
27114 		break;
27115 	}
27116 #else /* ! _MULTI_DATAMODEL */
27117 	copysz = sizeof (mhioc_resv_desc_list_t);
27118 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
27119 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27120 		    "sd_persistent_reservation_in_read_resv: "
27121 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
27122 		rval = EFAULT;
27123 		goto done;
27124 	}
27125 #endif /* ! _MULTI_DATAMODEL */
27126 
27127 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
27128 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
27129 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
27130 
27131 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
27132 	    data_len, data_bufp);
27133 	if (rval != 0) {
27134 		if (rval == EIO)
27135 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
27136 		else
27137 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
27138 		goto done;
27139 	}
27140 	in = (sd_prin_readresv_t *)data_bufp;
27141 	ptr->generation = BE_32(in->generation);
27142 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
27143 
27144 	/*
27145 	 * Return the min(listsize, listlen( keys
27146 	 */
27147 #ifdef _MULTI_DATAMODEL
27148 
27149 	switch (ddi_model_convert_from(flag & FMODELS)) {
27150 	case DDI_MODEL_ILP32:
27151 		resvlist32.listlen = resvlist.listlen;
27152 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
27153 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27154 			    "sd_persistent_reservation_in_read_resv: "
27155 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27156 			rval = EFAULT;
27157 			goto done;
27158 		}
27159 		break;
27160 
27161 	case DDI_MODEL_NONE:
27162 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
27163 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27164 			    "sd_persistent_reservation_in_read_resv: "
27165 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27166 			rval = EFAULT;
27167 			goto done;
27168 		}
27169 		break;
27170 	}
27171 
27172 #else /* ! _MULTI_DATAMODEL */
27173 
27174 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
27175 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27176 		    "sd_persistent_reservation_in_read_resv: "
27177 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27178 		rval = EFAULT;
27179 		goto done;
27180 	}
27181 
27182 #endif /* ! _MULTI_DATAMODEL */
27183 
27184 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
27185 	bufp = resvlist.list;
27186 	copysz = sizeof (mhioc_resv_desc_t);
27187 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
27188 	    i++, readresv_ptr++, bufp++) {
27189 
27190 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
27191 		    MHIOC_RESV_KEY_SIZE);
27192 		resvdesc.type  = readresv_ptr->type;
27193 		resvdesc.scope = readresv_ptr->scope;
27194 		resvdesc.scope_specific_addr =
27195 		    BE_32(readresv_ptr->scope_specific_addr);
27196 
27197 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
27198 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27199 			    "sd_persistent_reservation_in_read_resv: "
27200 			    "failed ddi_copyout: resvlist\n");
27201 			rval = EFAULT;
27202 			goto done;
27203 		}
27204 	}
27205 done:
27206 	sd_ssc_fini(ssc);
27207 	/* only if data_bufp is allocated, we need to free it */
27208 	if (data_bufp) {
27209 		kmem_free(data_bufp, data_len);
27210 	}
27211 	return (rval);
27212 }
27213 
27214 
27215 /*
27216  *    Function: sr_change_blkmode()
27217  *
27218  * Description: This routine is the driver entry point for handling CD-ROM
27219  *		block mode ioctl requests. Support for returning and changing
27220  *		the current block size in use by the device is implemented. The
27221  *		LBA size is changed via a MODE SELECT Block Descriptor.
27222  *
27223  *		This routine issues a mode sense with an allocation length of
27224  *		12 bytes for the mode page header and a single block descriptor.
27225  *
27226  *   Arguments: dev - the device 'dev_t'
27227  *		cmd - the request type; one of CDROMGBLKMODE (get) or
27228  *		      CDROMSBLKMODE (set)
27229  *		data - current block size or requested block size
27230  *		flag - this argument is a pass through to ddi_copyxxx() directly
27231  *		       from the mode argument of ioctl().
27232  *
27233  * Return Code: the code returned by sd_send_scsi_cmd()
27234  *		EINVAL if invalid arguments are provided
27235  *		EFAULT if ddi_copyxxx() fails
27236  *		ENXIO if fail ddi_get_soft_state
27237  *		EIO if invalid mode sense block descriptor length
27238  *
27239  */
27240 
27241 static int
27242 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
27243 {
27244 	struct sd_lun			*un = NULL;
27245 	struct mode_header		*sense_mhp, *select_mhp;
27246 	struct block_descriptor		*sense_desc, *select_desc;
27247 	int				current_bsize;
27248 	int				rval = EINVAL;
27249 	uchar_t				*sense = NULL;
27250 	uchar_t				*select = NULL;
27251 	sd_ssc_t			*ssc;
27252 
27253 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
27254 
27255 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27256 		return (ENXIO);
27257 	}
27258 
27259 	/*
27260 	 * The block length is changed via the Mode Select block descriptor, the
27261 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
27262 	 * required as part of this routine. Therefore the mode sense allocation
27263 	 * length is specified to be the length of a mode page header and a
27264 	 * block descriptor.
27265 	 */
27266 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27267 
27268 	ssc = sd_ssc_init(un);
27269 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
27270 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
27271 	sd_ssc_fini(ssc);
27272 	if (rval != 0) {
27273 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27274 		    "sr_change_blkmode: Mode Sense Failed\n");
27275 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27276 		return (rval);
27277 	}
27278 
27279 	/* Check the block descriptor len to handle only 1 block descriptor */
27280 	sense_mhp = (struct mode_header *)sense;
27281 	if ((sense_mhp->bdesc_length == 0) ||
27282 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
27283 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27284 		    "sr_change_blkmode: Mode Sense returned invalid block"
27285 		    " descriptor length\n");
27286 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27287 		return (EIO);
27288 	}
27289 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
27290 	current_bsize = ((sense_desc->blksize_hi << 16) |
27291 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
27292 
27293 	/* Process command */
27294 	switch (cmd) {
27295 	case CDROMGBLKMODE:
27296 		/* Return the block size obtained during the mode sense */
27297 		if (ddi_copyout(&current_bsize, (void *)data,
27298 		    sizeof (int), flag) != 0)
27299 			rval = EFAULT;
27300 		break;
27301 	case CDROMSBLKMODE:
27302 		/* Validate the requested block size */
27303 		switch (data) {
27304 		case CDROM_BLK_512:
27305 		case CDROM_BLK_1024:
27306 		case CDROM_BLK_2048:
27307 		case CDROM_BLK_2056:
27308 		case CDROM_BLK_2336:
27309 		case CDROM_BLK_2340:
27310 		case CDROM_BLK_2352:
27311 		case CDROM_BLK_2368:
27312 		case CDROM_BLK_2448:
27313 		case CDROM_BLK_2646:
27314 		case CDROM_BLK_2647:
27315 			break;
27316 		default:
27317 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27318 			    "sr_change_blkmode: "
27319 			    "Block Size '%ld' Not Supported\n", data);
27320 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27321 			return (EINVAL);
27322 		}
27323 
27324 		/*
27325 		 * The current block size matches the requested block size so
27326 		 * there is no need to send the mode select to change the size
27327 		 */
27328 		if (current_bsize == data) {
27329 			break;
27330 		}
27331 
27332 		/* Build the select data for the requested block size */
27333 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27334 		select_mhp = (struct mode_header *)select;
27335 		select_desc =
27336 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
27337 		/*
27338 		 * The LBA size is changed via the block descriptor, so the
27339 		 * descriptor is built according to the user data
27340 		 */
27341 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
27342 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
27343 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
27344 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
27345 
27346 		/* Send the mode select for the requested block size */
27347 		ssc = sd_ssc_init(un);
27348 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
27349 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27350 		    SD_PATH_STANDARD);
27351 		sd_ssc_fini(ssc);
27352 		if (rval != 0) {
27353 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27354 			    "sr_change_blkmode: Mode Select Failed\n");
27355 			/*
27356 			 * The mode select failed for the requested block size,
27357 			 * so reset the data for the original block size and
27358 			 * send it to the target. The error is indicated by the
27359 			 * return value for the failed mode select.
27360 			 */
27361 			select_desc->blksize_hi  = sense_desc->blksize_hi;
27362 			select_desc->blksize_mid = sense_desc->blksize_mid;
27363 			select_desc->blksize_lo  = sense_desc->blksize_lo;
27364 			ssc = sd_ssc_init(un);
27365 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
27366 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27367 			    SD_PATH_STANDARD);
27368 			sd_ssc_fini(ssc);
27369 		} else {
27370 			ASSERT(!mutex_owned(SD_MUTEX(un)));
27371 			mutex_enter(SD_MUTEX(un));
27372 			sd_update_block_info(un, (uint32_t)data, 0);
27373 			mutex_exit(SD_MUTEX(un));
27374 		}
27375 		break;
27376 	default:
27377 		/* should not reach here, but check anyway */
27378 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27379 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
27380 		rval = EINVAL;
27381 		break;
27382 	}
27383 
27384 	if (select) {
27385 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
27386 	}
27387 	if (sense) {
27388 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27389 	}
27390 	return (rval);
27391 }
27392 
27393 
27394 /*
27395  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27396  * implement driver support for getting and setting the CD speed. The command
27397  * set used will be based on the device type. If the device has not been
27398  * identified as MMC the Toshiba vendor specific mode page will be used. If
27399  * the device is MMC but does not support the Real Time Streaming feature
27400  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27401  * be used to read the speed.
27402  */
27403 
27404 /*
27405  *    Function: sr_change_speed()
27406  *
27407  * Description: This routine is the driver entry point for handling CD-ROM
27408  *		drive speed ioctl requests for devices supporting the Toshiba
27409  *		vendor specific drive speed mode page. Support for returning
27410  *		and changing the current drive speed in use by the device is
27411  *		implemented.
27412  *
27413  *   Arguments: dev - the device 'dev_t'
27414  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
27415  *		      CDROMSDRVSPEED (set)
27416  *		data - current drive speed or requested drive speed
27417  *		flag - this argument is a pass through to ddi_copyxxx() directly
27418  *		       from the mode argument of ioctl().
27419  *
27420  * Return Code: the code returned by sd_send_scsi_cmd()
27421  *		EINVAL if invalid arguments are provided
27422  *		EFAULT if ddi_copyxxx() fails
27423  *		ENXIO if fail ddi_get_soft_state
27424  *		EIO if invalid mode sense block descriptor length
27425  */
27426 
27427 static int
27428 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27429 {
27430 	struct sd_lun			*un = NULL;
27431 	struct mode_header		*sense_mhp, *select_mhp;
27432 	struct mode_speed		*sense_page, *select_page;
27433 	int				current_speed;
27434 	int				rval = EINVAL;
27435 	int				bd_len;
27436 	uchar_t				*sense = NULL;
27437 	uchar_t				*select = NULL;
27438 	sd_ssc_t			*ssc;
27439 
27440 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27441 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27442 		return (ENXIO);
27443 	}
27444 
27445 	/*
27446 	 * Note: The drive speed is being modified here according to a Toshiba
27447 	 * vendor specific mode page (0x31).
27448 	 */
27449 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27450 
27451 	ssc = sd_ssc_init(un);
27452 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
27453 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27454 	    SD_PATH_STANDARD);
27455 	sd_ssc_fini(ssc);
27456 	if (rval != 0) {
27457 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27458 		    "sr_change_speed: Mode Sense Failed\n");
27459 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27460 		return (rval);
27461 	}
27462 	sense_mhp  = (struct mode_header *)sense;
27463 
27464 	/* Check the block descriptor len to handle only 1 block descriptor */
27465 	bd_len = sense_mhp->bdesc_length;
27466 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27467 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27468 		    "sr_change_speed: Mode Sense returned invalid block "
27469 		    "descriptor length\n");
27470 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27471 		return (EIO);
27472 	}
27473 
27474 	sense_page = (struct mode_speed *)
27475 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27476 	current_speed = sense_page->speed;
27477 
27478 	/* Process command */
27479 	switch (cmd) {
27480 	case CDROMGDRVSPEED:
27481 		/* Return the drive speed obtained during the mode sense */
27482 		if (current_speed == 0x2) {
27483 			current_speed = CDROM_TWELVE_SPEED;
27484 		}
27485 		if (ddi_copyout(&current_speed, (void *)data,
27486 		    sizeof (int), flag) != 0) {
27487 			rval = EFAULT;
27488 		}
27489 		break;
27490 	case CDROMSDRVSPEED:
27491 		/* Validate the requested drive speed */
27492 		switch ((uchar_t)data) {
27493 		case CDROM_TWELVE_SPEED:
27494 			data = 0x2;
27495 			/*FALLTHROUGH*/
27496 		case CDROM_NORMAL_SPEED:
27497 		case CDROM_DOUBLE_SPEED:
27498 		case CDROM_QUAD_SPEED:
27499 		case CDROM_MAXIMUM_SPEED:
27500 			break;
27501 		default:
27502 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27503 			    "sr_change_speed: "
27504 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27505 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27506 			return (EINVAL);
27507 		}
27508 
27509 		/*
27510 		 * The current drive speed matches the requested drive speed so
27511 		 * there is no need to send the mode select to change the speed
27512 		 */
27513 		if (current_speed == data) {
27514 			break;
27515 		}
27516 
27517 		/* Build the select data for the requested drive speed */
27518 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27519 		select_mhp = (struct mode_header *)select;
27520 		select_mhp->bdesc_length = 0;
27521 		select_page =
27522 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27523 		select_page =
27524 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27525 		select_page->mode_page.code = CDROM_MODE_SPEED;
27526 		select_page->mode_page.length = 2;
27527 		select_page->speed = (uchar_t)data;
27528 
27529 		/* Send the mode select for the requested block size */
27530 		ssc = sd_ssc_init(un);
27531 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27532 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27533 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27534 		sd_ssc_fini(ssc);
27535 		if (rval != 0) {
27536 			/*
27537 			 * The mode select failed for the requested drive speed,
27538 			 * so reset the data for the original drive speed and
27539 			 * send it to the target. The error is indicated by the
27540 			 * return value for the failed mode select.
27541 			 */
27542 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27543 			    "sr_drive_speed: Mode Select Failed\n");
27544 			select_page->speed = sense_page->speed;
27545 			ssc = sd_ssc_init(un);
27546 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27547 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27548 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27549 			sd_ssc_fini(ssc);
27550 		}
27551 		break;
27552 	default:
27553 		/* should not reach here, but check anyway */
27554 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27555 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27556 		rval = EINVAL;
27557 		break;
27558 	}
27559 
27560 	if (select) {
27561 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27562 	}
27563 	if (sense) {
27564 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27565 	}
27566 
27567 	return (rval);
27568 }
27569 
27570 
27571 /*
27572  *    Function: sr_atapi_change_speed()
27573  *
27574  * Description: This routine is the driver entry point for handling CD-ROM
27575  *		drive speed ioctl requests for MMC devices that do not support
27576  *		the Real Time Streaming feature (0x107).
27577  *
27578  *		Note: This routine will use the SET SPEED command which may not
27579  *		be supported by all devices.
27580  *
27581  *   Arguments: dev- the device 'dev_t'
27582  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27583  *		     CDROMSDRVSPEED (set)
27584  *		data- current drive speed or requested drive speed
27585  *		flag- this argument is a pass through to ddi_copyxxx() directly
27586  *		      from the mode argument of ioctl().
27587  *
27588  * Return Code: the code returned by sd_send_scsi_cmd()
27589  *		EINVAL if invalid arguments are provided
27590  *		EFAULT if ddi_copyxxx() fails
27591  *		ENXIO if fail ddi_get_soft_state
27592  *		EIO if invalid mode sense block descriptor length
27593  */
27594 
27595 static int
27596 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27597 {
27598 	struct sd_lun			*un;
27599 	struct uscsi_cmd		*com = NULL;
27600 	struct mode_header_grp2		*sense_mhp;
27601 	uchar_t				*sense_page;
27602 	uchar_t				*sense = NULL;
27603 	char				cdb[CDB_GROUP5];
27604 	int				bd_len;
27605 	int				current_speed = 0;
27606 	int				max_speed = 0;
27607 	int				rval;
27608 	sd_ssc_t			*ssc;
27609 
27610 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27611 
27612 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27613 		return (ENXIO);
27614 	}
27615 
27616 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27617 
27618 	ssc = sd_ssc_init(un);
27619 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27620 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27621 	    SD_PATH_STANDARD);
27622 	sd_ssc_fini(ssc);
27623 	if (rval != 0) {
27624 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27625 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27626 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27627 		return (rval);
27628 	}
27629 
27630 	/* Check the block descriptor len to handle only 1 block descriptor */
27631 	sense_mhp = (struct mode_header_grp2 *)sense;
27632 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27633 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27634 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27635 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27636 		    "block descriptor length\n");
27637 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27638 		return (EIO);
27639 	}
27640 
27641 	/* Calculate the current and maximum drive speeds */
27642 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27643 	current_speed = (sense_page[14] << 8) | sense_page[15];
27644 	max_speed = (sense_page[8] << 8) | sense_page[9];
27645 
27646 	/* Process the command */
27647 	switch (cmd) {
27648 	case CDROMGDRVSPEED:
27649 		current_speed /= SD_SPEED_1X;
27650 		if (ddi_copyout(&current_speed, (void *)data,
27651 		    sizeof (int), flag) != 0)
27652 			rval = EFAULT;
27653 		break;
27654 	case CDROMSDRVSPEED:
27655 		/* Convert the speed code to KB/sec */
27656 		switch ((uchar_t)data) {
27657 		case CDROM_NORMAL_SPEED:
27658 			current_speed = SD_SPEED_1X;
27659 			break;
27660 		case CDROM_DOUBLE_SPEED:
27661 			current_speed = 2 * SD_SPEED_1X;
27662 			break;
27663 		case CDROM_QUAD_SPEED:
27664 			current_speed = 4 * SD_SPEED_1X;
27665 			break;
27666 		case CDROM_TWELVE_SPEED:
27667 			current_speed = 12 * SD_SPEED_1X;
27668 			break;
27669 		case CDROM_MAXIMUM_SPEED:
27670 			current_speed = 0xffff;
27671 			break;
27672 		default:
27673 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27674 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27675 			    (uchar_t)data);
27676 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27677 			return (EINVAL);
27678 		}
27679 
27680 		/* Check the request against the drive's max speed. */
27681 		if (current_speed != 0xffff) {
27682 			if (current_speed > max_speed) {
27683 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27684 				return (EINVAL);
27685 			}
27686 		}
27687 
27688 		/*
27689 		 * Build and send the SET SPEED command
27690 		 *
27691 		 * Note: The SET SPEED (0xBB) command used in this routine is
27692 		 * obsolete per the SCSI MMC spec but still supported in the
27693 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27694 		 * therefore the command is still implemented in this routine.
27695 		 */
27696 		bzero(cdb, sizeof (cdb));
27697 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27698 		cdb[2] = (uchar_t)(current_speed >> 8);
27699 		cdb[3] = (uchar_t)current_speed;
27700 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27701 		com->uscsi_cdb	   = (caddr_t)cdb;
27702 		com->uscsi_cdblen  = CDB_GROUP5;
27703 		com->uscsi_bufaddr = NULL;
27704 		com->uscsi_buflen  = 0;
27705 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27706 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27707 		break;
27708 	default:
27709 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27710 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27711 		rval = EINVAL;
27712 	}
27713 
27714 	if (sense) {
27715 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27716 	}
27717 	if (com) {
27718 		kmem_free(com, sizeof (*com));
27719 	}
27720 	return (rval);
27721 }
27722 
27723 
27724 /*
27725  *    Function: sr_pause_resume()
27726  *
27727  * Description: This routine is the driver entry point for handling CD-ROM
27728  *		pause/resume ioctl requests. This only affects the audio play
27729  *		operation.
27730  *
27731  *   Arguments: dev - the device 'dev_t'
27732  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27733  *		      for setting the resume bit of the cdb.
27734  *
27735  * Return Code: the code returned by sd_send_scsi_cmd()
27736  *		EINVAL if invalid mode specified
27737  *
27738  */
27739 
27740 static int
27741 sr_pause_resume(dev_t dev, int cmd)
27742 {
27743 	struct sd_lun		*un;
27744 	struct uscsi_cmd	*com;
27745 	char			cdb[CDB_GROUP1];
27746 	int			rval;
27747 
27748 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27749 		return (ENXIO);
27750 	}
27751 
27752 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27753 	bzero(cdb, CDB_GROUP1);
27754 	cdb[0] = SCMD_PAUSE_RESUME;
27755 	switch (cmd) {
27756 	case CDROMRESUME:
27757 		cdb[8] = 1;
27758 		break;
27759 	case CDROMPAUSE:
27760 		cdb[8] = 0;
27761 		break;
27762 	default:
27763 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27764 		    " Command '%x' Not Supported\n", cmd);
27765 		rval = EINVAL;
27766 		goto done;
27767 	}
27768 
27769 	com->uscsi_cdb    = cdb;
27770 	com->uscsi_cdblen = CDB_GROUP1;
27771 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27772 
27773 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27774 	    SD_PATH_STANDARD);
27775 
27776 done:
27777 	kmem_free(com, sizeof (*com));
27778 	return (rval);
27779 }
27780 
27781 
27782 /*
27783  *    Function: sr_play_msf()
27784  *
27785  * Description: This routine is the driver entry point for handling CD-ROM
27786  *		ioctl requests to output the audio signals at the specified
27787  *		starting address and continue the audio play until the specified
27788  *		ending address (CDROMPLAYMSF) The address is in Minute Second
27789  *		Frame (MSF) format.
27790  *
27791  *   Arguments: dev	- the device 'dev_t'
27792  *		data	- pointer to user provided audio msf structure,
27793  *		          specifying start/end addresses.
27794  *		flag	- this argument is a pass through to ddi_copyxxx()
27795  *		          directly from the mode argument of ioctl().
27796  *
27797  * Return Code: the code returned by sd_send_scsi_cmd()
27798  *		EFAULT if ddi_copyxxx() fails
27799  *		ENXIO if fail ddi_get_soft_state
27800  *		EINVAL if data pointer is NULL
27801  */
27802 
27803 static int
27804 sr_play_msf(dev_t dev, caddr_t data, int flag)
27805 {
27806 	struct sd_lun		*un;
27807 	struct uscsi_cmd	*com;
27808 	struct cdrom_msf	msf_struct;
27809 	struct cdrom_msf	*msf = &msf_struct;
27810 	char			cdb[CDB_GROUP1];
27811 	int			rval;
27812 
27813 	if (data == NULL) {
27814 		return (EINVAL);
27815 	}
27816 
27817 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27818 		return (ENXIO);
27819 	}
27820 
27821 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27822 		return (EFAULT);
27823 	}
27824 
27825 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27826 	bzero(cdb, CDB_GROUP1);
27827 	cdb[0] = SCMD_PLAYAUDIO_MSF;
27828 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
27829 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27830 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27831 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27832 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27833 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27834 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27835 	} else {
27836 		cdb[3] = msf->cdmsf_min0;
27837 		cdb[4] = msf->cdmsf_sec0;
27838 		cdb[5] = msf->cdmsf_frame0;
27839 		cdb[6] = msf->cdmsf_min1;
27840 		cdb[7] = msf->cdmsf_sec1;
27841 		cdb[8] = msf->cdmsf_frame1;
27842 	}
27843 	com->uscsi_cdb    = cdb;
27844 	com->uscsi_cdblen = CDB_GROUP1;
27845 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27846 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27847 	    SD_PATH_STANDARD);
27848 	kmem_free(com, sizeof (*com));
27849 	return (rval);
27850 }
27851 
27852 
27853 /*
27854  *    Function: sr_play_trkind()
27855  *
27856  * Description: This routine is the driver entry point for handling CD-ROM
27857  *		ioctl requests to output the audio signals at the specified
27858  *		starting address and continue the audio play until the specified
27859  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27860  *		format.
27861  *
27862  *   Arguments: dev	- the device 'dev_t'
27863  *		data	- pointer to user provided audio track/index structure,
27864  *		          specifying start/end addresses.
27865  *		flag	- this argument is a pass through to ddi_copyxxx()
27866  *		          directly from the mode argument of ioctl().
27867  *
27868  * Return Code: the code returned by sd_send_scsi_cmd()
27869  *		EFAULT if ddi_copyxxx() fails
27870  *		ENXIO if fail ddi_get_soft_state
27871  *		EINVAL if data pointer is NULL
27872  */
27873 
27874 static int
27875 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27876 {
27877 	struct cdrom_ti		ti_struct;
27878 	struct cdrom_ti		*ti = &ti_struct;
27879 	struct uscsi_cmd	*com = NULL;
27880 	char			cdb[CDB_GROUP1];
27881 	int			rval;
27882 
27883 	if (data == NULL) {
27884 		return (EINVAL);
27885 	}
27886 
27887 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27888 		return (EFAULT);
27889 	}
27890 
27891 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27892 	bzero(cdb, CDB_GROUP1);
27893 	cdb[0] = SCMD_PLAYAUDIO_TI;
27894 	cdb[4] = ti->cdti_trk0;
27895 	cdb[5] = ti->cdti_ind0;
27896 	cdb[7] = ti->cdti_trk1;
27897 	cdb[8] = ti->cdti_ind1;
27898 	com->uscsi_cdb    = cdb;
27899 	com->uscsi_cdblen = CDB_GROUP1;
27900 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27901 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27902 	    SD_PATH_STANDARD);
27903 	kmem_free(com, sizeof (*com));
27904 	return (rval);
27905 }
27906 
27907 
27908 /*
27909  *    Function: sr_read_all_subcodes()
27910  *
27911  * Description: This routine is the driver entry point for handling CD-ROM
27912  *		ioctl requests to return raw subcode data while the target is
27913  *		playing audio (CDROMSUBCODE).
27914  *
27915  *   Arguments: dev	- the device 'dev_t'
27916  *		data	- pointer to user provided cdrom subcode structure,
27917  *		          specifying the transfer length and address.
27918  *		flag	- this argument is a pass through to ddi_copyxxx()
27919  *		          directly from the mode argument of ioctl().
27920  *
27921  * Return Code: the code returned by sd_send_scsi_cmd()
27922  *		EFAULT if ddi_copyxxx() fails
27923  *		ENXIO if fail ddi_get_soft_state
27924  *		EINVAL if data pointer is NULL
27925  */
27926 
27927 static int
27928 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27929 {
27930 	struct sd_lun		*un = NULL;
27931 	struct uscsi_cmd	*com = NULL;
27932 	struct cdrom_subcode	*subcode = NULL;
27933 	int			rval;
27934 	size_t			buflen;
27935 	char			cdb[CDB_GROUP5];
27936 
27937 #ifdef _MULTI_DATAMODEL
27938 	/* To support ILP32 applications in an LP64 world */
27939 	struct cdrom_subcode32		cdrom_subcode32;
27940 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27941 #endif
27942 	if (data == NULL) {
27943 		return (EINVAL);
27944 	}
27945 
27946 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27947 		return (ENXIO);
27948 	}
27949 
27950 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27951 
27952 #ifdef _MULTI_DATAMODEL
27953 	switch (ddi_model_convert_from(flag & FMODELS)) {
27954 	case DDI_MODEL_ILP32:
27955 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27956 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27957 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27958 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27959 			return (EFAULT);
27960 		}
27961 		/* Convert the ILP32 uscsi data from the application to LP64 */
27962 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27963 		break;
27964 	case DDI_MODEL_NONE:
27965 		if (ddi_copyin(data, subcode,
27966 		    sizeof (struct cdrom_subcode), flag)) {
27967 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27968 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27969 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27970 			return (EFAULT);
27971 		}
27972 		break;
27973 	}
27974 #else /* ! _MULTI_DATAMODEL */
27975 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27976 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27977 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27978 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27979 		return (EFAULT);
27980 	}
27981 #endif /* _MULTI_DATAMODEL */
27982 
27983 	/*
27984 	 * Since MMC-2 expects max 3 bytes for length, check if the
27985 	 * length input is greater than 3 bytes
27986 	 */
27987 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27988 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27989 		    "sr_read_all_subcodes: "
27990 		    "cdrom transfer length too large: %d (limit %d)\n",
27991 		    subcode->cdsc_length, 0xFFFFFF);
27992 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27993 		return (EINVAL);
27994 	}
27995 
27996 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27997 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27998 	bzero(cdb, CDB_GROUP5);
27999 
28000 	if (un->un_f_mmc_cap == TRUE) {
28001 		cdb[0] = (char)SCMD_READ_CD;
28002 		cdb[2] = (char)0xff;
28003 		cdb[3] = (char)0xff;
28004 		cdb[4] = (char)0xff;
28005 		cdb[5] = (char)0xff;
28006 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
28007 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
28008 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
28009 		cdb[10] = 1;
28010 	} else {
28011 		/*
28012 		 * Note: A vendor specific command (0xDF) is being used here to
28013 		 * request a read of all subcodes.
28014 		 */
28015 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
28016 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
28017 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
28018 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
28019 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
28020 	}
28021 	com->uscsi_cdb	   = cdb;
28022 	com->uscsi_cdblen  = CDB_GROUP5;
28023 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
28024 	com->uscsi_buflen  = buflen;
28025 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28026 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28027 	    SD_PATH_STANDARD);
28028 	kmem_free(subcode, sizeof (struct cdrom_subcode));
28029 	kmem_free(com, sizeof (*com));
28030 	return (rval);
28031 }
28032 
28033 
28034 /*
28035  *    Function: sr_read_subchannel()
28036  *
28037  * Description: This routine is the driver entry point for handling CD-ROM
28038  *		ioctl requests to return the Q sub-channel data of the CD
28039  *		current position block. (CDROMSUBCHNL) The data includes the
28040  *		track number, index number, absolute CD-ROM address (LBA or MSF
28041  *		format per the user) , track relative CD-ROM address (LBA or MSF
28042  *		format per the user), control data and audio status.
28043  *
28044  *   Arguments: dev	- the device 'dev_t'
28045  *		data	- pointer to user provided cdrom sub-channel structure
28046  *		flag	- this argument is a pass through to ddi_copyxxx()
28047  *		          directly from the mode argument of ioctl().
28048  *
28049  * Return Code: the code returned by sd_send_scsi_cmd()
28050  *		EFAULT if ddi_copyxxx() fails
28051  *		ENXIO if fail ddi_get_soft_state
28052  *		EINVAL if data pointer is NULL
28053  */
28054 
28055 static int
28056 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
28057 {
28058 	struct sd_lun		*un;
28059 	struct uscsi_cmd	*com;
28060 	struct cdrom_subchnl	subchanel;
28061 	struct cdrom_subchnl	*subchnl = &subchanel;
28062 	char			cdb[CDB_GROUP1];
28063 	caddr_t			buffer;
28064 	int			rval;
28065 
28066 	if (data == NULL) {
28067 		return (EINVAL);
28068 	}
28069 
28070 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28071 	    (un->un_state == SD_STATE_OFFLINE)) {
28072 		return (ENXIO);
28073 	}
28074 
28075 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
28076 		return (EFAULT);
28077 	}
28078 
28079 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
28080 	bzero(cdb, CDB_GROUP1);
28081 	cdb[0] = SCMD_READ_SUBCHANNEL;
28082 	/* Set the MSF bit based on the user requested address format */
28083 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
28084 	/*
28085 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
28086 	 * returned
28087 	 */
28088 	cdb[2] = 0x40;
28089 	/*
28090 	 * Set byte 3 to specify the return data format. A value of 0x01
28091 	 * indicates that the CD-ROM current position should be returned.
28092 	 */
28093 	cdb[3] = 0x01;
28094 	cdb[8] = 0x10;
28095 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28096 	com->uscsi_cdb	   = cdb;
28097 	com->uscsi_cdblen  = CDB_GROUP1;
28098 	com->uscsi_bufaddr = buffer;
28099 	com->uscsi_buflen  = 16;
28100 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28101 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28102 	    SD_PATH_STANDARD);
28103 	if (rval != 0) {
28104 		kmem_free(buffer, 16);
28105 		kmem_free(com, sizeof (*com));
28106 		return (rval);
28107 	}
28108 
28109 	/* Process the returned Q sub-channel data */
28110 	subchnl->cdsc_audiostatus = buffer[1];
28111 	subchnl->cdsc_adr	= (buffer[5] & 0xF0) >> 4;
28112 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
28113 	subchnl->cdsc_trk	= buffer[6];
28114 	subchnl->cdsc_ind	= buffer[7];
28115 	if (subchnl->cdsc_format & CDROM_LBA) {
28116 		subchnl->cdsc_absaddr.lba =
28117 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
28118 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
28119 		subchnl->cdsc_reladdr.lba =
28120 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
28121 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
28122 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
28123 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
28124 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
28125 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
28126 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
28127 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
28128 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
28129 	} else {
28130 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
28131 		subchnl->cdsc_absaddr.msf.second = buffer[10];
28132 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
28133 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
28134 		subchnl->cdsc_reladdr.msf.second = buffer[14];
28135 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
28136 	}
28137 	kmem_free(buffer, 16);
28138 	kmem_free(com, sizeof (*com));
28139 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
28140 	    != 0) {
28141 		return (EFAULT);
28142 	}
28143 	return (rval);
28144 }
28145 
28146 
28147 /*
28148  *    Function: sr_read_tocentry()
28149  *
28150  * Description: This routine is the driver entry point for handling CD-ROM
28151  *		ioctl requests to read from the Table of Contents (TOC)
28152  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
28153  *		fields, the starting address (LBA or MSF format per the user)
28154  *		and the data mode if the user specified track is a data track.
28155  *
28156  *		Note: The READ HEADER (0x44) command used in this routine is
28157  *		obsolete per the SCSI MMC spec but still supported in the
28158  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
28159  *		therefore the command is still implemented in this routine.
28160  *
28161  *   Arguments: dev	- the device 'dev_t'
28162  *		data	- pointer to user provided toc entry structure,
28163  *			  specifying the track # and the address format
28164  *			  (LBA or MSF).
28165  *		flag	- this argument is a pass through to ddi_copyxxx()
28166  *		          directly from the mode argument of ioctl().
28167  *
28168  * Return Code: the code returned by sd_send_scsi_cmd()
28169  *		EFAULT if ddi_copyxxx() fails
28170  *		ENXIO if fail ddi_get_soft_state
28171  *		EINVAL if data pointer is NULL
28172  */
28173 
28174 static int
28175 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
28176 {
28177 	struct sd_lun		*un = NULL;
28178 	struct uscsi_cmd	*com;
28179 	struct cdrom_tocentry	toc_entry;
28180 	struct cdrom_tocentry	*entry = &toc_entry;
28181 	caddr_t			buffer;
28182 	int			rval;
28183 	char			cdb[CDB_GROUP1];
28184 
28185 	if (data == NULL) {
28186 		return (EINVAL);
28187 	}
28188 
28189 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28190 	    (un->un_state == SD_STATE_OFFLINE)) {
28191 		return (ENXIO);
28192 	}
28193 
28194 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
28195 		return (EFAULT);
28196 	}
28197 
28198 	/* Validate the requested track and address format */
28199 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
28200 		return (EINVAL);
28201 	}
28202 
28203 	if (entry->cdte_track == 0) {
28204 		return (EINVAL);
28205 	}
28206 
28207 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
28208 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28209 	bzero(cdb, CDB_GROUP1);
28210 
28211 	cdb[0] = SCMD_READ_TOC;
28212 	/* Set the MSF bit based on the user requested address format  */
28213 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
28214 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28215 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
28216 	} else {
28217 		cdb[6] = entry->cdte_track;
28218 	}
28219 
28220 	/*
28221 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
28222 	 * (4 byte TOC response header + 8 byte track descriptor)
28223 	 */
28224 	cdb[8] = 12;
28225 	com->uscsi_cdb	   = cdb;
28226 	com->uscsi_cdblen  = CDB_GROUP1;
28227 	com->uscsi_bufaddr = buffer;
28228 	com->uscsi_buflen  = 0x0C;
28229 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
28230 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28231 	    SD_PATH_STANDARD);
28232 	if (rval != 0) {
28233 		kmem_free(buffer, 12);
28234 		kmem_free(com, sizeof (*com));
28235 		return (rval);
28236 	}
28237 
28238 	/* Process the toc entry */
28239 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
28240 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
28241 	if (entry->cdte_format & CDROM_LBA) {
28242 		entry->cdte_addr.lba =
28243 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
28244 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
28245 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
28246 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
28247 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
28248 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
28249 		/*
28250 		 * Send a READ TOC command using the LBA address format to get
28251 		 * the LBA for the track requested so it can be used in the
28252 		 * READ HEADER request
28253 		 *
28254 		 * Note: The MSF bit of the READ HEADER command specifies the
28255 		 * output format. The block address specified in that command
28256 		 * must be in LBA format.
28257 		 */
28258 		cdb[1] = 0;
28259 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28260 		    SD_PATH_STANDARD);
28261 		if (rval != 0) {
28262 			kmem_free(buffer, 12);
28263 			kmem_free(com, sizeof (*com));
28264 			return (rval);
28265 		}
28266 	} else {
28267 		entry->cdte_addr.msf.minute	= buffer[9];
28268 		entry->cdte_addr.msf.second	= buffer[10];
28269 		entry->cdte_addr.msf.frame	= buffer[11];
28270 		/*
28271 		 * Send a READ TOC command using the LBA address format to get
28272 		 * the LBA for the track requested so it can be used in the
28273 		 * READ HEADER request
28274 		 *
28275 		 * Note: The MSF bit of the READ HEADER command specifies the
28276 		 * output format. The block address specified in that command
28277 		 * must be in LBA format.
28278 		 */
28279 		cdb[1] = 0;
28280 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28281 		    SD_PATH_STANDARD);
28282 		if (rval != 0) {
28283 			kmem_free(buffer, 12);
28284 			kmem_free(com, sizeof (*com));
28285 			return (rval);
28286 		}
28287 	}
28288 
28289 	/*
28290 	 * Build and send the READ HEADER command to determine the data mode of
28291 	 * the user specified track.
28292 	 */
28293 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
28294 	    (entry->cdte_track != CDROM_LEADOUT)) {
28295 		bzero(cdb, CDB_GROUP1);
28296 		cdb[0] = SCMD_READ_HEADER;
28297 		cdb[2] = buffer[8];
28298 		cdb[3] = buffer[9];
28299 		cdb[4] = buffer[10];
28300 		cdb[5] = buffer[11];
28301 		cdb[8] = 0x08;
28302 		com->uscsi_buflen = 0x08;
28303 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28304 		    SD_PATH_STANDARD);
28305 		if (rval == 0) {
28306 			entry->cdte_datamode = buffer[0];
28307 		} else {
28308 			/*
28309 			 * READ HEADER command failed, since this is
28310 			 * obsoleted in one spec, its better to return
28311 			 * -1 for an invlid track so that we can still
28312 			 * receive the rest of the TOC data.
28313 			 */
28314 			entry->cdte_datamode = (uchar_t)-1;
28315 		}
28316 	} else {
28317 		entry->cdte_datamode = (uchar_t)-1;
28318 	}
28319 
28320 	kmem_free(buffer, 12);
28321 	kmem_free(com, sizeof (*com));
28322 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
28323 		return (EFAULT);
28324 
28325 	return (rval);
28326 }
28327 
28328 
28329 /*
28330  *    Function: sr_read_tochdr()
28331  *
28332  * Description: This routine is the driver entry point for handling CD-ROM
28333  * 		ioctl requests to read the Table of Contents (TOC) header
28334  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
28335  *		and ending track numbers
28336  *
28337  *   Arguments: dev	- the device 'dev_t'
28338  *		data	- pointer to user provided toc header structure,
28339  *			  specifying the starting and ending track numbers.
28340  *		flag	- this argument is a pass through to ddi_copyxxx()
28341  *			  directly from the mode argument of ioctl().
28342  *
28343  * Return Code: the code returned by sd_send_scsi_cmd()
28344  *		EFAULT if ddi_copyxxx() fails
28345  *		ENXIO if fail ddi_get_soft_state
28346  *		EINVAL if data pointer is NULL
28347  */
28348 
28349 static int
28350 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
28351 {
28352 	struct sd_lun		*un;
28353 	struct uscsi_cmd	*com;
28354 	struct cdrom_tochdr	toc_header;
28355 	struct cdrom_tochdr	*hdr = &toc_header;
28356 	char			cdb[CDB_GROUP1];
28357 	int			rval;
28358 	caddr_t			buffer;
28359 
28360 	if (data == NULL) {
28361 		return (EINVAL);
28362 	}
28363 
28364 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28365 	    (un->un_state == SD_STATE_OFFLINE)) {
28366 		return (ENXIO);
28367 	}
28368 
28369 	buffer = kmem_zalloc(4, KM_SLEEP);
28370 	bzero(cdb, CDB_GROUP1);
28371 	cdb[0] = SCMD_READ_TOC;
28372 	/*
28373 	 * Specifying a track number of 0x00 in the READ TOC command indicates
28374 	 * that the TOC header should be returned
28375 	 */
28376 	cdb[6] = 0x00;
28377 	/*
28378 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28379 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28380 	 */
28381 	cdb[8] = 0x04;
28382 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28383 	com->uscsi_cdb	   = cdb;
28384 	com->uscsi_cdblen  = CDB_GROUP1;
28385 	com->uscsi_bufaddr = buffer;
28386 	com->uscsi_buflen  = 0x04;
28387 	com->uscsi_timeout = 300;
28388 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28389 
28390 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28391 	    SD_PATH_STANDARD);
28392 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28393 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28394 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28395 	} else {
28396 		hdr->cdth_trk0 = buffer[2];
28397 		hdr->cdth_trk1 = buffer[3];
28398 	}
28399 	kmem_free(buffer, 4);
28400 	kmem_free(com, sizeof (*com));
28401 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28402 		return (EFAULT);
28403 	}
28404 	return (rval);
28405 }
28406 
28407 
28408 /*
28409  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28410  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28411  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28412  * digital audio and extended architecture digital audio. These modes are
28413  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28414  * MMC specs.
28415  *
28416  * In addition to support for the various data formats these routines also
28417  * include support for devices that implement only the direct access READ
28418  * commands (0x08, 0x28), devices that implement the READ_CD commands
28419  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28420  * READ CDXA commands (0xD8, 0xDB)
28421  */
28422 
28423 /*
28424  *    Function: sr_read_mode1()
28425  *
28426  * Description: This routine is the driver entry point for handling CD-ROM
28427  *		ioctl read mode1 requests (CDROMREADMODE1).
28428  *
28429  *   Arguments: dev	- the device 'dev_t'
28430  *		data	- pointer to user provided cd read structure specifying
28431  *			  the lba buffer address and length.
28432  *		flag	- this argument is a pass through to ddi_copyxxx()
28433  *			  directly from the mode argument of ioctl().
28434  *
28435  * Return Code: the code returned by sd_send_scsi_cmd()
28436  *		EFAULT if ddi_copyxxx() fails
28437  *		ENXIO if fail ddi_get_soft_state
28438  *		EINVAL if data pointer is NULL
28439  */
28440 
28441 static int
28442 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28443 {
28444 	struct sd_lun		*un;
28445 	struct cdrom_read	mode1_struct;
28446 	struct cdrom_read	*mode1 = &mode1_struct;
28447 	int			rval;
28448 	sd_ssc_t		*ssc;
28449 
28450 #ifdef _MULTI_DATAMODEL
28451 	/* To support ILP32 applications in an LP64 world */
28452 	struct cdrom_read32	cdrom_read32;
28453 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28454 #endif /* _MULTI_DATAMODEL */
28455 
28456 	if (data == NULL) {
28457 		return (EINVAL);
28458 	}
28459 
28460 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28461 	    (un->un_state == SD_STATE_OFFLINE)) {
28462 		return (ENXIO);
28463 	}
28464 
28465 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28466 	    "sd_read_mode1: entry: un:0x%p\n", un);
28467 
28468 #ifdef _MULTI_DATAMODEL
28469 	switch (ddi_model_convert_from(flag & FMODELS)) {
28470 	case DDI_MODEL_ILP32:
28471 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28472 			return (EFAULT);
28473 		}
28474 		/* Convert the ILP32 uscsi data from the application to LP64 */
28475 		cdrom_read32tocdrom_read(cdrd32, mode1);
28476 		break;
28477 	case DDI_MODEL_NONE:
28478 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28479 			return (EFAULT);
28480 		}
28481 	}
28482 #else /* ! _MULTI_DATAMODEL */
28483 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28484 		return (EFAULT);
28485 	}
28486 #endif /* _MULTI_DATAMODEL */
28487 
28488 	ssc = sd_ssc_init(un);
28489 	rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
28490 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28491 	sd_ssc_fini(ssc);
28492 
28493 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28494 	    "sd_read_mode1: exit: un:0x%p\n", un);
28495 
28496 	return (rval);
28497 }
28498 
28499 
28500 /*
28501  *    Function: sr_read_cd_mode2()
28502  *
28503  * Description: This routine is the driver entry point for handling CD-ROM
28504  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28505  *		support the READ CD (0xBE) command or the 1st generation
28506  *		READ CD (0xD4) command.
28507  *
28508  *   Arguments: dev	- the device 'dev_t'
28509  *		data	- pointer to user provided cd read structure specifying
28510  *			  the lba buffer address and length.
28511  *		flag	- this argument is a pass through to ddi_copyxxx()
28512  *			  directly from the mode argument of ioctl().
28513  *
28514  * Return Code: the code returned by sd_send_scsi_cmd()
28515  *		EFAULT if ddi_copyxxx() fails
28516  *		ENXIO if fail ddi_get_soft_state
28517  *		EINVAL if data pointer is NULL
28518  */
28519 
28520 static int
28521 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28522 {
28523 	struct sd_lun		*un;
28524 	struct uscsi_cmd	*com;
28525 	struct cdrom_read	mode2_struct;
28526 	struct cdrom_read	*mode2 = &mode2_struct;
28527 	uchar_t			cdb[CDB_GROUP5];
28528 	int			nblocks;
28529 	int			rval;
28530 #ifdef _MULTI_DATAMODEL
28531 	/*  To support ILP32 applications in an LP64 world */
28532 	struct cdrom_read32	cdrom_read32;
28533 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28534 #endif /* _MULTI_DATAMODEL */
28535 
28536 	if (data == NULL) {
28537 		return (EINVAL);
28538 	}
28539 
28540 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28541 	    (un->un_state == SD_STATE_OFFLINE)) {
28542 		return (ENXIO);
28543 	}
28544 
28545 #ifdef _MULTI_DATAMODEL
28546 	switch (ddi_model_convert_from(flag & FMODELS)) {
28547 	case DDI_MODEL_ILP32:
28548 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28549 			return (EFAULT);
28550 		}
28551 		/* Convert the ILP32 uscsi data from the application to LP64 */
28552 		cdrom_read32tocdrom_read(cdrd32, mode2);
28553 		break;
28554 	case DDI_MODEL_NONE:
28555 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28556 			return (EFAULT);
28557 		}
28558 		break;
28559 	}
28560 
28561 #else /* ! _MULTI_DATAMODEL */
28562 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28563 		return (EFAULT);
28564 	}
28565 #endif /* _MULTI_DATAMODEL */
28566 
28567 	bzero(cdb, sizeof (cdb));
28568 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28569 		/* Read command supported by 1st generation atapi drives */
28570 		cdb[0] = SCMD_READ_CDD4;
28571 	} else {
28572 		/* Universal CD Access Command */
28573 		cdb[0] = SCMD_READ_CD;
28574 	}
28575 
28576 	/*
28577 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28578 	 */
28579 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28580 
28581 	/* set the start address */
28582 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28583 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28584 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28585 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28586 
28587 	/* set the transfer length */
28588 	nblocks = mode2->cdread_buflen / 2336;
28589 	cdb[6] = (uchar_t)(nblocks >> 16);
28590 	cdb[7] = (uchar_t)(nblocks >> 8);
28591 	cdb[8] = (uchar_t)nblocks;
28592 
28593 	/* set the filter bits */
28594 	cdb[9] = CDROM_READ_CD_USERDATA;
28595 
28596 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28597 	com->uscsi_cdb = (caddr_t)cdb;
28598 	com->uscsi_cdblen = sizeof (cdb);
28599 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28600 	com->uscsi_buflen = mode2->cdread_buflen;
28601 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28602 
28603 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28604 	    SD_PATH_STANDARD);
28605 	kmem_free(com, sizeof (*com));
28606 	return (rval);
28607 }
28608 
28609 
28610 /*
28611  *    Function: sr_read_mode2()
28612  *
28613  * Description: This routine is the driver entry point for handling CD-ROM
28614  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28615  *		do not support the READ CD (0xBE) command.
28616  *
28617  *   Arguments: dev	- the device 'dev_t'
28618  *		data	- pointer to user provided cd read structure specifying
28619  *			  the lba buffer address and length.
28620  *		flag	- this argument is a pass through to ddi_copyxxx()
28621  *			  directly from the mode argument of ioctl().
28622  *
28623  * Return Code: the code returned by sd_send_scsi_cmd()
28624  *		EFAULT if ddi_copyxxx() fails
28625  *		ENXIO if fail ddi_get_soft_state
28626  *		EINVAL if data pointer is NULL
28627  *		EIO if fail to reset block size
28628  *		EAGAIN if commands are in progress in the driver
28629  */
28630 
28631 static int
28632 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28633 {
28634 	struct sd_lun		*un;
28635 	struct cdrom_read	mode2_struct;
28636 	struct cdrom_read	*mode2 = &mode2_struct;
28637 	int			rval;
28638 	uint32_t		restore_blksize;
28639 	struct uscsi_cmd	*com;
28640 	uchar_t			cdb[CDB_GROUP0];
28641 	int			nblocks;
28642 
28643 #ifdef _MULTI_DATAMODEL
28644 	/* To support ILP32 applications in an LP64 world */
28645 	struct cdrom_read32	cdrom_read32;
28646 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28647 #endif /* _MULTI_DATAMODEL */
28648 
28649 	if (data == NULL) {
28650 		return (EINVAL);
28651 	}
28652 
28653 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28654 	    (un->un_state == SD_STATE_OFFLINE)) {
28655 		return (ENXIO);
28656 	}
28657 
28658 	/*
28659 	 * Because this routine will update the device and driver block size
28660 	 * being used we want to make sure there are no commands in progress.
28661 	 * If commands are in progress the user will have to try again.
28662 	 *
28663 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28664 	 * in sdioctl to protect commands from sdioctl through to the top of
28665 	 * sd_uscsi_strategy. See sdioctl for details.
28666 	 */
28667 	mutex_enter(SD_MUTEX(un));
28668 	if (un->un_ncmds_in_driver != 1) {
28669 		mutex_exit(SD_MUTEX(un));
28670 		return (EAGAIN);
28671 	}
28672 	mutex_exit(SD_MUTEX(un));
28673 
28674 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28675 	    "sd_read_mode2: entry: un:0x%p\n", un);
28676 
28677 #ifdef _MULTI_DATAMODEL
28678 	switch (ddi_model_convert_from(flag & FMODELS)) {
28679 	case DDI_MODEL_ILP32:
28680 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28681 			return (EFAULT);
28682 		}
28683 		/* Convert the ILP32 uscsi data from the application to LP64 */
28684 		cdrom_read32tocdrom_read(cdrd32, mode2);
28685 		break;
28686 	case DDI_MODEL_NONE:
28687 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28688 			return (EFAULT);
28689 		}
28690 		break;
28691 	}
28692 #else /* ! _MULTI_DATAMODEL */
28693 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28694 		return (EFAULT);
28695 	}
28696 #endif /* _MULTI_DATAMODEL */
28697 
28698 	/* Store the current target block size for restoration later */
28699 	restore_blksize = un->un_tgt_blocksize;
28700 
28701 	/* Change the device and soft state target block size to 2336 */
28702 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28703 		rval = EIO;
28704 		goto done;
28705 	}
28706 
28707 
28708 	bzero(cdb, sizeof (cdb));
28709 
28710 	/* set READ operation */
28711 	cdb[0] = SCMD_READ;
28712 
28713 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
28714 	mode2->cdread_lba >>= 2;
28715 
28716 	/* set the start address */
28717 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28718 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28719 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28720 
28721 	/* set the transfer length */
28722 	nblocks = mode2->cdread_buflen / 2336;
28723 	cdb[4] = (uchar_t)nblocks & 0xFF;
28724 
28725 	/* build command */
28726 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28727 	com->uscsi_cdb = (caddr_t)cdb;
28728 	com->uscsi_cdblen = sizeof (cdb);
28729 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28730 	com->uscsi_buflen = mode2->cdread_buflen;
28731 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28732 
28733 	/*
28734 	 * Issue SCSI command with user space address for read buffer.
28735 	 *
28736 	 * This sends the command through main channel in the driver.
28737 	 *
28738 	 * Since this is accessed via an IOCTL call, we go through the
28739 	 * standard path, so that if the device was powered down, then
28740 	 * it would be 'awakened' to handle the command.
28741 	 */
28742 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28743 	    SD_PATH_STANDARD);
28744 
28745 	kmem_free(com, sizeof (*com));
28746 
28747 	/* Restore the device and soft state target block size */
28748 	if (sr_sector_mode(dev, restore_blksize) != 0) {
28749 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28750 		    "can't do switch back to mode 1\n");
28751 		/*
28752 		 * If sd_send_scsi_READ succeeded we still need to report
28753 		 * an error because we failed to reset the block size
28754 		 */
28755 		if (rval == 0) {
28756 			rval = EIO;
28757 		}
28758 	}
28759 
28760 done:
28761 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28762 	    "sd_read_mode2: exit: un:0x%p\n", un);
28763 
28764 	return (rval);
28765 }
28766 
28767 
28768 /*
28769  *    Function: sr_sector_mode()
28770  *
28771  * Description: This utility function is used by sr_read_mode2 to set the target
28772  *		block size based on the user specified size. This is a legacy
28773  *		implementation based upon a vendor specific mode page
28774  *
28775  *   Arguments: dev	- the device 'dev_t'
28776  *		data	- flag indicating if block size is being set to 2336 or
28777  *			  512.
28778  *
28779  * Return Code: the code returned by sd_send_scsi_cmd()
28780  *		EFAULT if ddi_copyxxx() fails
28781  *		ENXIO if fail ddi_get_soft_state
28782  *		EINVAL if data pointer is NULL
28783  */
28784 
28785 static int
28786 sr_sector_mode(dev_t dev, uint32_t blksize)
28787 {
28788 	struct sd_lun	*un;
28789 	uchar_t		*sense;
28790 	uchar_t		*select;
28791 	int		rval;
28792 	sd_ssc_t	*ssc;
28793 
28794 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28795 	    (un->un_state == SD_STATE_OFFLINE)) {
28796 		return (ENXIO);
28797 	}
28798 
28799 	sense = kmem_zalloc(20, KM_SLEEP);
28800 
28801 	/* Note: This is a vendor specific mode page (0x81) */
28802 	ssc = sd_ssc_init(un);
28803 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28804 	    SD_PATH_STANDARD);
28805 	sd_ssc_fini(ssc);
28806 	if (rval != 0) {
28807 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28808 		    "sr_sector_mode: Mode Sense failed\n");
28809 		kmem_free(sense, 20);
28810 		return (rval);
28811 	}
28812 	select = kmem_zalloc(20, KM_SLEEP);
28813 	select[3] = 0x08;
28814 	select[10] = ((blksize >> 8) & 0xff);
28815 	select[11] = (blksize & 0xff);
28816 	select[12] = 0x01;
28817 	select[13] = 0x06;
28818 	select[14] = sense[14];
28819 	select[15] = sense[15];
28820 	if (blksize == SD_MODE2_BLKSIZE) {
28821 		select[14] |= 0x01;
28822 	}
28823 
28824 	ssc = sd_ssc_init(un);
28825 	rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28826 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28827 	sd_ssc_fini(ssc);
28828 	if (rval != 0) {
28829 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28830 		    "sr_sector_mode: Mode Select failed\n");
28831 	} else {
28832 		/*
28833 		 * Only update the softstate block size if we successfully
28834 		 * changed the device block mode.
28835 		 */
28836 		mutex_enter(SD_MUTEX(un));
28837 		sd_update_block_info(un, blksize, 0);
28838 		mutex_exit(SD_MUTEX(un));
28839 	}
28840 	kmem_free(sense, 20);
28841 	kmem_free(select, 20);
28842 	return (rval);
28843 }
28844 
28845 
28846 /*
28847  *    Function: sr_read_cdda()
28848  *
28849  * Description: This routine is the driver entry point for handling CD-ROM
28850  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28851  *		the target supports CDDA these requests are handled via a vendor
28852  *		specific command (0xD8) If the target does not support CDDA
28853  *		these requests are handled via the READ CD command (0xBE).
28854  *
28855  *   Arguments: dev	- the device 'dev_t'
28856  *		data	- pointer to user provided CD-DA structure specifying
28857  *			  the track starting address, transfer length, and
28858  *			  subcode options.
28859  *		flag	- this argument is a pass through to ddi_copyxxx()
28860  *			  directly from the mode argument of ioctl().
28861  *
28862  * Return Code: the code returned by sd_send_scsi_cmd()
28863  *		EFAULT if ddi_copyxxx() fails
28864  *		ENXIO if fail ddi_get_soft_state
28865  *		EINVAL if invalid arguments are provided
28866  *		ENOTTY
28867  */
28868 
28869 static int
28870 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28871 {
28872 	struct sd_lun			*un;
28873 	struct uscsi_cmd		*com;
28874 	struct cdrom_cdda		*cdda;
28875 	int				rval;
28876 	size_t				buflen;
28877 	char				cdb[CDB_GROUP5];
28878 
28879 #ifdef _MULTI_DATAMODEL
28880 	/* To support ILP32 applications in an LP64 world */
28881 	struct cdrom_cdda32	cdrom_cdda32;
28882 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28883 #endif /* _MULTI_DATAMODEL */
28884 
28885 	if (data == NULL) {
28886 		return (EINVAL);
28887 	}
28888 
28889 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28890 		return (ENXIO);
28891 	}
28892 
28893 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28894 
28895 #ifdef _MULTI_DATAMODEL
28896 	switch (ddi_model_convert_from(flag & FMODELS)) {
28897 	case DDI_MODEL_ILP32:
28898 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28899 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28900 			    "sr_read_cdda: ddi_copyin Failed\n");
28901 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28902 			return (EFAULT);
28903 		}
28904 		/* Convert the ILP32 uscsi data from the application to LP64 */
28905 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28906 		break;
28907 	case DDI_MODEL_NONE:
28908 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28909 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28910 			    "sr_read_cdda: ddi_copyin Failed\n");
28911 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28912 			return (EFAULT);
28913 		}
28914 		break;
28915 	}
28916 #else /* ! _MULTI_DATAMODEL */
28917 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28918 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28919 		    "sr_read_cdda: ddi_copyin Failed\n");
28920 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28921 		return (EFAULT);
28922 	}
28923 #endif /* _MULTI_DATAMODEL */
28924 
28925 	/*
28926 	 * Since MMC-2 expects max 3 bytes for length, check if the
28927 	 * length input is greater than 3 bytes
28928 	 */
28929 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28930 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28931 		    "cdrom transfer length too large: %d (limit %d)\n",
28932 		    cdda->cdda_length, 0xFFFFFF);
28933 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28934 		return (EINVAL);
28935 	}
28936 
28937 	switch (cdda->cdda_subcode) {
28938 	case CDROM_DA_NO_SUBCODE:
28939 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28940 		break;
28941 	case CDROM_DA_SUBQ:
28942 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28943 		break;
28944 	case CDROM_DA_ALL_SUBCODE:
28945 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28946 		break;
28947 	case CDROM_DA_SUBCODE_ONLY:
28948 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28949 		break;
28950 	default:
28951 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28952 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28953 		    cdda->cdda_subcode);
28954 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28955 		return (EINVAL);
28956 	}
28957 
28958 	/* Build and send the command */
28959 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28960 	bzero(cdb, CDB_GROUP5);
28961 
28962 	if (un->un_f_cfg_cdda == TRUE) {
28963 		cdb[0] = (char)SCMD_READ_CD;
28964 		cdb[1] = 0x04;
28965 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28966 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28967 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28968 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28969 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28970 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28971 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28972 		cdb[9] = 0x10;
28973 		switch (cdda->cdda_subcode) {
28974 		case CDROM_DA_NO_SUBCODE :
28975 			cdb[10] = 0x0;
28976 			break;
28977 		case CDROM_DA_SUBQ :
28978 			cdb[10] = 0x2;
28979 			break;
28980 		case CDROM_DA_ALL_SUBCODE :
28981 			cdb[10] = 0x1;
28982 			break;
28983 		case CDROM_DA_SUBCODE_ONLY :
28984 			/* FALLTHROUGH */
28985 		default :
28986 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28987 			kmem_free(com, sizeof (*com));
28988 			return (ENOTTY);
28989 		}
28990 	} else {
28991 		cdb[0] = (char)SCMD_READ_CDDA;
28992 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28993 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28994 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28995 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28996 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28997 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28998 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28999 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
29000 		cdb[10] = cdda->cdda_subcode;
29001 	}
29002 
29003 	com->uscsi_cdb = cdb;
29004 	com->uscsi_cdblen = CDB_GROUP5;
29005 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
29006 	com->uscsi_buflen = buflen;
29007 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29008 
29009 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
29010 	    SD_PATH_STANDARD);
29011 
29012 	kmem_free(cdda, sizeof (struct cdrom_cdda));
29013 	kmem_free(com, sizeof (*com));
29014 	return (rval);
29015 }
29016 
29017 
29018 /*
29019  *    Function: sr_read_cdxa()
29020  *
29021  * Description: This routine is the driver entry point for handling CD-ROM
29022  *		ioctl requests to return CD-XA (Extended Architecture) data.
29023  *		(CDROMCDXA).
29024  *
29025  *   Arguments: dev	- the device 'dev_t'
29026  *		data	- pointer to user provided CD-XA structure specifying
29027  *			  the data starting address, transfer length, and format
29028  *		flag	- this argument is a pass through to ddi_copyxxx()
29029  *			  directly from the mode argument of ioctl().
29030  *
29031  * Return Code: the code returned by sd_send_scsi_cmd()
29032  *		EFAULT if ddi_copyxxx() fails
29033  *		ENXIO if fail ddi_get_soft_state
29034  *		EINVAL if data pointer is NULL
29035  */
29036 
29037 static int
29038 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
29039 {
29040 	struct sd_lun		*un;
29041 	struct uscsi_cmd	*com;
29042 	struct cdrom_cdxa	*cdxa;
29043 	int			rval;
29044 	size_t			buflen;
29045 	char			cdb[CDB_GROUP5];
29046 	uchar_t			read_flags;
29047 
29048 #ifdef _MULTI_DATAMODEL
29049 	/* To support ILP32 applications in an LP64 world */
29050 	struct cdrom_cdxa32		cdrom_cdxa32;
29051 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
29052 #endif /* _MULTI_DATAMODEL */
29053 
29054 	if (data == NULL) {
29055 		return (EINVAL);
29056 	}
29057 
29058 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
29059 		return (ENXIO);
29060 	}
29061 
29062 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
29063 
29064 #ifdef _MULTI_DATAMODEL
29065 	switch (ddi_model_convert_from(flag & FMODELS)) {
29066 	case DDI_MODEL_ILP32:
29067 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
29068 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29069 			return (EFAULT);
29070 		}
29071 		/*
29072 		 * Convert the ILP32 uscsi data from the
29073 		 * application to LP64 for internal use.
29074 		 */
29075 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
29076 		break;
29077 	case DDI_MODEL_NONE:
29078 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
29079 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29080 			return (EFAULT);
29081 		}
29082 		break;
29083 	}
29084 #else /* ! _MULTI_DATAMODEL */
29085 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
29086 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29087 		return (EFAULT);
29088 	}
29089 #endif /* _MULTI_DATAMODEL */
29090 
29091 	/*
29092 	 * Since MMC-2 expects max 3 bytes for length, check if the
29093 	 * length input is greater than 3 bytes
29094 	 */
29095 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
29096 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
29097 		    "cdrom transfer length too large: %d (limit %d)\n",
29098 		    cdxa->cdxa_length, 0xFFFFFF);
29099 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29100 		return (EINVAL);
29101 	}
29102 
29103 	switch (cdxa->cdxa_format) {
29104 	case CDROM_XA_DATA:
29105 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
29106 		read_flags = 0x10;
29107 		break;
29108 	case CDROM_XA_SECTOR_DATA:
29109 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
29110 		read_flags = 0xf8;
29111 		break;
29112 	case CDROM_XA_DATA_W_ERROR:
29113 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
29114 		read_flags = 0xfc;
29115 		break;
29116 	default:
29117 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29118 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
29119 		    cdxa->cdxa_format);
29120 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29121 		return (EINVAL);
29122 	}
29123 
29124 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29125 	bzero(cdb, CDB_GROUP5);
29126 	if (un->un_f_mmc_cap == TRUE) {
29127 		cdb[0] = (char)SCMD_READ_CD;
29128 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
29129 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
29130 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
29131 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
29132 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
29133 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
29134 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
29135 		cdb[9] = (char)read_flags;
29136 	} else {
29137 		/*
29138 		 * Note: A vendor specific command (0xDB) is being used her to
29139 		 * request a read of all subcodes.
29140 		 */
29141 		cdb[0] = (char)SCMD_READ_CDXA;
29142 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
29143 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
29144 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
29145 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
29146 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
29147 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
29148 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
29149 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
29150 		cdb[10] = cdxa->cdxa_format;
29151 	}
29152 	com->uscsi_cdb	   = cdb;
29153 	com->uscsi_cdblen  = CDB_GROUP5;
29154 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
29155 	com->uscsi_buflen  = buflen;
29156 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29157 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
29158 	    SD_PATH_STANDARD);
29159 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29160 	kmem_free(com, sizeof (*com));
29161 	return (rval);
29162 }
29163 
29164 
29165 /*
29166  *    Function: sr_eject()
29167  *
29168  * Description: This routine is the driver entry point for handling CD-ROM
29169  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
29170  *
29171  *   Arguments: dev	- the device 'dev_t'
29172  *
29173  * Return Code: the code returned by sd_send_scsi_cmd()
29174  */
29175 
29176 static int
29177 sr_eject(dev_t dev)
29178 {
29179 	struct sd_lun	*un;
29180 	int		rval;
29181 	sd_ssc_t	*ssc;
29182 
29183 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29184 	    (un->un_state == SD_STATE_OFFLINE)) {
29185 		return (ENXIO);
29186 	}
29187 
29188 	/*
29189 	 * To prevent race conditions with the eject
29190 	 * command, keep track of an eject command as
29191 	 * it progresses. If we are already handling
29192 	 * an eject command in the driver for the given
29193 	 * unit and another request to eject is received
29194 	 * immediately return EAGAIN so we don't lose
29195 	 * the command if the current eject command fails.
29196 	 */
29197 	mutex_enter(SD_MUTEX(un));
29198 	if (un->un_f_ejecting == TRUE) {
29199 		mutex_exit(SD_MUTEX(un));
29200 		return (EAGAIN);
29201 	}
29202 	un->un_f_ejecting = TRUE;
29203 	mutex_exit(SD_MUTEX(un));
29204 
29205 	ssc = sd_ssc_init(un);
29206 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
29207 	    SD_PATH_STANDARD);
29208 	sd_ssc_fini(ssc);
29209 
29210 	if (rval != 0) {
29211 		mutex_enter(SD_MUTEX(un));
29212 		un->un_f_ejecting = FALSE;
29213 		mutex_exit(SD_MUTEX(un));
29214 		return (rval);
29215 	}
29216 
29217 	ssc = sd_ssc_init(un);
29218 	rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
29219 	    SD_TARGET_EJECT, SD_PATH_STANDARD);
29220 	sd_ssc_fini(ssc);
29221 
29222 	if (rval == 0) {
29223 		mutex_enter(SD_MUTEX(un));
29224 		sr_ejected(un);
29225 		un->un_mediastate = DKIO_EJECTED;
29226 		un->un_f_ejecting = FALSE;
29227 		cv_broadcast(&un->un_state_cv);
29228 		mutex_exit(SD_MUTEX(un));
29229 	} else {
29230 		mutex_enter(SD_MUTEX(un));
29231 		un->un_f_ejecting = FALSE;
29232 		mutex_exit(SD_MUTEX(un));
29233 	}
29234 	return (rval);
29235 }
29236 
29237 
29238 /*
29239  *    Function: sr_ejected()
29240  *
29241  * Description: This routine updates the soft state structure to invalidate the
29242  *		geometry information after the media has been ejected or a
29243  *		media eject has been detected.
29244  *
29245  *   Arguments: un - driver soft state (unit) structure
29246  */
29247 
29248 static void
29249 sr_ejected(struct sd_lun *un)
29250 {
29251 	struct sd_errstats *stp;
29252 
29253 	ASSERT(un != NULL);
29254 	ASSERT(mutex_owned(SD_MUTEX(un)));
29255 
29256 	un->un_f_blockcount_is_valid	= FALSE;
29257 	un->un_f_tgt_blocksize_is_valid	= FALSE;
29258 	mutex_exit(SD_MUTEX(un));
29259 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
29260 	mutex_enter(SD_MUTEX(un));
29261 
29262 	if (un->un_errstats != NULL) {
29263 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
29264 		stp->sd_capacity.value.ui64 = 0;
29265 	}
29266 }
29267 
29268 
29269 /*
29270  *    Function: sr_check_wp()
29271  *
29272  * Description: This routine checks the write protection of a removable
29273  *      media disk and hotpluggable devices via the write protect bit of
29274  *      the Mode Page Header device specific field. Some devices choke
29275  *      on unsupported mode page. In order to workaround this issue,
29276  *      this routine has been implemented to use 0x3f mode page(request
29277  *      for all pages) for all device types.
29278  *
29279  *   Arguments: dev             - the device 'dev_t'
29280  *
29281  * Return Code: int indicating if the device is write protected (1) or not (0)
29282  *
29283  *     Context: Kernel thread.
29284  *
29285  */
29286 
29287 static int
29288 sr_check_wp(dev_t dev)
29289 {
29290 	struct sd_lun	*un;
29291 	uchar_t		device_specific;
29292 	uchar_t		*sense;
29293 	int		hdrlen;
29294 	int		rval = FALSE;
29295 	int		status;
29296 	sd_ssc_t	*ssc;
29297 
29298 	/*
29299 	 * Note: The return codes for this routine should be reworked to
29300 	 * properly handle the case of a NULL softstate.
29301 	 */
29302 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
29303 		return (FALSE);
29304 	}
29305 
29306 	if (un->un_f_cfg_is_atapi == TRUE) {
29307 		/*
29308 		 * The mode page contents are not required; set the allocation
29309 		 * length for the mode page header only
29310 		 */
29311 		hdrlen = MODE_HEADER_LENGTH_GRP2;
29312 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29313 		ssc = sd_ssc_init(un);
29314 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
29315 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
29316 		sd_ssc_fini(ssc);
29317 		if (status != 0)
29318 			goto err_exit;
29319 		device_specific =
29320 		    ((struct mode_header_grp2 *)sense)->device_specific;
29321 	} else {
29322 		hdrlen = MODE_HEADER_LENGTH;
29323 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29324 		ssc = sd_ssc_init(un);
29325 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
29326 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
29327 		sd_ssc_fini(ssc);
29328 		if (status != 0)
29329 			goto err_exit;
29330 		device_specific =
29331 		    ((struct mode_header *)sense)->device_specific;
29332 	}
29333 
29334 
29335 	/*
29336 	 * Write protect mode sense failed; not all disks
29337 	 * understand this query. Return FALSE assuming that
29338 	 * these devices are not writable.
29339 	 */
29340 	if (device_specific & WRITE_PROTECT) {
29341 		rval = TRUE;
29342 	}
29343 
29344 err_exit:
29345 	kmem_free(sense, hdrlen);
29346 	return (rval);
29347 }
29348 
29349 /*
29350  *    Function: sr_volume_ctrl()
29351  *
29352  * Description: This routine is the driver entry point for handling CD-ROM
29353  *		audio output volume ioctl requests. (CDROMVOLCTRL)
29354  *
29355  *   Arguments: dev	- the device 'dev_t'
29356  *		data	- pointer to user audio volume control structure
29357  *		flag	- this argument is a pass through to ddi_copyxxx()
29358  *			  directly from the mode argument of ioctl().
29359  *
29360  * Return Code: the code returned by sd_send_scsi_cmd()
29361  *		EFAULT if ddi_copyxxx() fails
29362  *		ENXIO if fail ddi_get_soft_state
29363  *		EINVAL if data pointer is NULL
29364  *
29365  */
29366 
29367 static int
29368 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
29369 {
29370 	struct sd_lun		*un;
29371 	struct cdrom_volctrl    volume;
29372 	struct cdrom_volctrl    *vol = &volume;
29373 	uchar_t			*sense_page;
29374 	uchar_t			*select_page;
29375 	uchar_t			*sense;
29376 	uchar_t			*select;
29377 	int			sense_buflen;
29378 	int			select_buflen;
29379 	int			rval;
29380 	sd_ssc_t		*ssc;
29381 
29382 	if (data == NULL) {
29383 		return (EINVAL);
29384 	}
29385 
29386 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29387 	    (un->un_state == SD_STATE_OFFLINE)) {
29388 		return (ENXIO);
29389 	}
29390 
29391 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
29392 		return (EFAULT);
29393 	}
29394 
29395 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29396 		struct mode_header_grp2		*sense_mhp;
29397 		struct mode_header_grp2		*select_mhp;
29398 		int				bd_len;
29399 
29400 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29401 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
29402 		    MODEPAGE_AUDIO_CTRL_LEN;
29403 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29404 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29405 		ssc = sd_ssc_init(un);
29406 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
29407 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29408 		    SD_PATH_STANDARD);
29409 		sd_ssc_fini(ssc);
29410 
29411 		if (rval != 0) {
29412 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29413 			    "sr_volume_ctrl: Mode Sense Failed\n");
29414 			kmem_free(sense, sense_buflen);
29415 			kmem_free(select, select_buflen);
29416 			return (rval);
29417 		}
29418 		sense_mhp = (struct mode_header_grp2 *)sense;
29419 		select_mhp = (struct mode_header_grp2 *)select;
29420 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
29421 		    sense_mhp->bdesc_length_lo;
29422 		if (bd_len > MODE_BLK_DESC_LENGTH) {
29423 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29424 			    "sr_volume_ctrl: Mode Sense returned invalid "
29425 			    "block descriptor length\n");
29426 			kmem_free(sense, sense_buflen);
29427 			kmem_free(select, select_buflen);
29428 			return (EIO);
29429 		}
29430 		sense_page = (uchar_t *)
29431 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29432 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29433 		select_mhp->length_msb = 0;
29434 		select_mhp->length_lsb = 0;
29435 		select_mhp->bdesc_length_hi = 0;
29436 		select_mhp->bdesc_length_lo = 0;
29437 	} else {
29438 		struct mode_header		*sense_mhp, *select_mhp;
29439 
29440 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29441 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29442 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29443 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29444 		ssc = sd_ssc_init(un);
29445 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
29446 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29447 		    SD_PATH_STANDARD);
29448 		sd_ssc_fini(ssc);
29449 
29450 		if (rval != 0) {
29451 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29452 			    "sr_volume_ctrl: Mode Sense Failed\n");
29453 			kmem_free(sense, sense_buflen);
29454 			kmem_free(select, select_buflen);
29455 			return (rval);
29456 		}
29457 		sense_mhp  = (struct mode_header *)sense;
29458 		select_mhp = (struct mode_header *)select;
29459 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29460 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29461 			    "sr_volume_ctrl: Mode Sense returned invalid "
29462 			    "block descriptor length\n");
29463 			kmem_free(sense, sense_buflen);
29464 			kmem_free(select, select_buflen);
29465 			return (EIO);
29466 		}
29467 		sense_page = (uchar_t *)
29468 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29469 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29470 		select_mhp->length = 0;
29471 		select_mhp->bdesc_length = 0;
29472 	}
29473 	/*
29474 	 * Note: An audio control data structure could be created and overlayed
29475 	 * on the following in place of the array indexing method implemented.
29476 	 */
29477 
29478 	/* Build the select data for the user volume data */
29479 	select_page[0] = MODEPAGE_AUDIO_CTRL;
29480 	select_page[1] = 0xE;
29481 	/* Set the immediate bit */
29482 	select_page[2] = 0x04;
29483 	/* Zero out reserved fields */
29484 	select_page[3] = 0x00;
29485 	select_page[4] = 0x00;
29486 	/* Return sense data for fields not to be modified */
29487 	select_page[5] = sense_page[5];
29488 	select_page[6] = sense_page[6];
29489 	select_page[7] = sense_page[7];
29490 	/* Set the user specified volume levels for channel 0 and 1 */
29491 	select_page[8] = 0x01;
29492 	select_page[9] = vol->channel0;
29493 	select_page[10] = 0x02;
29494 	select_page[11] = vol->channel1;
29495 	/* Channel 2 and 3 are currently unsupported so return the sense data */
29496 	select_page[12] = sense_page[12];
29497 	select_page[13] = sense_page[13];
29498 	select_page[14] = sense_page[14];
29499 	select_page[15] = sense_page[15];
29500 
29501 	ssc = sd_ssc_init(un);
29502 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29503 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
29504 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29505 	} else {
29506 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
29507 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29508 	}
29509 	sd_ssc_fini(ssc);
29510 
29511 	kmem_free(sense, sense_buflen);
29512 	kmem_free(select, select_buflen);
29513 	return (rval);
29514 }
29515 
29516 
29517 /*
29518  *    Function: sr_read_sony_session_offset()
29519  *
29520  * Description: This routine is the driver entry point for handling CD-ROM
29521  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29522  *		The address of the first track in the last session of a
29523  *		multi-session CD-ROM is returned
29524  *
29525  *		Note: This routine uses a vendor specific key value in the
29526  *		command control field without implementing any vendor check here
29527  *		or in the ioctl routine.
29528  *
29529  *   Arguments: dev	- the device 'dev_t'
29530  *		data	- pointer to an int to hold the requested address
29531  *		flag	- this argument is a pass through to ddi_copyxxx()
29532  *			  directly from the mode argument of ioctl().
29533  *
29534  * Return Code: the code returned by sd_send_scsi_cmd()
29535  *		EFAULT if ddi_copyxxx() fails
29536  *		ENXIO if fail ddi_get_soft_state
29537  *		EINVAL if data pointer is NULL
29538  */
29539 
29540 static int
29541 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29542 {
29543 	struct sd_lun		*un;
29544 	struct uscsi_cmd	*com;
29545 	caddr_t			buffer;
29546 	char			cdb[CDB_GROUP1];
29547 	int			session_offset = 0;
29548 	int			rval;
29549 
29550 	if (data == NULL) {
29551 		return (EINVAL);
29552 	}
29553 
29554 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29555 	    (un->un_state == SD_STATE_OFFLINE)) {
29556 		return (ENXIO);
29557 	}
29558 
29559 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29560 	bzero(cdb, CDB_GROUP1);
29561 	cdb[0] = SCMD_READ_TOC;
29562 	/*
29563 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29564 	 * (4 byte TOC response header + 8 byte response data)
29565 	 */
29566 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29567 	/* Byte 9 is the control byte. A vendor specific value is used */
29568 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29569 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29570 	com->uscsi_cdb = cdb;
29571 	com->uscsi_cdblen = CDB_GROUP1;
29572 	com->uscsi_bufaddr = buffer;
29573 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29574 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29575 
29576 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29577 	    SD_PATH_STANDARD);
29578 	if (rval != 0) {
29579 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29580 		kmem_free(com, sizeof (*com));
29581 		return (rval);
29582 	}
29583 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29584 		session_offset =
29585 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29586 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29587 		/*
29588 		 * Offset returned offset in current lbasize block's. Convert to
29589 		 * 2k block's to return to the user
29590 		 */
29591 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29592 			session_offset >>= 2;
29593 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29594 			session_offset >>= 1;
29595 		}
29596 	}
29597 
29598 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29599 		rval = EFAULT;
29600 	}
29601 
29602 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29603 	kmem_free(com, sizeof (*com));
29604 	return (rval);
29605 }
29606 
29607 
29608 /*
29609  *    Function: sd_wm_cache_constructor()
29610  *
29611  * Description: Cache Constructor for the wmap cache for the read/modify/write
29612  * 		devices.
29613  *
29614  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29615  *		un	- sd_lun structure for the device.
29616  *		flag	- the km flags passed to constructor
29617  *
29618  * Return Code: 0 on success.
29619  *		-1 on failure.
29620  */
29621 
29622 /*ARGSUSED*/
29623 static int
29624 sd_wm_cache_constructor(void *wm, void *un, int flags)
29625 {
29626 	bzero(wm, sizeof (struct sd_w_map));
29627 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29628 	return (0);
29629 }
29630 
29631 
29632 /*
29633  *    Function: sd_wm_cache_destructor()
29634  *
29635  * Description: Cache destructor for the wmap cache for the read/modify/write
29636  * 		devices.
29637  *
29638  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29639  *		un	- sd_lun structure for the device.
29640  */
29641 /*ARGSUSED*/
29642 static void
29643 sd_wm_cache_destructor(void *wm, void *un)
29644 {
29645 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29646 }
29647 
29648 
29649 /*
29650  *    Function: sd_range_lock()
29651  *
29652  * Description: Lock the range of blocks specified as parameter to ensure
29653  *		that read, modify write is atomic and no other i/o writes
29654  *		to the same location. The range is specified in terms
29655  *		of start and end blocks. Block numbers are the actual
29656  *		media block numbers and not system.
29657  *
29658  *   Arguments: un	- sd_lun structure for the device.
29659  *		startb - The starting block number
29660  *		endb - The end block number
29661  *		typ - type of i/o - simple/read_modify_write
29662  *
29663  * Return Code: wm  - pointer to the wmap structure.
29664  *
29665  *     Context: This routine can sleep.
29666  */
29667 
29668 static struct sd_w_map *
29669 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29670 {
29671 	struct sd_w_map *wmp = NULL;
29672 	struct sd_w_map *sl_wmp = NULL;
29673 	struct sd_w_map *tmp_wmp;
29674 	wm_state state = SD_WM_CHK_LIST;
29675 
29676 
29677 	ASSERT(un != NULL);
29678 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29679 
29680 	mutex_enter(SD_MUTEX(un));
29681 
29682 	while (state != SD_WM_DONE) {
29683 
29684 		switch (state) {
29685 		case SD_WM_CHK_LIST:
29686 			/*
29687 			 * This is the starting state. Check the wmap list
29688 			 * to see if the range is currently available.
29689 			 */
29690 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29691 				/*
29692 				 * If this is a simple write and no rmw
29693 				 * i/o is pending then try to lock the
29694 				 * range as the range should be available.
29695 				 */
29696 				state = SD_WM_LOCK_RANGE;
29697 			} else {
29698 				tmp_wmp = sd_get_range(un, startb, endb);
29699 				if (tmp_wmp != NULL) {
29700 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29701 						/*
29702 						 * Should not keep onlist wmps
29703 						 * while waiting this macro
29704 						 * will also do wmp = NULL;
29705 						 */
29706 						FREE_ONLIST_WMAP(un, wmp);
29707 					}
29708 					/*
29709 					 * sl_wmp is the wmap on which wait
29710 					 * is done, since the tmp_wmp points
29711 					 * to the inuse wmap, set sl_wmp to
29712 					 * tmp_wmp and change the state to sleep
29713 					 */
29714 					sl_wmp = tmp_wmp;
29715 					state = SD_WM_WAIT_MAP;
29716 				} else {
29717 					state = SD_WM_LOCK_RANGE;
29718 				}
29719 
29720 			}
29721 			break;
29722 
29723 		case SD_WM_LOCK_RANGE:
29724 			ASSERT(un->un_wm_cache);
29725 			/*
29726 			 * The range need to be locked, try to get a wmap.
29727 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29728 			 * if possible as we will have to release the sd mutex
29729 			 * if we have to sleep.
29730 			 */
29731 			if (wmp == NULL)
29732 				wmp = kmem_cache_alloc(un->un_wm_cache,
29733 				    KM_NOSLEEP);
29734 			if (wmp == NULL) {
29735 				mutex_exit(SD_MUTEX(un));
29736 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29737 				    (sd_lun::un_wm_cache))
29738 				wmp = kmem_cache_alloc(un->un_wm_cache,
29739 				    KM_SLEEP);
29740 				mutex_enter(SD_MUTEX(un));
29741 				/*
29742 				 * we released the mutex so recheck and go to
29743 				 * check list state.
29744 				 */
29745 				state = SD_WM_CHK_LIST;
29746 			} else {
29747 				/*
29748 				 * We exit out of state machine since we
29749 				 * have the wmap. Do the housekeeping first.
29750 				 * place the wmap on the wmap list if it is not
29751 				 * on it already and then set the state to done.
29752 				 */
29753 				wmp->wm_start = startb;
29754 				wmp->wm_end = endb;
29755 				wmp->wm_flags = typ | SD_WM_BUSY;
29756 				if (typ & SD_WTYPE_RMW) {
29757 					un->un_rmw_count++;
29758 				}
29759 				/*
29760 				 * If not already on the list then link
29761 				 */
29762 				if (!ONLIST(un, wmp)) {
29763 					wmp->wm_next = un->un_wm;
29764 					wmp->wm_prev = NULL;
29765 					if (wmp->wm_next)
29766 						wmp->wm_next->wm_prev = wmp;
29767 					un->un_wm = wmp;
29768 				}
29769 				state = SD_WM_DONE;
29770 			}
29771 			break;
29772 
29773 		case SD_WM_WAIT_MAP:
29774 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29775 			/*
29776 			 * Wait is done on sl_wmp, which is set in the
29777 			 * check_list state.
29778 			 */
29779 			sl_wmp->wm_wanted_count++;
29780 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29781 			sl_wmp->wm_wanted_count--;
29782 			/*
29783 			 * We can reuse the memory from the completed sl_wmp
29784 			 * lock range for our new lock, but only if noone is
29785 			 * waiting for it.
29786 			 */
29787 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29788 			if (sl_wmp->wm_wanted_count == 0) {
29789 				if (wmp != NULL) {
29790 					CHK_N_FREEWMP(un, wmp);
29791 				}
29792 				wmp = sl_wmp;
29793 			}
29794 			sl_wmp = NULL;
29795 			/*
29796 			 * After waking up, need to recheck for availability of
29797 			 * range.
29798 			 */
29799 			state = SD_WM_CHK_LIST;
29800 			break;
29801 
29802 		default:
29803 			panic("sd_range_lock: "
29804 			    "Unknown state %d in sd_range_lock", state);
29805 			/*NOTREACHED*/
29806 		} /* switch(state) */
29807 
29808 	} /* while(state != SD_WM_DONE) */
29809 
29810 	mutex_exit(SD_MUTEX(un));
29811 
29812 	ASSERT(wmp != NULL);
29813 
29814 	return (wmp);
29815 }
29816 
29817 
29818 /*
29819  *    Function: sd_get_range()
29820  *
29821  * Description: Find if there any overlapping I/O to this one
29822  *		Returns the write-map of 1st such I/O, NULL otherwise.
29823  *
29824  *   Arguments: un	- sd_lun structure for the device.
29825  *		startb - The starting block number
29826  *		endb - The end block number
29827  *
29828  * Return Code: wm  - pointer to the wmap structure.
29829  */
29830 
29831 static struct sd_w_map *
29832 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29833 {
29834 	struct sd_w_map *wmp;
29835 
29836 	ASSERT(un != NULL);
29837 
29838 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29839 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
29840 			continue;
29841 		}
29842 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29843 			break;
29844 		}
29845 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29846 			break;
29847 		}
29848 	}
29849 
29850 	return (wmp);
29851 }
29852 
29853 
29854 /*
29855  *    Function: sd_free_inlist_wmap()
29856  *
29857  * Description: Unlink and free a write map struct.
29858  *
29859  *   Arguments: un      - sd_lun structure for the device.
29860  *		wmp	- sd_w_map which needs to be unlinked.
29861  */
29862 
29863 static void
29864 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29865 {
29866 	ASSERT(un != NULL);
29867 
29868 	if (un->un_wm == wmp) {
29869 		un->un_wm = wmp->wm_next;
29870 	} else {
29871 		wmp->wm_prev->wm_next = wmp->wm_next;
29872 	}
29873 
29874 	if (wmp->wm_next) {
29875 		wmp->wm_next->wm_prev = wmp->wm_prev;
29876 	}
29877 
29878 	wmp->wm_next = wmp->wm_prev = NULL;
29879 
29880 	kmem_cache_free(un->un_wm_cache, wmp);
29881 }
29882 
29883 
29884 /*
29885  *    Function: sd_range_unlock()
29886  *
29887  * Description: Unlock the range locked by wm.
29888  *		Free write map if nobody else is waiting on it.
29889  *
29890  *   Arguments: un      - sd_lun structure for the device.
29891  *              wmp     - sd_w_map which needs to be unlinked.
29892  */
29893 
29894 static void
29895 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29896 {
29897 	ASSERT(un != NULL);
29898 	ASSERT(wm != NULL);
29899 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29900 
29901 	mutex_enter(SD_MUTEX(un));
29902 
29903 	if (wm->wm_flags & SD_WTYPE_RMW) {
29904 		un->un_rmw_count--;
29905 	}
29906 
29907 	if (wm->wm_wanted_count) {
29908 		wm->wm_flags = 0;
29909 		/*
29910 		 * Broadcast that the wmap is available now.
29911 		 */
29912 		cv_broadcast(&wm->wm_avail);
29913 	} else {
29914 		/*
29915 		 * If no one is waiting on the map, it should be free'ed.
29916 		 */
29917 		sd_free_inlist_wmap(un, wm);
29918 	}
29919 
29920 	mutex_exit(SD_MUTEX(un));
29921 }
29922 
29923 
29924 /*
29925  *    Function: sd_read_modify_write_task
29926  *
29927  * Description: Called from a taskq thread to initiate the write phase of
29928  *		a read-modify-write request.  This is used for targets where
29929  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29930  *
29931  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29932  *
29933  *     Context: Called under taskq thread context.
29934  */
29935 
29936 static void
29937 sd_read_modify_write_task(void *arg)
29938 {
29939 	struct sd_mapblocksize_info	*bsp;
29940 	struct buf	*bp;
29941 	struct sd_xbuf	*xp;
29942 	struct sd_lun	*un;
29943 
29944 	bp = arg;	/* The bp is given in arg */
29945 	ASSERT(bp != NULL);
29946 
29947 	/* Get the pointer to the layer-private data struct */
29948 	xp = SD_GET_XBUF(bp);
29949 	ASSERT(xp != NULL);
29950 	bsp = xp->xb_private;
29951 	ASSERT(bsp != NULL);
29952 
29953 	un = SD_GET_UN(bp);
29954 	ASSERT(un != NULL);
29955 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29956 
29957 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29958 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29959 
29960 	/*
29961 	 * This is the write phase of a read-modify-write request, called
29962 	 * under the context of a taskq thread in response to the completion
29963 	 * of the read portion of the rmw request completing under interrupt
29964 	 * context. The write request must be sent from here down the iostart
29965 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29966 	 * we use the layer index saved in the layer-private data area.
29967 	 */
29968 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29969 
29970 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29971 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29972 }
29973 
29974 
29975 /*
29976  *    Function: sddump_do_read_of_rmw()
29977  *
29978  * Description: This routine will be called from sddump, If sddump is called
29979  *		with an I/O which not aligned on device blocksize boundary
29980  *		then the write has to be converted to read-modify-write.
29981  *		Do the read part here in order to keep sddump simple.
29982  *		Note - That the sd_mutex is held across the call to this
29983  *		routine.
29984  *
29985  *   Arguments: un	- sd_lun
29986  *		blkno	- block number in terms of media block size.
29987  *		nblk	- number of blocks.
29988  *		bpp	- pointer to pointer to the buf structure. On return
29989  *			from this function, *bpp points to the valid buffer
29990  *			to which the write has to be done.
29991  *
29992  * Return Code: 0 for success or errno-type return code
29993  */
29994 
29995 static int
29996 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29997     struct buf **bpp)
29998 {
29999 	int err;
30000 	int i;
30001 	int rval;
30002 	struct buf *bp;
30003 	struct scsi_pkt *pkt = NULL;
30004 	uint32_t target_blocksize;
30005 
30006 	ASSERT(un != NULL);
30007 	ASSERT(mutex_owned(SD_MUTEX(un)));
30008 
30009 	target_blocksize = un->un_tgt_blocksize;
30010 
30011 	mutex_exit(SD_MUTEX(un));
30012 
30013 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
30014 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
30015 	if (bp == NULL) {
30016 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30017 		    "no resources for dumping; giving up");
30018 		err = ENOMEM;
30019 		goto done;
30020 	}
30021 
30022 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
30023 	    blkno, nblk);
30024 	if (rval != 0) {
30025 		scsi_free_consistent_buf(bp);
30026 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30027 		    "no resources for dumping; giving up");
30028 		err = ENOMEM;
30029 		goto done;
30030 	}
30031 
30032 	pkt->pkt_flags |= FLAG_NOINTR;
30033 
30034 	err = EIO;
30035 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
30036 
30037 		/*
30038 		 * Scsi_poll returns 0 (success) if the command completes and
30039 		 * the status block is STATUS_GOOD.  We should only check
30040 		 * errors if this condition is not true.  Even then we should
30041 		 * send our own request sense packet only if we have a check
30042 		 * condition and auto request sense has not been performed by
30043 		 * the hba.
30044 		 */
30045 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
30046 
30047 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
30048 			err = 0;
30049 			break;
30050 		}
30051 
30052 		/*
30053 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
30054 		 * no need to read RQS data.
30055 		 */
30056 		if (pkt->pkt_reason == CMD_DEV_GONE) {
30057 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30058 			    "Error while dumping state with rmw..."
30059 			    "Device is gone\n");
30060 			break;
30061 		}
30062 
30063 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
30064 			SD_INFO(SD_LOG_DUMP, un,
30065 			    "sddump: read failed with CHECK, try # %d\n", i);
30066 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
30067 				(void) sd_send_polled_RQS(un);
30068 			}
30069 
30070 			continue;
30071 		}
30072 
30073 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
30074 			int reset_retval = 0;
30075 
30076 			SD_INFO(SD_LOG_DUMP, un,
30077 			    "sddump: read failed with BUSY, try # %d\n", i);
30078 
30079 			if (un->un_f_lun_reset_enabled == TRUE) {
30080 				reset_retval = scsi_reset(SD_ADDRESS(un),
30081 				    RESET_LUN);
30082 			}
30083 			if (reset_retval == 0) {
30084 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
30085 			}
30086 			(void) sd_send_polled_RQS(un);
30087 
30088 		} else {
30089 			SD_INFO(SD_LOG_DUMP, un,
30090 			    "sddump: read failed with 0x%x, try # %d\n",
30091 			    SD_GET_PKT_STATUS(pkt), i);
30092 			mutex_enter(SD_MUTEX(un));
30093 			sd_reset_target(un, pkt);
30094 			mutex_exit(SD_MUTEX(un));
30095 		}
30096 
30097 		/*
30098 		 * If we are not getting anywhere with lun/target resets,
30099 		 * let's reset the bus.
30100 		 */
30101 		if (i > SD_NDUMP_RETRIES/2) {
30102 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
30103 			(void) sd_send_polled_RQS(un);
30104 		}
30105 
30106 	}
30107 	scsi_destroy_pkt(pkt);
30108 
30109 	if (err != 0) {
30110 		scsi_free_consistent_buf(bp);
30111 		*bpp = NULL;
30112 	} else {
30113 		*bpp = bp;
30114 	}
30115 
30116 done:
30117 	mutex_enter(SD_MUTEX(un));
30118 	return (err);
30119 }
30120 
30121 
30122 /*
30123  *    Function: sd_failfast_flushq
30124  *
30125  * Description: Take all bp's on the wait queue that have B_FAILFAST set
30126  *		in b_flags and move them onto the failfast queue, then kick
30127  *		off a thread to return all bp's on the failfast queue to
30128  *		their owners with an error set.
30129  *
30130  *   Arguments: un - pointer to the soft state struct for the instance.
30131  *
30132  *     Context: may execute in interrupt context.
30133  */
30134 
30135 static void
30136 sd_failfast_flushq(struct sd_lun *un)
30137 {
30138 	struct buf *bp;
30139 	struct buf *next_waitq_bp;
30140 	struct buf *prev_waitq_bp = NULL;
30141 
30142 	ASSERT(un != NULL);
30143 	ASSERT(mutex_owned(SD_MUTEX(un)));
30144 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
30145 	ASSERT(un->un_failfast_bp == NULL);
30146 
30147 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
30148 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
30149 
30150 	/*
30151 	 * Check if we should flush all bufs when entering failfast state, or
30152 	 * just those with B_FAILFAST set.
30153 	 */
30154 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
30155 		/*
30156 		 * Move *all* bp's on the wait queue to the failfast flush
30157 		 * queue, including those that do NOT have B_FAILFAST set.
30158 		 */
30159 		if (un->un_failfast_headp == NULL) {
30160 			ASSERT(un->un_failfast_tailp == NULL);
30161 			un->un_failfast_headp = un->un_waitq_headp;
30162 		} else {
30163 			ASSERT(un->un_failfast_tailp != NULL);
30164 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
30165 		}
30166 
30167 		un->un_failfast_tailp = un->un_waitq_tailp;
30168 
30169 		/* update kstat for each bp moved out of the waitq */
30170 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
30171 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
30172 		}
30173 
30174 		/* empty the waitq */
30175 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
30176 
30177 	} else {
30178 		/*
30179 		 * Go thru the wait queue, pick off all entries with
30180 		 * B_FAILFAST set, and move these onto the failfast queue.
30181 		 */
30182 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
30183 			/*
30184 			 * Save the pointer to the next bp on the wait queue,
30185 			 * so we get to it on the next iteration of this loop.
30186 			 */
30187 			next_waitq_bp = bp->av_forw;
30188 
30189 			/*
30190 			 * If this bp from the wait queue does NOT have
30191 			 * B_FAILFAST set, just move on to the next element
30192 			 * in the wait queue. Note, this is the only place
30193 			 * where it is correct to set prev_waitq_bp.
30194 			 */
30195 			if ((bp->b_flags & B_FAILFAST) == 0) {
30196 				prev_waitq_bp = bp;
30197 				continue;
30198 			}
30199 
30200 			/*
30201 			 * Remove the bp from the wait queue.
30202 			 */
30203 			if (bp == un->un_waitq_headp) {
30204 				/* The bp is the first element of the waitq. */
30205 				un->un_waitq_headp = next_waitq_bp;
30206 				if (un->un_waitq_headp == NULL) {
30207 					/* The wait queue is now empty */
30208 					un->un_waitq_tailp = NULL;
30209 				}
30210 			} else {
30211 				/*
30212 				 * The bp is either somewhere in the middle
30213 				 * or at the end of the wait queue.
30214 				 */
30215 				ASSERT(un->un_waitq_headp != NULL);
30216 				ASSERT(prev_waitq_bp != NULL);
30217 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
30218 				    == 0);
30219 				if (bp == un->un_waitq_tailp) {
30220 					/* bp is the last entry on the waitq. */
30221 					ASSERT(next_waitq_bp == NULL);
30222 					un->un_waitq_tailp = prev_waitq_bp;
30223 				}
30224 				prev_waitq_bp->av_forw = next_waitq_bp;
30225 			}
30226 			bp->av_forw = NULL;
30227 
30228 			/*
30229 			 * update kstat since the bp is moved out of
30230 			 * the waitq
30231 			 */
30232 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
30233 
30234 			/*
30235 			 * Now put the bp onto the failfast queue.
30236 			 */
30237 			if (un->un_failfast_headp == NULL) {
30238 				/* failfast queue is currently empty */
30239 				ASSERT(un->un_failfast_tailp == NULL);
30240 				un->un_failfast_headp =
30241 				    un->un_failfast_tailp = bp;
30242 			} else {
30243 				/* Add the bp to the end of the failfast q */
30244 				ASSERT(un->un_failfast_tailp != NULL);
30245 				ASSERT(un->un_failfast_tailp->b_flags &
30246 				    B_FAILFAST);
30247 				un->un_failfast_tailp->av_forw = bp;
30248 				un->un_failfast_tailp = bp;
30249 			}
30250 		}
30251 	}
30252 
30253 	/*
30254 	 * Now return all bp's on the failfast queue to their owners.
30255 	 */
30256 	while ((bp = un->un_failfast_headp) != NULL) {
30257 
30258 		un->un_failfast_headp = bp->av_forw;
30259 		if (un->un_failfast_headp == NULL) {
30260 			un->un_failfast_tailp = NULL;
30261 		}
30262 
30263 		/*
30264 		 * We want to return the bp with a failure error code, but
30265 		 * we do not want a call to sd_start_cmds() to occur here,
30266 		 * so use sd_return_failed_command_no_restart() instead of
30267 		 * sd_return_failed_command().
30268 		 */
30269 		sd_return_failed_command_no_restart(un, bp, EIO);
30270 	}
30271 
30272 	/* Flush the xbuf queues if required. */
30273 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
30274 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
30275 	}
30276 
30277 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
30278 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
30279 }
30280 
30281 
30282 /*
30283  *    Function: sd_failfast_flushq_callback
30284  *
30285  * Description: Return TRUE if the given bp meets the criteria for failfast
30286  *		flushing. Used with ddi_xbuf_flushq(9F).
30287  *
30288  *   Arguments: bp - ptr to buf struct to be examined.
30289  *
30290  *     Context: Any
30291  */
30292 
30293 static int
30294 sd_failfast_flushq_callback(struct buf *bp)
30295 {
30296 	/*
30297 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
30298 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
30299 	 */
30300 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
30301 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
30302 }
30303 
30304 
30305 
30306 /*
30307  * Function: sd_setup_next_xfer
30308  *
30309  * Description: Prepare next I/O operation using DMA_PARTIAL
30310  *
30311  */
30312 
30313 static int
30314 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
30315     struct scsi_pkt *pkt, struct sd_xbuf *xp)
30316 {
30317 	ssize_t	num_blks_not_xfered;
30318 	daddr_t	strt_blk_num;
30319 	ssize_t	bytes_not_xfered;
30320 	int	rval;
30321 
30322 	ASSERT(pkt->pkt_resid == 0);
30323 
30324 	/*
30325 	 * Calculate next block number and amount to be transferred.
30326 	 *
30327 	 * How much data NOT transfered to the HBA yet.
30328 	 */
30329 	bytes_not_xfered = xp->xb_dma_resid;
30330 
30331 	/*
30332 	 * figure how many blocks NOT transfered to the HBA yet.
30333 	 */
30334 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
30335 
30336 	/*
30337 	 * set starting block number to the end of what WAS transfered.
30338 	 */
30339 	strt_blk_num = xp->xb_blkno +
30340 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
30341 
30342 	/*
30343 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
30344 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
30345 	 * the disk mutex here.
30346 	 */
30347 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
30348 	    strt_blk_num, num_blks_not_xfered);
30349 
30350 	if (rval == 0) {
30351 
30352 		/*
30353 		 * Success.
30354 		 *
30355 		 * Adjust things if there are still more blocks to be
30356 		 * transfered.
30357 		 */
30358 		xp->xb_dma_resid = pkt->pkt_resid;
30359 		pkt->pkt_resid = 0;
30360 
30361 		return (1);
30362 	}
30363 
30364 	/*
30365 	 * There's really only one possible return value from
30366 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
30367 	 * returns NULL.
30368 	 */
30369 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
30370 
30371 	bp->b_resid = bp->b_bcount;
30372 	bp->b_flags |= B_ERROR;
30373 
30374 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30375 	    "Error setting up next portion of DMA transfer\n");
30376 
30377 	return (0);
30378 }
30379 
30380 /*
30381  *    Function: sd_panic_for_res_conflict
30382  *
30383  * Description: Call panic with a string formatted with "Reservation Conflict"
30384  *		and a human readable identifier indicating the SD instance
30385  *		that experienced the reservation conflict.
30386  *
30387  *   Arguments: un - pointer to the soft state struct for the instance.
30388  *
30389  *     Context: may execute in interrupt context.
30390  */
30391 
30392 #define	SD_RESV_CONFLICT_FMT_LEN 40
30393 void
30394 sd_panic_for_res_conflict(struct sd_lun *un)
30395 {
30396 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30397 	char path_str[MAXPATHLEN];
30398 
30399 	(void) snprintf(panic_str, sizeof (panic_str),
30400 	    "Reservation Conflict\nDisk: %s",
30401 	    ddi_pathname(SD_DEVINFO(un), path_str));
30402 
30403 	panic(panic_str);
30404 }
30405 
30406 /*
30407  * Note: The following sd_faultinjection_ioctl( ) routines implement
30408  * driver support for handling fault injection for error analysis
30409  * causing faults in multiple layers of the driver.
30410  *
30411  */
30412 
30413 #ifdef SD_FAULT_INJECTION
30414 static uint_t   sd_fault_injection_on = 0;
30415 
30416 /*
30417  *    Function: sd_faultinjection_ioctl()
30418  *
30419  * Description: This routine is the driver entry point for handling
30420  *              faultinjection ioctls to inject errors into the
30421  *              layer model
30422  *
30423  *   Arguments: cmd	- the ioctl cmd received
30424  *		arg	- the arguments from user and returns
30425  */
30426 
30427 static void
30428 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un)
30429 {
30430 	uint_t i = 0;
30431 	uint_t rval;
30432 
30433 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30434 
30435 	mutex_enter(SD_MUTEX(un));
30436 
30437 	switch (cmd) {
30438 	case SDIOCRUN:
30439 		/* Allow pushed faults to be injected */
30440 		SD_INFO(SD_LOG_SDTEST, un,
30441 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
30442 
30443 		sd_fault_injection_on = 1;
30444 
30445 		SD_INFO(SD_LOG_IOERR, un,
30446 		    "sd_faultinjection_ioctl: run finished\n");
30447 		break;
30448 
30449 	case SDIOCSTART:
30450 		/* Start Injection Session */
30451 		SD_INFO(SD_LOG_SDTEST, un,
30452 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
30453 
30454 		sd_fault_injection_on = 0;
30455 		un->sd_injection_mask = 0xFFFFFFFF;
30456 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30457 			un->sd_fi_fifo_pkt[i] = NULL;
30458 			un->sd_fi_fifo_xb[i] = NULL;
30459 			un->sd_fi_fifo_un[i] = NULL;
30460 			un->sd_fi_fifo_arq[i] = NULL;
30461 		}
30462 		un->sd_fi_fifo_start = 0;
30463 		un->sd_fi_fifo_end = 0;
30464 
30465 		mutex_enter(&(un->un_fi_mutex));
30466 		un->sd_fi_log[0] = '\0';
30467 		un->sd_fi_buf_len = 0;
30468 		mutex_exit(&(un->un_fi_mutex));
30469 
30470 		SD_INFO(SD_LOG_IOERR, un,
30471 		    "sd_faultinjection_ioctl: start finished\n");
30472 		break;
30473 
30474 	case SDIOCSTOP:
30475 		/* Stop Injection Session */
30476 		SD_INFO(SD_LOG_SDTEST, un,
30477 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30478 		sd_fault_injection_on = 0;
30479 		un->sd_injection_mask = 0x0;
30480 
30481 		/* Empty stray or unuseds structs from fifo */
30482 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30483 			if (un->sd_fi_fifo_pkt[i] != NULL) {
30484 				kmem_free(un->sd_fi_fifo_pkt[i],
30485 				    sizeof (struct sd_fi_pkt));
30486 			}
30487 			if (un->sd_fi_fifo_xb[i] != NULL) {
30488 				kmem_free(un->sd_fi_fifo_xb[i],
30489 				    sizeof (struct sd_fi_xb));
30490 			}
30491 			if (un->sd_fi_fifo_un[i] != NULL) {
30492 				kmem_free(un->sd_fi_fifo_un[i],
30493 				    sizeof (struct sd_fi_un));
30494 			}
30495 			if (un->sd_fi_fifo_arq[i] != NULL) {
30496 				kmem_free(un->sd_fi_fifo_arq[i],
30497 				    sizeof (struct sd_fi_arq));
30498 			}
30499 			un->sd_fi_fifo_pkt[i] = NULL;
30500 			un->sd_fi_fifo_un[i] = NULL;
30501 			un->sd_fi_fifo_xb[i] = NULL;
30502 			un->sd_fi_fifo_arq[i] = NULL;
30503 		}
30504 		un->sd_fi_fifo_start = 0;
30505 		un->sd_fi_fifo_end = 0;
30506 
30507 		SD_INFO(SD_LOG_IOERR, un,
30508 		    "sd_faultinjection_ioctl: stop finished\n");
30509 		break;
30510 
30511 	case SDIOCINSERTPKT:
30512 		/* Store a packet struct to be pushed onto fifo */
30513 		SD_INFO(SD_LOG_SDTEST, un,
30514 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30515 
30516 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30517 
30518 		sd_fault_injection_on = 0;
30519 
30520 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30521 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30522 			kmem_free(un->sd_fi_fifo_pkt[i],
30523 			    sizeof (struct sd_fi_pkt));
30524 		}
30525 		if (arg != NULL) {
30526 			un->sd_fi_fifo_pkt[i] =
30527 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30528 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30529 				/* Alloc failed don't store anything */
30530 				break;
30531 			}
30532 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30533 			    sizeof (struct sd_fi_pkt), 0);
30534 			if (rval == -1) {
30535 				kmem_free(un->sd_fi_fifo_pkt[i],
30536 				    sizeof (struct sd_fi_pkt));
30537 				un->sd_fi_fifo_pkt[i] = NULL;
30538 			}
30539 		} else {
30540 			SD_INFO(SD_LOG_IOERR, un,
30541 			    "sd_faultinjection_ioctl: pkt null\n");
30542 		}
30543 		break;
30544 
30545 	case SDIOCINSERTXB:
30546 		/* Store a xb struct to be pushed onto fifo */
30547 		SD_INFO(SD_LOG_SDTEST, un,
30548 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30549 
30550 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30551 
30552 		sd_fault_injection_on = 0;
30553 
30554 		if (un->sd_fi_fifo_xb[i] != NULL) {
30555 			kmem_free(un->sd_fi_fifo_xb[i],
30556 			    sizeof (struct sd_fi_xb));
30557 			un->sd_fi_fifo_xb[i] = NULL;
30558 		}
30559 		if (arg != NULL) {
30560 			un->sd_fi_fifo_xb[i] =
30561 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30562 			if (un->sd_fi_fifo_xb[i] == NULL) {
30563 				/* Alloc failed don't store anything */
30564 				break;
30565 			}
30566 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30567 			    sizeof (struct sd_fi_xb), 0);
30568 
30569 			if (rval == -1) {
30570 				kmem_free(un->sd_fi_fifo_xb[i],
30571 				    sizeof (struct sd_fi_xb));
30572 				un->sd_fi_fifo_xb[i] = NULL;
30573 			}
30574 		} else {
30575 			SD_INFO(SD_LOG_IOERR, un,
30576 			    "sd_faultinjection_ioctl: xb null\n");
30577 		}
30578 		break;
30579 
30580 	case SDIOCINSERTUN:
30581 		/* Store a un struct to be pushed onto fifo */
30582 		SD_INFO(SD_LOG_SDTEST, un,
30583 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30584 
30585 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30586 
30587 		sd_fault_injection_on = 0;
30588 
30589 		if (un->sd_fi_fifo_un[i] != NULL) {
30590 			kmem_free(un->sd_fi_fifo_un[i],
30591 			    sizeof (struct sd_fi_un));
30592 			un->sd_fi_fifo_un[i] = NULL;
30593 		}
30594 		if (arg != NULL) {
30595 			un->sd_fi_fifo_un[i] =
30596 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30597 			if (un->sd_fi_fifo_un[i] == NULL) {
30598 				/* Alloc failed don't store anything */
30599 				break;
30600 			}
30601 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30602 			    sizeof (struct sd_fi_un), 0);
30603 			if (rval == -1) {
30604 				kmem_free(un->sd_fi_fifo_un[i],
30605 				    sizeof (struct sd_fi_un));
30606 				un->sd_fi_fifo_un[i] = NULL;
30607 			}
30608 
30609 		} else {
30610 			SD_INFO(SD_LOG_IOERR, un,
30611 			    "sd_faultinjection_ioctl: un null\n");
30612 		}
30613 
30614 		break;
30615 
30616 	case SDIOCINSERTARQ:
30617 		/* Store a arq struct to be pushed onto fifo */
30618 		SD_INFO(SD_LOG_SDTEST, un,
30619 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30620 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30621 
30622 		sd_fault_injection_on = 0;
30623 
30624 		if (un->sd_fi_fifo_arq[i] != NULL) {
30625 			kmem_free(un->sd_fi_fifo_arq[i],
30626 			    sizeof (struct sd_fi_arq));
30627 			un->sd_fi_fifo_arq[i] = NULL;
30628 		}
30629 		if (arg != NULL) {
30630 			un->sd_fi_fifo_arq[i] =
30631 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30632 			if (un->sd_fi_fifo_arq[i] == NULL) {
30633 				/* Alloc failed don't store anything */
30634 				break;
30635 			}
30636 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30637 			    sizeof (struct sd_fi_arq), 0);
30638 			if (rval == -1) {
30639 				kmem_free(un->sd_fi_fifo_arq[i],
30640 				    sizeof (struct sd_fi_arq));
30641 				un->sd_fi_fifo_arq[i] = NULL;
30642 			}
30643 
30644 		} else {
30645 			SD_INFO(SD_LOG_IOERR, un,
30646 			    "sd_faultinjection_ioctl: arq null\n");
30647 		}
30648 
30649 		break;
30650 
30651 	case SDIOCPUSH:
30652 		/* Push stored xb, pkt, un, and arq onto fifo */
30653 		sd_fault_injection_on = 0;
30654 
30655 		if (arg != NULL) {
30656 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30657 			if (rval != -1 &&
30658 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30659 				un->sd_fi_fifo_end += i;
30660 			}
30661 		} else {
30662 			SD_INFO(SD_LOG_IOERR, un,
30663 			    "sd_faultinjection_ioctl: push arg null\n");
30664 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30665 				un->sd_fi_fifo_end++;
30666 			}
30667 		}
30668 		SD_INFO(SD_LOG_IOERR, un,
30669 		    "sd_faultinjection_ioctl: push to end=%d\n",
30670 		    un->sd_fi_fifo_end);
30671 		break;
30672 
30673 	case SDIOCRETRIEVE:
30674 		/* Return buffer of log from Injection session */
30675 		SD_INFO(SD_LOG_SDTEST, un,
30676 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30677 
30678 		sd_fault_injection_on = 0;
30679 
30680 		mutex_enter(&(un->un_fi_mutex));
30681 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30682 		    un->sd_fi_buf_len+1, 0);
30683 		mutex_exit(&(un->un_fi_mutex));
30684 
30685 		if (rval == -1) {
30686 			/*
30687 			 * arg is possibly invalid setting
30688 			 * it to NULL for return
30689 			 */
30690 			arg = NULL;
30691 		}
30692 		break;
30693 	}
30694 
30695 	mutex_exit(SD_MUTEX(un));
30696 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: exit\n");
30697 }
30698 
30699 
30700 /*
30701  *    Function: sd_injection_log()
30702  *
30703  * Description: This routine adds buff to the already existing injection log
30704  *              for retrieval via faultinjection_ioctl for use in fault
30705  *              detection and recovery
30706  *
30707  *   Arguments: buf - the string to add to the log
30708  */
30709 
30710 static void
30711 sd_injection_log(char *buf, struct sd_lun *un)
30712 {
30713 	uint_t len;
30714 
30715 	ASSERT(un != NULL);
30716 	ASSERT(buf != NULL);
30717 
30718 	mutex_enter(&(un->un_fi_mutex));
30719 
30720 	len = min(strlen(buf), 255);
30721 	/* Add logged value to Injection log to be returned later */
30722 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30723 		uint_t	offset = strlen((char *)un->sd_fi_log);
30724 		char *destp = (char *)un->sd_fi_log + offset;
30725 		int i;
30726 		for (i = 0; i < len; i++) {
30727 			*destp++ = *buf++;
30728 		}
30729 		un->sd_fi_buf_len += len;
30730 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30731 	}
30732 
30733 	mutex_exit(&(un->un_fi_mutex));
30734 }
30735 
30736 
30737 /*
30738  *    Function: sd_faultinjection()
30739  *
30740  * Description: This routine takes the pkt and changes its
30741  *		content based on error injection scenerio.
30742  *
30743  *   Arguments: pktp	- packet to be changed
30744  */
30745 
30746 static void
30747 sd_faultinjection(struct scsi_pkt *pktp)
30748 {
30749 	uint_t i;
30750 	struct sd_fi_pkt *fi_pkt;
30751 	struct sd_fi_xb *fi_xb;
30752 	struct sd_fi_un *fi_un;
30753 	struct sd_fi_arq *fi_arq;
30754 	struct buf *bp;
30755 	struct sd_xbuf *xb;
30756 	struct sd_lun *un;
30757 
30758 	ASSERT(pktp != NULL);
30759 
30760 	/* pull bp xb and un from pktp */
30761 	bp = (struct buf *)pktp->pkt_private;
30762 	xb = SD_GET_XBUF(bp);
30763 	un = SD_GET_UN(bp);
30764 
30765 	ASSERT(un != NULL);
30766 
30767 	mutex_enter(SD_MUTEX(un));
30768 
30769 	SD_TRACE(SD_LOG_SDTEST, un,
30770 	    "sd_faultinjection: entry Injection from sdintr\n");
30771 
30772 	/* if injection is off return */
30773 	if (sd_fault_injection_on == 0 ||
30774 	    un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30775 		mutex_exit(SD_MUTEX(un));
30776 		return;
30777 	}
30778 
30779 	SD_INFO(SD_LOG_SDTEST, un,
30780 	    "sd_faultinjection: is working for copying\n");
30781 
30782 	/* take next set off fifo */
30783 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30784 
30785 	fi_pkt = un->sd_fi_fifo_pkt[i];
30786 	fi_xb = un->sd_fi_fifo_xb[i];
30787 	fi_un = un->sd_fi_fifo_un[i];
30788 	fi_arq = un->sd_fi_fifo_arq[i];
30789 
30790 
30791 	/* set variables accordingly */
30792 	/* set pkt if it was on fifo */
30793 	if (fi_pkt != NULL) {
30794 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30795 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30796 		if (fi_pkt->pkt_cdbp != 0xff)
30797 			SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30798 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30799 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30800 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30801 
30802 	}
30803 	/* set xb if it was on fifo */
30804 	if (fi_xb != NULL) {
30805 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30806 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30807 		if (fi_xb->xb_retry_count != 0)
30808 			SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30809 		SD_CONDSET(xb, xb, xb_victim_retry_count,
30810 		    "xb_victim_retry_count");
30811 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30812 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30813 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30814 
30815 		/* copy in block data from sense */
30816 		/*
30817 		 * if (fi_xb->xb_sense_data[0] != -1) {
30818 		 *	bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30819 		 *	SENSE_LENGTH);
30820 		 * }
30821 		 */
30822 		bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30823 
30824 		/* copy in extended sense codes */
30825 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30826 		    xb, es_code, "es_code");
30827 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30828 		    xb, es_key, "es_key");
30829 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30830 		    xb, es_add_code, "es_add_code");
30831 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30832 		    xb, es_qual_code, "es_qual_code");
30833 		struct scsi_extended_sense *esp;
30834 		esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30835 		esp->es_class = CLASS_EXTENDED_SENSE;
30836 	}
30837 
30838 	/* set un if it was on fifo */
30839 	if (fi_un != NULL) {
30840 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30841 		SD_CONDSET(un, un, un_ctype, "un_ctype");
30842 		SD_CONDSET(un, un, un_reset_retry_count,
30843 		    "un_reset_retry_count");
30844 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30845 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30846 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30847 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30848 		    "un_f_allow_bus_device_reset");
30849 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30850 
30851 	}
30852 
30853 	/* copy in auto request sense if it was on fifo */
30854 	if (fi_arq != NULL) {
30855 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30856 	}
30857 
30858 	/* free structs */
30859 	if (un->sd_fi_fifo_pkt[i] != NULL) {
30860 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30861 	}
30862 	if (un->sd_fi_fifo_xb[i] != NULL) {
30863 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30864 	}
30865 	if (un->sd_fi_fifo_un[i] != NULL) {
30866 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30867 	}
30868 	if (un->sd_fi_fifo_arq[i] != NULL) {
30869 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30870 	}
30871 
30872 	/*
30873 	 * kmem_free does not gurantee to set to NULL
30874 	 * since we uses these to determine if we set
30875 	 * values or not lets confirm they are always
30876 	 * NULL after free
30877 	 */
30878 	un->sd_fi_fifo_pkt[i] = NULL;
30879 	un->sd_fi_fifo_un[i] = NULL;
30880 	un->sd_fi_fifo_xb[i] = NULL;
30881 	un->sd_fi_fifo_arq[i] = NULL;
30882 
30883 	un->sd_fi_fifo_start++;
30884 
30885 	mutex_exit(SD_MUTEX(un));
30886 
30887 	SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30888 }
30889 
30890 #endif /* SD_FAULT_INJECTION */
30891 
30892 /*
30893  * This routine is invoked in sd_unit_attach(). Before calling it, the
30894  * properties in conf file should be processed already, and "hotpluggable"
30895  * property was processed also.
30896  *
30897  * The sd driver distinguishes 3 different type of devices: removable media,
30898  * non-removable media, and hotpluggable. Below the differences are defined:
30899  *
30900  * 1. Device ID
30901  *
30902  *     The device ID of a device is used to identify this device. Refer to
30903  *     ddi_devid_register(9F).
30904  *
30905  *     For a non-removable media disk device which can provide 0x80 or 0x83
30906  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30907  *     device ID is created to identify this device. For other non-removable
30908  *     media devices, a default device ID is created only if this device has
30909  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30910  *
30911  *     -------------------------------------------------------
30912  *     removable media   hotpluggable  | Can Have Device ID
30913  *     -------------------------------------------------------
30914  *         false             false     |     Yes
30915  *         false             true      |     Yes
30916  *         true                x       |     No
30917  *     ------------------------------------------------------
30918  *
30919  *
30920  * 2. SCSI group 4 commands
30921  *
30922  *     In SCSI specs, only some commands in group 4 command set can use
30923  *     8-byte addresses that can be used to access >2TB storage spaces.
30924  *     Other commands have no such capability. Without supporting group4,
30925  *     it is impossible to make full use of storage spaces of a disk with
30926  *     capacity larger than 2TB.
30927  *
30928  *     -----------------------------------------------
30929  *     removable media   hotpluggable   LP64  |  Group
30930  *     -----------------------------------------------
30931  *           false          false       false |   1
30932  *           false          false       true  |   4
30933  *           false          true        false |   1
30934  *           false          true        true  |   4
30935  *           true             x           x   |   5
30936  *     -----------------------------------------------
30937  *
30938  *
30939  * 3. Check for VTOC Label
30940  *
30941  *     If a direct-access disk has no EFI label, sd will check if it has a
30942  *     valid VTOC label. Now, sd also does that check for removable media
30943  *     and hotpluggable devices.
30944  *
30945  *     --------------------------------------------------------------
30946  *     Direct-Access   removable media    hotpluggable |  Check Label
30947  *     -------------------------------------------------------------
30948  *         false          false           false        |   No
30949  *         false          false           true         |   No
30950  *         false          true            false        |   Yes
30951  *         false          true            true         |   Yes
30952  *         true            x                x          |   Yes
30953  *     --------------------------------------------------------------
30954  *
30955  *
30956  * 4. Building default VTOC label
30957  *
30958  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30959  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30960  *     create default VTOC for them. Currently sd creates default VTOC label
30961  *     for all devices on x86 platform (VTOC_16), but only for removable
30962  *     media devices on SPARC (VTOC_8).
30963  *
30964  *     -----------------------------------------------------------
30965  *       removable media hotpluggable platform   |   Default Label
30966  *     -----------------------------------------------------------
30967  *             false          false    sparc     |     No
30968  *             false          true      x86      |     Yes
30969  *             false          true     sparc     |     Yes
30970  *             true             x        x       |     Yes
30971  *     ----------------------------------------------------------
30972  *
30973  *
30974  * 5. Supported blocksizes of target devices
30975  *
30976  *     Sd supports non-512-byte blocksize for removable media devices only.
30977  *     For other devices, only 512-byte blocksize is supported. This may be
30978  *     changed in near future because some RAID devices require non-512-byte
30979  *     blocksize
30980  *
30981  *     -----------------------------------------------------------
30982  *     removable media    hotpluggable    | non-512-byte blocksize
30983  *     -----------------------------------------------------------
30984  *           false          false         |   No
30985  *           false          true          |   No
30986  *           true             x           |   Yes
30987  *     -----------------------------------------------------------
30988  *
30989  *
30990  * 6. Automatic mount & unmount
30991  *
30992  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30993  *     if a device is removable media device. It return 1 for removable media
30994  *     devices, and 0 for others.
30995  *
30996  *     The automatic mounting subsystem should distinguish between the types
30997  *     of devices and apply automounting policies to each.
30998  *
30999  *
31000  * 7. fdisk partition management
31001  *
31002  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
31003  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
31004  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
31005  *     fdisk partitions on both x86 and SPARC platform.
31006  *
31007  *     -----------------------------------------------------------
31008  *       platform   removable media  USB/1394  |  fdisk supported
31009  *     -----------------------------------------------------------
31010  *        x86         X               X        |       true
31011  *     ------------------------------------------------------------
31012  *        sparc       X               X        |       false
31013  *     ------------------------------------------------------------
31014  *
31015  *
31016  * 8. MBOOT/MBR
31017  *
31018  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
31019  *     read/write mboot for removable media devices on sparc platform.
31020  *
31021  *     -----------------------------------------------------------
31022  *       platform   removable media  USB/1394  |  mboot supported
31023  *     -----------------------------------------------------------
31024  *        x86         X               X        |       true
31025  *     ------------------------------------------------------------
31026  *        sparc      false           false     |       false
31027  *        sparc      false           true      |       true
31028  *        sparc      true            false     |       true
31029  *        sparc      true            true      |       true
31030  *     ------------------------------------------------------------
31031  *
31032  *
31033  * 9.  error handling during opening device
31034  *
31035  *     If failed to open a disk device, an errno is returned. For some kinds
31036  *     of errors, different errno is returned depending on if this device is
31037  *     a removable media device. This brings USB/1394 hard disks in line with
31038  *     expected hard disk behavior. It is not expected that this breaks any
31039  *     application.
31040  *
31041  *     ------------------------------------------------------
31042  *       removable media    hotpluggable   |  errno
31043  *     ------------------------------------------------------
31044  *             false          false        |   EIO
31045  *             false          true         |   EIO
31046  *             true             x          |   ENXIO
31047  *     ------------------------------------------------------
31048  *
31049  *
31050  * 11. ioctls: DKIOCEJECT, CDROMEJECT
31051  *
31052  *     These IOCTLs are applicable only to removable media devices.
31053  *
31054  *     -----------------------------------------------------------
31055  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
31056  *     -----------------------------------------------------------
31057  *             false          false        |     No
31058  *             false          true         |     No
31059  *             true            x           |     Yes
31060  *     -----------------------------------------------------------
31061  *
31062  *
31063  * 12. Kstats for partitions
31064  *
31065  *     sd creates partition kstat for non-removable media devices. USB and
31066  *     Firewire hard disks now have partition kstats
31067  *
31068  *      ------------------------------------------------------
31069  *       removable media    hotpluggable   |   kstat
31070  *      ------------------------------------------------------
31071  *             false          false        |    Yes
31072  *             false          true         |    Yes
31073  *             true             x          |    No
31074  *       ------------------------------------------------------
31075  *
31076  *
31077  * 13. Removable media & hotpluggable properties
31078  *
31079  *     Sd driver creates a "removable-media" property for removable media
31080  *     devices. Parent nexus drivers create a "hotpluggable" property if
31081  *     it supports hotplugging.
31082  *
31083  *     ---------------------------------------------------------------------
31084  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
31085  *     ---------------------------------------------------------------------
31086  *       false            false       |    No                   No
31087  *       false            true        |    No                   Yes
31088  *       true             false       |    Yes                  No
31089  *       true             true        |    Yes                  Yes
31090  *     ---------------------------------------------------------------------
31091  *
31092  *
31093  * 14. Power Management
31094  *
31095  *     sd only power manages removable media devices or devices that support
31096  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
31097  *
31098  *     A parent nexus that supports hotplugging can also set "pm-capable"
31099  *     if the disk can be power managed.
31100  *
31101  *     ------------------------------------------------------------
31102  *       removable media hotpluggable pm-capable  |   power manage
31103  *     ------------------------------------------------------------
31104  *             false          false     false     |     No
31105  *             false          false     true      |     Yes
31106  *             false          true      false     |     No
31107  *             false          true      true      |     Yes
31108  *             true             x        x        |     Yes
31109  *     ------------------------------------------------------------
31110  *
31111  *      USB and firewire hard disks can now be power managed independently
31112  *      of the framebuffer
31113  *
31114  *
31115  * 15. Support for USB disks with capacity larger than 1TB
31116  *
31117  *     Currently, sd doesn't permit a fixed disk device with capacity
31118  *     larger than 1TB to be used in a 32-bit operating system environment.
31119  *     However, sd doesn't do that for removable media devices. Instead, it
31120  *     assumes that removable media devices cannot have a capacity larger
31121  *     than 1TB. Therefore, using those devices on 32-bit system is partially
31122  *     supported, which can cause some unexpected results.
31123  *
31124  *     ---------------------------------------------------------------------
31125  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
31126  *     ---------------------------------------------------------------------
31127  *             false          false  |   true         |     no
31128  *             false          true   |   true         |     no
31129  *             true           false  |   true         |     Yes
31130  *             true           true   |   true         |     Yes
31131  *     ---------------------------------------------------------------------
31132  *
31133  *
31134  * 16. Check write-protection at open time
31135  *
31136  *     When a removable media device is being opened for writing without NDELAY
31137  *     flag, sd will check if this device is writable. If attempting to open
31138  *     without NDELAY flag a write-protected device, this operation will abort.
31139  *
31140  *     ------------------------------------------------------------
31141  *       removable media    USB/1394   |   WP Check
31142  *     ------------------------------------------------------------
31143  *             false          false    |     No
31144  *             false          true     |     No
31145  *             true           false    |     Yes
31146  *             true           true     |     Yes
31147  *     ------------------------------------------------------------
31148  *
31149  *
31150  * 17. syslog when corrupted VTOC is encountered
31151  *
31152  *      Currently, if an invalid VTOC is encountered, sd only print syslog
31153  *      for fixed SCSI disks.
31154  *     ------------------------------------------------------------
31155  *       removable media    USB/1394   |   print syslog
31156  *     ------------------------------------------------------------
31157  *             false          false    |     Yes
31158  *             false          true     |     No
31159  *             true           false    |     No
31160  *             true           true     |     No
31161  *     ------------------------------------------------------------
31162  */
31163 static void
31164 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
31165 {
31166 	int	pm_cap;
31167 
31168 	ASSERT(un->un_sd);
31169 	ASSERT(un->un_sd->sd_inq);
31170 
31171 	/*
31172 	 * Enable SYNC CACHE support for all devices.
31173 	 */
31174 	un->un_f_sync_cache_supported = TRUE;
31175 
31176 	/*
31177 	 * Set the sync cache required flag to false.
31178 	 * This would ensure that there is no SYNC CACHE
31179 	 * sent when there are no writes
31180 	 */
31181 	un->un_f_sync_cache_required = FALSE;
31182 
31183 	if (un->un_sd->sd_inq->inq_rmb) {
31184 		/*
31185 		 * The media of this device is removable. And for this kind
31186 		 * of devices, it is possible to change medium after opening
31187 		 * devices. Thus we should support this operation.
31188 		 */
31189 		un->un_f_has_removable_media = TRUE;
31190 
31191 		/*
31192 		 * support non-512-byte blocksize of removable media devices
31193 		 */
31194 		un->un_f_non_devbsize_supported = TRUE;
31195 
31196 		/*
31197 		 * Assume that all removable media devices support DOOR_LOCK
31198 		 */
31199 		un->un_f_doorlock_supported = TRUE;
31200 
31201 		/*
31202 		 * For a removable media device, it is possible to be opened
31203 		 * with NDELAY flag when there is no media in drive, in this
31204 		 * case we don't care if device is writable. But if without
31205 		 * NDELAY flag, we need to check if media is write-protected.
31206 		 */
31207 		un->un_f_chk_wp_open = TRUE;
31208 
31209 		/*
31210 		 * need to start a SCSI watch thread to monitor media state,
31211 		 * when media is being inserted or ejected, notify syseventd.
31212 		 */
31213 		un->un_f_monitor_media_state = TRUE;
31214 
31215 		/*
31216 		 * Some devices don't support START_STOP_UNIT command.
31217 		 * Therefore, we'd better check if a device supports it
31218 		 * before sending it.
31219 		 */
31220 		un->un_f_check_start_stop = TRUE;
31221 
31222 		/*
31223 		 * support eject media ioctl:
31224 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
31225 		 */
31226 		un->un_f_eject_media_supported = TRUE;
31227 
31228 		/*
31229 		 * Because many removable-media devices don't support
31230 		 * LOG_SENSE, we couldn't use this command to check if
31231 		 * a removable media device support power-management.
31232 		 * We assume that they support power-management via
31233 		 * START_STOP_UNIT command and can be spun up and down
31234 		 * without limitations.
31235 		 */
31236 		un->un_f_pm_supported = TRUE;
31237 
31238 		/*
31239 		 * Need to create a zero length (Boolean) property
31240 		 * removable-media for the removable media devices.
31241 		 * Note that the return value of the property is not being
31242 		 * checked, since if unable to create the property
31243 		 * then do not want the attach to fail altogether. Consistent
31244 		 * with other property creation in attach.
31245 		 */
31246 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
31247 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
31248 
31249 	} else {
31250 		/*
31251 		 * create device ID for device
31252 		 */
31253 		un->un_f_devid_supported = TRUE;
31254 
31255 		/*
31256 		 * Spin up non-removable-media devices once it is attached
31257 		 */
31258 		un->un_f_attach_spinup = TRUE;
31259 
31260 		/*
31261 		 * According to SCSI specification, Sense data has two kinds of
31262 		 * format: fixed format, and descriptor format. At present, we
31263 		 * don't support descriptor format sense data for removable
31264 		 * media.
31265 		 */
31266 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
31267 			un->un_f_descr_format_supported = TRUE;
31268 		}
31269 
31270 		/*
31271 		 * kstats are created only for non-removable media devices.
31272 		 *
31273 		 * Set this in sd.conf to 0 in order to disable kstats.  The
31274 		 * default is 1, so they are enabled by default.
31275 		 */
31276 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
31277 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
31278 		    "enable-partition-kstats", 1));
31279 
31280 		/*
31281 		 * Check if HBA has set the "pm-capable" property.
31282 		 * If "pm-capable" exists and is non-zero then we can
31283 		 * power manage the device without checking the start/stop
31284 		 * cycle count log sense page.
31285 		 *
31286 		 * If "pm-capable" exists and is set to be false (0),
31287 		 * then we should not power manage the device.
31288 		 *
31289 		 * If "pm-capable" doesn't exist then pm_cap will
31290 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
31291 		 * sd will check the start/stop cycle count log sense page
31292 		 * and power manage the device if the cycle count limit has
31293 		 * not been exceeded.
31294 		 */
31295 		pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
31296 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
31297 		if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
31298 			un->un_f_log_sense_supported = TRUE;
31299 			if (!un->un_f_power_condition_disabled &&
31300 			    SD_INQUIRY(un)->inq_ansi == 6) {
31301 				un->un_f_power_condition_supported = TRUE;
31302 			}
31303 		} else {
31304 			/*
31305 			 * pm-capable property exists.
31306 			 *
31307 			 * Convert "TRUE" values for pm_cap to
31308 			 * SD_PM_CAPABLE_IS_TRUE to make it easier to check
31309 			 * later. "TRUE" values are any values defined in
31310 			 * inquiry.h.
31311 			 */
31312 			if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
31313 				un->un_f_log_sense_supported = FALSE;
31314 			} else {
31315 				/* SD_PM_CAPABLE_IS_TRUE case */
31316 				un->un_f_pm_supported = TRUE;
31317 				if (!un->un_f_power_condition_disabled &&
31318 				    SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
31319 					un->un_f_power_condition_supported =
31320 					    TRUE;
31321 				}
31322 				if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
31323 					un->un_f_log_sense_supported = TRUE;
31324 					un->un_f_pm_log_sense_smart =
31325 					    SD_PM_CAP_SMART_LOG(pm_cap);
31326 				}
31327 			}
31328 
31329 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
31330 			    "sd_unit_attach: un:0x%p pm-capable "
31331 			    "property set to %d.\n", un, un->un_f_pm_supported);
31332 		}
31333 	}
31334 
31335 	if (un->un_f_is_hotpluggable) {
31336 
31337 		/*
31338 		 * Have to watch hotpluggable devices as well, since
31339 		 * that's the only way for userland applications to
31340 		 * detect hot removal while device is busy/mounted.
31341 		 */
31342 		un->un_f_monitor_media_state = TRUE;
31343 
31344 		un->un_f_check_start_stop = TRUE;
31345 
31346 	}
31347 }
31348 
31349 /*
31350  * sd_tg_rdwr:
31351  * Provides rdwr access for cmlb via sd_tgops. The start_block is
31352  * in sys block size, req_length in bytes.
31353  *
31354  */
31355 static int
31356 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
31357     diskaddr_t start_block, size_t reqlength, void *tg_cookie)
31358 {
31359 	struct sd_lun *un;
31360 	int path_flag = (int)(uintptr_t)tg_cookie;
31361 	char *dkl = NULL;
31362 	diskaddr_t real_addr = start_block;
31363 	diskaddr_t first_byte, end_block;
31364 
31365 	size_t	buffer_size = reqlength;
31366 	int rval = 0;
31367 	diskaddr_t	cap;
31368 	uint32_t	lbasize;
31369 	sd_ssc_t	*ssc;
31370 
31371 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31372 	if (un == NULL)
31373 		return (ENXIO);
31374 
31375 	if (cmd != TG_READ && cmd != TG_WRITE)
31376 		return (EINVAL);
31377 
31378 	ssc = sd_ssc_init(un);
31379 	mutex_enter(SD_MUTEX(un));
31380 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
31381 		mutex_exit(SD_MUTEX(un));
31382 		rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31383 		    &lbasize, path_flag);
31384 		if (rval != 0)
31385 			goto done1;
31386 		mutex_enter(SD_MUTEX(un));
31387 		sd_update_block_info(un, lbasize, cap);
31388 		if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
31389 			mutex_exit(SD_MUTEX(un));
31390 			rval = EIO;
31391 			goto done;
31392 		}
31393 	}
31394 
31395 	if (NOT_DEVBSIZE(un)) {
31396 		/*
31397 		 * sys_blocksize != tgt_blocksize, need to re-adjust
31398 		 * blkno and save the index to beginning of dk_label
31399 		 */
31400 		first_byte  = SD_SYSBLOCKS2BYTES(start_block);
31401 		real_addr = first_byte / un->un_tgt_blocksize;
31402 
31403 		end_block = (first_byte + reqlength +
31404 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
31405 
31406 		/* round up buffer size to multiple of target block size */
31407 		buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
31408 
31409 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
31410 		    "label_addr: 0x%x allocation size: 0x%x\n",
31411 		    real_addr, buffer_size);
31412 
31413 		if (((first_byte % un->un_tgt_blocksize) != 0) ||
31414 		    (reqlength % un->un_tgt_blocksize) != 0)
31415 			/* the request is not aligned */
31416 			dkl = kmem_zalloc(buffer_size, KM_SLEEP);
31417 	}
31418 
31419 	/*
31420 	 * The MMC standard allows READ CAPACITY to be
31421 	 * inaccurate by a bounded amount (in the interest of
31422 	 * response latency).  As a result, failed READs are
31423 	 * commonplace (due to the reading of metadata and not
31424 	 * data). Depending on the per-Vendor/drive Sense data,
31425 	 * the failed READ can cause many (unnecessary) retries.
31426 	 */
31427 
31428 	if (ISCD(un) && (cmd == TG_READ) &&
31429 	    (un->un_f_blockcount_is_valid == TRUE) &&
31430 	    ((start_block == (un->un_blockcount - 1))||
31431 	    (start_block == (un->un_blockcount - 2)))) {
31432 			path_flag = SD_PATH_DIRECT_PRIORITY;
31433 	}
31434 
31435 	mutex_exit(SD_MUTEX(un));
31436 	if (cmd == TG_READ) {
31437 		rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
31438 		    buffer_size, real_addr, path_flag);
31439 		if (dkl != NULL)
31440 			bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
31441 			    real_addr), bufaddr, reqlength);
31442 	} else {
31443 		if (dkl) {
31444 			rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
31445 			    real_addr, path_flag);
31446 			if (rval) {
31447 				goto done1;
31448 			}
31449 			bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
31450 			    real_addr), reqlength);
31451 		}
31452 		rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
31453 		    buffer_size, real_addr, path_flag);
31454 	}
31455 
31456 done1:
31457 	if (dkl != NULL)
31458 		kmem_free(dkl, buffer_size);
31459 
31460 	if (rval != 0) {
31461 		if (rval == EIO)
31462 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
31463 		else
31464 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31465 	}
31466 done:
31467 	sd_ssc_fini(ssc);
31468 	return (rval);
31469 }
31470 
31471 
31472 static int
31473 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
31474 {
31475 
31476 	struct sd_lun *un;
31477 	diskaddr_t	cap;
31478 	uint32_t	lbasize;
31479 	int		path_flag = (int)(uintptr_t)tg_cookie;
31480 	int		ret = 0;
31481 
31482 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31483 	if (un == NULL)
31484 		return (ENXIO);
31485 
31486 	switch (cmd) {
31487 	case TG_GETPHYGEOM:
31488 	case TG_GETVIRTGEOM:
31489 	case TG_GETCAPACITY:
31490 	case TG_GETBLOCKSIZE:
31491 		mutex_enter(SD_MUTEX(un));
31492 
31493 		if ((un->un_f_blockcount_is_valid == TRUE) &&
31494 		    (un->un_f_tgt_blocksize_is_valid == TRUE)) {
31495 			cap = un->un_blockcount;
31496 			lbasize = un->un_tgt_blocksize;
31497 			mutex_exit(SD_MUTEX(un));
31498 		} else {
31499 			sd_ssc_t	*ssc;
31500 			mutex_exit(SD_MUTEX(un));
31501 			ssc = sd_ssc_init(un);
31502 			ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31503 			    &lbasize, path_flag);
31504 			if (ret != 0) {
31505 				if (ret == EIO)
31506 					sd_ssc_assessment(ssc,
31507 					    SD_FMT_STATUS_CHECK);
31508 				else
31509 					sd_ssc_assessment(ssc,
31510 					    SD_FMT_IGNORE);
31511 				sd_ssc_fini(ssc);
31512 				return (ret);
31513 			}
31514 			sd_ssc_fini(ssc);
31515 			mutex_enter(SD_MUTEX(un));
31516 			sd_update_block_info(un, lbasize, cap);
31517 			if ((un->un_f_blockcount_is_valid == FALSE) ||
31518 			    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
31519 				mutex_exit(SD_MUTEX(un));
31520 				return (EIO);
31521 			}
31522 			mutex_exit(SD_MUTEX(un));
31523 		}
31524 
31525 		if (cmd == TG_GETCAPACITY) {
31526 			*(diskaddr_t *)arg = cap;
31527 			return (0);
31528 		}
31529 
31530 		if (cmd == TG_GETBLOCKSIZE) {
31531 			*(uint32_t *)arg = lbasize;
31532 			return (0);
31533 		}
31534 
31535 		if (cmd == TG_GETPHYGEOM)
31536 			ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
31537 			    cap, lbasize, path_flag);
31538 		else
31539 			/* TG_GETVIRTGEOM */
31540 			ret = sd_get_virtual_geometry(un,
31541 			    (cmlb_geom_t *)arg, cap, lbasize);
31542 
31543 		return (ret);
31544 
31545 	case TG_GETATTR:
31546 		mutex_enter(SD_MUTEX(un));
31547 		((tg_attribute_t *)arg)->media_is_writable =
31548 		    un->un_f_mmc_writable_media;
31549 		((tg_attribute_t *)arg)->media_is_solid_state =
31550 		    un->un_f_is_solid_state;
31551 		((tg_attribute_t *)arg)->media_is_rotational =
31552 		    un->un_f_is_rotational;
31553 		mutex_exit(SD_MUTEX(un));
31554 		return (0);
31555 	default:
31556 		return (ENOTTY);
31557 
31558 	}
31559 }
31560 
31561 /*
31562  *    Function: sd_ssc_ereport_post
31563  *
31564  * Description: Will be called when SD driver need to post an ereport.
31565  *
31566  *    Context: Kernel thread or interrupt context.
31567  */
31568 
31569 #define	DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
31570 
31571 static void
31572 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31573 {
31574 	int uscsi_path_instance = 0;
31575 	uchar_t	uscsi_pkt_reason;
31576 	uint32_t uscsi_pkt_state;
31577 	uint32_t uscsi_pkt_statistics;
31578 	uint64_t uscsi_ena;
31579 	uchar_t op_code;
31580 	uint8_t *sensep;
31581 	union scsi_cdb *cdbp;
31582 	uint_t cdblen = 0;
31583 	uint_t senlen = 0;
31584 	struct sd_lun *un;
31585 	dev_info_t *dip;
31586 	char *devid;
31587 	int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31588 	    SSC_FLAGS_INVALID_STATUS |
31589 	    SSC_FLAGS_INVALID_SENSE |
31590 	    SSC_FLAGS_INVALID_DATA;
31591 	char assessment[16];
31592 
31593 	ASSERT(ssc != NULL);
31594 	ASSERT(ssc->ssc_uscsi_cmd != NULL);
31595 	ASSERT(ssc->ssc_uscsi_info != NULL);
31596 
31597 	un = ssc->ssc_un;
31598 	ASSERT(un != NULL);
31599 
31600 	dip = un->un_sd->sd_dev;
31601 
31602 	/*
31603 	 * Get the devid:
31604 	 *	devid will only be passed to non-transport error reports.
31605 	 */
31606 	devid = DEVI(dip)->devi_devid_str;
31607 
31608 	/*
31609 	 * If we are syncing or dumping, the command will not be executed
31610 	 * so we bypass this situation.
31611 	 */
31612 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31613 	    (un->un_state == SD_STATE_DUMPING))
31614 		return;
31615 
31616 	uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31617 	uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31618 	uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31619 	uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31620 	uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31621 
31622 	sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31623 	cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31624 
31625 	/* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31626 	if (cdbp == NULL) {
31627 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31628 		    "sd_ssc_ereport_post meet empty cdb\n");
31629 		return;
31630 	}
31631 
31632 	op_code = cdbp->scc_cmd;
31633 
31634 	cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31635 	senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31636 	    ssc->ssc_uscsi_cmd->uscsi_rqresid);
31637 
31638 	if (senlen > 0)
31639 		ASSERT(sensep != NULL);
31640 
31641 	/*
31642 	 * Initialize drv_assess to corresponding values.
31643 	 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31644 	 * on the sense-key returned back.
31645 	 */
31646 	switch (drv_assess) {
31647 		case SD_FM_DRV_RECOVERY:
31648 			(void) sprintf(assessment, "%s", "recovered");
31649 			break;
31650 		case SD_FM_DRV_RETRY:
31651 			(void) sprintf(assessment, "%s", "retry");
31652 			break;
31653 		case SD_FM_DRV_NOTICE:
31654 			(void) sprintf(assessment, "%s", "info");
31655 			break;
31656 		case SD_FM_DRV_FATAL:
31657 		default:
31658 			(void) sprintf(assessment, "%s", "unknown");
31659 	}
31660 	/*
31661 	 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31662 	 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31663 	 * driver-assessment will always be "recovered" here.
31664 	 */
31665 	if (drv_assess == SD_FM_DRV_RECOVERY) {
31666 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31667 		    "cmd.disk.recovered", uscsi_ena, devid, NULL,
31668 		    DDI_NOSLEEP, NULL,
31669 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31670 		    DEVID_IF_KNOWN(devid),
31671 		    "driver-assessment", DATA_TYPE_STRING, assessment,
31672 		    "op-code", DATA_TYPE_UINT8, op_code,
31673 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31674 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31675 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31676 		    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31677 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31678 		    NULL);
31679 		return;
31680 	}
31681 
31682 	/*
31683 	 * If there is un-expected/un-decodable data, we should post
31684 	 * ereport.io.scsi.cmd.disk.dev.uderr.
31685 	 * driver-assessment will be set based on parameter drv_assess.
31686 	 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31687 	 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31688 	 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31689 	 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31690 	 */
31691 	if (ssc->ssc_flags & ssc_invalid_flags) {
31692 		if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31693 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31694 			    NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31695 			    NULL, DDI_NOSLEEP, NULL,
31696 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31697 			    DEVID_IF_KNOWN(devid),
31698 			    "driver-assessment", DATA_TYPE_STRING,
31699 			    drv_assess == SD_FM_DRV_FATAL ?
31700 			    "fail" : assessment,
31701 			    "op-code", DATA_TYPE_UINT8, op_code,
31702 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31703 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31704 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31705 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31706 			    "pkt-stats", DATA_TYPE_UINT32,
31707 			    uscsi_pkt_statistics,
31708 			    "stat-code", DATA_TYPE_UINT8,
31709 			    ssc->ssc_uscsi_cmd->uscsi_status,
31710 			    "un-decode-info", DATA_TYPE_STRING,
31711 			    ssc->ssc_info,
31712 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31713 			    senlen, sensep,
31714 			    NULL);
31715 		} else {
31716 			/*
31717 			 * For other type of invalid data, the
31718 			 * un-decode-value field would be empty because the
31719 			 * un-decodable content could be seen from upper
31720 			 * level payload or inside un-decode-info.
31721 			 */
31722 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31723 			    NULL,
31724 			    "cmd.disk.dev.uderr", uscsi_ena, devid,
31725 			    NULL, DDI_NOSLEEP, NULL,
31726 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31727 			    DEVID_IF_KNOWN(devid),
31728 			    "driver-assessment", DATA_TYPE_STRING,
31729 			    drv_assess == SD_FM_DRV_FATAL ?
31730 			    "fail" : assessment,
31731 			    "op-code", DATA_TYPE_UINT8, op_code,
31732 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31733 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31734 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31735 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31736 			    "pkt-stats", DATA_TYPE_UINT32,
31737 			    uscsi_pkt_statistics,
31738 			    "stat-code", DATA_TYPE_UINT8,
31739 			    ssc->ssc_uscsi_cmd->uscsi_status,
31740 			    "un-decode-info", DATA_TYPE_STRING,
31741 			    ssc->ssc_info,
31742 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31743 			    0, NULL,
31744 			    NULL);
31745 		}
31746 		ssc->ssc_flags &= ~ssc_invalid_flags;
31747 		return;
31748 	}
31749 
31750 	if (uscsi_pkt_reason != CMD_CMPLT ||
31751 	    (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31752 		/*
31753 		 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31754 		 * set inside sd_start_cmds due to errors(bad packet or
31755 		 * fatal transport error), we should take it as a
31756 		 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31757 		 * driver-assessment will be set based on drv_assess.
31758 		 * We will set devid to NULL because it is a transport
31759 		 * error.
31760 		 */
31761 		if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31762 			ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31763 
31764 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31765 		    "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31766 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31767 		    DEVID_IF_KNOWN(devid),
31768 		    "driver-assessment", DATA_TYPE_STRING,
31769 		    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31770 		    "op-code", DATA_TYPE_UINT8, op_code,
31771 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31772 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31773 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31774 		    "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31775 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31776 		    NULL);
31777 	} else {
31778 		/*
31779 		 * If we got here, we have a completed command, and we need
31780 		 * to further investigate the sense data to see what kind
31781 		 * of ereport we should post.
31782 		 * No ereport is needed if sense-key is KEY_RECOVERABLE_ERROR
31783 		 * and asc/ascq is "ATA PASS-THROUGH INFORMATION AVAILABLE".
31784 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr if sense-key is
31785 		 * KEY_MEDIUM_ERROR.
31786 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31787 		 * driver-assessment will be set based on the parameter
31788 		 * drv_assess.
31789 		 */
31790 		if (senlen > 0) {
31791 			/*
31792 			 * Here we have sense data available.
31793 			 */
31794 			uint8_t sense_key = scsi_sense_key(sensep);
31795 			uint8_t sense_asc = scsi_sense_asc(sensep);
31796 			uint8_t sense_ascq = scsi_sense_ascq(sensep);
31797 
31798 			if (sense_key == KEY_RECOVERABLE_ERROR &&
31799 			    sense_asc == 0x00 && sense_ascq == 0x1d)
31800 				return;
31801 
31802 			if (sense_key == KEY_MEDIUM_ERROR) {
31803 				/*
31804 				 * driver-assessment should be "fatal" if
31805 				 * drv_assess is SD_FM_DRV_FATAL.
31806 				 */
31807 				scsi_fm_ereport_post(un->un_sd,
31808 				    uscsi_path_instance, NULL,
31809 				    "cmd.disk.dev.rqs.merr",
31810 				    uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31811 				    FM_VERSION, DATA_TYPE_UINT8,
31812 				    FM_EREPORT_VERS0,
31813 				    DEVID_IF_KNOWN(devid),
31814 				    "driver-assessment",
31815 				    DATA_TYPE_STRING,
31816 				    drv_assess == SD_FM_DRV_FATAL ?
31817 				    "fatal" : assessment,
31818 				    "op-code",
31819 				    DATA_TYPE_UINT8, op_code,
31820 				    "cdb",
31821 				    DATA_TYPE_UINT8_ARRAY, cdblen,
31822 				    ssc->ssc_uscsi_cmd->uscsi_cdb,
31823 				    "pkt-reason",
31824 				    DATA_TYPE_UINT8, uscsi_pkt_reason,
31825 				    "pkt-state",
31826 				    DATA_TYPE_UINT8, uscsi_pkt_state,
31827 				    "pkt-stats",
31828 				    DATA_TYPE_UINT32,
31829 				    uscsi_pkt_statistics,
31830 				    "stat-code",
31831 				    DATA_TYPE_UINT8,
31832 				    ssc->ssc_uscsi_cmd->uscsi_status,
31833 				    "key",
31834 				    DATA_TYPE_UINT8,
31835 				    scsi_sense_key(sensep),
31836 				    "asc",
31837 				    DATA_TYPE_UINT8,
31838 				    scsi_sense_asc(sensep),
31839 				    "ascq",
31840 				    DATA_TYPE_UINT8,
31841 				    scsi_sense_ascq(sensep),
31842 				    "sense-data",
31843 				    DATA_TYPE_UINT8_ARRAY,
31844 				    senlen, sensep,
31845 				    "lba",
31846 				    DATA_TYPE_UINT64,
31847 				    ssc->ssc_uscsi_info->ui_lba,
31848 				    NULL);
31849 			} else {
31850 				/*
31851 				 * if sense-key == 0x4(hardware
31852 				 * error), driver-assessment should
31853 				 * be "fatal" if drv_assess is
31854 				 * SD_FM_DRV_FATAL.
31855 				 */
31856 				scsi_fm_ereport_post(un->un_sd,
31857 				    uscsi_path_instance, NULL,
31858 				    "cmd.disk.dev.rqs.derr",
31859 				    uscsi_ena, devid,
31860 				    NULL, DDI_NOSLEEP, NULL,
31861 				    FM_VERSION,
31862 				    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31863 				    DEVID_IF_KNOWN(devid),
31864 				    "driver-assessment",
31865 				    DATA_TYPE_STRING,
31866 				    drv_assess == SD_FM_DRV_FATAL ?
31867 				    (sense_key == 0x4 ?
31868 				    "fatal" : "fail") : assessment,
31869 				    "op-code",
31870 				    DATA_TYPE_UINT8, op_code,
31871 				    "cdb",
31872 				    DATA_TYPE_UINT8_ARRAY, cdblen,
31873 				    ssc->ssc_uscsi_cmd->uscsi_cdb,
31874 				    "pkt-reason",
31875 				    DATA_TYPE_UINT8, uscsi_pkt_reason,
31876 				    "pkt-state",
31877 				    DATA_TYPE_UINT8, uscsi_pkt_state,
31878 				    "pkt-stats",
31879 				    DATA_TYPE_UINT32,
31880 				    uscsi_pkt_statistics,
31881 				    "stat-code",
31882 				    DATA_TYPE_UINT8,
31883 				    ssc->ssc_uscsi_cmd->uscsi_status,
31884 				    "key",
31885 				    DATA_TYPE_UINT8,
31886 				    scsi_sense_key(sensep),
31887 				    "asc",
31888 				    DATA_TYPE_UINT8,
31889 				    scsi_sense_asc(sensep),
31890 				    "ascq",
31891 				    DATA_TYPE_UINT8,
31892 				    scsi_sense_ascq(sensep),
31893 				    "sense-data",
31894 				    DATA_TYPE_UINT8_ARRAY,
31895 				    senlen, sensep,
31896 				    NULL);
31897 			}
31898 		} else {
31899 			/*
31900 			 * For stat_code == STATUS_GOOD, this is not a
31901 			 * hardware error.
31902 			 */
31903 			if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31904 				return;
31905 
31906 			/*
31907 			 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31908 			 * stat-code but with sense data unavailable.
31909 			 * driver-assessment will be set based on parameter
31910 			 * drv_assess.
31911 			 */
31912 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31913 			    NULL,
31914 			    "cmd.disk.dev.serr", uscsi_ena,
31915 			    devid, NULL, DDI_NOSLEEP, NULL,
31916 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31917 			    DEVID_IF_KNOWN(devid),
31918 			    "driver-assessment", DATA_TYPE_STRING,
31919 			    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31920 			    "op-code", DATA_TYPE_UINT8, op_code,
31921 			    "cdb",
31922 			    DATA_TYPE_UINT8_ARRAY,
31923 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31924 			    "pkt-reason",
31925 			    DATA_TYPE_UINT8, uscsi_pkt_reason,
31926 			    "pkt-state",
31927 			    DATA_TYPE_UINT8, uscsi_pkt_state,
31928 			    "pkt-stats",
31929 			    DATA_TYPE_UINT32, uscsi_pkt_statistics,
31930 			    "stat-code",
31931 			    DATA_TYPE_UINT8,
31932 			    ssc->ssc_uscsi_cmd->uscsi_status,
31933 			    NULL);
31934 		}
31935 	}
31936 }
31937 
31938 /*
31939  *     Function: sd_ssc_extract_info
31940  *
31941  * Description: Extract information available to help generate ereport.
31942  *
31943  *     Context: Kernel thread or interrupt context.
31944  */
31945 static void
31946 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31947     struct buf *bp, struct sd_xbuf *xp)
31948 {
31949 	size_t senlen = 0;
31950 	union scsi_cdb *cdbp;
31951 	int path_instance;
31952 	/*
31953 	 * Need scsi_cdb_size array to determine the cdb length.
31954 	 */
31955 	extern uchar_t	scsi_cdb_size[];
31956 
31957 	ASSERT(un != NULL);
31958 	ASSERT(pktp != NULL);
31959 	ASSERT(bp != NULL);
31960 	ASSERT(xp != NULL);
31961 	ASSERT(ssc != NULL);
31962 	ASSERT(mutex_owned(SD_MUTEX(un)));
31963 
31964 	/*
31965 	 * Transfer the cdb buffer pointer here.
31966 	 */
31967 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31968 
31969 	ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31970 	ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31971 
31972 	/*
31973 	 * Transfer the sense data buffer pointer if sense data is available,
31974 	 * calculate the sense data length first.
31975 	 */
31976 	if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31977 	    (xp->xb_sense_state & STATE_ARQ_DONE)) {
31978 		/*
31979 		 * For arq case, we will enter here.
31980 		 */
31981 		if (xp->xb_sense_state & STATE_XARQ_DONE) {
31982 			senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31983 		} else {
31984 			senlen = SENSE_LENGTH;
31985 		}
31986 	} else {
31987 		/*
31988 		 * For non-arq case, we will enter this branch.
31989 		 */
31990 		if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31991 		    (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31992 			senlen = SENSE_LENGTH - xp->xb_sense_resid;
31993 		}
31994 
31995 	}
31996 
31997 	ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31998 	ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31999 	ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
32000 
32001 	ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
32002 
32003 	/*
32004 	 * Only transfer path_instance when scsi_pkt was properly allocated.
32005 	 */
32006 	path_instance = pktp->pkt_path_instance;
32007 	if (scsi_pkt_allocated_correctly(pktp) && path_instance)
32008 		ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
32009 	else
32010 		ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
32011 
32012 	/*
32013 	 * Copy in the other fields we may need when posting ereport.
32014 	 */
32015 	ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
32016 	ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
32017 	ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
32018 	ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
32019 
32020 	/*
32021 	 * For partially read/write command, we will not create ena
32022 	 * in case of a successful command be reconized as recovered.
32023 	 */
32024 	if ((pktp->pkt_reason == CMD_CMPLT) &&
32025 	    (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
32026 	    (senlen == 0)) {
32027 		return;
32028 	}
32029 
32030 	/*
32031 	 * To associate ereports of a single command execution flow, we
32032 	 * need a shared ena for a specific command.
32033 	 */
32034 	if (xp->xb_ena == 0)
32035 		xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
32036 	ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
32037 }
32038 
32039 
32040 /*
32041  *     Function: sd_check_bdc_vpd
32042  *
32043  * Description: Query the optional INQUIRY VPD page 0xb1. If the device
32044  *              supports VPD page 0xb1, sd examines the MEDIUM ROTATION
32045  *              RATE.
32046  *
32047  *		Set the following based on RPM value:
32048  *		= 0	device is not solid state, non-rotational
32049  *		= 1	device is solid state, non-rotational
32050  *		> 1	device is not solid state, rotational
32051  *
32052  *     Context: Kernel thread or interrupt context.
32053  */
32054 
32055 static void
32056 sd_check_bdc_vpd(sd_ssc_t *ssc)
32057 {
32058 	int		rval		= 0;
32059 	uchar_t		*inqb1		= NULL;
32060 	size_t		inqb1_len	= MAX_INQUIRY_SIZE;
32061 	size_t		inqb1_resid	= 0;
32062 	struct sd_lun	*un;
32063 
32064 	ASSERT(ssc != NULL);
32065 	un = ssc->ssc_un;
32066 	ASSERT(un != NULL);
32067 	ASSERT(!mutex_owned(SD_MUTEX(un)));
32068 
32069 	mutex_enter(SD_MUTEX(un));
32070 	un->un_f_is_rotational = TRUE;
32071 	un->un_f_is_solid_state = FALSE;
32072 
32073 	if (ISCD(un)) {
32074 		mutex_exit(SD_MUTEX(un));
32075 		return;
32076 	}
32077 
32078 	if (sd_check_vpd_page_support(ssc) == 0 &&
32079 	    un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
32080 		mutex_exit(SD_MUTEX(un));
32081 		/* collect page b1 data */
32082 		inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
32083 
32084 		rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
32085 		    0x01, 0xB1, &inqb1_resid);
32086 
32087 		if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
32088 			SD_TRACE(SD_LOG_COMMON, un,
32089 			    "sd_check_bdc_vpd: \
32090 			    successfully get VPD page: %x \
32091 			    PAGE LENGTH: %x BYTE 4: %x \
32092 			    BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
32093 			    inqb1[5]);
32094 
32095 			mutex_enter(SD_MUTEX(un));
32096 			/*
32097 			 * Check the MEDIUM ROTATION RATE.
32098 			 */
32099 			if (inqb1[4] == 0) {
32100 				if (inqb1[5] == 0) {
32101 					un->un_f_is_rotational = FALSE;
32102 				} else if (inqb1[5] == 1) {
32103 					un->un_f_is_rotational = FALSE;
32104 					un->un_f_is_solid_state = TRUE;
32105 					/*
32106 					 * Solid state drives don't need
32107 					 * disksort.
32108 					 */
32109 					un->un_f_disksort_disabled = TRUE;
32110 				}
32111 			}
32112 			mutex_exit(SD_MUTEX(un));
32113 		} else if (rval != 0) {
32114 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
32115 		}
32116 
32117 		kmem_free(inqb1, inqb1_len);
32118 	} else {
32119 		mutex_exit(SD_MUTEX(un));
32120 	}
32121 }
32122 
32123 /*
32124  *	Function: sd_check_emulation_mode
32125  *
32126  *   Description: Check whether the SSD is at emulation mode
32127  *		  by issuing READ_CAPACITY_16 to see whether
32128  *		  we can get physical block size of the drive.
32129  *
32130  *	 Context: Kernel thread or interrupt context.
32131  */
32132 
32133 static void
32134 sd_check_emulation_mode(sd_ssc_t *ssc)
32135 {
32136 	int		rval = 0;
32137 	uint64_t	capacity;
32138 	uint_t		lbasize;
32139 	uint_t		pbsize;
32140 	int		i;
32141 	int		devid_len;
32142 	struct sd_lun	*un;
32143 
32144 	ASSERT(ssc != NULL);
32145 	un = ssc->ssc_un;
32146 	ASSERT(un != NULL);
32147 	ASSERT(!mutex_owned(SD_MUTEX(un)));
32148 
32149 	mutex_enter(SD_MUTEX(un));
32150 	if (ISCD(un)) {
32151 		mutex_exit(SD_MUTEX(un));
32152 		return;
32153 	}
32154 
32155 	if (un->un_f_descr_format_supported) {
32156 		mutex_exit(SD_MUTEX(un));
32157 		rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
32158 		    &pbsize, SD_PATH_DIRECT);
32159 		mutex_enter(SD_MUTEX(un));
32160 
32161 		if (rval != 0) {
32162 			un->un_phy_blocksize = DEV_BSIZE;
32163 		} else {
32164 			if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
32165 				un->un_phy_blocksize = DEV_BSIZE;
32166 			} else if (pbsize > un->un_phy_blocksize) {
32167 				/*
32168 				 * Don't reset the physical blocksize
32169 				 * unless we've detected a larger value.
32170 				 */
32171 				un->un_phy_blocksize = pbsize;
32172 			}
32173 		}
32174 	}
32175 
32176 	for (i = 0; i < sd_flash_dev_table_size; i++) {
32177 		devid_len = (int)strlen(sd_flash_dev_table[i]);
32178 		if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
32179 		    == SD_SUCCESS) {
32180 			un->un_phy_blocksize = SSD_SECSIZE;
32181 			if (un->un_f_is_solid_state &&
32182 			    un->un_phy_blocksize != un->un_tgt_blocksize)
32183 				un->un_f_enable_rmw = TRUE;
32184 		}
32185 	}
32186 
32187 	mutex_exit(SD_MUTEX(un));
32188 }
32189