xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * SCSI disk target driver.
30  */
31 #include <sys/scsi/scsi.h>
32 #include <sys/dkbad.h>
33 #include <sys/dklabel.h>
34 #include <sys/dkio.h>
35 #include <sys/fdio.h>
36 #include <sys/cdio.h>
37 #include <sys/mhd.h>
38 #include <sys/vtoc.h>
39 #include <sys/dktp/fdisk.h>
40 #include <sys/file.h>
41 #include <sys/stat.h>
42 #include <sys/kstat.h>
43 #include <sys/vtrace.h>
44 #include <sys/note.h>
45 #include <sys/thread.h>
46 #include <sys/proc.h>
47 #include <sys/efi_partition.h>
48 #include <sys/var.h>
49 #include <sys/aio_req.h>
50 
51 #ifdef __lock_lint
52 #define	_LP64
53 #define	__amd64
54 #endif
55 
56 #if (defined(__fibre))
57 /* Note: is there a leadville version of the following? */
58 #include <sys/fc4/fcal_linkapp.h>
59 #endif
60 #include <sys/taskq.h>
61 #include <sys/uuid.h>
62 #include <sys/byteorder.h>
63 #include <sys/sdt.h>
64 
65 #include "sd_xbuf.h"
66 
67 #include <sys/scsi/targets/sddef.h>
68 
69 
70 /*
71  * Loadable module info.
72  */
73 #if (defined(__fibre))
74 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver %I%"
75 char _depends_on[]	= "misc/scsi drv/fcp";
76 #else
77 #define	SD_MODULE_NAME	"SCSI Disk Driver %I%"
78 char _depends_on[]	= "misc/scsi";
79 #endif
80 
81 /*
82  * Define the interconnect type, to allow the driver to distinguish
83  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
84  *
85  * This is really for backward compatability. In the future, the driver
86  * should actually check the "interconnect-type" property as reported by
87  * the HBA; however at present this property is not defined by all HBAs,
88  * so we will use this #define (1) to permit the driver to run in
89  * backward-compatability mode; and (2) to print a notification message
90  * if an FC HBA does not support the "interconnect-type" property.  The
91  * behavior of the driver will be to assume parallel SCSI behaviors unless
92  * the "interconnect-type" property is defined by the HBA **AND** has a
93  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
94  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
95  * Channel behaviors (as per the old ssd).  (Note that the
96  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
97  * will result in the driver assuming parallel SCSI behaviors.)
98  *
99  * (see common/sys/scsi/impl/services.h)
100  *
101  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
102  * since some FC HBAs may already support that, and there is some code in
103  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
104  * default would confuse that code, and besides things should work fine
105  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
106  * "interconnect_type" property.
107  *
108  * Notes for off-by-1 workaround:
109  * -----------------------------
110  *
111  *    SCSI READ_CAPACITY command returns the LBA number of the
112  *    last logical block, but sd once treated this number as
113  *    disks' capacity on x86 platform. And LBAs are addressed
114  *    based 0. So the last block was lost on x86 platform.
115  *
116  *    Now, we remove this workaround. In order for present sd
117  *    driver to work with disks which are labeled/partitioned
118  *    via previous sd, we add workaround as follows:
119  *
120  *    1) Locate backup EFI label: sd searchs the next to last
121  *       block for backup EFI label if it can't find it on the
122  *       last block;
123  *    2) Calculate geometry: refer to sd_convert_geometry(), If
124  *       capacity increasing by 1 causes disks' capacity to cross
125  *       over the limits in table CHS_values, geometry info will
126  *       change. This will raise an issue: In case that primary
127  *       VTOC label is destroyed, format commandline can restore
128  *       it via backup VTOC labels. And format locates backup VTOC
129  *       labels by use of geometry from sd driver. So changing
130  *       geometry will prevent format from finding backup VTOC
131  *       labels. To eliminate this side effect for compatibility,
132  *       sd uses (capacity -1) to calculate geometry;
133  *    3) 1TB disks: VTOC uses 32-bit signed int, thus sd doesn't
134  *       support VTOC for a disk which has more than DK_MAX_BLOCKS
135  *       LBAs. However, for exactly 1TB disk, it was treated as
136  *       (1T - 512)B in the past, and could have VTOC. To overcome
137  *       this, if an exactly 1TB disk has solaris fdisk partition,
138  *       it will be allowed to work with sd.
139  */
140 #if (defined(__fibre))
141 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
142 #else
143 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
144 #endif
145 
146 /*
147  * The name of the driver, established from the module name in _init.
148  */
149 static	char *sd_label			= NULL;
150 
151 /*
152  * Driver name is unfortunately prefixed on some driver.conf properties.
153  */
154 #if (defined(__fibre))
155 #define	sd_max_xfer_size		ssd_max_xfer_size
156 #define	sd_config_list			ssd_config_list
157 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
158 static	char *sd_config_list		= "ssd-config-list";
159 #else
160 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
161 static	char *sd_config_list		= "sd-config-list";
162 #endif
163 
164 /*
165  * Driver global variables
166  */
167 
168 #if (defined(__fibre))
169 /*
170  * These #defines are to avoid namespace collisions that occur because this
171  * code is currently used to compile two seperate driver modules: sd and ssd.
172  * All global variables need to be treated this way (even if declared static)
173  * in order to allow the debugger to resolve the names properly.
174  * It is anticipated that in the near future the ssd module will be obsoleted,
175  * at which time this namespace issue should go away.
176  */
177 #define	sd_state			ssd_state
178 #define	sd_io_time			ssd_io_time
179 #define	sd_failfast_enable		ssd_failfast_enable
180 #define	sd_ua_retry_count		ssd_ua_retry_count
181 #define	sd_report_pfa			ssd_report_pfa
182 #define	sd_max_throttle			ssd_max_throttle
183 #define	sd_min_throttle			ssd_min_throttle
184 #define	sd_rot_delay			ssd_rot_delay
185 
186 #define	sd_retry_on_reservation_conflict	\
187 					ssd_retry_on_reservation_conflict
188 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
189 #define	sd_resv_conflict_name		ssd_resv_conflict_name
190 
191 #define	sd_component_mask		ssd_component_mask
192 #define	sd_level_mask			ssd_level_mask
193 #define	sd_debug_un			ssd_debug_un
194 #define	sd_error_level			ssd_error_level
195 
196 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
197 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
198 
199 #define	sd_tr				ssd_tr
200 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
201 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
202 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
203 #define	sd_check_media_time		ssd_check_media_time
204 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
205 #define	sd_label_mutex			ssd_label_mutex
206 #define	sd_detach_mutex			ssd_detach_mutex
207 #define	sd_log_buf			ssd_log_buf
208 #define	sd_log_mutex			ssd_log_mutex
209 
210 #define	sd_disk_table			ssd_disk_table
211 #define	sd_disk_table_size		ssd_disk_table_size
212 #define	sd_sense_mutex			ssd_sense_mutex
213 #define	sd_cdbtab			ssd_cdbtab
214 
215 #define	sd_cb_ops			ssd_cb_ops
216 #define	sd_ops				ssd_ops
217 #define	sd_additional_codes		ssd_additional_codes
218 
219 #define	sd_minor_data			ssd_minor_data
220 #define	sd_minor_data_efi		ssd_minor_data_efi
221 
222 #define	sd_tq				ssd_tq
223 #define	sd_wmr_tq			ssd_wmr_tq
224 #define	sd_taskq_name			ssd_taskq_name
225 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
226 #define	sd_taskq_minalloc		ssd_taskq_minalloc
227 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
228 
229 #define	sd_dump_format_string		ssd_dump_format_string
230 
231 #define	sd_iostart_chain		ssd_iostart_chain
232 #define	sd_iodone_chain			ssd_iodone_chain
233 
234 #define	sd_pm_idletime			ssd_pm_idletime
235 
236 #define	sd_force_pm_supported		ssd_force_pm_supported
237 
238 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
239 
240 #endif
241 
242 
243 #ifdef	SDDEBUG
244 int	sd_force_pm_supported		= 0;
245 #endif	/* SDDEBUG */
246 
247 void *sd_state				= NULL;
248 int sd_io_time				= SD_IO_TIME;
249 int sd_failfast_enable			= 1;
250 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
251 int sd_report_pfa			= 1;
252 int sd_max_throttle			= SD_MAX_THROTTLE;
253 int sd_min_throttle			= SD_MIN_THROTTLE;
254 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
255 int sd_qfull_throttle_enable		= TRUE;
256 
257 int sd_retry_on_reservation_conflict	= 1;
258 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
259 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
260 
261 static int sd_dtype_optical_bind	= -1;
262 
263 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
264 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
265 
266 /*
267  * Global data for debug logging. To enable debug printing, sd_component_mask
268  * and sd_level_mask should be set to the desired bit patterns as outlined in
269  * sddef.h.
270  */
271 uint_t	sd_component_mask		= 0x0;
272 uint_t	sd_level_mask			= 0x0;
273 struct	sd_lun *sd_debug_un		= NULL;
274 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
275 
276 /* Note: these may go away in the future... */
277 static uint32_t	sd_xbuf_active_limit	= 512;
278 static uint32_t sd_xbuf_reserve_limit	= 16;
279 
280 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
281 
282 /*
283  * Timer value used to reset the throttle after it has been reduced
284  * (typically in response to TRAN_BUSY or STATUS_QFULL)
285  */
286 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
287 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
288 
289 /*
290  * Interval value associated with the media change scsi watch.
291  */
292 static int sd_check_media_time		= 3000000;
293 
294 /*
295  * Wait value used for in progress operations during a DDI_SUSPEND
296  */
297 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
298 
299 /*
300  * sd_label_mutex protects a static buffer used in the disk label
301  * component of the driver
302  */
303 static kmutex_t sd_label_mutex;
304 
305 /*
306  * sd_detach_mutex protects un_layer_count, un_detach_count, and
307  * un_opens_in_progress in the sd_lun structure.
308  */
309 static kmutex_t sd_detach_mutex;
310 
311 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
312 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
313 
314 /*
315  * Global buffer and mutex for debug logging
316  */
317 static char	sd_log_buf[1024];
318 static kmutex_t	sd_log_mutex;
319 
320 /*
321  * Structs and globals for recording attached lun information.
322  * This maintains a chain. Each node in the chain represents a SCSI controller.
323  * The structure records the number of luns attached to each target connected
324  * with the controller.
325  * For parallel scsi device only.
326  */
327 struct sd_scsi_hba_tgt_lun {
328 	struct sd_scsi_hba_tgt_lun	*next;
329 	dev_info_t			*pdip;
330 	int				nlun[NTARGETS_WIDE];
331 };
332 
333 /*
334  * Flag to indicate the lun is attached or detached
335  */
336 #define	SD_SCSI_LUN_ATTACH	0
337 #define	SD_SCSI_LUN_DETACH	1
338 
339 static kmutex_t	sd_scsi_target_lun_mutex;
340 static struct sd_scsi_hba_tgt_lun	*sd_scsi_target_lun_head = NULL;
341 
342 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
343     sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
344 
345 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
346     sd_scsi_target_lun_head))
347 
348 /*
349  * "Smart" Probe Caching structs, globals, #defines, etc.
350  * For parallel scsi and non-self-identify device only.
351  */
352 
353 /*
354  * The following resources and routines are implemented to support
355  * "smart" probing, which caches the scsi_probe() results in an array,
356  * in order to help avoid long probe times.
357  */
358 struct sd_scsi_probe_cache {
359 	struct	sd_scsi_probe_cache	*next;
360 	dev_info_t	*pdip;
361 	int		cache[NTARGETS_WIDE];
362 };
363 
364 static kmutex_t	sd_scsi_probe_cache_mutex;
365 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
366 
367 /*
368  * Really we only need protection on the head of the linked list, but
369  * better safe than sorry.
370  */
371 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
372     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
373 
374 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
375     sd_scsi_probe_cache_head))
376 
377 
378 /*
379  * Vendor specific data name property declarations
380  */
381 
382 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
383 
384 static sd_tunables seagate_properties = {
385 	SEAGATE_THROTTLE_VALUE,
386 	0,
387 	0,
388 	0,
389 	0,
390 	0,
391 	0,
392 	0,
393 	0
394 };
395 
396 
397 static sd_tunables fujitsu_properties = {
398 	FUJITSU_THROTTLE_VALUE,
399 	0,
400 	0,
401 	0,
402 	0,
403 	0,
404 	0,
405 	0,
406 	0
407 };
408 
409 static sd_tunables ibm_properties = {
410 	IBM_THROTTLE_VALUE,
411 	0,
412 	0,
413 	0,
414 	0,
415 	0,
416 	0,
417 	0,
418 	0
419 };
420 
421 static sd_tunables purple_properties = {
422 	PURPLE_THROTTLE_VALUE,
423 	0,
424 	0,
425 	PURPLE_BUSY_RETRIES,
426 	PURPLE_RESET_RETRY_COUNT,
427 	PURPLE_RESERVE_RELEASE_TIME,
428 	0,
429 	0,
430 	0
431 };
432 
433 static sd_tunables sve_properties = {
434 	SVE_THROTTLE_VALUE,
435 	0,
436 	0,
437 	SVE_BUSY_RETRIES,
438 	SVE_RESET_RETRY_COUNT,
439 	SVE_RESERVE_RELEASE_TIME,
440 	SVE_MIN_THROTTLE_VALUE,
441 	SVE_DISKSORT_DISABLED_FLAG,
442 	0
443 };
444 
445 static sd_tunables maserati_properties = {
446 	0,
447 	0,
448 	0,
449 	0,
450 	0,
451 	0,
452 	0,
453 	MASERATI_DISKSORT_DISABLED_FLAG,
454 	MASERATI_LUN_RESET_ENABLED_FLAG
455 };
456 
457 static sd_tunables pirus_properties = {
458 	PIRUS_THROTTLE_VALUE,
459 	0,
460 	PIRUS_NRR_COUNT,
461 	PIRUS_BUSY_RETRIES,
462 	PIRUS_RESET_RETRY_COUNT,
463 	0,
464 	PIRUS_MIN_THROTTLE_VALUE,
465 	PIRUS_DISKSORT_DISABLED_FLAG,
466 	PIRUS_LUN_RESET_ENABLED_FLAG
467 };
468 
469 #endif
470 
471 #if (defined(__sparc) && !defined(__fibre)) || \
472 	(defined(__i386) || defined(__amd64))
473 
474 
475 static sd_tunables elite_properties = {
476 	ELITE_THROTTLE_VALUE,
477 	0,
478 	0,
479 	0,
480 	0,
481 	0,
482 	0,
483 	0,
484 	0
485 };
486 
487 static sd_tunables st31200n_properties = {
488 	ST31200N_THROTTLE_VALUE,
489 	0,
490 	0,
491 	0,
492 	0,
493 	0,
494 	0,
495 	0,
496 	0
497 };
498 
499 #endif /* Fibre or not */
500 
501 static sd_tunables lsi_properties_scsi = {
502 	LSI_THROTTLE_VALUE,
503 	0,
504 	LSI_NOTREADY_RETRIES,
505 	0,
506 	0,
507 	0,
508 	0,
509 	0,
510 	0
511 };
512 
513 static sd_tunables symbios_properties = {
514 	SYMBIOS_THROTTLE_VALUE,
515 	0,
516 	SYMBIOS_NOTREADY_RETRIES,
517 	0,
518 	0,
519 	0,
520 	0,
521 	0,
522 	0
523 };
524 
525 static sd_tunables lsi_properties = {
526 	0,
527 	0,
528 	LSI_NOTREADY_RETRIES,
529 	0,
530 	0,
531 	0,
532 	0,
533 	0,
534 	0
535 };
536 
537 static sd_tunables lsi_oem_properties = {
538 	0,
539 	0,
540 	LSI_OEM_NOTREADY_RETRIES,
541 	0,
542 	0,
543 	0,
544 	0,
545 	0,
546 	0
547 };
548 
549 
550 
551 #if (defined(SD_PROP_TST))
552 
553 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
554 #define	SD_TST_THROTTLE_VAL	16
555 #define	SD_TST_NOTREADY_VAL	12
556 #define	SD_TST_BUSY_VAL		60
557 #define	SD_TST_RST_RETRY_VAL	36
558 #define	SD_TST_RSV_REL_TIME	60
559 
560 static sd_tunables tst_properties = {
561 	SD_TST_THROTTLE_VAL,
562 	SD_TST_CTYPE_VAL,
563 	SD_TST_NOTREADY_VAL,
564 	SD_TST_BUSY_VAL,
565 	SD_TST_RST_RETRY_VAL,
566 	SD_TST_RSV_REL_TIME,
567 	0,
568 	0,
569 	0
570 };
571 #endif
572 
573 /* This is similiar to the ANSI toupper implementation */
574 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
575 
576 /*
577  * Static Driver Configuration Table
578  *
579  * This is the table of disks which need throttle adjustment (or, perhaps
580  * something else as defined by the flags at a future time.)  device_id
581  * is a string consisting of concatenated vid (vendor), pid (product/model)
582  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
583  * the parts of the string are as defined by the sizes in the scsi_inquiry
584  * structure.  Device type is searched as far as the device_id string is
585  * defined.  Flags defines which values are to be set in the driver from the
586  * properties list.
587  *
588  * Entries below which begin and end with a "*" are a special case.
589  * These do not have a specific vendor, and the string which follows
590  * can appear anywhere in the 16 byte PID portion of the inquiry data.
591  *
592  * Entries below which begin and end with a " " (blank) are a special
593  * case. The comparison function will treat multiple consecutive blanks
594  * as equivalent to a single blank. For example, this causes a
595  * sd_disk_table entry of " NEC CDROM " to match a device's id string
596  * of  "NEC       CDROM".
597  *
598  * Note: The MD21 controller type has been obsoleted.
599  *	 ST318202F is a Legacy device
600  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
601  *	 made with an FC connection. The entries here are a legacy.
602  */
603 static sd_disk_config_t sd_disk_table[] = {
604 #if defined(__fibre) || defined(__i386) || defined(__amd64)
605 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
606 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
607 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
608 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
609 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
610 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
611 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
612 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
613 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
614 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
615 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
616 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
617 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
618 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
619 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
620 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
621 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
622 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
623 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
624 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
625 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
626 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
627 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
628 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
629 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
630 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
631 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
632 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
633 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
634 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
635 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
636 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
637 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
638 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
639 	{ "IBM     1814",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
640 	{ "IBM     1814-200",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
641 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
642 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
643 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
644 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
645 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
646 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
647 	{ "Fujitsu SX300",	SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
648 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
649 	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
650 			SD_CONF_BSET_BSY_RETRY_COUNT|
651 			SD_CONF_BSET_RST_RETRIES|
652 			SD_CONF_BSET_RSV_REL_TIME,
653 		&purple_properties },
654 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
655 		SD_CONF_BSET_BSY_RETRY_COUNT|
656 		SD_CONF_BSET_RST_RETRIES|
657 		SD_CONF_BSET_RSV_REL_TIME|
658 		SD_CONF_BSET_MIN_THROTTLE|
659 		SD_CONF_BSET_DISKSORT_DISABLED,
660 		&sve_properties },
661 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
662 			SD_CONF_BSET_BSY_RETRY_COUNT|
663 			SD_CONF_BSET_RST_RETRIES|
664 			SD_CONF_BSET_RSV_REL_TIME,
665 		&purple_properties },
666 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
667 		SD_CONF_BSET_LUN_RESET_ENABLED,
668 		&maserati_properties },
669 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
670 		SD_CONF_BSET_NRR_COUNT|
671 		SD_CONF_BSET_BSY_RETRY_COUNT|
672 		SD_CONF_BSET_RST_RETRIES|
673 		SD_CONF_BSET_MIN_THROTTLE|
674 		SD_CONF_BSET_DISKSORT_DISABLED|
675 		SD_CONF_BSET_LUN_RESET_ENABLED,
676 		&pirus_properties },
677 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
678 		SD_CONF_BSET_NRR_COUNT|
679 		SD_CONF_BSET_BSY_RETRY_COUNT|
680 		SD_CONF_BSET_RST_RETRIES|
681 		SD_CONF_BSET_MIN_THROTTLE|
682 		SD_CONF_BSET_DISKSORT_DISABLED|
683 		SD_CONF_BSET_LUN_RESET_ENABLED,
684 		&pirus_properties },
685 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
686 		SD_CONF_BSET_NRR_COUNT|
687 		SD_CONF_BSET_BSY_RETRY_COUNT|
688 		SD_CONF_BSET_RST_RETRIES|
689 		SD_CONF_BSET_MIN_THROTTLE|
690 		SD_CONF_BSET_DISKSORT_DISABLED|
691 		SD_CONF_BSET_LUN_RESET_ENABLED,
692 		&pirus_properties },
693 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
694 		SD_CONF_BSET_NRR_COUNT|
695 		SD_CONF_BSET_BSY_RETRY_COUNT|
696 		SD_CONF_BSET_RST_RETRIES|
697 		SD_CONF_BSET_MIN_THROTTLE|
698 		SD_CONF_BSET_DISKSORT_DISABLED|
699 		SD_CONF_BSET_LUN_RESET_ENABLED,
700 		&pirus_properties },
701 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
702 		SD_CONF_BSET_NRR_COUNT|
703 		SD_CONF_BSET_BSY_RETRY_COUNT|
704 		SD_CONF_BSET_RST_RETRIES|
705 		SD_CONF_BSET_MIN_THROTTLE|
706 		SD_CONF_BSET_DISKSORT_DISABLED|
707 		SD_CONF_BSET_LUN_RESET_ENABLED,
708 		&pirus_properties },
709 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
710 		SD_CONF_BSET_NRR_COUNT|
711 		SD_CONF_BSET_BSY_RETRY_COUNT|
712 		SD_CONF_BSET_RST_RETRIES|
713 		SD_CONF_BSET_MIN_THROTTLE|
714 		SD_CONF_BSET_DISKSORT_DISABLED|
715 		SD_CONF_BSET_LUN_RESET_ENABLED,
716 		&pirus_properties },
717 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
718 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
719 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
720 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
721 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
722 #endif /* fibre or NON-sparc platforms */
723 #if ((defined(__sparc) && !defined(__fibre)) ||\
724 	(defined(__i386) || defined(__amd64)))
725 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
726 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
727 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
728 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
729 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
730 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
731 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
732 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
733 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
734 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
735 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
736 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
737 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
738 	    &symbios_properties },
739 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
740 	    &lsi_properties_scsi },
741 #if defined(__i386) || defined(__amd64)
742 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
743 				    | SD_CONF_BSET_READSUB_BCD
744 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
745 				    | SD_CONF_BSET_NO_READ_HEADER
746 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
747 
748 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
749 				    | SD_CONF_BSET_READSUB_BCD
750 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
751 				    | SD_CONF_BSET_NO_READ_HEADER
752 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
753 #endif /* __i386 || __amd64 */
754 #endif /* sparc NON-fibre or NON-sparc platforms */
755 
756 #if (defined(SD_PROP_TST))
757 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
758 				| SD_CONF_BSET_CTYPE
759 				| SD_CONF_BSET_NRR_COUNT
760 				| SD_CONF_BSET_FAB_DEVID
761 				| SD_CONF_BSET_NOCACHE
762 				| SD_CONF_BSET_BSY_RETRY_COUNT
763 				| SD_CONF_BSET_PLAYMSF_BCD
764 				| SD_CONF_BSET_READSUB_BCD
765 				| SD_CONF_BSET_READ_TOC_TRK_BCD
766 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
767 				| SD_CONF_BSET_NO_READ_HEADER
768 				| SD_CONF_BSET_READ_CD_XD4
769 				| SD_CONF_BSET_RST_RETRIES
770 				| SD_CONF_BSET_RSV_REL_TIME
771 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
772 #endif
773 };
774 
775 static const int sd_disk_table_size =
776 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
777 
778 
779 /*
780  * Return codes of sd_uselabel().
781  */
782 #define	SD_LABEL_IS_VALID		0
783 #define	SD_LABEL_IS_INVALID		1
784 
785 #define	SD_INTERCONNECT_PARALLEL	0
786 #define	SD_INTERCONNECT_FABRIC		1
787 #define	SD_INTERCONNECT_FIBRE		2
788 #define	SD_INTERCONNECT_SSA		3
789 #define	SD_INTERCONNECT_SATA		4
790 #define	SD_IS_PARALLEL_SCSI(un)		\
791 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
792 #define	SD_IS_SERIAL(un)		\
793 	((un)->un_interconnect_type == SD_INTERCONNECT_SATA)
794 
795 /*
796  * Definitions used by device id registration routines
797  */
798 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
799 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
800 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
801 #define	WD_NODE			7	/* the whole disk minor */
802 
803 static kmutex_t sd_sense_mutex = {0};
804 
805 /*
806  * Macros for updates of the driver state
807  */
808 #define	New_state(un, s)        \
809 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
810 #define	Restore_state(un)	\
811 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
812 
813 static struct sd_cdbinfo sd_cdbtab[] = {
814 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
815 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
816 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
817 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
818 };
819 
820 /*
821  * Specifies the number of seconds that must have elapsed since the last
822  * cmd. has completed for a device to be declared idle to the PM framework.
823  */
824 static int sd_pm_idletime = 1;
825 
826 /*
827  * Internal function prototypes
828  */
829 
830 #if (defined(__fibre))
831 /*
832  * These #defines are to avoid namespace collisions that occur because this
833  * code is currently used to compile two seperate driver modules: sd and ssd.
834  * All function names need to be treated this way (even if declared static)
835  * in order to allow the debugger to resolve the names properly.
836  * It is anticipated that in the near future the ssd module will be obsoleted,
837  * at which time this ugliness should go away.
838  */
839 #define	sd_log_trace			ssd_log_trace
840 #define	sd_log_info			ssd_log_info
841 #define	sd_log_err			ssd_log_err
842 #define	sdprobe				ssdprobe
843 #define	sdinfo				ssdinfo
844 #define	sd_prop_op			ssd_prop_op
845 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
846 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
847 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
848 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
849 #define	sd_scsi_target_lun_init		ssd_scsi_target_lun_init
850 #define	sd_scsi_target_lun_fini		ssd_scsi_target_lun_fini
851 #define	sd_scsi_get_target_lun_count	ssd_scsi_get_target_lun_count
852 #define	sd_scsi_update_lun_on_target	ssd_scsi_update_lun_on_target
853 #define	sd_spin_up_unit			ssd_spin_up_unit
854 #define	sd_enable_descr_sense		ssd_enable_descr_sense
855 #define	sd_reenable_dsense_task		ssd_reenable_dsense_task
856 #define	sd_set_mmc_caps			ssd_set_mmc_caps
857 #define	sd_read_unit_properties		ssd_read_unit_properties
858 #define	sd_process_sdconf_file		ssd_process_sdconf_file
859 #define	sd_process_sdconf_table		ssd_process_sdconf_table
860 #define	sd_sdconf_id_match		ssd_sdconf_id_match
861 #define	sd_blank_cmp			ssd_blank_cmp
862 #define	sd_chk_vers1_data		ssd_chk_vers1_data
863 #define	sd_set_vers1_properties		ssd_set_vers1_properties
864 #define	sd_validate_geometry		ssd_validate_geometry
865 
866 #if defined(_SUNOS_VTOC_16)
867 #define	sd_convert_geometry		ssd_convert_geometry
868 #endif
869 
870 #define	sd_resync_geom_caches		ssd_resync_geom_caches
871 #define	sd_read_fdisk			ssd_read_fdisk
872 #define	sd_get_physical_geometry	ssd_get_physical_geometry
873 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
874 #define	sd_update_block_info		ssd_update_block_info
875 #define	sd_swap_efi_gpt			ssd_swap_efi_gpt
876 #define	sd_swap_efi_gpe			ssd_swap_efi_gpe
877 #define	sd_validate_efi			ssd_validate_efi
878 #define	sd_use_efi			ssd_use_efi
879 #define	sd_uselabel			ssd_uselabel
880 #define	sd_build_default_label		ssd_build_default_label
881 #define	sd_has_max_chs_vals		ssd_has_max_chs_vals
882 #define	sd_inq_fill			ssd_inq_fill
883 #define	sd_register_devid		ssd_register_devid
884 #define	sd_get_devid_block		ssd_get_devid_block
885 #define	sd_get_devid			ssd_get_devid
886 #define	sd_create_devid			ssd_create_devid
887 #define	sd_write_deviceid		ssd_write_deviceid
888 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
889 #define	sd_setup_pm			ssd_setup_pm
890 #define	sd_create_pm_components		ssd_create_pm_components
891 #define	sd_ddi_suspend			ssd_ddi_suspend
892 #define	sd_ddi_pm_suspend		ssd_ddi_pm_suspend
893 #define	sd_ddi_resume			ssd_ddi_resume
894 #define	sd_ddi_pm_resume		ssd_ddi_pm_resume
895 #define	sdpower				ssdpower
896 #define	sdattach			ssdattach
897 #define	sddetach			ssddetach
898 #define	sd_unit_attach			ssd_unit_attach
899 #define	sd_unit_detach			ssd_unit_detach
900 #define	sd_set_unit_attributes		ssd_set_unit_attributes
901 #define	sd_create_minor_nodes		ssd_create_minor_nodes
902 #define	sd_create_errstats		ssd_create_errstats
903 #define	sd_set_errstats			ssd_set_errstats
904 #define	sd_set_pstats			ssd_set_pstats
905 #define	sddump				ssddump
906 #define	sd_scsi_poll			ssd_scsi_poll
907 #define	sd_send_polled_RQS		ssd_send_polled_RQS
908 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
909 #define	sd_init_event_callbacks		ssd_init_event_callbacks
910 #define	sd_event_callback		ssd_event_callback
911 #define	sd_cache_control		ssd_cache_control
912 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
913 #define	sd_make_device			ssd_make_device
914 #define	sdopen				ssdopen
915 #define	sdclose				ssdclose
916 #define	sd_ready_and_valid		ssd_ready_and_valid
917 #define	sdmin				ssdmin
918 #define	sdread				ssdread
919 #define	sdwrite				ssdwrite
920 #define	sdaread				ssdaread
921 #define	sdawrite			ssdawrite
922 #define	sdstrategy			ssdstrategy
923 #define	sdioctl				ssdioctl
924 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
925 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
926 #define	sd_checksum_iostart		ssd_checksum_iostart
927 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
928 #define	sd_pm_iostart			ssd_pm_iostart
929 #define	sd_core_iostart			ssd_core_iostart
930 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
931 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
932 #define	sd_checksum_iodone		ssd_checksum_iodone
933 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
934 #define	sd_pm_iodone			ssd_pm_iodone
935 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
936 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
937 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
938 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
939 #define	sd_buf_iodone			ssd_buf_iodone
940 #define	sd_uscsi_strategy		ssd_uscsi_strategy
941 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
942 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
943 #define	sd_uscsi_iodone			ssd_uscsi_iodone
944 #define	sd_xbuf_strategy		ssd_xbuf_strategy
945 #define	sd_xbuf_init			ssd_xbuf_init
946 #define	sd_pm_entry			ssd_pm_entry
947 #define	sd_pm_exit			ssd_pm_exit
948 
949 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
950 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
951 
952 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
953 #define	sdintr				ssdintr
954 #define	sd_start_cmds			ssd_start_cmds
955 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
956 #define	sd_bioclone_alloc		ssd_bioclone_alloc
957 #define	sd_bioclone_free		ssd_bioclone_free
958 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
959 #define	sd_shadow_buf_free		ssd_shadow_buf_free
960 #define	sd_print_transport_rejected_message	\
961 					ssd_print_transport_rejected_message
962 #define	sd_retry_command		ssd_retry_command
963 #define	sd_set_retry_bp			ssd_set_retry_bp
964 #define	sd_send_request_sense_command	ssd_send_request_sense_command
965 #define	sd_start_retry_command		ssd_start_retry_command
966 #define	sd_start_direct_priority_command	\
967 					ssd_start_direct_priority_command
968 #define	sd_return_failed_command	ssd_return_failed_command
969 #define	sd_return_failed_command_no_restart	\
970 					ssd_return_failed_command_no_restart
971 #define	sd_return_command		ssd_return_command
972 #define	sd_sync_with_callback		ssd_sync_with_callback
973 #define	sdrunout			ssdrunout
974 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
975 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
976 #define	sd_reduce_throttle		ssd_reduce_throttle
977 #define	sd_restore_throttle		ssd_restore_throttle
978 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
979 #define	sd_init_cdb_limits		ssd_init_cdb_limits
980 #define	sd_pkt_status_good		ssd_pkt_status_good
981 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
982 #define	sd_pkt_status_busy		ssd_pkt_status_busy
983 #define	sd_pkt_status_reservation_conflict	\
984 					ssd_pkt_status_reservation_conflict
985 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
986 #define	sd_handle_request_sense		ssd_handle_request_sense
987 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
988 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
989 #define	sd_validate_sense_data		ssd_validate_sense_data
990 #define	sd_decode_sense			ssd_decode_sense
991 #define	sd_print_sense_msg		ssd_print_sense_msg
992 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
993 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
994 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
995 #define	sd_sense_key_medium_or_hardware_error	\
996 					ssd_sense_key_medium_or_hardware_error
997 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
998 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
999 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
1000 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
1001 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
1002 #define	sd_sense_key_default		ssd_sense_key_default
1003 #define	sd_print_retry_msg		ssd_print_retry_msg
1004 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
1005 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
1006 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
1007 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
1008 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
1009 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
1010 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
1011 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
1012 #define	sd_pkt_reason_default		ssd_pkt_reason_default
1013 #define	sd_reset_target			ssd_reset_target
1014 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
1015 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
1016 #define	sd_taskq_create			ssd_taskq_create
1017 #define	sd_taskq_delete			ssd_taskq_delete
1018 #define	sd_media_change_task		ssd_media_change_task
1019 #define	sd_handle_mchange		ssd_handle_mchange
1020 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
1021 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
1022 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
1023 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
1024 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
1025 					sd_send_scsi_feature_GET_CONFIGURATION
1026 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
1027 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
1028 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
1029 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
1030 					ssd_send_scsi_PERSISTENT_RESERVE_IN
1031 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
1032 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
1033 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
1034 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
1035 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1036 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
1037 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
1038 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
1039 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
1040 #define	sd_alloc_rqs			ssd_alloc_rqs
1041 #define	sd_free_rqs			ssd_free_rqs
1042 #define	sd_dump_memory			ssd_dump_memory
1043 #define	sd_uscsi_ioctl			ssd_uscsi_ioctl
1044 #define	sd_get_media_info		ssd_get_media_info
1045 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
1046 #define	sd_dkio_get_geometry		ssd_dkio_get_geometry
1047 #define	sd_dkio_set_geometry		ssd_dkio_set_geometry
1048 #define	sd_dkio_get_partition		ssd_dkio_get_partition
1049 #define	sd_dkio_set_partition		ssd_dkio_set_partition
1050 #define	sd_dkio_partition		ssd_dkio_partition
1051 #define	sd_dkio_get_vtoc		ssd_dkio_get_vtoc
1052 #define	sd_dkio_get_efi			ssd_dkio_get_efi
1053 #define	sd_build_user_vtoc		ssd_build_user_vtoc
1054 #define	sd_dkio_set_vtoc		ssd_dkio_set_vtoc
1055 #define	sd_dkio_set_efi			ssd_dkio_set_efi
1056 #define	sd_build_label_vtoc		ssd_build_label_vtoc
1057 #define	sd_write_label			ssd_write_label
1058 #define	sd_clear_vtoc			ssd_clear_vtoc
1059 #define	sd_clear_efi			ssd_clear_efi
1060 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
1061 #define	sd_setup_next_xfer		ssd_setup_next_xfer
1062 #define	sd_dkio_get_temp		ssd_dkio_get_temp
1063 #define	sd_dkio_get_mboot		ssd_dkio_get_mboot
1064 #define	sd_dkio_set_mboot		ssd_dkio_set_mboot
1065 #define	sd_setup_default_geometry	ssd_setup_default_geometry
1066 #define	sd_update_fdisk_and_vtoc	ssd_update_fdisk_and_vtoc
1067 #define	sd_check_mhd			ssd_check_mhd
1068 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1069 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1070 #define	sd_sname			ssd_sname
1071 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1072 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1073 #define	sd_take_ownership		ssd_take_ownership
1074 #define	sd_reserve_release		ssd_reserve_release
1075 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1076 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1077 #define	sd_persistent_reservation_in_read_keys	\
1078 					ssd_persistent_reservation_in_read_keys
1079 #define	sd_persistent_reservation_in_read_resv	\
1080 					ssd_persistent_reservation_in_read_resv
1081 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1082 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1083 #define	sd_mhdioc_release		ssd_mhdioc_release
1084 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1085 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1086 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1087 #define	sr_change_blkmode		ssr_change_blkmode
1088 #define	sr_change_speed			ssr_change_speed
1089 #define	sr_atapi_change_speed		ssr_atapi_change_speed
1090 #define	sr_pause_resume			ssr_pause_resume
1091 #define	sr_play_msf			ssr_play_msf
1092 #define	sr_play_trkind			ssr_play_trkind
1093 #define	sr_read_all_subcodes		ssr_read_all_subcodes
1094 #define	sr_read_subchannel		ssr_read_subchannel
1095 #define	sr_read_tocentry		ssr_read_tocentry
1096 #define	sr_read_tochdr			ssr_read_tochdr
1097 #define	sr_read_cdda			ssr_read_cdda
1098 #define	sr_read_cdxa			ssr_read_cdxa
1099 #define	sr_read_mode1			ssr_read_mode1
1100 #define	sr_read_mode2			ssr_read_mode2
1101 #define	sr_read_cd_mode2		ssr_read_cd_mode2
1102 #define	sr_sector_mode			ssr_sector_mode
1103 #define	sr_eject			ssr_eject
1104 #define	sr_ejected			ssr_ejected
1105 #define	sr_check_wp			ssr_check_wp
1106 #define	sd_check_media			ssd_check_media
1107 #define	sd_media_watch_cb		ssd_media_watch_cb
1108 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1109 #define	sr_volume_ctrl			ssr_volume_ctrl
1110 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1111 #define	sd_log_page_supported		ssd_log_page_supported
1112 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1113 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1114 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1115 #define	sd_range_lock			ssd_range_lock
1116 #define	sd_get_range			ssd_get_range
1117 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1118 #define	sd_range_unlock			ssd_range_unlock
1119 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1120 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1121 
1122 #define	sd_iostart_chain		ssd_iostart_chain
1123 #define	sd_iodone_chain			ssd_iodone_chain
1124 #define	sd_initpkt_map			ssd_initpkt_map
1125 #define	sd_destroypkt_map		ssd_destroypkt_map
1126 #define	sd_chain_type_map		ssd_chain_type_map
1127 #define	sd_chain_index_map		ssd_chain_index_map
1128 
1129 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1130 #define	sd_failfast_flushq		ssd_failfast_flushq
1131 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1132 
1133 #define	sd_is_lsi			ssd_is_lsi
1134 
1135 #endif	/* #if (defined(__fibre)) */
1136 
1137 
1138 int _init(void);
1139 int _fini(void);
1140 int _info(struct modinfo *modinfop);
1141 
1142 /*PRINTFLIKE3*/
1143 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1144 /*PRINTFLIKE3*/
1145 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1146 /*PRINTFLIKE3*/
1147 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1148 
1149 static int sdprobe(dev_info_t *devi);
1150 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1151     void **result);
1152 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1153     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1154 
1155 /*
1156  * Smart probe for parallel scsi
1157  */
1158 static void sd_scsi_probe_cache_init(void);
1159 static void sd_scsi_probe_cache_fini(void);
1160 static void sd_scsi_clear_probe_cache(void);
1161 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1162 
1163 /*
1164  * Attached luns on target for parallel scsi
1165  */
1166 static void sd_scsi_target_lun_init(void);
1167 static void sd_scsi_target_lun_fini(void);
1168 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1169 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1170 
1171 static int	sd_spin_up_unit(struct sd_lun *un);
1172 #ifdef _LP64
1173 static void	sd_enable_descr_sense(struct sd_lun *un);
1174 static void	sd_reenable_dsense_task(void *arg);
1175 #endif /* _LP64 */
1176 
1177 static void	sd_set_mmc_caps(struct sd_lun *un);
1178 
1179 static void sd_read_unit_properties(struct sd_lun *un);
1180 static int  sd_process_sdconf_file(struct sd_lun *un);
1181 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1182     int *data_list, sd_tunables *values);
1183 static void sd_process_sdconf_table(struct sd_lun *un);
1184 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1185 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1186 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1187 	int list_len, char *dataname_ptr);
1188 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1189     sd_tunables *prop_list);
1190 static int  sd_validate_geometry(struct sd_lun *un, int path_flag);
1191 
1192 #if defined(_SUNOS_VTOC_16)
1193 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g);
1194 #endif
1195 
1196 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize,
1197 	int path_flag);
1198 static int  sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize,
1199 	int path_flag);
1200 static void sd_get_physical_geometry(struct sd_lun *un,
1201 	struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag);
1202 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity,
1203 	int lbasize);
1204 static int  sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag);
1205 static void sd_swap_efi_gpt(efi_gpt_t *);
1206 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *);
1207 static int sd_validate_efi(efi_gpt_t *);
1208 static int sd_use_efi(struct sd_lun *, int);
1209 static void sd_build_default_label(struct sd_lun *un);
1210 
1211 #if defined(_FIRMWARE_NEEDS_FDISK)
1212 static int  sd_has_max_chs_vals(struct ipart *fdp);
1213 #endif
1214 static void sd_inq_fill(char *p, int l, char *s);
1215 
1216 
1217 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi,
1218     int reservation_flag);
1219 static daddr_t  sd_get_devid_block(struct sd_lun *un);
1220 static int  sd_get_devid(struct sd_lun *un);
1221 static int  sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len);
1222 static ddi_devid_t sd_create_devid(struct sd_lun *un);
1223 static int  sd_write_deviceid(struct sd_lun *un);
1224 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1225 static int  sd_check_vpd_page_support(struct sd_lun *un);
1226 
1227 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi);
1228 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1229 
1230 static int  sd_ddi_suspend(dev_info_t *devi);
1231 static int  sd_ddi_pm_suspend(struct sd_lun *un);
1232 static int  sd_ddi_resume(dev_info_t *devi);
1233 static int  sd_ddi_pm_resume(struct sd_lun *un);
1234 static int  sdpower(dev_info_t *devi, int component, int level);
1235 
1236 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1237 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1238 static int  sd_unit_attach(dev_info_t *devi);
1239 static int  sd_unit_detach(dev_info_t *devi);
1240 
1241 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1242 static int  sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi);
1243 static void sd_create_errstats(struct sd_lun *un, int instance);
1244 static void sd_set_errstats(struct sd_lun *un);
1245 static void sd_set_pstats(struct sd_lun *un);
1246 
1247 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1248 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1249 static int  sd_send_polled_RQS(struct sd_lun *un);
1250 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1251 
1252 #if (defined(__fibre))
1253 /*
1254  * Event callbacks (photon)
1255  */
1256 static void sd_init_event_callbacks(struct sd_lun *un);
1257 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1258 #endif
1259 
1260 /*
1261  * Defines for sd_cache_control
1262  */
1263 
1264 #define	SD_CACHE_ENABLE		1
1265 #define	SD_CACHE_DISABLE	0
1266 #define	SD_CACHE_NOCHANGE	-1
1267 
1268 static int   sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag);
1269 static int   sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled);
1270 static dev_t sd_make_device(dev_info_t *devi);
1271 
1272 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1273 	uint64_t capacity);
1274 
1275 /*
1276  * Driver entry point functions.
1277  */
1278 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1279 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1280 static int  sd_ready_and_valid(struct sd_lun *un);
1281 
1282 static void sdmin(struct buf *bp);
1283 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1284 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1285 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1286 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1287 
1288 static int sdstrategy(struct buf *bp);
1289 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1290 
1291 /*
1292  * Function prototypes for layering functions in the iostart chain.
1293  */
1294 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1295 	struct buf *bp);
1296 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1297 	struct buf *bp);
1298 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1299 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1300 	struct buf *bp);
1301 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1302 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1303 
1304 /*
1305  * Function prototypes for layering functions in the iodone chain.
1306  */
1307 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1308 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1309 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1310 	struct buf *bp);
1311 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1312 	struct buf *bp);
1313 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1314 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1315 	struct buf *bp);
1316 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1317 
1318 /*
1319  * Prototypes for functions to support buf(9S) based IO.
1320  */
1321 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1322 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1323 static void sd_destroypkt_for_buf(struct buf *);
1324 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1325 	struct buf *bp, int flags,
1326 	int (*callback)(caddr_t), caddr_t callback_arg,
1327 	diskaddr_t lba, uint32_t blockcount);
1328 #if defined(__i386) || defined(__amd64)
1329 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1330 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1331 #endif /* defined(__i386) || defined(__amd64) */
1332 
1333 /*
1334  * Prototypes for functions to support USCSI IO.
1335  */
1336 static int sd_uscsi_strategy(struct buf *bp);
1337 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1338 static void sd_destroypkt_for_uscsi(struct buf *);
1339 
1340 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1341 	uchar_t chain_type, void *pktinfop);
1342 
1343 static int  sd_pm_entry(struct sd_lun *un);
1344 static void sd_pm_exit(struct sd_lun *un);
1345 
1346 static void sd_pm_idletimeout_handler(void *arg);
1347 
1348 /*
1349  * sd_core internal functions (used at the sd_core_io layer).
1350  */
1351 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1352 static void sdintr(struct scsi_pkt *pktp);
1353 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1354 
1355 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd,
1356 	enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace,
1357 	int path_flag);
1358 
1359 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1360 	daddr_t blkno, int (*func)(struct buf *));
1361 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1362 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1363 static void sd_bioclone_free(struct buf *bp);
1364 static void sd_shadow_buf_free(struct buf *bp);
1365 
1366 static void sd_print_transport_rejected_message(struct sd_lun *un,
1367 	struct sd_xbuf *xp, int code);
1368 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1369     void *arg, int code);
1370 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1371     void *arg, int code);
1372 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1373     void *arg, int code);
1374 
1375 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1376 	int retry_check_flag,
1377 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1378 		int c),
1379 	void *user_arg, int failure_code,  clock_t retry_delay,
1380 	void (*statp)(kstat_io_t *));
1381 
1382 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1383 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1384 
1385 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1386 	struct scsi_pkt *pktp);
1387 static void sd_start_retry_command(void *arg);
1388 static void sd_start_direct_priority_command(void *arg);
1389 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1390 	int errcode);
1391 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1392 	struct buf *bp, int errcode);
1393 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1394 static void sd_sync_with_callback(struct sd_lun *un);
1395 static int sdrunout(caddr_t arg);
1396 
1397 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1398 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1399 
1400 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1401 static void sd_restore_throttle(void *arg);
1402 
1403 static void sd_init_cdb_limits(struct sd_lun *un);
1404 
1405 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1406 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1407 
1408 /*
1409  * Error handling functions
1410  */
1411 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1412 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1413 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1414 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1415 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1416 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1417 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1418 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1419 
1420 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1421 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1422 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1423 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1424 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1425 	struct sd_xbuf *xp);
1426 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1427 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1428 
1429 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1430 	void *arg, int code);
1431 
1432 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1433 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1434 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1435 	uint8_t *sense_datap,
1436 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1437 static void sd_sense_key_not_ready(struct sd_lun *un,
1438 	uint8_t *sense_datap,
1439 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1440 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1441 	uint8_t *sense_datap,
1442 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1443 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1444 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1445 static void sd_sense_key_unit_attention(struct sd_lun *un,
1446 	uint8_t *sense_datap,
1447 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1448 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1449 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1450 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1451 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1452 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1453 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1454 static void sd_sense_key_default(struct sd_lun *un,
1455 	uint8_t *sense_datap,
1456 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1457 
1458 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1459 	void *arg, int flag);
1460 
1461 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1462 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1463 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1464 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1465 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1466 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1467 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1468 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1469 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1470 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1471 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1472 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1473 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1474 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1475 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1476 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1477 
1478 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1479 
1480 static void sd_start_stop_unit_callback(void *arg);
1481 static void sd_start_stop_unit_task(void *arg);
1482 
1483 static void sd_taskq_create(void);
1484 static void sd_taskq_delete(void);
1485 static void sd_media_change_task(void *arg);
1486 
1487 static int sd_handle_mchange(struct sd_lun *un);
1488 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag);
1489 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp,
1490 	uint32_t *lbap, int path_flag);
1491 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
1492 	uint32_t *lbap, int path_flag);
1493 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag,
1494 	int path_flag);
1495 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr,
1496 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1497 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag);
1498 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un,
1499 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1500 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un,
1501 	uchar_t usr_cmd, uchar_t *usr_bufp);
1502 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1503 	struct dk_callback *dkc);
1504 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1505 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un,
1506 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1507 	uchar_t *bufaddr, uint_t buflen);
1508 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
1509 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1510 	uchar_t *bufaddr, uint_t buflen, char feature);
1511 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize,
1512 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1513 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize,
1514 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1515 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
1516 	size_t buflen, daddr_t start_block, int path_flag);
1517 #define	sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag)	\
1518 	sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \
1519 	path_flag)
1520 #define	sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag)	\
1521 	sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\
1522 	path_flag)
1523 
1524 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr,
1525 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1526 	uint16_t param_ptr, int path_flag);
1527 
1528 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1529 static void sd_free_rqs(struct sd_lun *un);
1530 
1531 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1532 	uchar_t *data, int len, int fmt);
1533 static void sd_panic_for_res_conflict(struct sd_lun *un);
1534 
1535 /*
1536  * Disk Ioctl Function Prototypes
1537  */
1538 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag);
1539 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1540 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1541 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag,
1542 	int geom_validated);
1543 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag);
1544 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag,
1545 	int geom_validated);
1546 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag);
1547 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag,
1548 	int geom_validated);
1549 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag);
1550 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag);
1551 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1552 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag);
1553 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag);
1554 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1555 static int sd_write_label(dev_t dev);
1556 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl);
1557 static void sd_clear_vtoc(struct sd_lun *un);
1558 static void sd_clear_efi(struct sd_lun *un);
1559 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1560 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag);
1561 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag);
1562 static void sd_setup_default_geometry(struct sd_lun *un);
1563 #if defined(__i386) || defined(__amd64)
1564 static int sd_update_fdisk_and_vtoc(struct sd_lun *un);
1565 #endif
1566 
1567 /*
1568  * Multi-host Ioctl Prototypes
1569  */
1570 static int sd_check_mhd(dev_t dev, int interval);
1571 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1572 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1573 static char *sd_sname(uchar_t status);
1574 static void sd_mhd_resvd_recover(void *arg);
1575 static void sd_resv_reclaim_thread();
1576 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1577 static int sd_reserve_release(dev_t dev, int cmd);
1578 static void sd_rmv_resv_reclaim_req(dev_t dev);
1579 static void sd_mhd_reset_notify_cb(caddr_t arg);
1580 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1581 	mhioc_inkeys_t *usrp, int flag);
1582 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1583 	mhioc_inresvs_t *usrp, int flag);
1584 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1585 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1586 static int sd_mhdioc_release(dev_t dev);
1587 static int sd_mhdioc_register_devid(dev_t dev);
1588 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1589 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1590 
1591 /*
1592  * SCSI removable prototypes
1593  */
1594 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1595 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1596 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1597 static int sr_pause_resume(dev_t dev, int mode);
1598 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1599 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1600 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1601 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1602 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1603 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1604 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1605 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1606 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1607 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1608 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1609 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1610 static int sr_eject(dev_t dev);
1611 static void sr_ejected(register struct sd_lun *un);
1612 static int sr_check_wp(dev_t dev);
1613 static int sd_check_media(dev_t dev, enum dkio_state state);
1614 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1615 static void sd_delayed_cv_broadcast(void *arg);
1616 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1617 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1618 
1619 static int sd_log_page_supported(struct sd_lun *un, int log_page);
1620 
1621 /*
1622  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1623  */
1624 static void sd_check_for_writable_cd(struct sd_lun *un);
1625 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1626 static void sd_wm_cache_destructor(void *wm, void *un);
1627 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1628 	daddr_t endb, ushort_t typ);
1629 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1630 	daddr_t endb);
1631 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1632 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1633 static void sd_read_modify_write_task(void * arg);
1634 static int
1635 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1636 	struct buf **bpp);
1637 
1638 
1639 /*
1640  * Function prototypes for failfast support.
1641  */
1642 static void sd_failfast_flushq(struct sd_lun *un);
1643 static int sd_failfast_flushq_callback(struct buf *bp);
1644 
1645 /*
1646  * Function prototypes to check for lsi devices
1647  */
1648 static void sd_is_lsi(struct sd_lun *un);
1649 
1650 /*
1651  * Function prototypes for x86 support
1652  */
1653 #if defined(__i386) || defined(__amd64)
1654 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1655 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1656 #endif
1657 
1658 /*
1659  * Constants for failfast support:
1660  *
1661  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1662  * failfast processing being performed.
1663  *
1664  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1665  * failfast processing on all bufs with B_FAILFAST set.
1666  */
1667 
1668 #define	SD_FAILFAST_INACTIVE		0
1669 #define	SD_FAILFAST_ACTIVE		1
1670 
1671 /*
1672  * Bitmask to control behavior of buf(9S) flushes when a transition to
1673  * the failfast state occurs. Optional bits include:
1674  *
1675  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1676  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1677  * be flushed.
1678  *
1679  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1680  * driver, in addition to the regular wait queue. This includes the xbuf
1681  * queues. When clear, only the driver's wait queue will be flushed.
1682  */
1683 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1684 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1685 
1686 /*
1687  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1688  * to flush all queues within the driver.
1689  */
1690 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1691 
1692 
1693 /*
1694  * SD Testing Fault Injection
1695  */
1696 #ifdef SD_FAULT_INJECTION
1697 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1698 static void sd_faultinjection(struct scsi_pkt *pktp);
1699 static void sd_injection_log(char *buf, struct sd_lun *un);
1700 #endif
1701 
1702 /*
1703  * Device driver ops vector
1704  */
1705 static struct cb_ops sd_cb_ops = {
1706 	sdopen,			/* open */
1707 	sdclose,		/* close */
1708 	sdstrategy,		/* strategy */
1709 	nodev,			/* print */
1710 	sddump,			/* dump */
1711 	sdread,			/* read */
1712 	sdwrite,		/* write */
1713 	sdioctl,		/* ioctl */
1714 	nodev,			/* devmap */
1715 	nodev,			/* mmap */
1716 	nodev,			/* segmap */
1717 	nochpoll,		/* poll */
1718 	sd_prop_op,		/* cb_prop_op */
1719 	0,			/* streamtab  */
1720 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1721 	CB_REV,			/* cb_rev */
1722 	sdaread, 		/* async I/O read entry point */
1723 	sdawrite		/* async I/O write entry point */
1724 };
1725 
1726 static struct dev_ops sd_ops = {
1727 	DEVO_REV,		/* devo_rev, */
1728 	0,			/* refcnt  */
1729 	sdinfo,			/* info */
1730 	nulldev,		/* identify */
1731 	sdprobe,		/* probe */
1732 	sdattach,		/* attach */
1733 	sddetach,		/* detach */
1734 	nodev,			/* reset */
1735 	&sd_cb_ops,		/* driver operations */
1736 	NULL,			/* bus operations */
1737 	sdpower			/* power */
1738 };
1739 
1740 
1741 /*
1742  * This is the loadable module wrapper.
1743  */
1744 #include <sys/modctl.h>
1745 
1746 static struct modldrv modldrv = {
1747 	&mod_driverops,		/* Type of module. This one is a driver */
1748 	SD_MODULE_NAME,		/* Module name. */
1749 	&sd_ops			/* driver ops */
1750 };
1751 
1752 
1753 static struct modlinkage modlinkage = {
1754 	MODREV_1,
1755 	&modldrv,
1756 	NULL
1757 };
1758 
1759 
1760 static struct scsi_asq_key_strings sd_additional_codes[] = {
1761 	0x81, 0, "Logical Unit is Reserved",
1762 	0x85, 0, "Audio Address Not Valid",
1763 	0xb6, 0, "Media Load Mechanism Failed",
1764 	0xB9, 0, "Audio Play Operation Aborted",
1765 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1766 	0x53, 2, "Medium removal prevented",
1767 	0x6f, 0, "Authentication failed during key exchange",
1768 	0x6f, 1, "Key not present",
1769 	0x6f, 2, "Key not established",
1770 	0x6f, 3, "Read without proper authentication",
1771 	0x6f, 4, "Mismatched region to this logical unit",
1772 	0x6f, 5, "Region reset count error",
1773 	0xffff, 0x0, NULL
1774 };
1775 
1776 
1777 /*
1778  * Struct for passing printing information for sense data messages
1779  */
1780 struct sd_sense_info {
1781 	int	ssi_severity;
1782 	int	ssi_pfa_flag;
1783 };
1784 
1785 /*
1786  * Table of function pointers for iostart-side routines. Seperate "chains"
1787  * of layered function calls are formed by placing the function pointers
1788  * sequentially in the desired order. Functions are called according to an
1789  * incrementing table index ordering. The last function in each chain must
1790  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1791  * in the sd_iodone_chain[] array.
1792  *
1793  * Note: It may seem more natural to organize both the iostart and iodone
1794  * functions together, into an array of structures (or some similar
1795  * organization) with a common index, rather than two seperate arrays which
1796  * must be maintained in synchronization. The purpose of this division is
1797  * to achiece improved performance: individual arrays allows for more
1798  * effective cache line utilization on certain platforms.
1799  */
1800 
1801 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1802 
1803 
1804 static sd_chain_t sd_iostart_chain[] = {
1805 
1806 	/* Chain for buf IO for disk drive targets (PM enabled) */
1807 	sd_mapblockaddr_iostart,	/* Index: 0 */
1808 	sd_pm_iostart,			/* Index: 1 */
1809 	sd_core_iostart,		/* Index: 2 */
1810 
1811 	/* Chain for buf IO for disk drive targets (PM disabled) */
1812 	sd_mapblockaddr_iostart,	/* Index: 3 */
1813 	sd_core_iostart,		/* Index: 4 */
1814 
1815 	/* Chain for buf IO for removable-media targets (PM enabled) */
1816 	sd_mapblockaddr_iostart,	/* Index: 5 */
1817 	sd_mapblocksize_iostart,	/* Index: 6 */
1818 	sd_pm_iostart,			/* Index: 7 */
1819 	sd_core_iostart,		/* Index: 8 */
1820 
1821 	/* Chain for buf IO for removable-media targets (PM disabled) */
1822 	sd_mapblockaddr_iostart,	/* Index: 9 */
1823 	sd_mapblocksize_iostart,	/* Index: 10 */
1824 	sd_core_iostart,		/* Index: 11 */
1825 
1826 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1827 	sd_mapblockaddr_iostart,	/* Index: 12 */
1828 	sd_checksum_iostart,		/* Index: 13 */
1829 	sd_pm_iostart,			/* Index: 14 */
1830 	sd_core_iostart,		/* Index: 15 */
1831 
1832 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1833 	sd_mapblockaddr_iostart,	/* Index: 16 */
1834 	sd_checksum_iostart,		/* Index: 17 */
1835 	sd_core_iostart,		/* Index: 18 */
1836 
1837 	/* Chain for USCSI commands (all targets) */
1838 	sd_pm_iostart,			/* Index: 19 */
1839 	sd_core_iostart,		/* Index: 20 */
1840 
1841 	/* Chain for checksumming USCSI commands (all targets) */
1842 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1843 	sd_pm_iostart,			/* Index: 22 */
1844 	sd_core_iostart,		/* Index: 23 */
1845 
1846 	/* Chain for "direct" USCSI commands (all targets) */
1847 	sd_core_iostart,		/* Index: 24 */
1848 
1849 	/* Chain for "direct priority" USCSI commands (all targets) */
1850 	sd_core_iostart,		/* Index: 25 */
1851 };
1852 
1853 /*
1854  * Macros to locate the first function of each iostart chain in the
1855  * sd_iostart_chain[] array. These are located by the index in the array.
1856  */
1857 #define	SD_CHAIN_DISK_IOSTART			0
1858 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1859 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1860 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1861 #define	SD_CHAIN_CHKSUM_IOSTART			12
1862 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1863 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1864 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1865 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1866 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1867 
1868 
1869 /*
1870  * Table of function pointers for the iodone-side routines for the driver-
1871  * internal layering mechanism.  The calling sequence for iodone routines
1872  * uses a decrementing table index, so the last routine called in a chain
1873  * must be at the lowest array index location for that chain.  The last
1874  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1875  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1876  * of the functions in an iodone side chain must correspond to the ordering
1877  * of the iostart routines for that chain.  Note that there is no iodone
1878  * side routine that corresponds to sd_core_iostart(), so there is no
1879  * entry in the table for this.
1880  */
1881 
1882 static sd_chain_t sd_iodone_chain[] = {
1883 
1884 	/* Chain for buf IO for disk drive targets (PM enabled) */
1885 	sd_buf_iodone,			/* Index: 0 */
1886 	sd_mapblockaddr_iodone,		/* Index: 1 */
1887 	sd_pm_iodone,			/* Index: 2 */
1888 
1889 	/* Chain for buf IO for disk drive targets (PM disabled) */
1890 	sd_buf_iodone,			/* Index: 3 */
1891 	sd_mapblockaddr_iodone,		/* Index: 4 */
1892 
1893 	/* Chain for buf IO for removable-media targets (PM enabled) */
1894 	sd_buf_iodone,			/* Index: 5 */
1895 	sd_mapblockaddr_iodone,		/* Index: 6 */
1896 	sd_mapblocksize_iodone,		/* Index: 7 */
1897 	sd_pm_iodone,			/* Index: 8 */
1898 
1899 	/* Chain for buf IO for removable-media targets (PM disabled) */
1900 	sd_buf_iodone,			/* Index: 9 */
1901 	sd_mapblockaddr_iodone,		/* Index: 10 */
1902 	sd_mapblocksize_iodone,		/* Index: 11 */
1903 
1904 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1905 	sd_buf_iodone,			/* Index: 12 */
1906 	sd_mapblockaddr_iodone,		/* Index: 13 */
1907 	sd_checksum_iodone,		/* Index: 14 */
1908 	sd_pm_iodone,			/* Index: 15 */
1909 
1910 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1911 	sd_buf_iodone,			/* Index: 16 */
1912 	sd_mapblockaddr_iodone,		/* Index: 17 */
1913 	sd_checksum_iodone,		/* Index: 18 */
1914 
1915 	/* Chain for USCSI commands (non-checksum targets) */
1916 	sd_uscsi_iodone,		/* Index: 19 */
1917 	sd_pm_iodone,			/* Index: 20 */
1918 
1919 	/* Chain for USCSI commands (checksum targets) */
1920 	sd_uscsi_iodone,		/* Index: 21 */
1921 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1922 	sd_pm_iodone,			/* Index: 22 */
1923 
1924 	/* Chain for "direct" USCSI commands (all targets) */
1925 	sd_uscsi_iodone,		/* Index: 24 */
1926 
1927 	/* Chain for "direct priority" USCSI commands (all targets) */
1928 	sd_uscsi_iodone,		/* Index: 25 */
1929 };
1930 
1931 
1932 /*
1933  * Macros to locate the "first" function in the sd_iodone_chain[] array for
1934  * each iodone-side chain. These are located by the array index, but as the
1935  * iodone side functions are called in a decrementing-index order, the
1936  * highest index number in each chain must be specified (as these correspond
1937  * to the first function in the iodone chain that will be called by the core
1938  * at IO completion time).
1939  */
1940 
1941 #define	SD_CHAIN_DISK_IODONE			2
1942 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
1943 #define	SD_CHAIN_RMMEDIA_IODONE			8
1944 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
1945 #define	SD_CHAIN_CHKSUM_IODONE			15
1946 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
1947 #define	SD_CHAIN_USCSI_CMD_IODONE		20
1948 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
1949 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
1950 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
1951 
1952 
1953 
1954 
1955 /*
1956  * Array to map a layering chain index to the appropriate initpkt routine.
1957  * The redundant entries are present so that the index used for accessing
1958  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1959  * with this table as well.
1960  */
1961 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
1962 
1963 static sd_initpkt_t	sd_initpkt_map[] = {
1964 
1965 	/* Chain for buf IO for disk drive targets (PM enabled) */
1966 	sd_initpkt_for_buf,		/* Index: 0 */
1967 	sd_initpkt_for_buf,		/* Index: 1 */
1968 	sd_initpkt_for_buf,		/* Index: 2 */
1969 
1970 	/* Chain for buf IO for disk drive targets (PM disabled) */
1971 	sd_initpkt_for_buf,		/* Index: 3 */
1972 	sd_initpkt_for_buf,		/* Index: 4 */
1973 
1974 	/* Chain for buf IO for removable-media targets (PM enabled) */
1975 	sd_initpkt_for_buf,		/* Index: 5 */
1976 	sd_initpkt_for_buf,		/* Index: 6 */
1977 	sd_initpkt_for_buf,		/* Index: 7 */
1978 	sd_initpkt_for_buf,		/* Index: 8 */
1979 
1980 	/* Chain for buf IO for removable-media targets (PM disabled) */
1981 	sd_initpkt_for_buf,		/* Index: 9 */
1982 	sd_initpkt_for_buf,		/* Index: 10 */
1983 	sd_initpkt_for_buf,		/* Index: 11 */
1984 
1985 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1986 	sd_initpkt_for_buf,		/* Index: 12 */
1987 	sd_initpkt_for_buf,		/* Index: 13 */
1988 	sd_initpkt_for_buf,		/* Index: 14 */
1989 	sd_initpkt_for_buf,		/* Index: 15 */
1990 
1991 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1992 	sd_initpkt_for_buf,		/* Index: 16 */
1993 	sd_initpkt_for_buf,		/* Index: 17 */
1994 	sd_initpkt_for_buf,		/* Index: 18 */
1995 
1996 	/* Chain for USCSI commands (non-checksum targets) */
1997 	sd_initpkt_for_uscsi,		/* Index: 19 */
1998 	sd_initpkt_for_uscsi,		/* Index: 20 */
1999 
2000 	/* Chain for USCSI commands (checksum targets) */
2001 	sd_initpkt_for_uscsi,		/* Index: 21 */
2002 	sd_initpkt_for_uscsi,		/* Index: 22 */
2003 	sd_initpkt_for_uscsi,		/* Index: 22 */
2004 
2005 	/* Chain for "direct" USCSI commands (all targets) */
2006 	sd_initpkt_for_uscsi,		/* Index: 24 */
2007 
2008 	/* Chain for "direct priority" USCSI commands (all targets) */
2009 	sd_initpkt_for_uscsi,		/* Index: 25 */
2010 
2011 };
2012 
2013 
2014 /*
2015  * Array to map a layering chain index to the appropriate destroypktpkt routine.
2016  * The redundant entries are present so that the index used for accessing
2017  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2018  * with this table as well.
2019  */
2020 typedef void (*sd_destroypkt_t)(struct buf *);
2021 
2022 static sd_destroypkt_t	sd_destroypkt_map[] = {
2023 
2024 	/* Chain for buf IO for disk drive targets (PM enabled) */
2025 	sd_destroypkt_for_buf,		/* Index: 0 */
2026 	sd_destroypkt_for_buf,		/* Index: 1 */
2027 	sd_destroypkt_for_buf,		/* Index: 2 */
2028 
2029 	/* Chain for buf IO for disk drive targets (PM disabled) */
2030 	sd_destroypkt_for_buf,		/* Index: 3 */
2031 	sd_destroypkt_for_buf,		/* Index: 4 */
2032 
2033 	/* Chain for buf IO for removable-media targets (PM enabled) */
2034 	sd_destroypkt_for_buf,		/* Index: 5 */
2035 	sd_destroypkt_for_buf,		/* Index: 6 */
2036 	sd_destroypkt_for_buf,		/* Index: 7 */
2037 	sd_destroypkt_for_buf,		/* Index: 8 */
2038 
2039 	/* Chain for buf IO for removable-media targets (PM disabled) */
2040 	sd_destroypkt_for_buf,		/* Index: 9 */
2041 	sd_destroypkt_for_buf,		/* Index: 10 */
2042 	sd_destroypkt_for_buf,		/* Index: 11 */
2043 
2044 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2045 	sd_destroypkt_for_buf,		/* Index: 12 */
2046 	sd_destroypkt_for_buf,		/* Index: 13 */
2047 	sd_destroypkt_for_buf,		/* Index: 14 */
2048 	sd_destroypkt_for_buf,		/* Index: 15 */
2049 
2050 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2051 	sd_destroypkt_for_buf,		/* Index: 16 */
2052 	sd_destroypkt_for_buf,		/* Index: 17 */
2053 	sd_destroypkt_for_buf,		/* Index: 18 */
2054 
2055 	/* Chain for USCSI commands (non-checksum targets) */
2056 	sd_destroypkt_for_uscsi,	/* Index: 19 */
2057 	sd_destroypkt_for_uscsi,	/* Index: 20 */
2058 
2059 	/* Chain for USCSI commands (checksum targets) */
2060 	sd_destroypkt_for_uscsi,	/* Index: 21 */
2061 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2062 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2063 
2064 	/* Chain for "direct" USCSI commands (all targets) */
2065 	sd_destroypkt_for_uscsi,	/* Index: 24 */
2066 
2067 	/* Chain for "direct priority" USCSI commands (all targets) */
2068 	sd_destroypkt_for_uscsi,	/* Index: 25 */
2069 
2070 };
2071 
2072 
2073 
2074 /*
2075  * Array to map a layering chain index to the appropriate chain "type".
2076  * The chain type indicates a specific property/usage of the chain.
2077  * The redundant entries are present so that the index used for accessing
2078  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2079  * with this table as well.
2080  */
2081 
2082 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2083 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2084 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2085 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2086 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2087 						/* (for error recovery) */
2088 
2089 static int sd_chain_type_map[] = {
2090 
2091 	/* Chain for buf IO for disk drive targets (PM enabled) */
2092 	SD_CHAIN_BUFIO,			/* Index: 0 */
2093 	SD_CHAIN_BUFIO,			/* Index: 1 */
2094 	SD_CHAIN_BUFIO,			/* Index: 2 */
2095 
2096 	/* Chain for buf IO for disk drive targets (PM disabled) */
2097 	SD_CHAIN_BUFIO,			/* Index: 3 */
2098 	SD_CHAIN_BUFIO,			/* Index: 4 */
2099 
2100 	/* Chain for buf IO for removable-media targets (PM enabled) */
2101 	SD_CHAIN_BUFIO,			/* Index: 5 */
2102 	SD_CHAIN_BUFIO,			/* Index: 6 */
2103 	SD_CHAIN_BUFIO,			/* Index: 7 */
2104 	SD_CHAIN_BUFIO,			/* Index: 8 */
2105 
2106 	/* Chain for buf IO for removable-media targets (PM disabled) */
2107 	SD_CHAIN_BUFIO,			/* Index: 9 */
2108 	SD_CHAIN_BUFIO,			/* Index: 10 */
2109 	SD_CHAIN_BUFIO,			/* Index: 11 */
2110 
2111 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2112 	SD_CHAIN_BUFIO,			/* Index: 12 */
2113 	SD_CHAIN_BUFIO,			/* Index: 13 */
2114 	SD_CHAIN_BUFIO,			/* Index: 14 */
2115 	SD_CHAIN_BUFIO,			/* Index: 15 */
2116 
2117 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2118 	SD_CHAIN_BUFIO,			/* Index: 16 */
2119 	SD_CHAIN_BUFIO,			/* Index: 17 */
2120 	SD_CHAIN_BUFIO,			/* Index: 18 */
2121 
2122 	/* Chain for USCSI commands (non-checksum targets) */
2123 	SD_CHAIN_USCSI,			/* Index: 19 */
2124 	SD_CHAIN_USCSI,			/* Index: 20 */
2125 
2126 	/* Chain for USCSI commands (checksum targets) */
2127 	SD_CHAIN_USCSI,			/* Index: 21 */
2128 	SD_CHAIN_USCSI,			/* Index: 22 */
2129 	SD_CHAIN_USCSI,			/* Index: 22 */
2130 
2131 	/* Chain for "direct" USCSI commands (all targets) */
2132 	SD_CHAIN_DIRECT,		/* Index: 24 */
2133 
2134 	/* Chain for "direct priority" USCSI commands (all targets) */
2135 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2136 };
2137 
2138 
2139 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2140 #define	SD_IS_BUFIO(xp)			\
2141 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2142 
2143 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2144 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2145 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2146 
2147 
2148 
2149 /*
2150  * Struct, array, and macros to map a specific chain to the appropriate
2151  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2152  *
2153  * The sd_chain_index_map[] array is used at attach time to set the various
2154  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2155  * chain to be used with the instance. This allows different instances to use
2156  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2157  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2158  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2159  * dynamically & without the use of locking; and (2) a layer may update the
2160  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2161  * to allow for deferred processing of an IO within the same chain from a
2162  * different execution context.
2163  */
2164 
2165 struct sd_chain_index {
2166 	int	sci_iostart_index;
2167 	int	sci_iodone_index;
2168 };
2169 
2170 static struct sd_chain_index	sd_chain_index_map[] = {
2171 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2172 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2173 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2174 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2175 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2176 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2177 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2178 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2179 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2180 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2181 };
2182 
2183 
2184 /*
2185  * The following are indexes into the sd_chain_index_map[] array.
2186  */
2187 
2188 /* un->un_buf_chain_type must be set to one of these */
2189 #define	SD_CHAIN_INFO_DISK		0
2190 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2191 #define	SD_CHAIN_INFO_RMMEDIA		2
2192 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2193 #define	SD_CHAIN_INFO_CHKSUM		4
2194 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2195 
2196 /* un->un_uscsi_chain_type must be set to one of these */
2197 #define	SD_CHAIN_INFO_USCSI_CMD		6
2198 /* USCSI with PM disabled is the same as DIRECT */
2199 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2200 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2201 
2202 /* un->un_direct_chain_type must be set to one of these */
2203 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2204 
2205 /* un->un_priority_chain_type must be set to one of these */
2206 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2207 
2208 /* size for devid inquiries */
2209 #define	MAX_INQUIRY_SIZE		0xF0
2210 
2211 /*
2212  * Macros used by functions to pass a given buf(9S) struct along to the
2213  * next function in the layering chain for further processing.
2214  *
2215  * In the following macros, passing more than three arguments to the called
2216  * routines causes the optimizer for the SPARC compiler to stop doing tail
2217  * call elimination which results in significant performance degradation.
2218  */
2219 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2220 	((*(sd_iostart_chain[index]))(index, un, bp))
2221 
2222 #define	SD_BEGIN_IODONE(index, un, bp)	\
2223 	((*(sd_iodone_chain[index]))(index, un, bp))
2224 
2225 #define	SD_NEXT_IOSTART(index, un, bp)				\
2226 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2227 
2228 #define	SD_NEXT_IODONE(index, un, bp)				\
2229 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2230 
2231 /*
2232  *    Function: _init
2233  *
2234  * Description: This is the driver _init(9E) entry point.
2235  *
2236  * Return Code: Returns the value from mod_install(9F) or
2237  *		ddi_soft_state_init(9F) as appropriate.
2238  *
2239  *     Context: Called when driver module loaded.
2240  */
2241 
2242 int
2243 _init(void)
2244 {
2245 	int	err;
2246 
2247 	/* establish driver name from module name */
2248 	sd_label = mod_modname(&modlinkage);
2249 
2250 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2251 		SD_MAXUNIT);
2252 
2253 	if (err != 0) {
2254 		return (err);
2255 	}
2256 
2257 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2258 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2259 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2260 
2261 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2262 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2263 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2264 
2265 	/*
2266 	 * it's ok to init here even for fibre device
2267 	 */
2268 	sd_scsi_probe_cache_init();
2269 
2270 	sd_scsi_target_lun_init();
2271 
2272 	/*
2273 	 * Creating taskq before mod_install ensures that all callers (threads)
2274 	 * that enter the module after a successfull mod_install encounter
2275 	 * a valid taskq.
2276 	 */
2277 	sd_taskq_create();
2278 
2279 	err = mod_install(&modlinkage);
2280 	if (err != 0) {
2281 		/* delete taskq if install fails */
2282 		sd_taskq_delete();
2283 
2284 		mutex_destroy(&sd_detach_mutex);
2285 		mutex_destroy(&sd_log_mutex);
2286 		mutex_destroy(&sd_label_mutex);
2287 
2288 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2289 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2290 		cv_destroy(&sd_tr.srq_inprocess_cv);
2291 
2292 		sd_scsi_probe_cache_fini();
2293 
2294 		sd_scsi_target_lun_fini();
2295 
2296 		ddi_soft_state_fini(&sd_state);
2297 		return (err);
2298 	}
2299 
2300 	return (err);
2301 }
2302 
2303 
2304 /*
2305  *    Function: _fini
2306  *
2307  * Description: This is the driver _fini(9E) entry point.
2308  *
2309  * Return Code: Returns the value from mod_remove(9F)
2310  *
2311  *     Context: Called when driver module is unloaded.
2312  */
2313 
2314 int
2315 _fini(void)
2316 {
2317 	int err;
2318 
2319 	if ((err = mod_remove(&modlinkage)) != 0) {
2320 		return (err);
2321 	}
2322 
2323 	sd_taskq_delete();
2324 
2325 	mutex_destroy(&sd_detach_mutex);
2326 	mutex_destroy(&sd_log_mutex);
2327 	mutex_destroy(&sd_label_mutex);
2328 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2329 
2330 	sd_scsi_probe_cache_fini();
2331 
2332 	sd_scsi_target_lun_fini();
2333 
2334 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2335 	cv_destroy(&sd_tr.srq_inprocess_cv);
2336 
2337 	ddi_soft_state_fini(&sd_state);
2338 
2339 	return (err);
2340 }
2341 
2342 
2343 /*
2344  *    Function: _info
2345  *
2346  * Description: This is the driver _info(9E) entry point.
2347  *
2348  *   Arguments: modinfop - pointer to the driver modinfo structure
2349  *
2350  * Return Code: Returns the value from mod_info(9F).
2351  *
2352  *     Context: Kernel thread context
2353  */
2354 
2355 int
2356 _info(struct modinfo *modinfop)
2357 {
2358 	return (mod_info(&modlinkage, modinfop));
2359 }
2360 
2361 
2362 /*
2363  * The following routines implement the driver message logging facility.
2364  * They provide component- and level- based debug output filtering.
2365  * Output may also be restricted to messages for a single instance by
2366  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2367  * to NULL, then messages for all instances are printed.
2368  *
2369  * These routines have been cloned from each other due to the language
2370  * constraints of macros and variable argument list processing.
2371  */
2372 
2373 
2374 /*
2375  *    Function: sd_log_err
2376  *
2377  * Description: This routine is called by the SD_ERROR macro for debug
2378  *		logging of error conditions.
2379  *
2380  *   Arguments: comp - driver component being logged
2381  *		dev  - pointer to driver info structure
2382  *		fmt  - error string and format to be logged
2383  */
2384 
2385 static void
2386 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2387 {
2388 	va_list		ap;
2389 	dev_info_t	*dev;
2390 
2391 	ASSERT(un != NULL);
2392 	dev = SD_DEVINFO(un);
2393 	ASSERT(dev != NULL);
2394 
2395 	/*
2396 	 * Filter messages based on the global component and level masks.
2397 	 * Also print if un matches the value of sd_debug_un, or if
2398 	 * sd_debug_un is set to NULL.
2399 	 */
2400 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2401 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2402 		mutex_enter(&sd_log_mutex);
2403 		va_start(ap, fmt);
2404 		(void) vsprintf(sd_log_buf, fmt, ap);
2405 		va_end(ap);
2406 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2407 		mutex_exit(&sd_log_mutex);
2408 	}
2409 #ifdef SD_FAULT_INJECTION
2410 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2411 	if (un->sd_injection_mask & comp) {
2412 		mutex_enter(&sd_log_mutex);
2413 		va_start(ap, fmt);
2414 		(void) vsprintf(sd_log_buf, fmt, ap);
2415 		va_end(ap);
2416 		sd_injection_log(sd_log_buf, un);
2417 		mutex_exit(&sd_log_mutex);
2418 	}
2419 #endif
2420 }
2421 
2422 
2423 /*
2424  *    Function: sd_log_info
2425  *
2426  * Description: This routine is called by the SD_INFO macro for debug
2427  *		logging of general purpose informational conditions.
2428  *
2429  *   Arguments: comp - driver component being logged
2430  *		dev  - pointer to driver info structure
2431  *		fmt  - info string and format to be logged
2432  */
2433 
2434 static void
2435 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2436 {
2437 	va_list		ap;
2438 	dev_info_t	*dev;
2439 
2440 	ASSERT(un != NULL);
2441 	dev = SD_DEVINFO(un);
2442 	ASSERT(dev != NULL);
2443 
2444 	/*
2445 	 * Filter messages based on the global component and level masks.
2446 	 * Also print if un matches the value of sd_debug_un, or if
2447 	 * sd_debug_un is set to NULL.
2448 	 */
2449 	if ((sd_component_mask & component) &&
2450 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2451 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2452 		mutex_enter(&sd_log_mutex);
2453 		va_start(ap, fmt);
2454 		(void) vsprintf(sd_log_buf, fmt, ap);
2455 		va_end(ap);
2456 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2457 		mutex_exit(&sd_log_mutex);
2458 	}
2459 #ifdef SD_FAULT_INJECTION
2460 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2461 	if (un->sd_injection_mask & component) {
2462 		mutex_enter(&sd_log_mutex);
2463 		va_start(ap, fmt);
2464 		(void) vsprintf(sd_log_buf, fmt, ap);
2465 		va_end(ap);
2466 		sd_injection_log(sd_log_buf, un);
2467 		mutex_exit(&sd_log_mutex);
2468 	}
2469 #endif
2470 }
2471 
2472 
2473 /*
2474  *    Function: sd_log_trace
2475  *
2476  * Description: This routine is called by the SD_TRACE macro for debug
2477  *		logging of trace conditions (i.e. function entry/exit).
2478  *
2479  *   Arguments: comp - driver component being logged
2480  *		dev  - pointer to driver info structure
2481  *		fmt  - trace string and format to be logged
2482  */
2483 
2484 static void
2485 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2486 {
2487 	va_list		ap;
2488 	dev_info_t	*dev;
2489 
2490 	ASSERT(un != NULL);
2491 	dev = SD_DEVINFO(un);
2492 	ASSERT(dev != NULL);
2493 
2494 	/*
2495 	 * Filter messages based on the global component and level masks.
2496 	 * Also print if un matches the value of sd_debug_un, or if
2497 	 * sd_debug_un is set to NULL.
2498 	 */
2499 	if ((sd_component_mask & component) &&
2500 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2501 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2502 		mutex_enter(&sd_log_mutex);
2503 		va_start(ap, fmt);
2504 		(void) vsprintf(sd_log_buf, fmt, ap);
2505 		va_end(ap);
2506 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2507 		mutex_exit(&sd_log_mutex);
2508 	}
2509 #ifdef SD_FAULT_INJECTION
2510 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2511 	if (un->sd_injection_mask & component) {
2512 		mutex_enter(&sd_log_mutex);
2513 		va_start(ap, fmt);
2514 		(void) vsprintf(sd_log_buf, fmt, ap);
2515 		va_end(ap);
2516 		sd_injection_log(sd_log_buf, un);
2517 		mutex_exit(&sd_log_mutex);
2518 	}
2519 #endif
2520 }
2521 
2522 
2523 /*
2524  *    Function: sdprobe
2525  *
2526  * Description: This is the driver probe(9e) entry point function.
2527  *
2528  *   Arguments: devi - opaque device info handle
2529  *
2530  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2531  *              DDI_PROBE_FAILURE: If the probe failed.
2532  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2533  *				   but may be present in the future.
2534  */
2535 
2536 static int
2537 sdprobe(dev_info_t *devi)
2538 {
2539 	struct scsi_device	*devp;
2540 	int			rval;
2541 	int			instance;
2542 
2543 	/*
2544 	 * if it wasn't for pln, sdprobe could actually be nulldev
2545 	 * in the "__fibre" case.
2546 	 */
2547 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2548 		return (DDI_PROBE_DONTCARE);
2549 	}
2550 
2551 	devp = ddi_get_driver_private(devi);
2552 
2553 	if (devp == NULL) {
2554 		/* Ooops... nexus driver is mis-configured... */
2555 		return (DDI_PROBE_FAILURE);
2556 	}
2557 
2558 	instance = ddi_get_instance(devi);
2559 
2560 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2561 		return (DDI_PROBE_PARTIAL);
2562 	}
2563 
2564 	/*
2565 	 * Call the SCSA utility probe routine to see if we actually
2566 	 * have a target at this SCSI nexus.
2567 	 */
2568 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2569 	case SCSIPROBE_EXISTS:
2570 		switch (devp->sd_inq->inq_dtype) {
2571 		case DTYPE_DIRECT:
2572 			rval = DDI_PROBE_SUCCESS;
2573 			break;
2574 		case DTYPE_RODIRECT:
2575 			/* CDs etc. Can be removable media */
2576 			rval = DDI_PROBE_SUCCESS;
2577 			break;
2578 		case DTYPE_OPTICAL:
2579 			/*
2580 			 * Rewritable optical driver HP115AA
2581 			 * Can also be removable media
2582 			 */
2583 
2584 			/*
2585 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2586 			 * pre solaris 9 sparc sd behavior is required
2587 			 *
2588 			 * If first time through and sd_dtype_optical_bind
2589 			 * has not been set in /etc/system check properties
2590 			 */
2591 
2592 			if (sd_dtype_optical_bind  < 0) {
2593 			    sd_dtype_optical_bind = ddi_prop_get_int
2594 				(DDI_DEV_T_ANY,	devi,	0,
2595 				"optical-device-bind",	1);
2596 			}
2597 
2598 			if (sd_dtype_optical_bind == 0) {
2599 				rval = DDI_PROBE_FAILURE;
2600 			} else {
2601 				rval = DDI_PROBE_SUCCESS;
2602 			}
2603 			break;
2604 
2605 		case DTYPE_NOTPRESENT:
2606 		default:
2607 			rval = DDI_PROBE_FAILURE;
2608 			break;
2609 		}
2610 		break;
2611 	default:
2612 		rval = DDI_PROBE_PARTIAL;
2613 		break;
2614 	}
2615 
2616 	/*
2617 	 * This routine checks for resource allocation prior to freeing,
2618 	 * so it will take care of the "smart probing" case where a
2619 	 * scsi_probe() may or may not have been issued and will *not*
2620 	 * free previously-freed resources.
2621 	 */
2622 	scsi_unprobe(devp);
2623 	return (rval);
2624 }
2625 
2626 
2627 /*
2628  *    Function: sdinfo
2629  *
2630  * Description: This is the driver getinfo(9e) entry point function.
2631  * 		Given the device number, return the devinfo pointer from
2632  *		the scsi_device structure or the instance number
2633  *		associated with the dev_t.
2634  *
2635  *   Arguments: dip     - pointer to device info structure
2636  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2637  *			  DDI_INFO_DEVT2INSTANCE)
2638  *		arg     - driver dev_t
2639  *		resultp - user buffer for request response
2640  *
2641  * Return Code: DDI_SUCCESS
2642  *              DDI_FAILURE
2643  */
2644 /* ARGSUSED */
2645 static int
2646 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2647 {
2648 	struct sd_lun	*un;
2649 	dev_t		dev;
2650 	int		instance;
2651 	int		error;
2652 
2653 	switch (infocmd) {
2654 	case DDI_INFO_DEVT2DEVINFO:
2655 		dev = (dev_t)arg;
2656 		instance = SDUNIT(dev);
2657 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2658 			return (DDI_FAILURE);
2659 		}
2660 		*result = (void *) SD_DEVINFO(un);
2661 		error = DDI_SUCCESS;
2662 		break;
2663 	case DDI_INFO_DEVT2INSTANCE:
2664 		dev = (dev_t)arg;
2665 		instance = SDUNIT(dev);
2666 		*result = (void *)(uintptr_t)instance;
2667 		error = DDI_SUCCESS;
2668 		break;
2669 	default:
2670 		error = DDI_FAILURE;
2671 	}
2672 	return (error);
2673 }
2674 
2675 /*
2676  *    Function: sd_prop_op
2677  *
2678  * Description: This is the driver prop_op(9e) entry point function.
2679  *		Return the number of blocks for the partition in question
2680  *		or forward the request to the property facilities.
2681  *
2682  *   Arguments: dev       - device number
2683  *		dip       - pointer to device info structure
2684  *		prop_op   - property operator
2685  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2686  *		name      - pointer to property name
2687  *		valuep    - pointer or address of the user buffer
2688  *		lengthp   - property length
2689  *
2690  * Return Code: DDI_PROP_SUCCESS
2691  *              DDI_PROP_NOT_FOUND
2692  *              DDI_PROP_UNDEFINED
2693  *              DDI_PROP_NO_MEMORY
2694  *              DDI_PROP_BUF_TOO_SMALL
2695  */
2696 
2697 static int
2698 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2699 	char *name, caddr_t valuep, int *lengthp)
2700 {
2701 	int		instance = ddi_get_instance(dip);
2702 	struct sd_lun	*un;
2703 	uint64_t	nblocks64;
2704 
2705 	/*
2706 	 * Our dynamic properties are all device specific and size oriented.
2707 	 * Requests issued under conditions where size is valid are passed
2708 	 * to ddi_prop_op_nblocks with the size information, otherwise the
2709 	 * request is passed to ddi_prop_op. Size depends on valid geometry.
2710 	 */
2711 	un = ddi_get_soft_state(sd_state, instance);
2712 	if ((dev == DDI_DEV_T_ANY) || (un == NULL) ||
2713 	    (un->un_f_geometry_is_valid == FALSE)) {
2714 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2715 		    name, valuep, lengthp));
2716 	} else {
2717 		/* get nblocks value */
2718 		ASSERT(!mutex_owned(SD_MUTEX(un)));
2719 		mutex_enter(SD_MUTEX(un));
2720 		nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk;
2721 		mutex_exit(SD_MUTEX(un));
2722 
2723 		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
2724 		    name, valuep, lengthp, nblocks64));
2725 	}
2726 }
2727 
2728 /*
2729  * The following functions are for smart probing:
2730  * sd_scsi_probe_cache_init()
2731  * sd_scsi_probe_cache_fini()
2732  * sd_scsi_clear_probe_cache()
2733  * sd_scsi_probe_with_cache()
2734  */
2735 
2736 /*
2737  *    Function: sd_scsi_probe_cache_init
2738  *
2739  * Description: Initializes the probe response cache mutex and head pointer.
2740  *
2741  *     Context: Kernel thread context
2742  */
2743 
2744 static void
2745 sd_scsi_probe_cache_init(void)
2746 {
2747 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2748 	sd_scsi_probe_cache_head = NULL;
2749 }
2750 
2751 
2752 /*
2753  *    Function: sd_scsi_probe_cache_fini
2754  *
2755  * Description: Frees all resources associated with the probe response cache.
2756  *
2757  *     Context: Kernel thread context
2758  */
2759 
2760 static void
2761 sd_scsi_probe_cache_fini(void)
2762 {
2763 	struct sd_scsi_probe_cache *cp;
2764 	struct sd_scsi_probe_cache *ncp;
2765 
2766 	/* Clean up our smart probing linked list */
2767 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2768 		ncp = cp->next;
2769 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2770 	}
2771 	sd_scsi_probe_cache_head = NULL;
2772 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2773 }
2774 
2775 
2776 /*
2777  *    Function: sd_scsi_clear_probe_cache
2778  *
2779  * Description: This routine clears the probe response cache. This is
2780  *		done when open() returns ENXIO so that when deferred
2781  *		attach is attempted (possibly after a device has been
2782  *		turned on) we will retry the probe. Since we don't know
2783  *		which target we failed to open, we just clear the
2784  *		entire cache.
2785  *
2786  *     Context: Kernel thread context
2787  */
2788 
2789 static void
2790 sd_scsi_clear_probe_cache(void)
2791 {
2792 	struct sd_scsi_probe_cache	*cp;
2793 	int				i;
2794 
2795 	mutex_enter(&sd_scsi_probe_cache_mutex);
2796 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2797 		/*
2798 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2799 		 * force probing to be performed the next time
2800 		 * sd_scsi_probe_with_cache is called.
2801 		 */
2802 		for (i = 0; i < NTARGETS_WIDE; i++) {
2803 			cp->cache[i] = SCSIPROBE_EXISTS;
2804 		}
2805 	}
2806 	mutex_exit(&sd_scsi_probe_cache_mutex);
2807 }
2808 
2809 
2810 /*
2811  *    Function: sd_scsi_probe_with_cache
2812  *
2813  * Description: This routine implements support for a scsi device probe
2814  *		with cache. The driver maintains a cache of the target
2815  *		responses to scsi probes. If we get no response from a
2816  *		target during a probe inquiry, we remember that, and we
2817  *		avoid additional calls to scsi_probe on non-zero LUNs
2818  *		on the same target until the cache is cleared. By doing
2819  *		so we avoid the 1/4 sec selection timeout for nonzero
2820  *		LUNs. lun0 of a target is always probed.
2821  *
2822  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2823  *              waitfunc - indicates what the allocator routines should
2824  *			   do when resources are not available. This value
2825  *			   is passed on to scsi_probe() when that routine
2826  *			   is called.
2827  *
2828  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2829  *		otherwise the value returned by scsi_probe(9F).
2830  *
2831  *     Context: Kernel thread context
2832  */
2833 
2834 static int
2835 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2836 {
2837 	struct sd_scsi_probe_cache	*cp;
2838 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
2839 	int		lun, tgt;
2840 
2841 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2842 	    SCSI_ADDR_PROP_LUN, 0);
2843 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2844 	    SCSI_ADDR_PROP_TARGET, -1);
2845 
2846 	/* Make sure caching enabled and target in range */
2847 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
2848 		/* do it the old way (no cache) */
2849 		return (scsi_probe(devp, waitfn));
2850 	}
2851 
2852 	mutex_enter(&sd_scsi_probe_cache_mutex);
2853 
2854 	/* Find the cache for this scsi bus instance */
2855 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2856 		if (cp->pdip == pdip) {
2857 			break;
2858 		}
2859 	}
2860 
2861 	/* If we can't find a cache for this pdip, create one */
2862 	if (cp == NULL) {
2863 		int i;
2864 
2865 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
2866 		    KM_SLEEP);
2867 		cp->pdip = pdip;
2868 		cp->next = sd_scsi_probe_cache_head;
2869 		sd_scsi_probe_cache_head = cp;
2870 		for (i = 0; i < NTARGETS_WIDE; i++) {
2871 			cp->cache[i] = SCSIPROBE_EXISTS;
2872 		}
2873 	}
2874 
2875 	mutex_exit(&sd_scsi_probe_cache_mutex);
2876 
2877 	/* Recompute the cache for this target if LUN zero */
2878 	if (lun == 0) {
2879 		cp->cache[tgt] = SCSIPROBE_EXISTS;
2880 	}
2881 
2882 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
2883 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
2884 		return (SCSIPROBE_NORESP);
2885 	}
2886 
2887 	/* Do the actual probe; save & return the result */
2888 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
2889 }
2890 
2891 
2892 /*
2893  *    Function: sd_scsi_target_lun_init
2894  *
2895  * Description: Initializes the attached lun chain mutex and head pointer.
2896  *
2897  *     Context: Kernel thread context
2898  */
2899 
2900 static void
2901 sd_scsi_target_lun_init(void)
2902 {
2903 	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
2904 	sd_scsi_target_lun_head = NULL;
2905 }
2906 
2907 
2908 /*
2909  *    Function: sd_scsi_target_lun_fini
2910  *
2911  * Description: Frees all resources associated with the attached lun
2912  *              chain
2913  *
2914  *     Context: Kernel thread context
2915  */
2916 
2917 static void
2918 sd_scsi_target_lun_fini(void)
2919 {
2920 	struct sd_scsi_hba_tgt_lun	*cp;
2921 	struct sd_scsi_hba_tgt_lun	*ncp;
2922 
2923 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
2924 		ncp = cp->next;
2925 		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
2926 	}
2927 	sd_scsi_target_lun_head = NULL;
2928 	mutex_destroy(&sd_scsi_target_lun_mutex);
2929 }
2930 
2931 
2932 /*
2933  *    Function: sd_scsi_get_target_lun_count
2934  *
2935  * Description: This routine will check in the attached lun chain to see
2936  * 		how many luns are attached on the required SCSI controller
2937  * 		and target. Currently, some capabilities like tagged queue
2938  *		are supported per target based by HBA. So all luns in a
2939  *		target have the same capabilities. Based on this assumption,
2940  * 		sd should only set these capabilities once per target. This
2941  *		function is called when sd needs to decide how many luns
2942  *		already attached on a target.
2943  *
2944  *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
2945  *			  controller device.
2946  *              target	- The target ID on the controller's SCSI bus.
2947  *
2948  * Return Code: The number of luns attached on the required target and
2949  *		controller.
2950  *		-1 if target ID is not in parallel SCSI scope or the given
2951  * 		dip is not in the chain.
2952  *
2953  *     Context: Kernel thread context
2954  */
2955 
2956 static int
2957 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
2958 {
2959 	struct sd_scsi_hba_tgt_lun	*cp;
2960 
2961 	if ((target < 0) || (target >= NTARGETS_WIDE)) {
2962 		return (-1);
2963 	}
2964 
2965 	mutex_enter(&sd_scsi_target_lun_mutex);
2966 
2967 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
2968 		if (cp->pdip == dip) {
2969 			break;
2970 		}
2971 	}
2972 
2973 	mutex_exit(&sd_scsi_target_lun_mutex);
2974 
2975 	if (cp == NULL) {
2976 		return (-1);
2977 	}
2978 
2979 	return (cp->nlun[target]);
2980 }
2981 
2982 
2983 /*
2984  *    Function: sd_scsi_update_lun_on_target
2985  *
2986  * Description: This routine is used to update the attached lun chain when a
2987  *		lun is attached or detached on a target.
2988  *
2989  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
2990  *                        controller device.
2991  *              target  - The target ID on the controller's SCSI bus.
2992  *		flag	- Indicate the lun is attached or detached.
2993  *
2994  *     Context: Kernel thread context
2995  */
2996 
2997 static void
2998 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
2999 {
3000 	struct sd_scsi_hba_tgt_lun	*cp;
3001 
3002 	mutex_enter(&sd_scsi_target_lun_mutex);
3003 
3004 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3005 		if (cp->pdip == dip) {
3006 			break;
3007 		}
3008 	}
3009 
3010 	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3011 		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3012 		    KM_SLEEP);
3013 		cp->pdip = dip;
3014 		cp->next = sd_scsi_target_lun_head;
3015 		sd_scsi_target_lun_head = cp;
3016 	}
3017 
3018 	mutex_exit(&sd_scsi_target_lun_mutex);
3019 
3020 	if (cp != NULL) {
3021 		if (flag == SD_SCSI_LUN_ATTACH) {
3022 			cp->nlun[target] ++;
3023 		} else {
3024 			cp->nlun[target] --;
3025 		}
3026 	}
3027 }
3028 
3029 
3030 /*
3031  *    Function: sd_spin_up_unit
3032  *
3033  * Description: Issues the following commands to spin-up the device:
3034  *		START STOP UNIT, and INQUIRY.
3035  *
3036  *   Arguments: un - driver soft state (unit) structure
3037  *
3038  * Return Code: 0 - success
3039  *		EIO - failure
3040  *		EACCES - reservation conflict
3041  *
3042  *     Context: Kernel thread context
3043  */
3044 
3045 static int
3046 sd_spin_up_unit(struct sd_lun *un)
3047 {
3048 	size_t	resid		= 0;
3049 	int	has_conflict	= FALSE;
3050 	uchar_t *bufaddr;
3051 
3052 	ASSERT(un != NULL);
3053 
3054 	/*
3055 	 * Send a throwaway START UNIT command.
3056 	 *
3057 	 * If we fail on this, we don't care presently what precisely
3058 	 * is wrong.  EMC's arrays will also fail this with a check
3059 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
3060 	 * we don't want to fail the attach because it may become
3061 	 * "active" later.
3062 	 */
3063 	if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT)
3064 	    == EACCES)
3065 		has_conflict = TRUE;
3066 
3067 	/*
3068 	 * Send another INQUIRY command to the target. This is necessary for
3069 	 * non-removable media direct access devices because their INQUIRY data
3070 	 * may not be fully qualified until they are spun up (perhaps via the
3071 	 * START command above).  Note: This seems to be needed for some
3072 	 * legacy devices only.) The INQUIRY command should succeed even if a
3073 	 * Reservation Conflict is present.
3074 	 */
3075 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3076 	if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) {
3077 		kmem_free(bufaddr, SUN_INQSIZE);
3078 		return (EIO);
3079 	}
3080 
3081 	/*
3082 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3083 	 * Note that this routine does not return a failure here even if the
3084 	 * INQUIRY command did not return any data.  This is a legacy behavior.
3085 	 */
3086 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3087 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3088 	}
3089 
3090 	kmem_free(bufaddr, SUN_INQSIZE);
3091 
3092 	/* If we hit a reservation conflict above, tell the caller. */
3093 	if (has_conflict == TRUE) {
3094 		return (EACCES);
3095 	}
3096 
3097 	return (0);
3098 }
3099 
3100 #ifdef _LP64
3101 /*
3102  *    Function: sd_enable_descr_sense
3103  *
3104  * Description: This routine attempts to select descriptor sense format
3105  *		using the Control mode page.  Devices that support 64 bit
3106  *		LBAs (for >2TB luns) should also implement descriptor
3107  *		sense data so we will call this function whenever we see
3108  *		a lun larger than 2TB.  If for some reason the device
3109  *		supports 64 bit LBAs but doesn't support descriptor sense
3110  *		presumably the mode select will fail.  Everything will
3111  *		continue to work normally except that we will not get
3112  *		complete sense data for commands that fail with an LBA
3113  *		larger than 32 bits.
3114  *
3115  *   Arguments: un - driver soft state (unit) structure
3116  *
3117  *     Context: Kernel thread context only
3118  */
3119 
3120 static void
3121 sd_enable_descr_sense(struct sd_lun *un)
3122 {
3123 	uchar_t			*header;
3124 	struct mode_control_scsi3 *ctrl_bufp;
3125 	size_t			buflen;
3126 	size_t			bd_len;
3127 
3128 	/*
3129 	 * Read MODE SENSE page 0xA, Control Mode Page
3130 	 */
3131 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3132 	    sizeof (struct mode_control_scsi3);
3133 	header = kmem_zalloc(buflen, KM_SLEEP);
3134 	if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
3135 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) {
3136 		SD_ERROR(SD_LOG_COMMON, un,
3137 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
3138 		goto eds_exit;
3139 	}
3140 
3141 	/*
3142 	 * Determine size of Block Descriptors in order to locate
3143 	 * the mode page data. ATAPI devices return 0, SCSI devices
3144 	 * should return MODE_BLK_DESC_LENGTH.
3145 	 */
3146 	bd_len  = ((struct mode_header *)header)->bdesc_length;
3147 
3148 	ctrl_bufp = (struct mode_control_scsi3 *)
3149 	    (header + MODE_HEADER_LENGTH + bd_len);
3150 
3151 	/*
3152 	 * Clear PS bit for MODE SELECT
3153 	 */
3154 	ctrl_bufp->mode_page.ps = 0;
3155 
3156 	/*
3157 	 * Set D_SENSE to enable descriptor sense format.
3158 	 */
3159 	ctrl_bufp->d_sense = 1;
3160 
3161 	/*
3162 	 * Use MODE SELECT to commit the change to the D_SENSE bit
3163 	 */
3164 	if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
3165 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) {
3166 		SD_INFO(SD_LOG_COMMON, un,
3167 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
3168 		goto eds_exit;
3169 	}
3170 
3171 eds_exit:
3172 	kmem_free(header, buflen);
3173 }
3174 
3175 /*
3176  *    Function: sd_reenable_dsense_task
3177  *
3178  * Description: Re-enable descriptor sense after device or bus reset
3179  *
3180  *     Context: Executes in a taskq() thread context
3181  */
3182 static void
3183 sd_reenable_dsense_task(void *arg)
3184 {
3185 	struct	sd_lun	*un = arg;
3186 
3187 	ASSERT(un != NULL);
3188 	sd_enable_descr_sense(un);
3189 }
3190 #endif /* _LP64 */
3191 
3192 /*
3193  *    Function: sd_set_mmc_caps
3194  *
3195  * Description: This routine determines if the device is MMC compliant and if
3196  *		the device supports CDDA via a mode sense of the CDVD
3197  *		capabilities mode page. Also checks if the device is a
3198  *		dvdram writable device.
3199  *
3200  *   Arguments: un - driver soft state (unit) structure
3201  *
3202  *     Context: Kernel thread context only
3203  */
3204 
3205 static void
3206 sd_set_mmc_caps(struct sd_lun *un)
3207 {
3208 	struct mode_header_grp2		*sense_mhp;
3209 	uchar_t				*sense_page;
3210 	caddr_t				buf;
3211 	int				bd_len;
3212 	int				status;
3213 	struct uscsi_cmd		com;
3214 	int				rtn;
3215 	uchar_t				*out_data_rw, *out_data_hd;
3216 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3217 
3218 	ASSERT(un != NULL);
3219 
3220 	/*
3221 	 * The flags which will be set in this function are - mmc compliant,
3222 	 * dvdram writable device, cdda support. Initialize them to FALSE
3223 	 * and if a capability is detected - it will be set to TRUE.
3224 	 */
3225 	un->un_f_mmc_cap = FALSE;
3226 	un->un_f_dvdram_writable_device = FALSE;
3227 	un->un_f_cfg_cdda = FALSE;
3228 
3229 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3230 	status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf,
3231 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3232 
3233 	if (status != 0) {
3234 		/* command failed; just return */
3235 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3236 		return;
3237 	}
3238 	/*
3239 	 * If the mode sense request for the CDROM CAPABILITIES
3240 	 * page (0x2A) succeeds the device is assumed to be MMC.
3241 	 */
3242 	un->un_f_mmc_cap = TRUE;
3243 
3244 	/* Get to the page data */
3245 	sense_mhp = (struct mode_header_grp2 *)buf;
3246 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3247 	    sense_mhp->bdesc_length_lo;
3248 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3249 		/*
3250 		 * We did not get back the expected block descriptor
3251 		 * length so we cannot determine if the device supports
3252 		 * CDDA. However, we still indicate the device is MMC
3253 		 * according to the successful response to the page
3254 		 * 0x2A mode sense request.
3255 		 */
3256 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3257 		    "sd_set_mmc_caps: Mode Sense returned "
3258 		    "invalid block descriptor length\n");
3259 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3260 		return;
3261 	}
3262 
3263 	/* See if read CDDA is supported */
3264 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3265 	    bd_len);
3266 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3267 
3268 	/* See if writing DVD RAM is supported. */
3269 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3270 	if (un->un_f_dvdram_writable_device == TRUE) {
3271 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3272 		return;
3273 	}
3274 
3275 	/*
3276 	 * If the device presents DVD or CD capabilities in the mode
3277 	 * page, we can return here since a RRD will not have
3278 	 * these capabilities.
3279 	 */
3280 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3281 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3282 		return;
3283 	}
3284 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3285 
3286 	/*
3287 	 * If un->un_f_dvdram_writable_device is still FALSE,
3288 	 * check for a Removable Rigid Disk (RRD).  A RRD
3289 	 * device is identified by the features RANDOM_WRITABLE and
3290 	 * HARDWARE_DEFECT_MANAGEMENT.
3291 	 */
3292 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3293 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3294 
3295 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
3296 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3297 	    RANDOM_WRITABLE);
3298 	if (rtn != 0) {
3299 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3300 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3301 		return;
3302 	}
3303 
3304 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3305 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3306 
3307 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
3308 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3309 	    HARDWARE_DEFECT_MANAGEMENT);
3310 	if (rtn == 0) {
3311 		/*
3312 		 * We have good information, check for random writable
3313 		 * and hardware defect features.
3314 		 */
3315 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3316 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3317 			un->un_f_dvdram_writable_device = TRUE;
3318 		}
3319 	}
3320 
3321 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3322 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3323 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3324 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3325 }
3326 
3327 /*
3328  *    Function: sd_check_for_writable_cd
3329  *
3330  * Description: This routine determines if the media in the device is
3331  *		writable or not. It uses the get configuration command (0x46)
3332  *		to determine if the media is writable
3333  *
3334  *   Arguments: un - driver soft state (unit) structure
3335  *
3336  *     Context: Never called at interrupt context.
3337  */
3338 
3339 static void
3340 sd_check_for_writable_cd(struct sd_lun *un)
3341 {
3342 	struct uscsi_cmd		com;
3343 	uchar_t				*out_data;
3344 	uchar_t				*rqbuf;
3345 	int				rtn;
3346 	uchar_t				*out_data_rw, *out_data_hd;
3347 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3348 	struct mode_header_grp2		*sense_mhp;
3349 	uchar_t				*sense_page;
3350 	caddr_t				buf;
3351 	int				bd_len;
3352 	int				status;
3353 
3354 	ASSERT(un != NULL);
3355 	ASSERT(mutex_owned(SD_MUTEX(un)));
3356 
3357 	/*
3358 	 * Initialize the writable media to false, if configuration info.
3359 	 * tells us otherwise then only we will set it.
3360 	 */
3361 	un->un_f_mmc_writable_media = FALSE;
3362 	mutex_exit(SD_MUTEX(un));
3363 
3364 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3365 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3366 
3367 	rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH,
3368 	    out_data, SD_PROFILE_HEADER_LEN);
3369 
3370 	mutex_enter(SD_MUTEX(un));
3371 	if (rtn == 0) {
3372 		/*
3373 		 * We have good information, check for writable DVD.
3374 		 */
3375 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3376 			un->un_f_mmc_writable_media = TRUE;
3377 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3378 			kmem_free(rqbuf, SENSE_LENGTH);
3379 			return;
3380 		}
3381 	}
3382 
3383 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3384 	kmem_free(rqbuf, SENSE_LENGTH);
3385 
3386 	/*
3387 	 * Determine if this is a RRD type device.
3388 	 */
3389 	mutex_exit(SD_MUTEX(un));
3390 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3391 	status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf,
3392 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3393 	mutex_enter(SD_MUTEX(un));
3394 	if (status != 0) {
3395 		/* command failed; just return */
3396 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3397 		return;
3398 	}
3399 
3400 	/* Get to the page data */
3401 	sense_mhp = (struct mode_header_grp2 *)buf;
3402 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3403 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3404 		/*
3405 		 * We did not get back the expected block descriptor length so
3406 		 * we cannot check the mode page.
3407 		 */
3408 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3409 		    "sd_check_for_writable_cd: Mode Sense returned "
3410 		    "invalid block descriptor length\n");
3411 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3412 		return;
3413 	}
3414 
3415 	/*
3416 	 * If the device presents DVD or CD capabilities in the mode
3417 	 * page, we can return here since a RRD device will not have
3418 	 * these capabilities.
3419 	 */
3420 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3421 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3422 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3423 		return;
3424 	}
3425 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3426 
3427 	/*
3428 	 * If un->un_f_mmc_writable_media is still FALSE,
3429 	 * check for RRD type media.  A RRD device is identified
3430 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3431 	 */
3432 	mutex_exit(SD_MUTEX(un));
3433 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3434 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3435 
3436 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
3437 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3438 	    RANDOM_WRITABLE);
3439 	if (rtn != 0) {
3440 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3441 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3442 		mutex_enter(SD_MUTEX(un));
3443 		return;
3444 	}
3445 
3446 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3447 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3448 
3449 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
3450 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3451 	    HARDWARE_DEFECT_MANAGEMENT);
3452 	mutex_enter(SD_MUTEX(un));
3453 	if (rtn == 0) {
3454 		/*
3455 		 * We have good information, check for random writable
3456 		 * and hardware defect features as current.
3457 		 */
3458 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3459 		    (out_data_rw[10] & 0x1) &&
3460 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3461 		    (out_data_hd[10] & 0x1)) {
3462 			un->un_f_mmc_writable_media = TRUE;
3463 		}
3464 	}
3465 
3466 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3467 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3468 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3469 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3470 }
3471 
3472 /*
3473  *    Function: sd_read_unit_properties
3474  *
3475  * Description: The following implements a property lookup mechanism.
3476  *		Properties for particular disks (keyed on vendor, model
3477  *		and rev numbers) are sought in the sd.conf file via
3478  *		sd_process_sdconf_file(), and if not found there, are
3479  *		looked for in a list hardcoded in this driver via
3480  *		sd_process_sdconf_table() Once located the properties
3481  *		are used to update the driver unit structure.
3482  *
3483  *   Arguments: un - driver soft state (unit) structure
3484  */
3485 
3486 static void
3487 sd_read_unit_properties(struct sd_lun *un)
3488 {
3489 	/*
3490 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3491 	 * the "sd-config-list" property (from the sd.conf file) or if
3492 	 * there was not a match for the inquiry vid/pid. If this event
3493 	 * occurs the static driver configuration table is searched for
3494 	 * a match.
3495 	 */
3496 	ASSERT(un != NULL);
3497 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3498 		sd_process_sdconf_table(un);
3499 	}
3500 
3501 	/* check for LSI device */
3502 	sd_is_lsi(un);
3503 
3504 
3505 }
3506 
3507 
3508 /*
3509  *    Function: sd_process_sdconf_file
3510  *
3511  * Description: Use ddi_getlongprop to obtain the properties from the
3512  *		driver's config file (ie, sd.conf) and update the driver
3513  *		soft state structure accordingly.
3514  *
3515  *   Arguments: un - driver soft state (unit) structure
3516  *
3517  * Return Code: SD_SUCCESS - The properties were successfully set according
3518  *			     to the driver configuration file.
3519  *		SD_FAILURE - The driver config list was not obtained or
3520  *			     there was no vid/pid match. This indicates that
3521  *			     the static config table should be used.
3522  *
3523  * The config file has a property, "sd-config-list", which consists of
3524  * one or more duplets as follows:
3525  *
3526  *  sd-config-list=
3527  *	<duplet>,
3528  *	[<duplet>,]
3529  *	[<duplet>];
3530  *
3531  * The structure of each duplet is as follows:
3532  *
3533  *  <duplet>:= <vid+pid>,<data-property-name_list>
3534  *
3535  * The first entry of the duplet is the device ID string (the concatenated
3536  * vid & pid; not to be confused with a device_id).  This is defined in
3537  * the same way as in the sd_disk_table.
3538  *
3539  * The second part of the duplet is a string that identifies a
3540  * data-property-name-list. The data-property-name-list is defined as
3541  * follows:
3542  *
3543  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3544  *
3545  * The syntax of <data-property-name> depends on the <version> field.
3546  *
3547  * If version = SD_CONF_VERSION_1 we have the following syntax:
3548  *
3549  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3550  *
3551  * where the prop0 value will be used to set prop0 if bit0 set in the
3552  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3553  *
3554  */
3555 
3556 static int
3557 sd_process_sdconf_file(struct sd_lun *un)
3558 {
3559 	char	*config_list = NULL;
3560 	int	config_list_len;
3561 	int	len;
3562 	int	dupletlen = 0;
3563 	char	*vidptr;
3564 	int	vidlen;
3565 	char	*dnlist_ptr;
3566 	char	*dataname_ptr;
3567 	int	dnlist_len;
3568 	int	dataname_len;
3569 	int	*data_list;
3570 	int	data_list_len;
3571 	int	rval = SD_FAILURE;
3572 	int	i;
3573 
3574 	ASSERT(un != NULL);
3575 
3576 	/* Obtain the configuration list associated with the .conf file */
3577 	if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS,
3578 	    sd_config_list, (caddr_t)&config_list, &config_list_len)
3579 	    != DDI_PROP_SUCCESS) {
3580 		return (SD_FAILURE);
3581 	}
3582 
3583 	/*
3584 	 * Compare vids in each duplet to the inquiry vid - if a match is
3585 	 * made, get the data value and update the soft state structure
3586 	 * accordingly.
3587 	 *
3588 	 * Note: This algorithm is complex and difficult to maintain. It should
3589 	 * be replaced with a more robust implementation.
3590 	 */
3591 	for (len = config_list_len, vidptr = config_list; len > 0;
3592 	    vidptr += dupletlen, len -= dupletlen) {
3593 		/*
3594 		 * Note: The assumption here is that each vid entry is on
3595 		 * a unique line from its associated duplet.
3596 		 */
3597 		vidlen = dupletlen = (int)strlen(vidptr);
3598 		if ((vidlen == 0) ||
3599 		    (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
3600 			dupletlen++;
3601 			continue;
3602 		}
3603 
3604 		/*
3605 		 * dnlist contains 1 or more blank separated
3606 		 * data-property-name entries
3607 		 */
3608 		dnlist_ptr = vidptr + vidlen + 1;
3609 		dnlist_len = (int)strlen(dnlist_ptr);
3610 		dupletlen += dnlist_len + 2;
3611 
3612 		/*
3613 		 * Set a pointer for the first data-property-name
3614 		 * entry in the list
3615 		 */
3616 		dataname_ptr = dnlist_ptr;
3617 		dataname_len = 0;
3618 
3619 		/*
3620 		 * Loop through all data-property-name entries in the
3621 		 * data-property-name-list setting the properties for each.
3622 		 */
3623 		while (dataname_len < dnlist_len) {
3624 			int version;
3625 
3626 			/*
3627 			 * Determine the length of the current
3628 			 * data-property-name entry by indexing until a
3629 			 * blank or NULL is encountered. When the space is
3630 			 * encountered reset it to a NULL for compliance
3631 			 * with ddi_getlongprop().
3632 			 */
3633 			for (i = 0; ((dataname_ptr[i] != ' ') &&
3634 			    (dataname_ptr[i] != '\0')); i++) {
3635 				;
3636 			}
3637 
3638 			dataname_len += i;
3639 			/* If not null terminated, Make it so */
3640 			if (dataname_ptr[i] == ' ') {
3641 				dataname_ptr[i] = '\0';
3642 			}
3643 			dataname_len++;
3644 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3645 			    "sd_process_sdconf_file: disk:%s, data:%s\n",
3646 			    vidptr, dataname_ptr);
3647 
3648 			/* Get the data list */
3649 			if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0,
3650 			    dataname_ptr, (caddr_t)&data_list, &data_list_len)
3651 			    != DDI_PROP_SUCCESS) {
3652 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3653 				    "sd_process_sdconf_file: data property (%s)"
3654 				    " has no value\n", dataname_ptr);
3655 				dataname_ptr = dnlist_ptr + dataname_len;
3656 				continue;
3657 			}
3658 
3659 			version = data_list[0];
3660 
3661 			if (version == SD_CONF_VERSION_1) {
3662 				sd_tunables values;
3663 
3664 				/* Set the properties */
3665 				if (sd_chk_vers1_data(un, data_list[1],
3666 				    &data_list[2], data_list_len, dataname_ptr)
3667 				    == SD_SUCCESS) {
3668 					sd_get_tunables_from_conf(un,
3669 					    data_list[1], &data_list[2],
3670 					    &values);
3671 					sd_set_vers1_properties(un,
3672 					    data_list[1], &values);
3673 					rval = SD_SUCCESS;
3674 				} else {
3675 					rval = SD_FAILURE;
3676 				}
3677 			} else {
3678 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3679 				    "data property %s version 0x%x is invalid.",
3680 				    dataname_ptr, version);
3681 				rval = SD_FAILURE;
3682 			}
3683 			kmem_free(data_list, data_list_len);
3684 			dataname_ptr = dnlist_ptr + dataname_len;
3685 		}
3686 	}
3687 
3688 	/* free up the memory allocated by ddi_getlongprop */
3689 	if (config_list) {
3690 		kmem_free(config_list, config_list_len);
3691 	}
3692 
3693 	return (rval);
3694 }
3695 
3696 /*
3697  *    Function: sd_get_tunables_from_conf()
3698  *
3699  *
3700  *    This function reads the data list from the sd.conf file and pulls
3701  *    the values that can have numeric values as arguments and places
3702  *    the values in the apropriate sd_tunables member.
3703  *    Since the order of the data list members varies across platforms
3704  *    This function reads them from the data list in a platform specific
3705  *    order and places them into the correct sd_tunable member that is
3706  *    a consistant across all platforms.
3707  */
3708 static void
3709 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
3710     sd_tunables *values)
3711 {
3712 	int i;
3713 	int mask;
3714 
3715 	bzero(values, sizeof (sd_tunables));
3716 
3717 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3718 
3719 		mask = 1 << i;
3720 		if (mask > flags) {
3721 			break;
3722 		}
3723 
3724 		switch (mask & flags) {
3725 		case 0:	/* This mask bit not set in flags */
3726 			continue;
3727 		case SD_CONF_BSET_THROTTLE:
3728 			values->sdt_throttle = data_list[i];
3729 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3730 			    "sd_get_tunables_from_conf: throttle = %d\n",
3731 			    values->sdt_throttle);
3732 			break;
3733 		case SD_CONF_BSET_CTYPE:
3734 			values->sdt_ctype = data_list[i];
3735 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3736 			    "sd_get_tunables_from_conf: ctype = %d\n",
3737 			    values->sdt_ctype);
3738 			break;
3739 		case SD_CONF_BSET_NRR_COUNT:
3740 			values->sdt_not_rdy_retries = data_list[i];
3741 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3742 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
3743 			    values->sdt_not_rdy_retries);
3744 			break;
3745 		case SD_CONF_BSET_BSY_RETRY_COUNT:
3746 			values->sdt_busy_retries = data_list[i];
3747 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3748 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
3749 			    values->sdt_busy_retries);
3750 			break;
3751 		case SD_CONF_BSET_RST_RETRIES:
3752 			values->sdt_reset_retries = data_list[i];
3753 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3754 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
3755 			    values->sdt_reset_retries);
3756 			break;
3757 		case SD_CONF_BSET_RSV_REL_TIME:
3758 			values->sdt_reserv_rel_time = data_list[i];
3759 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3760 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
3761 			    values->sdt_reserv_rel_time);
3762 			break;
3763 		case SD_CONF_BSET_MIN_THROTTLE:
3764 			values->sdt_min_throttle = data_list[i];
3765 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3766 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
3767 			    values->sdt_min_throttle);
3768 			break;
3769 		case SD_CONF_BSET_DISKSORT_DISABLED:
3770 			values->sdt_disk_sort_dis = data_list[i];
3771 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3772 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
3773 			    values->sdt_disk_sort_dis);
3774 			break;
3775 		case SD_CONF_BSET_LUN_RESET_ENABLED:
3776 			values->sdt_lun_reset_enable = data_list[i];
3777 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3778 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
3779 			    "\n", values->sdt_lun_reset_enable);
3780 			break;
3781 		}
3782 	}
3783 }
3784 
3785 /*
3786  *    Function: sd_process_sdconf_table
3787  *
3788  * Description: Search the static configuration table for a match on the
3789  *		inquiry vid/pid and update the driver soft state structure
3790  *		according to the table property values for the device.
3791  *
3792  *		The form of a configuration table entry is:
3793  *		  <vid+pid>,<flags>,<property-data>
3794  *		  "SEAGATE ST42400N",1,63,0,0			(Fibre)
3795  *		  "SEAGATE ST42400N",1,63,0,0,0,0		(Sparc)
3796  *		  "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0	(Intel)
3797  *
3798  *   Arguments: un - driver soft state (unit) structure
3799  */
3800 
3801 static void
3802 sd_process_sdconf_table(struct sd_lun *un)
3803 {
3804 	char	*id = NULL;
3805 	int	table_index;
3806 	int	idlen;
3807 
3808 	ASSERT(un != NULL);
3809 	for (table_index = 0; table_index < sd_disk_table_size;
3810 	    table_index++) {
3811 		id = sd_disk_table[table_index].device_id;
3812 		idlen = strlen(id);
3813 		if (idlen == 0) {
3814 			continue;
3815 		}
3816 
3817 		/*
3818 		 * The static configuration table currently does not
3819 		 * implement version 10 properties. Additionally,
3820 		 * multiple data-property-name entries are not
3821 		 * implemented in the static configuration table.
3822 		 */
3823 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
3824 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3825 			    "sd_process_sdconf_table: disk %s\n", id);
3826 			sd_set_vers1_properties(un,
3827 			    sd_disk_table[table_index].flags,
3828 			    sd_disk_table[table_index].properties);
3829 			break;
3830 		}
3831 	}
3832 }
3833 
3834 
3835 /*
3836  *    Function: sd_sdconf_id_match
3837  *
3838  * Description: This local function implements a case sensitive vid/pid
3839  *		comparison as well as the boundary cases of wild card and
3840  *		multiple blanks.
3841  *
3842  *		Note: An implicit assumption made here is that the scsi
3843  *		inquiry structure will always keep the vid, pid and
3844  *		revision strings in consecutive sequence, so they can be
3845  *		read as a single string. If this assumption is not the
3846  *		case, a separate string, to be used for the check, needs
3847  *		to be built with these strings concatenated.
3848  *
3849  *   Arguments: un - driver soft state (unit) structure
3850  *		id - table or config file vid/pid
3851  *		idlen  - length of the vid/pid (bytes)
3852  *
3853  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3854  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3855  */
3856 
3857 static int
3858 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
3859 {
3860 	struct scsi_inquiry	*sd_inq;
3861 	int 			rval = SD_SUCCESS;
3862 
3863 	ASSERT(un != NULL);
3864 	sd_inq = un->un_sd->sd_inq;
3865 	ASSERT(id != NULL);
3866 
3867 	/*
3868 	 * We use the inq_vid as a pointer to a buffer containing the
3869 	 * vid and pid and use the entire vid/pid length of the table
3870 	 * entry for the comparison. This works because the inq_pid
3871 	 * data member follows inq_vid in the scsi_inquiry structure.
3872 	 */
3873 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
3874 		/*
3875 		 * The user id string is compared to the inquiry vid/pid
3876 		 * using a case insensitive comparison and ignoring
3877 		 * multiple spaces.
3878 		 */
3879 		rval = sd_blank_cmp(un, id, idlen);
3880 		if (rval != SD_SUCCESS) {
3881 			/*
3882 			 * User id strings that start and end with a "*"
3883 			 * are a special case. These do not have a
3884 			 * specific vendor, and the product string can
3885 			 * appear anywhere in the 16 byte PID portion of
3886 			 * the inquiry data. This is a simple strstr()
3887 			 * type search for the user id in the inquiry data.
3888 			 */
3889 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
3890 				char	*pidptr = &id[1];
3891 				int	i;
3892 				int	j;
3893 				int	pidstrlen = idlen - 2;
3894 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
3895 				    pidstrlen;
3896 
3897 				if (j < 0) {
3898 					return (SD_FAILURE);
3899 				}
3900 				for (i = 0; i < j; i++) {
3901 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
3902 					    pidptr, pidstrlen) == 0) {
3903 						rval = SD_SUCCESS;
3904 						break;
3905 					}
3906 				}
3907 			}
3908 		}
3909 	}
3910 	return (rval);
3911 }
3912 
3913 
3914 /*
3915  *    Function: sd_blank_cmp
3916  *
3917  * Description: If the id string starts and ends with a space, treat
3918  *		multiple consecutive spaces as equivalent to a single
3919  *		space. For example, this causes a sd_disk_table entry
3920  *		of " NEC CDROM " to match a device's id string of
3921  *		"NEC       CDROM".
3922  *
3923  *		Note: The success exit condition for this routine is if
3924  *		the pointer to the table entry is '\0' and the cnt of
3925  *		the inquiry length is zero. This will happen if the inquiry
3926  *		string returned by the device is padded with spaces to be
3927  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
3928  *		SCSI spec states that the inquiry string is to be padded with
3929  *		spaces.
3930  *
3931  *   Arguments: un - driver soft state (unit) structure
3932  *		id - table or config file vid/pid
3933  *		idlen  - length of the vid/pid (bytes)
3934  *
3935  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3936  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3937  */
3938 
3939 static int
3940 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
3941 {
3942 	char		*p1;
3943 	char		*p2;
3944 	int		cnt;
3945 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
3946 	    sizeof (SD_INQUIRY(un)->inq_pid);
3947 
3948 	ASSERT(un != NULL);
3949 	p2 = un->un_sd->sd_inq->inq_vid;
3950 	ASSERT(id != NULL);
3951 	p1 = id;
3952 
3953 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
3954 		/*
3955 		 * Note: string p1 is terminated by a NUL but string p2
3956 		 * isn't.  The end of p2 is determined by cnt.
3957 		 */
3958 		for (;;) {
3959 			/* skip over any extra blanks in both strings */
3960 			while ((*p1 != '\0') && (*p1 == ' ')) {
3961 				p1++;
3962 			}
3963 			while ((cnt != 0) && (*p2 == ' ')) {
3964 				p2++;
3965 				cnt--;
3966 			}
3967 
3968 			/* compare the two strings */
3969 			if ((cnt == 0) ||
3970 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
3971 				break;
3972 			}
3973 			while ((cnt > 0) &&
3974 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
3975 				p1++;
3976 				p2++;
3977 				cnt--;
3978 			}
3979 		}
3980 	}
3981 
3982 	/* return SD_SUCCESS if both strings match */
3983 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
3984 }
3985 
3986 
3987 /*
3988  *    Function: sd_chk_vers1_data
3989  *
3990  * Description: Verify the version 1 device properties provided by the
3991  *		user via the configuration file
3992  *
3993  *   Arguments: un	     - driver soft state (unit) structure
3994  *		flags	     - integer mask indicating properties to be set
3995  *		prop_list    - integer list of property values
3996  *		list_len     - length of user provided data
3997  *
3998  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
3999  *		SD_FAILURE - Indicates the user provided data is invalid
4000  */
4001 
4002 static int
4003 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4004     int list_len, char *dataname_ptr)
4005 {
4006 	int i;
4007 	int mask = 1;
4008 	int index = 0;
4009 
4010 	ASSERT(un != NULL);
4011 
4012 	/* Check for a NULL property name and list */
4013 	if (dataname_ptr == NULL) {
4014 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4015 		    "sd_chk_vers1_data: NULL data property name.");
4016 		return (SD_FAILURE);
4017 	}
4018 	if (prop_list == NULL) {
4019 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4020 		    "sd_chk_vers1_data: %s NULL data property list.",
4021 		    dataname_ptr);
4022 		return (SD_FAILURE);
4023 	}
4024 
4025 	/* Display a warning if undefined bits are set in the flags */
4026 	if (flags & ~SD_CONF_BIT_MASK) {
4027 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4028 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4029 		    "Properties not set.",
4030 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4031 		return (SD_FAILURE);
4032 	}
4033 
4034 	/*
4035 	 * Verify the length of the list by identifying the highest bit set
4036 	 * in the flags and validating that the property list has a length
4037 	 * up to the index of this bit.
4038 	 */
4039 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4040 		if (flags & mask) {
4041 			index++;
4042 		}
4043 		mask = 1 << i;
4044 	}
4045 	if ((list_len / sizeof (int)) < (index + 2)) {
4046 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4047 		    "sd_chk_vers1_data: "
4048 		    "Data property list %s size is incorrect. "
4049 		    "Properties not set.", dataname_ptr);
4050 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4051 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4052 		return (SD_FAILURE);
4053 	}
4054 	return (SD_SUCCESS);
4055 }
4056 
4057 
4058 /*
4059  *    Function: sd_set_vers1_properties
4060  *
4061  * Description: Set version 1 device properties based on a property list
4062  *		retrieved from the driver configuration file or static
4063  *		configuration table. Version 1 properties have the format:
4064  *
4065  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4066  *
4067  *		where the prop0 value will be used to set prop0 if bit0
4068  *		is set in the flags
4069  *
4070  *   Arguments: un	     - driver soft state (unit) structure
4071  *		flags	     - integer mask indicating properties to be set
4072  *		prop_list    - integer list of property values
4073  */
4074 
4075 static void
4076 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4077 {
4078 	ASSERT(un != NULL);
4079 
4080 	/*
4081 	 * Set the flag to indicate cache is to be disabled. An attempt
4082 	 * to disable the cache via sd_cache_control() will be made
4083 	 * later during attach once the basic initialization is complete.
4084 	 */
4085 	if (flags & SD_CONF_BSET_NOCACHE) {
4086 		un->un_f_opt_disable_cache = TRUE;
4087 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4088 		    "sd_set_vers1_properties: caching disabled flag set\n");
4089 	}
4090 
4091 	/* CD-specific configuration parameters */
4092 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4093 		un->un_f_cfg_playmsf_bcd = TRUE;
4094 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4095 		    "sd_set_vers1_properties: playmsf_bcd set\n");
4096 	}
4097 	if (flags & SD_CONF_BSET_READSUB_BCD) {
4098 		un->un_f_cfg_readsub_bcd = TRUE;
4099 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4100 		    "sd_set_vers1_properties: readsub_bcd set\n");
4101 	}
4102 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4103 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
4104 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4105 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4106 	}
4107 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4108 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
4109 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4110 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4111 	}
4112 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4113 		un->un_f_cfg_no_read_header = TRUE;
4114 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4115 			    "sd_set_vers1_properties: no_read_header set\n");
4116 	}
4117 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
4118 		un->un_f_cfg_read_cd_xd4 = TRUE;
4119 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4120 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
4121 	}
4122 
4123 	/* Support for devices which do not have valid/unique serial numbers */
4124 	if (flags & SD_CONF_BSET_FAB_DEVID) {
4125 		un->un_f_opt_fab_devid = TRUE;
4126 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4127 		    "sd_set_vers1_properties: fab_devid bit set\n");
4128 	}
4129 
4130 	/* Support for user throttle configuration */
4131 	if (flags & SD_CONF_BSET_THROTTLE) {
4132 		ASSERT(prop_list != NULL);
4133 		un->un_saved_throttle = un->un_throttle =
4134 		    prop_list->sdt_throttle;
4135 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4136 		    "sd_set_vers1_properties: throttle set to %d\n",
4137 		    prop_list->sdt_throttle);
4138 	}
4139 
4140 	/* Set the per disk retry count according to the conf file or table. */
4141 	if (flags & SD_CONF_BSET_NRR_COUNT) {
4142 		ASSERT(prop_list != NULL);
4143 		if (prop_list->sdt_not_rdy_retries) {
4144 			un->un_notready_retry_count =
4145 				prop_list->sdt_not_rdy_retries;
4146 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4147 			    "sd_set_vers1_properties: not ready retry count"
4148 			    " set to %d\n", un->un_notready_retry_count);
4149 		}
4150 	}
4151 
4152 	/* The controller type is reported for generic disk driver ioctls */
4153 	if (flags & SD_CONF_BSET_CTYPE) {
4154 		ASSERT(prop_list != NULL);
4155 		switch (prop_list->sdt_ctype) {
4156 		case CTYPE_CDROM:
4157 			un->un_ctype = prop_list->sdt_ctype;
4158 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4159 			    "sd_set_vers1_properties: ctype set to "
4160 			    "CTYPE_CDROM\n");
4161 			break;
4162 		case CTYPE_CCS:
4163 			un->un_ctype = prop_list->sdt_ctype;
4164 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4165 				"sd_set_vers1_properties: ctype set to "
4166 				"CTYPE_CCS\n");
4167 			break;
4168 		case CTYPE_ROD:		/* RW optical */
4169 			un->un_ctype = prop_list->sdt_ctype;
4170 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4171 			    "sd_set_vers1_properties: ctype set to "
4172 			    "CTYPE_ROD\n");
4173 			break;
4174 		default:
4175 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4176 			    "sd_set_vers1_properties: Could not set "
4177 			    "invalid ctype value (%d)",
4178 			    prop_list->sdt_ctype);
4179 		}
4180 	}
4181 
4182 	/* Purple failover timeout */
4183 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4184 		ASSERT(prop_list != NULL);
4185 		un->un_busy_retry_count =
4186 			prop_list->sdt_busy_retries;
4187 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4188 		    "sd_set_vers1_properties: "
4189 		    "busy retry count set to %d\n",
4190 		    un->un_busy_retry_count);
4191 	}
4192 
4193 	/* Purple reset retry count */
4194 	if (flags & SD_CONF_BSET_RST_RETRIES) {
4195 		ASSERT(prop_list != NULL);
4196 		un->un_reset_retry_count =
4197 			prop_list->sdt_reset_retries;
4198 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4199 		    "sd_set_vers1_properties: "
4200 		    "reset retry count set to %d\n",
4201 		    un->un_reset_retry_count);
4202 	}
4203 
4204 	/* Purple reservation release timeout */
4205 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4206 		ASSERT(prop_list != NULL);
4207 		un->un_reserve_release_time =
4208 			prop_list->sdt_reserv_rel_time;
4209 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4210 		    "sd_set_vers1_properties: "
4211 		    "reservation release timeout set to %d\n",
4212 		    un->un_reserve_release_time);
4213 	}
4214 
4215 	/*
4216 	 * Driver flag telling the driver to verify that no commands are pending
4217 	 * for a device before issuing a Test Unit Ready. This is a workaround
4218 	 * for a firmware bug in some Seagate eliteI drives.
4219 	 */
4220 	if (flags & SD_CONF_BSET_TUR_CHECK) {
4221 		un->un_f_cfg_tur_check = TRUE;
4222 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4223 		    "sd_set_vers1_properties: tur queue check set\n");
4224 	}
4225 
4226 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4227 		un->un_min_throttle = prop_list->sdt_min_throttle;
4228 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4229 		    "sd_set_vers1_properties: min throttle set to %d\n",
4230 		    un->un_min_throttle);
4231 	}
4232 
4233 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4234 		un->un_f_disksort_disabled =
4235 		    (prop_list->sdt_disk_sort_dis != 0) ?
4236 		    TRUE : FALSE;
4237 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4238 		    "sd_set_vers1_properties: disksort disabled "
4239 		    "flag set to %d\n",
4240 		    prop_list->sdt_disk_sort_dis);
4241 	}
4242 
4243 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4244 		un->un_f_lun_reset_enabled =
4245 		    (prop_list->sdt_lun_reset_enable != 0) ?
4246 		    TRUE : FALSE;
4247 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4248 		    "sd_set_vers1_properties: lun reset enabled "
4249 		    "flag set to %d\n",
4250 		    prop_list->sdt_lun_reset_enable);
4251 	}
4252 
4253 	/*
4254 	 * Validate the throttle values.
4255 	 * If any of the numbers are invalid, set everything to defaults.
4256 	 */
4257 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4258 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4259 	    (un->un_min_throttle > un->un_throttle)) {
4260 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4261 		un->un_min_throttle = sd_min_throttle;
4262 	}
4263 }
4264 
4265 /*
4266  *   Function: sd_is_lsi()
4267  *
4268  *   Description: Check for lsi devices, step throught the static device
4269  *	table to match vid/pid.
4270  *
4271  *   Args: un - ptr to sd_lun
4272  *
4273  *   Notes:  When creating new LSI property, need to add the new LSI property
4274  *		to this function.
4275  */
4276 static void
4277 sd_is_lsi(struct sd_lun *un)
4278 {
4279 	char	*id = NULL;
4280 	int	table_index;
4281 	int	idlen;
4282 	void	*prop;
4283 
4284 	ASSERT(un != NULL);
4285 	for (table_index = 0; table_index < sd_disk_table_size;
4286 	    table_index++) {
4287 		id = sd_disk_table[table_index].device_id;
4288 		idlen = strlen(id);
4289 		if (idlen == 0) {
4290 			continue;
4291 		}
4292 
4293 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4294 			prop = sd_disk_table[table_index].properties;
4295 			if (prop == &lsi_properties ||
4296 			    prop == &lsi_oem_properties ||
4297 			    prop == &lsi_properties_scsi ||
4298 			    prop == &symbios_properties) {
4299 				un->un_f_cfg_is_lsi = TRUE;
4300 			}
4301 			break;
4302 		}
4303 	}
4304 }
4305 
4306 
4307 /*
4308  * The following routines support reading and interpretation of disk labels,
4309  * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and
4310  * fdisk tables.
4311  */
4312 
4313 /*
4314  *    Function: sd_validate_geometry
4315  *
4316  * Description: Read the label from the disk (if present). Update the unit's
4317  *		geometry and vtoc information from the data in the label.
4318  *		Verify that the label is valid.
4319  *
4320  *   Arguments: un - driver soft state (unit) structure
4321  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4322  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4323  *			to use the USCSI "direct" chain and bypass the normal
4324  *			command waitq.
4325  *
4326  * Return Code: 0 - Successful completion
4327  *		EINVAL  - Invalid value in un->un_tgt_blocksize or
4328  *			  un->un_blockcount; or label on disk is corrupted
4329  *			  or unreadable.
4330  *		EACCES  - Reservation conflict at the device.
4331  *		ENOMEM  - Resource allocation error
4332  *		ENOTSUP - geometry not applicable
4333  *
4334  *     Context: Kernel thread only (can sleep).
4335  */
4336 
4337 static int
4338 sd_validate_geometry(struct sd_lun *un, int path_flag)
4339 {
4340 	static	char		labelstring[128];
4341 	static	char		buf[256];
4342 	char	*label		= NULL;
4343 	int	label_error = 0;
4344 	int	gvalid		= un->un_f_geometry_is_valid;
4345 	int	lbasize;
4346 	uint_t	capacity;
4347 	int	count;
4348 
4349 	ASSERT(un != NULL);
4350 	ASSERT(mutex_owned(SD_MUTEX(un)));
4351 
4352 	/*
4353 	 * If the required values are not valid, then try getting them
4354 	 * once via read capacity. If that fails, then fail this call.
4355 	 * This is necessary with the new mpxio failover behavior in
4356 	 * the T300 where we can get an attach for the inactive path
4357 	 * before the active path. The inactive path fails commands with
4358 	 * sense data of 02,04,88 which happens to the read capacity
4359 	 * before mpxio has had sufficient knowledge to know if it should
4360 	 * force a fail over or not. (Which it won't do at attach anyhow).
4361 	 * If the read capacity at attach time fails, un_tgt_blocksize and
4362 	 * un_blockcount won't be valid.
4363 	 */
4364 	if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
4365 	    (un->un_f_blockcount_is_valid != TRUE)) {
4366 		uint64_t	cap;
4367 		uint32_t	lbasz;
4368 		int		rval;
4369 
4370 		mutex_exit(SD_MUTEX(un));
4371 		rval = sd_send_scsi_READ_CAPACITY(un, &cap,
4372 		    &lbasz, SD_PATH_DIRECT);
4373 		mutex_enter(SD_MUTEX(un));
4374 		if (rval == 0) {
4375 			/*
4376 			 * The following relies on
4377 			 * sd_send_scsi_READ_CAPACITY never
4378 			 * returning 0 for capacity and/or lbasize.
4379 			 */
4380 			sd_update_block_info(un, lbasz, cap);
4381 		}
4382 
4383 		if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
4384 		    (un->un_f_blockcount_is_valid != TRUE)) {
4385 			return (EINVAL);
4386 		}
4387 	}
4388 
4389 	/*
4390 	 * Copy the lbasize and capacity so that if they're reset while we're
4391 	 * not holding the SD_MUTEX, we will continue to use valid values
4392 	 * after the SD_MUTEX is reacquired. (4119659)
4393 	 */
4394 	lbasize  = un->un_tgt_blocksize;
4395 	capacity = un->un_blockcount;
4396 
4397 #if defined(_SUNOS_VTOC_16)
4398 	/*
4399 	 * Set up the "whole disk" fdisk partition; this should always
4400 	 * exist, regardless of whether the disk contains an fdisk table
4401 	 * or vtoc.
4402 	 */
4403 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
4404 	un->un_map[P0_RAW_DISK].dkl_nblk  = capacity;
4405 #endif
4406 
4407 	/*
4408 	 * Refresh the logical and physical geometry caches.
4409 	 * (data from MODE SENSE format/rigid disk geometry pages,
4410 	 * and scsi_ifgetcap("geometry").
4411 	 */
4412 	sd_resync_geom_caches(un, capacity, lbasize, path_flag);
4413 
4414 	label_error = sd_use_efi(un, path_flag);
4415 	if (label_error == 0) {
4416 		/* found a valid EFI label */
4417 		SD_TRACE(SD_LOG_IO_PARTITION, un,
4418 			"sd_validate_geometry: found EFI label\n");
4419 		un->un_solaris_offset = 0;
4420 		un->un_solaris_size = capacity;
4421 		return (ENOTSUP);
4422 	}
4423 	if (un->un_blockcount > DK_MAX_BLOCKS) {
4424 		if (label_error == ESRCH) {
4425 			/*
4426 			 * they've configured a LUN over 1TB, but used
4427 			 * format.dat to restrict format's view of the
4428 			 * capacity to be under 1TB
4429 			 */
4430 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4431 "is >1TB and has a VTOC label: use format(1M) to either decrease the");
4432 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
4433 "size to be < 1TB or relabel the disk with an EFI label");
4434 		} else {
4435 			/* unlabeled disk over 1TB */
4436 #if defined(__i386) || defined(__amd64)
4437 			/*
4438 			 * Refer to comments on off-by-1 at the head of the file
4439 			 * A 1TB disk was treated as (1T - 512)B in the past,
4440 			 * thus, it might have valid solaris partition. We
4441 			 * will return ENOTSUP later only if this disk has no
4442 			 * valid solaris partition.
4443 			 */
4444 			if ((un->un_tgt_blocksize != un->un_sys_blocksize) ||
4445 			    (un->un_blockcount - 1 > DK_MAX_BLOCKS) ||
4446 			    un->un_f_has_removable_media ||
4447 			    un->un_f_is_hotpluggable)
4448 #endif
4449 				return (ENOTSUP);
4450 		}
4451 	}
4452 	label_error = 0;
4453 
4454 	/*
4455 	 * at this point it is either labeled with a VTOC or it is
4456 	 * under 1TB (<= 1TB actually for off-by-1)
4457 	 */
4458 	if (un->un_f_vtoc_label_supported) {
4459 		struct	dk_label *dkl;
4460 		offset_t dkl1;
4461 		offset_t label_addr, real_addr;
4462 		int	rval;
4463 		size_t	buffer_size;
4464 
4465 		/*
4466 		 * Note: This will set up un->un_solaris_size and
4467 		 * un->un_solaris_offset.
4468 		 */
4469 		switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) {
4470 		case SD_CMD_RESERVATION_CONFLICT:
4471 			ASSERT(mutex_owned(SD_MUTEX(un)));
4472 			return (EACCES);
4473 		case SD_CMD_FAILURE:
4474 			ASSERT(mutex_owned(SD_MUTEX(un)));
4475 			return (ENOMEM);
4476 		}
4477 
4478 		if (un->un_solaris_size <= DK_LABEL_LOC) {
4479 
4480 #if defined(__i386) || defined(__amd64)
4481 			/*
4482 			 * Refer to comments on off-by-1 at the head of the file
4483 			 * This is for 1TB disk only. Since that there is no
4484 			 * solaris partitions, return ENOTSUP as we do for
4485 			 * >1TB disk.
4486 			 */
4487 			if (un->un_blockcount > DK_MAX_BLOCKS)
4488 				return (ENOTSUP);
4489 #endif
4490 			/*
4491 			 * Found fdisk table but no Solaris partition entry,
4492 			 * so don't call sd_uselabel() and don't create
4493 			 * a default label.
4494 			 */
4495 			label_error = 0;
4496 			un->un_f_geometry_is_valid = TRUE;
4497 			goto no_solaris_partition;
4498 		}
4499 		label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC);
4500 
4501 #if defined(__i386) || defined(__amd64)
4502 		/*
4503 		 * Refer to comments on off-by-1 at the head of the file
4504 		 * Now, this 1TB disk has valid solaris partition. It
4505 		 * must be created by previous sd driver, we have to
4506 		 * treat it as (1T-512)B.
4507 		 */
4508 		if (un->un_blockcount > DK_MAX_BLOCKS) {
4509 			un->un_f_capacity_adjusted = 1;
4510 			un->un_blockcount = DK_MAX_BLOCKS;
4511 			un->un_map[P0_RAW_DISK].dkl_nblk  = DK_MAX_BLOCKS;
4512 
4513 			/*
4514 			 * Refer to sd_read_fdisk, when there is no
4515 			 * fdisk partition table, un_solaris_size is
4516 			 * set to disk's capacity. In this case, we
4517 			 * need to adjust it
4518 			 */
4519 			if (un->un_solaris_size > DK_MAX_BLOCKS)
4520 				un->un_solaris_size = DK_MAX_BLOCKS;
4521 			sd_resync_geom_caches(un, DK_MAX_BLOCKS,
4522 			    lbasize, path_flag);
4523 		}
4524 #endif
4525 
4526 		/*
4527 		 * sys_blocksize != tgt_blocksize, need to re-adjust
4528 		 * blkno and save the index to beginning of dk_label
4529 		 */
4530 		real_addr = SD_SYS2TGTBLOCK(un, label_addr);
4531 		buffer_size = SD_REQBYTES2TGTBYTES(un,
4532 		    sizeof (struct dk_label));
4533 
4534 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: "
4535 		    "label_addr: 0x%x allocation size: 0x%x\n",
4536 		    label_addr, buffer_size);
4537 		dkl = kmem_zalloc(buffer_size, KM_NOSLEEP);
4538 		if (dkl == NULL) {
4539 			return (ENOMEM);
4540 		}
4541 
4542 		mutex_exit(SD_MUTEX(un));
4543 		rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr,
4544 		    path_flag);
4545 		mutex_enter(SD_MUTEX(un));
4546 
4547 		switch (rval) {
4548 		case 0:
4549 			/*
4550 			 * sd_uselabel will establish that the geometry
4551 			 * is valid.
4552 			 * For sys_blocksize != tgt_blocksize, need
4553 			 * to index into the beginning of dk_label
4554 			 */
4555 			dkl1 = (daddr_t)dkl
4556 				+ SD_TGTBYTEOFFSET(un, label_addr, real_addr);
4557 			if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1,
4558 			    path_flag) != SD_LABEL_IS_VALID) {
4559 				label_error = EINVAL;
4560 			}
4561 			break;
4562 		case EACCES:
4563 			label_error = EACCES;
4564 			break;
4565 		default:
4566 			label_error = EINVAL;
4567 			break;
4568 		}
4569 
4570 		kmem_free(dkl, buffer_size);
4571 
4572 #if defined(_SUNOS_VTOC_8)
4573 		label = (char *)un->un_asciilabel;
4574 #elif defined(_SUNOS_VTOC_16)
4575 		label = (char *)un->un_vtoc.v_asciilabel;
4576 #else
4577 #error "No VTOC format defined."
4578 #endif
4579 	}
4580 
4581 	/*
4582 	 * If a valid label was not found, AND if no reservation conflict
4583 	 * was detected, then go ahead and create a default label (4069506).
4584 	 */
4585 	if (un->un_f_default_vtoc_supported && (label_error != EACCES)) {
4586 		if (un->un_f_geometry_is_valid == FALSE) {
4587 			sd_build_default_label(un);
4588 		}
4589 		label_error = 0;
4590 	}
4591 
4592 no_solaris_partition:
4593 	if ((!un->un_f_has_removable_media ||
4594 	    (un->un_f_has_removable_media &&
4595 		un->un_mediastate == DKIO_EJECTED)) &&
4596 		(un->un_state == SD_STATE_NORMAL && !gvalid)) {
4597 		/*
4598 		 * Print out a message indicating who and what we are.
4599 		 * We do this only when we happen to really validate the
4600 		 * geometry. We may call sd_validate_geometry() at other
4601 		 * times, e.g., ioctl()'s like Get VTOC in which case we
4602 		 * don't want to print the label.
4603 		 * If the geometry is valid, print the label string,
4604 		 * else print vendor and product info, if available
4605 		 */
4606 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
4607 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label);
4608 		} else {
4609 			mutex_enter(&sd_label_mutex);
4610 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
4611 			    labelstring);
4612 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
4613 			    &labelstring[64]);
4614 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
4615 			    labelstring, &labelstring[64]);
4616 			if (un->un_f_blockcount_is_valid == TRUE) {
4617 				(void) sprintf(&buf[strlen(buf)],
4618 				    ", %llu %u byte blocks\n",
4619 				    (longlong_t)un->un_blockcount,
4620 				    un->un_tgt_blocksize);
4621 			} else {
4622 				(void) sprintf(&buf[strlen(buf)],
4623 				    ", (unknown capacity)\n");
4624 			}
4625 			SD_INFO(SD_LOG_ATTACH_DETACH, un, buf);
4626 			mutex_exit(&sd_label_mutex);
4627 		}
4628 	}
4629 
4630 #if defined(_SUNOS_VTOC_16)
4631 	/*
4632 	 * If we have valid geometry, set up the remaining fdisk partitions.
4633 	 * Note that dkl_cylno is not used for the fdisk map entries, so
4634 	 * we set it to an entirely bogus value.
4635 	 */
4636 	for (count = 0; count < FD_NUMPART; count++) {
4637 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
4638 		un->un_map[FDISK_P1 + count].dkl_nblk =
4639 		    un->un_fmap[count].fmap_nblk;
4640 
4641 		un->un_offset[FDISK_P1 + count] =
4642 		    un->un_fmap[count].fmap_start;
4643 	}
4644 #endif
4645 
4646 	for (count = 0; count < NDKMAP; count++) {
4647 #if defined(_SUNOS_VTOC_8)
4648 		struct dk_map *lp  = &un->un_map[count];
4649 		un->un_offset[count] =
4650 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
4651 #elif defined(_SUNOS_VTOC_16)
4652 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
4653 
4654 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
4655 #else
4656 #error "No VTOC format defined."
4657 #endif
4658 	}
4659 
4660 	return (label_error);
4661 }
4662 
4663 
4664 #if defined(_SUNOS_VTOC_16)
4665 /*
4666  * Macro: MAX_BLKS
4667  *
4668  *	This macro is used for table entries where we need to have the largest
4669  *	possible sector value for that head & SPT (sectors per track)
4670  *	combination.  Other entries for some smaller disk sizes are set by
4671  *	convention to match those used by X86 BIOS usage.
4672  */
4673 #define	MAX_BLKS(heads, spt)	UINT16_MAX * heads * spt, heads, spt
4674 
4675 /*
4676  *    Function: sd_convert_geometry
4677  *
4678  * Description: Convert physical geometry into a dk_geom structure. In
4679  *		other words, make sure we don't wrap 16-bit values.
4680  *		e.g. converting from geom_cache to dk_geom
4681  *
4682  *     Context: Kernel thread only
4683  */
4684 static void
4685 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g)
4686 {
4687 	int i;
4688 	static const struct chs_values {
4689 		uint_t max_cap;		/* Max Capacity for this HS. */
4690 		uint_t nhead;		/* Heads to use. */
4691 		uint_t nsect;		/* SPT to use. */
4692 	} CHS_values[] = {
4693 		{0x00200000,  64, 32},		/* 1GB or smaller disk. */
4694 		{0x01000000, 128, 32},		/* 8GB or smaller disk. */
4695 		{MAX_BLKS(255,  63)},		/* 502.02GB or smaller disk. */
4696 		{MAX_BLKS(255, 126)},		/* .98TB or smaller disk. */
4697 		{DK_MAX_BLOCKS, 255, 189}	/* Max size is just under 1TB */
4698 	};
4699 
4700 	/* Unlabeled SCSI floppy device */
4701 	if (capacity <= 0x1000) {
4702 		un_g->dkg_nhead = 2;
4703 		un_g->dkg_ncyl = 80;
4704 		un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl);
4705 		return;
4706 	}
4707 
4708 	/*
4709 	 * For all devices we calculate cylinders using the
4710 	 * heads and sectors we assign based on capacity of the
4711 	 * device.  The table is designed to be compatible with the
4712 	 * way other operating systems lay out fdisk tables for X86
4713 	 * and to insure that the cylinders never exceed 65535 to
4714 	 * prevent problems with X86 ioctls that report geometry.
4715 	 * We use SPT that are multiples of 63, since other OSes that
4716 	 * are not limited to 16-bits for cylinders stop at 63 SPT
4717 	 * we make do by using multiples of 63 SPT.
4718 	 *
4719 	 * Note than capacities greater than or equal to 1TB will simply
4720 	 * get the largest geometry from the table. This should be okay
4721 	 * since disks this large shouldn't be using CHS values anyway.
4722 	 */
4723 	for (i = 0; CHS_values[i].max_cap < capacity &&
4724 	    CHS_values[i].max_cap != DK_MAX_BLOCKS; i++)
4725 		;
4726 
4727 	un_g->dkg_nhead = CHS_values[i].nhead;
4728 	un_g->dkg_nsect = CHS_values[i].nsect;
4729 }
4730 #endif
4731 
4732 
4733 /*
4734  *    Function: sd_resync_geom_caches
4735  *
4736  * Description: (Re)initialize both geometry caches: the virtual geometry
4737  *		information is extracted from the HBA (the "geometry"
4738  *		capability), and the physical geometry cache data is
4739  *		generated by issuing MODE SENSE commands.
4740  *
4741  *   Arguments: un - driver soft state (unit) structure
4742  *		capacity - disk capacity in #blocks
4743  *		lbasize - disk block size in bytes
4744  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4745  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4746  *			to use the USCSI "direct" chain and bypass the normal
4747  *			command waitq.
4748  *
4749  *     Context: Kernel thread only (can sleep).
4750  */
4751 
4752 static void
4753 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize,
4754 	int path_flag)
4755 {
4756 	struct 	geom_cache 	pgeom;
4757 	struct 	geom_cache	*pgeom_p = &pgeom;
4758 	int 	spc;
4759 	unsigned short nhead;
4760 	unsigned short nsect;
4761 
4762 	ASSERT(un != NULL);
4763 	ASSERT(mutex_owned(SD_MUTEX(un)));
4764 
4765 	/*
4766 	 * Ask the controller for its logical geometry.
4767 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
4768 	 * then the lgeom cache will be invalid.
4769 	 */
4770 	sd_get_virtual_geometry(un, capacity, lbasize);
4771 
4772 	/*
4773 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
4774 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
4775 	 */
4776 	if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) {
4777 		/*
4778 		 * Note: Perhaps this needs to be more adaptive? The rationale
4779 		 * is that, if there's no HBA geometry from the HBA driver, any
4780 		 * guess is good, since this is the physical geometry. If MODE
4781 		 * SENSE fails this gives a max cylinder size for non-LBA access
4782 		 */
4783 		nhead = 255;
4784 		nsect = 63;
4785 	} else {
4786 		nhead = un->un_lgeom.g_nhead;
4787 		nsect = un->un_lgeom.g_nsect;
4788 	}
4789 
4790 	if (ISCD(un)) {
4791 		pgeom_p->g_nhead = 1;
4792 		pgeom_p->g_nsect = nsect * nhead;
4793 	} else {
4794 		pgeom_p->g_nhead = nhead;
4795 		pgeom_p->g_nsect = nsect;
4796 	}
4797 
4798 	spc = pgeom_p->g_nhead * pgeom_p->g_nsect;
4799 	pgeom_p->g_capacity = capacity;
4800 	pgeom_p->g_ncyl = pgeom_p->g_capacity / spc;
4801 	pgeom_p->g_acyl = 0;
4802 
4803 	/*
4804 	 * Retrieve fresh geometry data from the hardware, stash it
4805 	 * here temporarily before we rebuild the incore label.
4806 	 *
4807 	 * We want to use the MODE SENSE commands to derive the
4808 	 * physical geometry of the device, but if either command
4809 	 * fails, the logical geometry is used as the fallback for
4810 	 * disk label geometry.
4811 	 */
4812 	mutex_exit(SD_MUTEX(un));
4813 	sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag);
4814 	mutex_enter(SD_MUTEX(un));
4815 
4816 	/*
4817 	 * Now update the real copy while holding the mutex. This
4818 	 * way the global copy is never in an inconsistent state.
4819 	 */
4820 	bcopy(pgeom_p, &un->un_pgeom,  sizeof (un->un_pgeom));
4821 
4822 	SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: "
4823 	    "(cached from lgeom)\n");
4824 	SD_INFO(SD_LOG_COMMON, un,
4825 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
4826 	    un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl,
4827 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
4828 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
4829 	    "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize,
4830 	    un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv,
4831 	    un->un_pgeom.g_rpm);
4832 }
4833 
4834 
4835 /*
4836  *    Function: sd_read_fdisk
4837  *
4838  * Description: utility routine to read the fdisk table.
4839  *
4840  *   Arguments: un - driver soft state (unit) structure
4841  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4842  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4843  *			to use the USCSI "direct" chain and bypass the normal
4844  *			command waitq.
4845  *
4846  * Return Code: SD_CMD_SUCCESS
4847  *		SD_CMD_FAILURE
4848  *
4849  *     Context: Kernel thread only (can sleep).
4850  */
4851 /* ARGSUSED */
4852 static int
4853 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag)
4854 {
4855 #if defined(_NO_FDISK_PRESENT)
4856 
4857 	un->un_solaris_offset = 0;
4858 	un->un_solaris_size = capacity;
4859 	bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4860 	return (SD_CMD_SUCCESS);
4861 
4862 #elif defined(_FIRMWARE_NEEDS_FDISK)
4863 
4864 	struct ipart	*fdp;
4865 	struct mboot	*mbp;
4866 	struct ipart	fdisk[FD_NUMPART];
4867 	int		i;
4868 	char		sigbuf[2];
4869 	caddr_t		bufp;
4870 	int		uidx;
4871 	int		rval;
4872 	int		lba = 0;
4873 	uint_t		solaris_offset;	/* offset to solaris part. */
4874 	daddr_t		solaris_size;	/* size of solaris partition */
4875 	uint32_t	blocksize;
4876 
4877 	ASSERT(un != NULL);
4878 	ASSERT(mutex_owned(SD_MUTEX(un)));
4879 	ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE);
4880 
4881 	blocksize = un->un_tgt_blocksize;
4882 
4883 	/*
4884 	 * Start off assuming no fdisk table
4885 	 */
4886 	solaris_offset = 0;
4887 	solaris_size   = capacity;
4888 
4889 	mutex_exit(SD_MUTEX(un));
4890 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
4891 	rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag);
4892 	mutex_enter(SD_MUTEX(un));
4893 
4894 	if (rval != 0) {
4895 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4896 		    "sd_read_fdisk: fdisk read err\n");
4897 		kmem_free(bufp, blocksize);
4898 		return (SD_CMD_FAILURE);
4899 	}
4900 
4901 	mbp = (struct mboot *)bufp;
4902 
4903 	/*
4904 	 * The fdisk table does not begin on a 4-byte boundary within the
4905 	 * master boot record, so we copy it to an aligned structure to avoid
4906 	 * alignment exceptions on some processors.
4907 	 */
4908 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
4909 
4910 	/*
4911 	 * Check for lba support before verifying sig; sig might not be
4912 	 * there, say on a blank disk, but the max_chs mark may still
4913 	 * be present.
4914 	 *
4915 	 * Note: LBA support and BEFs are an x86-only concept but this
4916 	 * code should work OK on SPARC as well.
4917 	 */
4918 
4919 	/*
4920 	 * First, check for lba-access-ok on root node (or prom root node)
4921 	 * if present there, don't need to search fdisk table.
4922 	 */
4923 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
4924 	    "lba-access-ok", 0) != 0) {
4925 		/* All drives do LBA; don't search fdisk table */
4926 		lba = 1;
4927 	} else {
4928 		/* Okay, look for mark in fdisk table */
4929 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
4930 			/* accumulate "lba" value from all partitions */
4931 			lba = (lba || sd_has_max_chs_vals(fdp));
4932 		}
4933 	}
4934 
4935 	if (lba != 0) {
4936 		dev_t dev = sd_make_device(SD_DEVINFO(un));
4937 
4938 		if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS,
4939 		    "lba-access-ok", 0) == 0) {
4940 			/* not found; create it */
4941 			if (ddi_prop_create(dev, SD_DEVINFO(un), 0,
4942 			    "lba-access-ok", (caddr_t)NULL, 0) !=
4943 			    DDI_PROP_SUCCESS) {
4944 				SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4945 				    "sd_read_fdisk: Can't create lba property "
4946 				    "for instance %d\n",
4947 				    ddi_get_instance(SD_DEVINFO(un)));
4948 			}
4949 		}
4950 	}
4951 
4952 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
4953 
4954 	/*
4955 	 * Endian-independent signature check
4956 	 */
4957 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
4958 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
4959 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4960 		    "sd_read_fdisk: no fdisk\n");
4961 		bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4962 		rval = SD_CMD_SUCCESS;
4963 		goto done;
4964 	}
4965 
4966 #ifdef SDDEBUG
4967 	if (sd_level_mask & SD_LOGMASK_INFO) {
4968 		fdp = fdisk;
4969 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n");
4970 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "         relsect    "
4971 		    "numsect         sysid       bootid\n");
4972 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
4973 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4974 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
4975 			    i, fdp->relsect, fdp->numsect,
4976 			    fdp->systid, fdp->bootid);
4977 		}
4978 	}
4979 #endif
4980 
4981 	/*
4982 	 * Try to find the unix partition
4983 	 */
4984 	uidx = -1;
4985 	solaris_offset = 0;
4986 	solaris_size   = 0;
4987 
4988 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
4989 		int	relsect;
4990 		int	numsect;
4991 
4992 		if (fdp->numsect == 0) {
4993 			un->un_fmap[i].fmap_start = 0;
4994 			un->un_fmap[i].fmap_nblk  = 0;
4995 			continue;
4996 		}
4997 
4998 		/*
4999 		 * Data in the fdisk table is little-endian.
5000 		 */
5001 		relsect = LE_32(fdp->relsect);
5002 		numsect = LE_32(fdp->numsect);
5003 
5004 		un->un_fmap[i].fmap_start = relsect;
5005 		un->un_fmap[i].fmap_nblk  = numsect;
5006 
5007 		if (fdp->systid != SUNIXOS &&
5008 		    fdp->systid != SUNIXOS2 &&
5009 		    fdp->systid != EFI_PMBR) {
5010 			continue;
5011 		}
5012 
5013 		/*
5014 		 * use the last active solaris partition id found
5015 		 * (there should only be 1 active partition id)
5016 		 *
5017 		 * if there are no active solaris partition id
5018 		 * then use the first inactive solaris partition id
5019 		 */
5020 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
5021 			uidx = i;
5022 			solaris_offset = relsect;
5023 			solaris_size   = numsect;
5024 		}
5025 	}
5026 
5027 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx",
5028 	    un->un_solaris_offset, un->un_solaris_size);
5029 
5030 	rval = SD_CMD_SUCCESS;
5031 
5032 done:
5033 
5034 	/*
5035 	 * Clear the VTOC info, only if the Solaris partition entry
5036 	 * has moved, changed size, been deleted, or if the size of
5037 	 * the partition is too small to even fit the label sector.
5038 	 */
5039 	if ((un->un_solaris_offset != solaris_offset) ||
5040 	    (un->un_solaris_size != solaris_size) ||
5041 	    solaris_size <= DK_LABEL_LOC) {
5042 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx",
5043 			solaris_offset, solaris_size);
5044 		bzero(&un->un_g, sizeof (struct dk_geom));
5045 		bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
5046 		bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map)));
5047 		un->un_f_geometry_is_valid = FALSE;
5048 	}
5049 	un->un_solaris_offset = solaris_offset;
5050 	un->un_solaris_size = solaris_size;
5051 	kmem_free(bufp, blocksize);
5052 	return (rval);
5053 
5054 #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
5055 #error "fdisk table presence undetermined for this platform."
5056 #endif	/* #if defined(_NO_FDISK_PRESENT) */
5057 }
5058 
5059 
5060 /*
5061  *    Function: sd_get_physical_geometry
5062  *
5063  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
5064  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
5065  *		target, and use this information to initialize the physical
5066  *		geometry cache specified by pgeom_p.
5067  *
5068  *		MODE SENSE is an optional command, so failure in this case
5069  *		does not necessarily denote an error. We want to use the
5070  *		MODE SENSE commands to derive the physical geometry of the
5071  *		device, but if either command fails, the logical geometry is
5072  *		used as the fallback for disk label geometry.
5073  *
5074  *		This requires that un->un_blockcount and un->un_tgt_blocksize
5075  *		have already been initialized for the current target and
5076  *		that the current values be passed as args so that we don't
5077  *		end up ever trying to use -1 as a valid value. This could
5078  *		happen if either value is reset while we're not holding
5079  *		the mutex.
5080  *
5081  *   Arguments: un - driver soft state (unit) structure
5082  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
5083  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
5084  *			to use the USCSI "direct" chain and bypass the normal
5085  *			command waitq.
5086  *
5087  *     Context: Kernel thread only (can sleep).
5088  */
5089 
5090 static void
5091 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p,
5092 	int capacity, int lbasize, int path_flag)
5093 {
5094 	struct	mode_format	*page3p;
5095 	struct	mode_geometry	*page4p;
5096 	struct	mode_header	*headerp;
5097 	int	sector_size;
5098 	int	nsect;
5099 	int	nhead;
5100 	int	ncyl;
5101 	int	intrlv;
5102 	int	spc;
5103 	int	modesense_capacity;
5104 	int	rpm;
5105 	int	bd_len;
5106 	int	mode_header_length;
5107 	uchar_t	*p3bufp;
5108 	uchar_t	*p4bufp;
5109 	int	cdbsize;
5110 
5111 	ASSERT(un != NULL);
5112 	ASSERT(!(mutex_owned(SD_MUTEX(un))));
5113 
5114 	if (un->un_f_blockcount_is_valid != TRUE) {
5115 		return;
5116 	}
5117 
5118 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
5119 		return;
5120 	}
5121 
5122 	if (lbasize == 0) {
5123 		if (ISCD(un)) {
5124 			lbasize = 2048;
5125 		} else {
5126 			lbasize = un->un_sys_blocksize;
5127 		}
5128 	}
5129 	pgeom_p->g_secsize = (unsigned short)lbasize;
5130 
5131 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
5132 
5133 	/*
5134 	 * Retrieve MODE SENSE page 3 - Format Device Page
5135 	 */
5136 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
5137 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp,
5138 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag)
5139 	    != 0) {
5140 		SD_ERROR(SD_LOG_COMMON, un,
5141 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
5142 		goto page3_exit;
5143 	}
5144 
5145 	/*
5146 	 * Determine size of Block Descriptors in order to locate the mode
5147 	 * page data.  ATAPI devices return 0, SCSI devices should return
5148 	 * MODE_BLK_DESC_LENGTH.
5149 	 */
5150 	headerp = (struct mode_header *)p3bufp;
5151 	if (un->un_f_cfg_is_atapi == TRUE) {
5152 		struct mode_header_grp2 *mhp =
5153 		    (struct mode_header_grp2 *)headerp;
5154 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
5155 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5156 	} else {
5157 		mode_header_length = MODE_HEADER_LENGTH;
5158 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5159 	}
5160 
5161 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5162 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
5163 		    "received unexpected bd_len of %d, page3\n", bd_len);
5164 		goto page3_exit;
5165 	}
5166 
5167 	page3p = (struct mode_format *)
5168 	    ((caddr_t)headerp + mode_header_length + bd_len);
5169 
5170 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5171 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
5172 		    "mode sense pg3 code mismatch %d\n",
5173 		    page3p->mode_page.code);
5174 		goto page3_exit;
5175 	}
5176 
5177 	/*
5178 	 * Use this physical geometry data only if BOTH MODE SENSE commands
5179 	 * complete successfully; otherwise, revert to the logical geometry.
5180 	 * So, we need to save everything in temporary variables.
5181 	 */
5182 	sector_size = BE_16(page3p->data_bytes_sect);
5183 
5184 	/*
5185 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5186 	 */
5187 	if (sector_size == 0) {
5188 		sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize;
5189 	} else {
5190 		sector_size &= ~(un->un_sys_blocksize - 1);
5191 	}
5192 
5193 	nsect  = BE_16(page3p->sect_track);
5194 	intrlv = BE_16(page3p->interleave);
5195 
5196 	SD_INFO(SD_LOG_COMMON, un,
5197 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
5198 	SD_INFO(SD_LOG_COMMON, un,
5199 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
5200 	    page3p->mode_page.code, nsect, sector_size);
5201 	SD_INFO(SD_LOG_COMMON, un,
5202 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5203 	    BE_16(page3p->track_skew),
5204 	    BE_16(page3p->cylinder_skew));
5205 
5206 
5207 	/*
5208 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5209 	 */
5210 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5211 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp,
5212 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag)
5213 	    != 0) {
5214 		SD_ERROR(SD_LOG_COMMON, un,
5215 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
5216 		goto page4_exit;
5217 	}
5218 
5219 	/*
5220 	 * Determine size of Block Descriptors in order to locate the mode
5221 	 * page data.  ATAPI devices return 0, SCSI devices should return
5222 	 * MODE_BLK_DESC_LENGTH.
5223 	 */
5224 	headerp = (struct mode_header *)p4bufp;
5225 	if (un->un_f_cfg_is_atapi == TRUE) {
5226 		struct mode_header_grp2 *mhp =
5227 		    (struct mode_header_grp2 *)headerp;
5228 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5229 	} else {
5230 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5231 	}
5232 
5233 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5234 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
5235 		    "received unexpected bd_len of %d, page4\n", bd_len);
5236 		goto page4_exit;
5237 	}
5238 
5239 	page4p = (struct mode_geometry *)
5240 	    ((caddr_t)headerp + mode_header_length + bd_len);
5241 
5242 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5243 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
5244 		    "mode sense pg4 code mismatch %d\n",
5245 		    page4p->mode_page.code);
5246 		goto page4_exit;
5247 	}
5248 
5249 	/*
5250 	 * Stash the data now, after we know that both commands completed.
5251 	 */
5252 
5253 	mutex_enter(SD_MUTEX(un));
5254 
5255 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
5256 	spc   = nhead * nsect;
5257 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5258 	rpm   = BE_16(page4p->rpm);
5259 
5260 	modesense_capacity = spc * ncyl;
5261 
5262 	SD_INFO(SD_LOG_COMMON, un,
5263 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5264 	SD_INFO(SD_LOG_COMMON, un,
5265 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5266 	SD_INFO(SD_LOG_COMMON, un,
5267 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
5268 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
5269 	    (void *)pgeom_p, capacity);
5270 
5271 	/*
5272 	 * Compensate if the drive's geometry is not rectangular, i.e.,
5273 	 * the product of C * H * S returned by MODE SENSE >= that returned
5274 	 * by read capacity. This is an idiosyncrasy of the original x86
5275 	 * disk subsystem.
5276 	 */
5277 	if (modesense_capacity >= capacity) {
5278 		SD_INFO(SD_LOG_COMMON, un,
5279 		    "sd_get_physical_geometry: adjusting acyl; "
5280 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
5281 		    (modesense_capacity - capacity + spc - 1) / spc);
5282 		if (sector_size != 0) {
5283 			/* 1243403: NEC D38x7 drives don't support sec size */
5284 			pgeom_p->g_secsize = (unsigned short)sector_size;
5285 		}
5286 		pgeom_p->g_nsect    = (unsigned short)nsect;
5287 		pgeom_p->g_nhead    = (unsigned short)nhead;
5288 		pgeom_p->g_capacity = capacity;
5289 		pgeom_p->g_acyl	    =
5290 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5291 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5292 	}
5293 
5294 	pgeom_p->g_rpm    = (unsigned short)rpm;
5295 	pgeom_p->g_intrlv = (unsigned short)intrlv;
5296 
5297 	SD_INFO(SD_LOG_COMMON, un,
5298 	    "sd_get_physical_geometry: mode sense geometry:\n");
5299 	SD_INFO(SD_LOG_COMMON, un,
5300 	    "   nsect: %d; sector size: %d; interlv: %d\n",
5301 	    nsect, sector_size, intrlv);
5302 	SD_INFO(SD_LOG_COMMON, un,
5303 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5304 	    nhead, ncyl, rpm, modesense_capacity);
5305 	SD_INFO(SD_LOG_COMMON, un,
5306 	    "sd_get_physical_geometry: (cached)\n");
5307 	SD_INFO(SD_LOG_COMMON, un,
5308 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5309 	    un->un_pgeom.g_ncyl,  un->un_pgeom.g_acyl,
5310 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
5311 	SD_INFO(SD_LOG_COMMON, un,
5312 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5313 	    un->un_pgeom.g_secsize, un->un_pgeom.g_capacity,
5314 	    un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm);
5315 
5316 	mutex_exit(SD_MUTEX(un));
5317 
5318 page4_exit:
5319 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5320 page3_exit:
5321 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5322 }
5323 
5324 
5325 /*
5326  *    Function: sd_get_virtual_geometry
5327  *
5328  * Description: Ask the controller to tell us about the target device.
5329  *
5330  *   Arguments: un - pointer to softstate
5331  *		capacity - disk capacity in #blocks
5332  *		lbasize - disk block size in bytes
5333  *
5334  *     Context: Kernel thread only
5335  */
5336 
5337 static void
5338 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize)
5339 {
5340 	struct	geom_cache 	*lgeom_p = &un->un_lgeom;
5341 	uint_t	geombuf;
5342 	int	spc;
5343 
5344 	ASSERT(un != NULL);
5345 	ASSERT(mutex_owned(SD_MUTEX(un)));
5346 
5347 	mutex_exit(SD_MUTEX(un));
5348 
5349 	/* Set sector size, and total number of sectors */
5350 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5351 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5352 
5353 	/* Let the HBA tell us its geometry */
5354 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5355 
5356 	mutex_enter(SD_MUTEX(un));
5357 
5358 	/* A value of -1 indicates an undefined "geometry" property */
5359 	if (geombuf == (-1)) {
5360 		return;
5361 	}
5362 
5363 	/* Initialize the logical geometry cache. */
5364 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5365 	lgeom_p->g_nsect   = geombuf & 0xffff;
5366 	lgeom_p->g_secsize = un->un_sys_blocksize;
5367 
5368 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5369 
5370 	/*
5371 	 * Note: The driver originally converted the capacity value from
5372 	 * target blocks to system blocks. However, the capacity value passed
5373 	 * to this routine is already in terms of system blocks (this scaling
5374 	 * is done when the READ CAPACITY command is issued and processed).
5375 	 * This 'error' may have gone undetected because the usage of g_ncyl
5376 	 * (which is based upon g_capacity) is very limited within the driver
5377 	 */
5378 	lgeom_p->g_capacity = capacity;
5379 
5380 	/*
5381 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5382 	 * hba may return zero values if the device has been removed.
5383 	 */
5384 	if (spc == 0) {
5385 		lgeom_p->g_ncyl = 0;
5386 	} else {
5387 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5388 	}
5389 	lgeom_p->g_acyl = 0;
5390 
5391 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5392 	SD_INFO(SD_LOG_COMMON, un,
5393 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5394 	    un->un_lgeom.g_ncyl,  un->un_lgeom.g_acyl,
5395 	    un->un_lgeom.g_nhead, un->un_lgeom.g_nsect);
5396 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
5397 	    "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize,
5398 	    un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm);
5399 }
5400 
5401 
5402 /*
5403  *    Function: sd_update_block_info
5404  *
5405  * Description: Calculate a byte count to sector count bitshift value
5406  *		from sector size.
5407  *
5408  *   Arguments: un: unit struct.
5409  *		lbasize: new target sector size
5410  *		capacity: new target capacity, ie. block count
5411  *
5412  *     Context: Kernel thread context
5413  */
5414 
5415 static void
5416 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5417 {
5418 	if (lbasize != 0) {
5419 		un->un_tgt_blocksize = lbasize;
5420 		un->un_f_tgt_blocksize_is_valid	= TRUE;
5421 	}
5422 
5423 	if (capacity != 0) {
5424 		un->un_blockcount		= capacity;
5425 		un->un_f_blockcount_is_valid	= TRUE;
5426 	}
5427 }
5428 
5429 
5430 static void
5431 sd_swap_efi_gpt(efi_gpt_t *e)
5432 {
5433 	_NOTE(ASSUMING_PROTECTED(*e))
5434 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
5435 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
5436 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
5437 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
5438 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
5439 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
5440 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
5441 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
5442 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
5443 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
5444 	e->efi_gpt_NumberOfPartitionEntries =
5445 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
5446 	e->efi_gpt_SizeOfPartitionEntry =
5447 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
5448 	e->efi_gpt_PartitionEntryArrayCRC32 =
5449 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
5450 }
5451 
5452 static void
5453 sd_swap_efi_gpe(int nparts, efi_gpe_t *p)
5454 {
5455 	int i;
5456 
5457 	_NOTE(ASSUMING_PROTECTED(*p))
5458 	for (i = 0; i < nparts; i++) {
5459 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
5460 		    p[i].efi_gpe_PartitionTypeGUID);
5461 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
5462 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
5463 		/* PartitionAttrs */
5464 	}
5465 }
5466 
5467 static int
5468 sd_validate_efi(efi_gpt_t *labp)
5469 {
5470 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
5471 		return (EINVAL);
5472 	/* at least 96 bytes in this version of the spec. */
5473 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
5474 	    labp->efi_gpt_HeaderSize)
5475 		return (EINVAL);
5476 	/* this should be 128 bytes */
5477 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
5478 		return (EINVAL);
5479 	return (0);
5480 }
5481 
5482 static int
5483 sd_use_efi(struct sd_lun *un, int path_flag)
5484 {
5485 	int		i;
5486 	int		rval = 0;
5487 	efi_gpe_t	*partitions;
5488 	uchar_t		*buf;
5489 	uint_t		lbasize;
5490 	uint64_t	cap = 0;
5491 	uint_t		nparts;
5492 	diskaddr_t	gpe_lba;
5493 	struct uuid	uuid_type_reserved = EFI_RESERVED;
5494 
5495 	ASSERT(mutex_owned(SD_MUTEX(un)));
5496 	lbasize = un->un_tgt_blocksize;
5497 	un->un_reserved = -1;
5498 
5499 	mutex_exit(SD_MUTEX(un));
5500 
5501 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
5502 
5503 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
5504 		rval = EINVAL;
5505 		goto done_err;
5506 	}
5507 
5508 	rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag);
5509 	if (rval) {
5510 		goto done_err;
5511 	}
5512 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
5513 		/* not ours */
5514 		rval = ESRCH;
5515 		goto done_err;
5516 	}
5517 
5518 	rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag);
5519 	if (rval) {
5520 		goto done_err;
5521 	}
5522 	sd_swap_efi_gpt((efi_gpt_t *)buf);
5523 
5524 	if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) {
5525 		/*
5526 		 * Couldn't read the primary, try the backup.  Our
5527 		 * capacity at this point could be based on CHS, so
5528 		 * check what the device reports.
5529 		 */
5530 		rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
5531 		    path_flag);
5532 		if (rval) {
5533 			goto done_err;
5534 		}
5535 
5536 		/*
5537 		 * The MMC standard allows READ CAPACITY to be
5538 		 * inaccurate by a bounded amount (in the interest of
5539 		 * response latency).  As a result, failed READs are
5540 		 * commonplace (due to the reading of metadata and not
5541 		 * data). Depending on the per-Vendor/drive Sense data,
5542 		 * the failed READ can cause many (unnecessary) retries.
5543 		 */
5544 		if ((rval = sd_send_scsi_READ(un, buf, lbasize,
5545 		    cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY :
5546 			path_flag)) != 0) {
5547 				goto done_err;
5548 		}
5549 
5550 		sd_swap_efi_gpt((efi_gpt_t *)buf);
5551 		if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) {
5552 
5553 			/*
5554 			 * Refer to comments related to off-by-1 at the
5555 			 * header of this file. Search the next to last
5556 			 * block for backup EFI label.
5557 			 */
5558 			if ((rval = sd_send_scsi_READ(un, buf, lbasize,
5559 			    cap - 2, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY :
5560 				path_flag)) != 0) {
5561 					goto done_err;
5562 			}
5563 			sd_swap_efi_gpt((efi_gpt_t *)buf);
5564 			if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0)
5565 				goto done_err;
5566 		}
5567 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5568 		    "primary label corrupt; using backup\n");
5569 	}
5570 
5571 	if (cap == 0)
5572 		rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
5573 		    path_flag);
5574 
5575 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
5576 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
5577 
5578 	rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba,
5579 	    path_flag);
5580 	if (rval) {
5581 		goto done_err;
5582 	}
5583 	partitions = (efi_gpe_t *)buf;
5584 
5585 	if (nparts > MAXPART) {
5586 		nparts = MAXPART;
5587 	}
5588 	sd_swap_efi_gpe(nparts, partitions);
5589 
5590 	mutex_enter(SD_MUTEX(un));
5591 
5592 	/* Fill in partition table. */
5593 	for (i = 0; i < nparts; i++) {
5594 		if (partitions->efi_gpe_StartingLBA != 0 ||
5595 		    partitions->efi_gpe_EndingLBA != 0) {
5596 			un->un_map[i].dkl_cylno =
5597 			    partitions->efi_gpe_StartingLBA;
5598 			un->un_map[i].dkl_nblk =
5599 			    partitions->efi_gpe_EndingLBA -
5600 			    partitions->efi_gpe_StartingLBA + 1;
5601 			un->un_offset[i] =
5602 			    partitions->efi_gpe_StartingLBA;
5603 		}
5604 		if (un->un_reserved == -1) {
5605 			if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
5606 			    &uuid_type_reserved, sizeof (struct uuid)) == 0) {
5607 				un->un_reserved = i;
5608 			}
5609 		}
5610 		if (i == WD_NODE) {
5611 			/*
5612 			 * minor number 7 corresponds to the whole disk
5613 			 */
5614 			un->un_map[i].dkl_cylno = 0;
5615 			un->un_map[i].dkl_nblk = un->un_blockcount;
5616 			un->un_offset[i] = 0;
5617 		}
5618 		partitions++;
5619 	}
5620 	un->un_solaris_offset = 0;
5621 	un->un_solaris_size = cap;
5622 	un->un_f_geometry_is_valid = TRUE;
5623 
5624 	/* clear the vtoc label */
5625 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
5626 
5627 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
5628 	return (0);
5629 
5630 done_err:
5631 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
5632 	mutex_enter(SD_MUTEX(un));
5633 	/*
5634 	 * if we didn't find something that could look like a VTOC
5635 	 * and the disk is over 1TB, we know there isn't a valid label.
5636 	 * Otherwise let sd_uselabel decide what to do.  We only
5637 	 * want to invalidate this if we're certain the label isn't
5638 	 * valid because sd_prop_op will now fail, which in turn
5639 	 * causes things like opens and stats on the partition to fail.
5640 	 */
5641 	if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) {
5642 		un->un_f_geometry_is_valid = FALSE;
5643 	}
5644 	return (rval);
5645 }
5646 
5647 
5648 /*
5649  *    Function: sd_uselabel
5650  *
5651  * Description: Validate the disk label and update the relevant data (geometry,
5652  *		partition, vtoc, and capacity data) in the sd_lun struct.
5653  *		Marks the geometry of the unit as being valid.
5654  *
5655  *   Arguments: un: unit struct.
5656  *		dk_label: disk label
5657  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
5658  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
5659  *			to use the USCSI "direct" chain and bypass the normal
5660  *			command waitq.
5661  *
5662  * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry,
5663  *		partition, vtoc, and capacity data are good.
5664  *
5665  *		SD_LABEL_IS_INVALID: Magic number or checksum error in the
5666  *		label; or computed capacity does not jibe with capacity
5667  *		reported from the READ CAPACITY command.
5668  *
5669  *     Context: Kernel thread only (can sleep).
5670  */
5671 
5672 static int
5673 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag)
5674 {
5675 	short	*sp;
5676 	short	sum;
5677 	short	count;
5678 	int	label_error = SD_LABEL_IS_VALID;
5679 	int	i;
5680 	int	capacity;
5681 	int	part_end;
5682 	int	track_capacity;
5683 	int	err;
5684 #if defined(_SUNOS_VTOC_16)
5685 	struct	dkl_partition	*vpartp;
5686 #endif
5687 	ASSERT(un != NULL);
5688 	ASSERT(mutex_owned(SD_MUTEX(un)));
5689 
5690 	/* Validate the magic number of the label. */
5691 	if (labp->dkl_magic != DKL_MAGIC) {
5692 #if defined(__sparc)
5693 		if ((un->un_state == SD_STATE_NORMAL) &&
5694 			un->un_f_vtoc_errlog_supported) {
5695 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5696 			    "Corrupt label; wrong magic number\n");
5697 		}
5698 #endif
5699 		return (SD_LABEL_IS_INVALID);
5700 	}
5701 
5702 	/* Validate the checksum of the label. */
5703 	sp  = (short *)labp;
5704 	sum = 0;
5705 	count = sizeof (struct dk_label) / sizeof (short);
5706 	while (count--)	 {
5707 		sum ^= *sp++;
5708 	}
5709 
5710 	if (sum != 0) {
5711 #if	defined(_SUNOS_VTOC_16)
5712 		if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) {
5713 #elif defined(_SUNOS_VTOC_8)
5714 		if ((un->un_state == SD_STATE_NORMAL) &&
5715 		    un->un_f_vtoc_errlog_supported) {
5716 #endif
5717 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5718 			    "Corrupt label - label checksum failed\n");
5719 		}
5720 		return (SD_LABEL_IS_INVALID);
5721 	}
5722 
5723 
5724 	/*
5725 	 * Fill in geometry structure with data from label.
5726 	 */
5727 	bzero(&un->un_g, sizeof (struct dk_geom));
5728 	un->un_g.dkg_ncyl   = labp->dkl_ncyl;
5729 	un->un_g.dkg_acyl   = labp->dkl_acyl;
5730 	un->un_g.dkg_bcyl   = 0;
5731 	un->un_g.dkg_nhead  = labp->dkl_nhead;
5732 	un->un_g.dkg_nsect  = labp->dkl_nsect;
5733 	un->un_g.dkg_intrlv = labp->dkl_intrlv;
5734 
5735 #if defined(_SUNOS_VTOC_8)
5736 	un->un_g.dkg_gap1   = labp->dkl_gap1;
5737 	un->un_g.dkg_gap2   = labp->dkl_gap2;
5738 	un->un_g.dkg_bhead  = labp->dkl_bhead;
5739 #endif
5740 #if defined(_SUNOS_VTOC_16)
5741 	un->un_dkg_skew = labp->dkl_skew;
5742 #endif
5743 
5744 #if defined(__i386) || defined(__amd64)
5745 	un->un_g.dkg_apc = labp->dkl_apc;
5746 #endif
5747 
5748 	/*
5749 	 * Currently we rely on the values in the label being accurate. If
5750 	 * dlk_rpm or dlk_pcly are zero in the label, use a default value.
5751 	 *
5752 	 * Note: In the future a MODE SENSE may be used to retrieve this data,
5753 	 * although this command is optional in SCSI-2.
5754 	 */
5755 	un->un_g.dkg_rpm  = (labp->dkl_rpm  != 0) ? labp->dkl_rpm  : 3600;
5756 	un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
5757 	    (un->un_g.dkg_ncyl + un->un_g.dkg_acyl);
5758 
5759 	/*
5760 	 * The Read and Write reinstruct values may not be valid
5761 	 * for older disks.
5762 	 */
5763 	un->un_g.dkg_read_reinstruct  = labp->dkl_read_reinstruct;
5764 	un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
5765 
5766 	/* Fill in partition table. */
5767 #if defined(_SUNOS_VTOC_8)
5768 	for (i = 0; i < NDKMAP; i++) {
5769 		un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
5770 		un->un_map[i].dkl_nblk  = labp->dkl_map[i].dkl_nblk;
5771 	}
5772 #endif
5773 #if  defined(_SUNOS_VTOC_16)
5774 	vpartp		= labp->dkl_vtoc.v_part;
5775 	track_capacity	= labp->dkl_nhead * labp->dkl_nsect;
5776 
5777 	/* Prevent divide by zero */
5778 	if (track_capacity == 0) {
5779 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5780 		    "Corrupt label - zero nhead or nsect value\n");
5781 
5782 		return (SD_LABEL_IS_INVALID);
5783 	}
5784 
5785 	for (i = 0; i < NDKMAP; i++, vpartp++) {
5786 		un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity;
5787 		un->un_map[i].dkl_nblk  = vpartp->p_size;
5788 	}
5789 #endif
5790 
5791 	/* Fill in VTOC Structure. */
5792 	bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc));
5793 #if defined(_SUNOS_VTOC_8)
5794 	/*
5795 	 * The 8-slice vtoc does not include the ascii label; save it into
5796 	 * the device's soft state structure here.
5797 	 */
5798 	bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII);
5799 #endif
5800 
5801 	/* Now look for a valid capacity. */
5802 	track_capacity	= (un->un_g.dkg_nhead * un->un_g.dkg_nsect);
5803 	capacity	= (un->un_g.dkg_ncyl  * track_capacity);
5804 
5805 	if (un->un_g.dkg_acyl) {
5806 #if defined(__i386) || defined(__amd64)
5807 		/* we may have > 1 alts cylinder */
5808 		capacity += (track_capacity * un->un_g.dkg_acyl);
5809 #else
5810 		capacity += track_capacity;
5811 #endif
5812 	}
5813 
5814 	/*
5815 	 * Force check here to ensure the computed capacity is valid.
5816 	 * If capacity is zero, it indicates an invalid label and
5817 	 * we should abort updating the relevant data then.
5818 	 */
5819 	if (capacity == 0) {
5820 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5821 		    "Corrupt label - no valid capacity could be retrieved\n");
5822 
5823 		return (SD_LABEL_IS_INVALID);
5824 	}
5825 
5826 	/* Mark the geometry as valid. */
5827 	un->un_f_geometry_is_valid = TRUE;
5828 
5829 	/*
5830 	 * At this point, un->un_blockcount should contain valid data from
5831 	 * the READ CAPACITY command.
5832 	 */
5833 	if (un->un_f_blockcount_is_valid != TRUE) {
5834 		/*
5835 		 * We have a situation where the target didn't give us a good
5836 		 * READ CAPACITY value, yet there appears to be a valid label.
5837 		 * In this case, we'll fake the capacity.
5838 		 */
5839 		un->un_blockcount = capacity;
5840 		un->un_f_blockcount_is_valid = TRUE;
5841 		goto done;
5842 	}
5843 
5844 
5845 	if ((capacity <= un->un_blockcount) ||
5846 	    (un->un_state != SD_STATE_NORMAL)) {
5847 #if defined(_SUNOS_VTOC_8)
5848 		/*
5849 		 * We can't let this happen on drives that are subdivided
5850 		 * into logical disks (i.e., that have an fdisk table).
5851 		 * The un_blockcount field should always hold the full media
5852 		 * size in sectors, period.  This code would overwrite
5853 		 * un_blockcount with the size of the Solaris fdisk partition.
5854 		 */
5855 		SD_ERROR(SD_LOG_COMMON, un,
5856 		    "sd_uselabel: Label %d blocks; Drive %d blocks\n",
5857 		    capacity, un->un_blockcount);
5858 		un->un_blockcount = capacity;
5859 		un->un_f_blockcount_is_valid = TRUE;
5860 #endif	/* defined(_SUNOS_VTOC_8) */
5861 		goto done;
5862 	}
5863 
5864 	if (ISCD(un)) {
5865 		/* For CDROMs, we trust that the data in the label is OK. */
5866 #if defined(_SUNOS_VTOC_8)
5867 		for (i = 0; i < NDKMAP; i++) {
5868 			part_end = labp->dkl_nhead * labp->dkl_nsect *
5869 			    labp->dkl_map[i].dkl_cylno +
5870 			    labp->dkl_map[i].dkl_nblk  - 1;
5871 
5872 			if ((labp->dkl_map[i].dkl_nblk) &&
5873 			    (part_end > un->un_blockcount)) {
5874 				un->un_f_geometry_is_valid = FALSE;
5875 				break;
5876 			}
5877 		}
5878 #endif
5879 #if defined(_SUNOS_VTOC_16)
5880 		vpartp = &(labp->dkl_vtoc.v_part[0]);
5881 		for (i = 0; i < NDKMAP; i++, vpartp++) {
5882 			part_end = vpartp->p_start + vpartp->p_size;
5883 			if ((vpartp->p_size > 0) &&
5884 			    (part_end > un->un_blockcount)) {
5885 				un->un_f_geometry_is_valid = FALSE;
5886 				break;
5887 			}
5888 		}
5889 #endif
5890 	} else {
5891 		uint64_t t_capacity;
5892 		uint32_t t_lbasize;
5893 
5894 		mutex_exit(SD_MUTEX(un));
5895 		err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize,
5896 		    path_flag);
5897 		ASSERT(t_capacity <= DK_MAX_BLOCKS);
5898 		mutex_enter(SD_MUTEX(un));
5899 
5900 		if (err == 0) {
5901 			sd_update_block_info(un, t_lbasize, t_capacity);
5902 		}
5903 
5904 		if (capacity > un->un_blockcount) {
5905 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5906 			    "Corrupt label - bad geometry\n");
5907 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
5908 			    "Label says %u blocks; Drive says %llu blocks\n",
5909 			    capacity, (unsigned long long)un->un_blockcount);
5910 			un->un_f_geometry_is_valid = FALSE;
5911 			label_error = SD_LABEL_IS_INVALID;
5912 		}
5913 	}
5914 
5915 done:
5916 
5917 	SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n");
5918 	SD_INFO(SD_LOG_COMMON, un,
5919 	    "   ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
5920 	    un->un_g.dkg_ncyl,  un->un_g.dkg_acyl,
5921 	    un->un_g.dkg_nhead, un->un_g.dkg_nsect);
5922 	SD_INFO(SD_LOG_COMMON, un,
5923 	    "   lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n",
5924 	    un->un_tgt_blocksize, un->un_blockcount,
5925 	    un->un_g.dkg_intrlv, un->un_g.dkg_rpm);
5926 	SD_INFO(SD_LOG_COMMON, un, "   wrt_reinstr: %d; rd_reinstr: %d\n",
5927 	    un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct);
5928 
5929 	ASSERT(mutex_owned(SD_MUTEX(un)));
5930 
5931 	return (label_error);
5932 }
5933 
5934 
5935 /*
5936  *    Function: sd_build_default_label
5937  *
5938  * Description: Generate a default label for those devices that do not have
5939  *		one, e.g., new media, removable cartridges, etc..
5940  *
5941  *     Context: Kernel thread only
5942  */
5943 
5944 static void
5945 sd_build_default_label(struct sd_lun *un)
5946 {
5947 #if defined(_SUNOS_VTOC_16)
5948 	uint_t	phys_spc;
5949 	uint_t	disksize;
5950 	struct	dk_geom un_g;
5951 	uint64_t capacity;
5952 #endif
5953 
5954 	ASSERT(un != NULL);
5955 	ASSERT(mutex_owned(SD_MUTEX(un)));
5956 
5957 #if defined(_SUNOS_VTOC_8)
5958 	/*
5959 	 * Note: This is a legacy check for non-removable devices on VTOC_8
5960 	 * only. This may be a valid check for VTOC_16 as well.
5961 	 * Once we understand why there is this difference between SPARC and
5962 	 * x86 platform, we could remove this legacy check.
5963 	 */
5964 	ASSERT(un->un_f_default_vtoc_supported);
5965 #endif
5966 
5967 	bzero(&un->un_g, sizeof (struct dk_geom));
5968 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
5969 	bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map)));
5970 
5971 #if defined(_SUNOS_VTOC_8)
5972 
5973 	/*
5974 	 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
5975 	 * But it is still necessary to set up various geometry information,
5976 	 * and we are doing this here.
5977 	 */
5978 
5979 	/*
5980 	 * For the rpm, we use the minimum for the disk.  For the head, cyl,
5981 	 * and number of sector per track, if the capacity <= 1GB, head = 64,
5982 	 * sect = 32.  else head = 255, sect 63 Note: the capacity should be
5983 	 * equal to C*H*S values.  This will cause some truncation of size due
5984 	 * to round off errors. For CD-ROMs, this truncation can have adverse
5985 	 * side effects, so returning ncyl and nhead as 1. The nsect will
5986 	 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
5987 	 */
5988 	if (ISCD(un)) {
5989 		/*
5990 		 * Preserve the old behavior for non-writable
5991 		 * medias. Since dkg_nsect is a ushort, it
5992 		 * will lose bits as cdroms have more than
5993 		 * 65536 sectors. So if we recalculate
5994 		 * capacity, it will become much shorter.
5995 		 * But the dkg_* information is not
5996 		 * used for CDROMs so it is OK. But for
5997 		 * Writable CDs we need this information
5998 		 * to be valid (for newfs say). So we
5999 		 * make nsect and nhead > 1 that way
6000 		 * nsect can still stay within ushort limit
6001 		 * without losing any bits.
6002 		 */
6003 		if (un->un_f_mmc_writable_media == TRUE) {
6004 			un->un_g.dkg_nhead = 64;
6005 			un->un_g.dkg_nsect = 32;
6006 			un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32);
6007 			un->un_blockcount = un->un_g.dkg_ncyl *
6008 			    un->un_g.dkg_nhead * un->un_g.dkg_nsect;
6009 		} else {
6010 			un->un_g.dkg_ncyl  = 1;
6011 			un->un_g.dkg_nhead = 1;
6012 			un->un_g.dkg_nsect = un->un_blockcount;
6013 		}
6014 	} else {
6015 		if (un->un_blockcount <= 0x1000) {
6016 			/* unlabeled SCSI floppy device */
6017 			un->un_g.dkg_nhead = 2;
6018 			un->un_g.dkg_ncyl = 80;
6019 			un->un_g.dkg_nsect = un->un_blockcount / (2 * 80);
6020 		} else if (un->un_blockcount <= 0x200000) {
6021 			un->un_g.dkg_nhead = 64;
6022 			un->un_g.dkg_nsect = 32;
6023 			un->un_g.dkg_ncyl  = un->un_blockcount / (64 * 32);
6024 		} else {
6025 			un->un_g.dkg_nhead = 255;
6026 			un->un_g.dkg_nsect = 63;
6027 			un->un_g.dkg_ncyl  = un->un_blockcount / (255 * 63);
6028 		}
6029 		un->un_blockcount =
6030 		    un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect;
6031 	}
6032 
6033 	un->un_g.dkg_acyl	= 0;
6034 	un->un_g.dkg_bcyl	= 0;
6035 	un->un_g.dkg_rpm	= 200;
6036 	un->un_asciilabel[0]	= '\0';
6037 	un->un_g.dkg_pcyl	= un->un_g.dkg_ncyl;
6038 
6039 	un->un_map[0].dkl_cylno = 0;
6040 	un->un_map[0].dkl_nblk  = un->un_blockcount;
6041 	un->un_map[2].dkl_cylno = 0;
6042 	un->un_map[2].dkl_nblk  = un->un_blockcount;
6043 
6044 #elif defined(_SUNOS_VTOC_16)
6045 
6046 	if (un->un_solaris_size == 0) {
6047 		/*
6048 		 * Got fdisk table but no solaris entry therefore
6049 		 * don't create a default label
6050 		 */
6051 		un->un_f_geometry_is_valid = TRUE;
6052 		return;
6053 	}
6054 
6055 	/*
6056 	 * For CDs we continue to use the physical geometry to calculate
6057 	 * number of cylinders. All other devices must convert the
6058 	 * physical geometry (geom_cache) to values that will fit
6059 	 * in a dk_geom structure.
6060 	 */
6061 	if (ISCD(un)) {
6062 		phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect;
6063 	} else {
6064 		/* Convert physical geometry to disk geometry */
6065 		bzero(&un_g, sizeof (struct dk_geom));
6066 
6067 		/*
6068 		 * Refer to comments related to off-by-1 at the
6069 		 * header of this file.
6070 		 * Before caculating geometry, capacity should be
6071 		 * decreased by 1. That un_f_capacity_adjusted is
6072 		 * TRUE means that we are treating a 1TB disk as
6073 		 * (1T - 512)B. And the capacity of disks is already
6074 		 * decreased by 1.
6075 		 */
6076 		if (!un->un_f_capacity_adjusted &&
6077 		    !un->un_f_has_removable_media &&
6078 		    !un->un_f_is_hotpluggable &&
6079 			un->un_tgt_blocksize == un->un_sys_blocksize)
6080 			capacity = un->un_blockcount - 1;
6081 		else
6082 			capacity = un->un_blockcount;
6083 
6084 		sd_convert_geometry(capacity, &un_g);
6085 		bcopy(&un_g, &un->un_g, sizeof (un->un_g));
6086 		phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect;
6087 	}
6088 
6089 	ASSERT(phys_spc != 0);
6090 	un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc;
6091 	un->un_g.dkg_acyl = DK_ACYL;
6092 	un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL;
6093 	disksize = un->un_g.dkg_ncyl * phys_spc;
6094 
6095 	if (ISCD(un)) {
6096 		/*
6097 		 * CD's don't use the "heads * sectors * cyls"-type of
6098 		 * geometry, but instead use the entire capacity of the media.
6099 		 */
6100 		disksize = un->un_solaris_size;
6101 		un->un_g.dkg_nhead = 1;
6102 		un->un_g.dkg_nsect = 1;
6103 		un->un_g.dkg_rpm =
6104 		    (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm;
6105 
6106 		un->un_vtoc.v_part[0].p_start = 0;
6107 		un->un_vtoc.v_part[0].p_size  = disksize;
6108 		un->un_vtoc.v_part[0].p_tag   = V_BACKUP;
6109 		un->un_vtoc.v_part[0].p_flag  = V_UNMNT;
6110 
6111 		un->un_map[0].dkl_cylno = 0;
6112 		un->un_map[0].dkl_nblk  = disksize;
6113 		un->un_offset[0] = 0;
6114 
6115 	} else {
6116 		/*
6117 		 * Hard disks and removable media cartridges
6118 		 */
6119 		un->un_g.dkg_rpm =
6120 		    (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm;
6121 		un->un_vtoc.v_sectorsz = un->un_sys_blocksize;
6122 
6123 		/* Add boot slice */
6124 		un->un_vtoc.v_part[8].p_start = 0;
6125 		un->un_vtoc.v_part[8].p_size  = phys_spc;
6126 		un->un_vtoc.v_part[8].p_tag   = V_BOOT;
6127 		un->un_vtoc.v_part[8].p_flag  = V_UNMNT;
6128 
6129 		un->un_map[8].dkl_cylno = 0;
6130 		un->un_map[8].dkl_nblk  = phys_spc;
6131 		un->un_offset[8] = 0;
6132 	}
6133 
6134 	un->un_g.dkg_apc = 0;
6135 	un->un_vtoc.v_nparts = V_NUMPAR;
6136 	un->un_vtoc.v_version = V_VERSION;
6137 
6138 	/* Add backup slice */
6139 	un->un_vtoc.v_part[2].p_start = 0;
6140 	un->un_vtoc.v_part[2].p_size  = disksize;
6141 	un->un_vtoc.v_part[2].p_tag   = V_BACKUP;
6142 	un->un_vtoc.v_part[2].p_flag  = V_UNMNT;
6143 
6144 	un->un_map[2].dkl_cylno = 0;
6145 	un->un_map[2].dkl_nblk  = disksize;
6146 	un->un_offset[2] = 0;
6147 
6148 	(void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
6149 	    " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl,
6150 	    un->un_g.dkg_nhead, un->un_g.dkg_nsect);
6151 
6152 #else
6153 #error "No VTOC format defined."
6154 #endif
6155 
6156 	un->un_g.dkg_read_reinstruct  = 0;
6157 	un->un_g.dkg_write_reinstruct = 0;
6158 
6159 	un->un_g.dkg_intrlv = 1;
6160 
6161 	un->un_vtoc.v_sanity  = VTOC_SANE;
6162 
6163 	un->un_f_geometry_is_valid = TRUE;
6164 
6165 	SD_INFO(SD_LOG_COMMON, un,
6166 	    "sd_build_default_label: Default label created: "
6167 	    "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
6168 	    un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead,
6169 	    un->un_g.dkg_nsect, un->un_blockcount);
6170 }
6171 
6172 
6173 #if defined(_FIRMWARE_NEEDS_FDISK)
6174 /*
6175  * Max CHS values, as they are encoded into bytes, for 1022/254/63
6176  */
6177 #define	LBA_MAX_SECT	(63 | ((1022 & 0x300) >> 2))
6178 #define	LBA_MAX_CYL	(1022 & 0xFF)
6179 #define	LBA_MAX_HEAD	(254)
6180 
6181 
6182 /*
6183  *    Function: sd_has_max_chs_vals
6184  *
6185  * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum.
6186  *
6187  *   Arguments: fdp - ptr to CHS info
6188  *
6189  * Return Code: True or false
6190  *
6191  *     Context: Any.
6192  */
6193 
6194 static int
6195 sd_has_max_chs_vals(struct ipart *fdp)
6196 {
6197 	return ((fdp->begcyl  == LBA_MAX_CYL)	&&
6198 	    (fdp->beghead == LBA_MAX_HEAD)	&&
6199 	    (fdp->begsect == LBA_MAX_SECT)	&&
6200 	    (fdp->endcyl  == LBA_MAX_CYL)	&&
6201 	    (fdp->endhead == LBA_MAX_HEAD)	&&
6202 	    (fdp->endsect == LBA_MAX_SECT));
6203 }
6204 #endif
6205 
6206 
6207 /*
6208  *    Function: sd_inq_fill
6209  *
6210  * Description: Print a piece of inquiry data, cleaned up for non-printable
6211  *		characters and stopping at the first space character after
6212  *		the beginning of the passed string;
6213  *
6214  *   Arguments: p - source string
6215  *		l - maximum length to copy
6216  *		s - destination string
6217  *
6218  *     Context: Any.
6219  */
6220 
6221 static void
6222 sd_inq_fill(char *p, int l, char *s)
6223 {
6224 	unsigned i = 0;
6225 	char c;
6226 
6227 	while (i++ < l) {
6228 		if ((c = *p++) < ' ' || c >= 0x7F) {
6229 			c = '*';
6230 		} else if (i != 1 && c == ' ') {
6231 			break;
6232 		}
6233 		*s++ = c;
6234 	}
6235 	*s++ = 0;
6236 }
6237 
6238 
6239 /*
6240  *    Function: sd_register_devid
6241  *
6242  * Description: This routine will obtain the device id information from the
6243  *		target, obtain the serial number, and register the device
6244  *		id with the ddi framework.
6245  *
6246  *   Arguments: devi - the system's dev_info_t for the device.
6247  *		un - driver soft state (unit) structure
6248  *		reservation_flag - indicates if a reservation conflict
6249  *		occurred during attach
6250  *
6251  *     Context: Kernel Thread
6252  */
6253 static void
6254 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag)
6255 {
6256 	int		rval		= 0;
6257 	uchar_t		*inq80		= NULL;
6258 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
6259 	size_t		inq80_resid	= 0;
6260 	uchar_t		*inq83		= NULL;
6261 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
6262 	size_t		inq83_resid	= 0;
6263 
6264 	ASSERT(un != NULL);
6265 	ASSERT(mutex_owned(SD_MUTEX(un)));
6266 	ASSERT((SD_DEVINFO(un)) == devi);
6267 
6268 	/*
6269 	 * This is the case of antiquated Sun disk drives that have the
6270 	 * FAB_DEVID property set in the disk_table.  These drives
6271 	 * manage the devid's by storing them in last 2 available sectors
6272 	 * on the drive and have them fabricated by the ddi layer by calling
6273 	 * ddi_devid_init and passing the DEVID_FAB flag.
6274 	 */
6275 	if (un->un_f_opt_fab_devid == TRUE) {
6276 		/*
6277 		 * Depending on EINVAL isn't reliable, since a reserved disk
6278 		 * may result in invalid geometry, so check to make sure a
6279 		 * reservation conflict did not occur during attach.
6280 		 */
6281 		if ((sd_get_devid(un) == EINVAL) &&
6282 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
6283 			/*
6284 			 * The devid is invalid AND there is no reservation
6285 			 * conflict.  Fabricate a new devid.
6286 			 */
6287 			(void) sd_create_devid(un);
6288 		}
6289 
6290 		/* Register the devid if it exists */
6291 		if (un->un_devid != NULL) {
6292 			(void) ddi_devid_register(SD_DEVINFO(un),
6293 			    un->un_devid);
6294 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
6295 			    "sd_register_devid: Devid Fabricated\n");
6296 		}
6297 		return;
6298 	}
6299 
6300 	/*
6301 	 * We check the availibility of the World Wide Name (0x83) and Unit
6302 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
6303 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
6304 	 * 0x83 is availible, that is the best choice.  Our next choice is
6305 	 * 0x80.  If neither are availible, we munge the devid from the device
6306 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
6307 	 * to fabricate a devid for non-Sun qualified disks.
6308 	 */
6309 	if (sd_check_vpd_page_support(un) == 0) {
6310 		/* collect page 80 data if available */
6311 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
6312 
6313 			mutex_exit(SD_MUTEX(un));
6314 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
6315 			rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len,
6316 			    0x01, 0x80, &inq80_resid);
6317 
6318 			if (rval != 0) {
6319 				kmem_free(inq80, inq80_len);
6320 				inq80 = NULL;
6321 				inq80_len = 0;
6322 			}
6323 			mutex_enter(SD_MUTEX(un));
6324 		}
6325 
6326 		/* collect page 83 data if available */
6327 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
6328 			mutex_exit(SD_MUTEX(un));
6329 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
6330 			rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len,
6331 			    0x01, 0x83, &inq83_resid);
6332 
6333 			if (rval != 0) {
6334 				kmem_free(inq83, inq83_len);
6335 				inq83 = NULL;
6336 				inq83_len = 0;
6337 			}
6338 			mutex_enter(SD_MUTEX(un));
6339 		}
6340 	}
6341 
6342 	/* encode best devid possible based on data available */
6343 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
6344 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
6345 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
6346 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
6347 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
6348 
6349 		/* devid successfully encoded, register devid */
6350 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
6351 
6352 	} else {
6353 		/*
6354 		 * Unable to encode a devid based on data available.
6355 		 * This is not a Sun qualified disk.  Older Sun disk
6356 		 * drives that have the SD_FAB_DEVID property
6357 		 * set in the disk_table and non Sun qualified
6358 		 * disks are treated in the same manner.  These
6359 		 * drives manage the devid's by storing them in
6360 		 * last 2 available sectors on the drive and
6361 		 * have them fabricated by the ddi layer by
6362 		 * calling ddi_devid_init and passing the
6363 		 * DEVID_FAB flag.
6364 		 * Create a fabricate devid only if there's no
6365 		 * fabricate devid existed.
6366 		 */
6367 		if (sd_get_devid(un) == EINVAL) {
6368 			(void) sd_create_devid(un);
6369 		}
6370 		un->un_f_opt_fab_devid = TRUE;
6371 
6372 		/* Register the devid if it exists */
6373 		if (un->un_devid != NULL) {
6374 			(void) ddi_devid_register(SD_DEVINFO(un),
6375 			    un->un_devid);
6376 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
6377 			    "sd_register_devid: devid fabricated using "
6378 			    "ddi framework\n");
6379 		}
6380 	}
6381 
6382 	/* clean up resources */
6383 	if (inq80 != NULL) {
6384 		kmem_free(inq80, inq80_len);
6385 	}
6386 	if (inq83 != NULL) {
6387 		kmem_free(inq83, inq83_len);
6388 	}
6389 }
6390 
6391 static daddr_t
6392 sd_get_devid_block(struct sd_lun *un)
6393 {
6394 	daddr_t			spc, blk, head, cyl;
6395 
6396 	if ((un->un_f_geometry_is_valid == FALSE) ||
6397 	    (un->un_solaris_size < DK_LABEL_LOC))
6398 		return (-1);
6399 
6400 	if (un->un_vtoc.v_sanity != VTOC_SANE) {
6401 		/* EFI labeled */
6402 		if (un->un_reserved != -1) {
6403 			blk = un->un_map[un->un_reserved].dkl_cylno;
6404 		} else {
6405 			return (-1);
6406 		}
6407 	} else {
6408 		/* SMI labeled */
6409 		/* this geometry doesn't allow us to write a devid */
6410 		if (un->un_g.dkg_acyl < 2) {
6411 			return (-1);
6412 		}
6413 
6414 		/*
6415 		 * Subtract 2 guarantees that the next to last cylinder
6416 		 * is used
6417 		 */
6418 		cyl  = un->un_g.dkg_ncyl  + un->un_g.dkg_acyl - 2;
6419 		spc  = un->un_g.dkg_nhead * un->un_g.dkg_nsect;
6420 		head = un->un_g.dkg_nhead - 1;
6421 		blk  = (cyl * (spc - un->un_g.dkg_apc)) +
6422 		    (head * un->un_g.dkg_nsect) + 1;
6423 	}
6424 	return (blk);
6425 }
6426 
6427 /*
6428  *    Function: sd_get_devid
6429  *
6430  * Description: This routine will return 0 if a valid device id has been
6431  *		obtained from the target and stored in the soft state. If a
6432  *		valid device id has not been previously read and stored, a
6433  *		read attempt will be made.
6434  *
6435  *   Arguments: un - driver soft state (unit) structure
6436  *
6437  * Return Code: 0 if we successfully get the device id
6438  *
6439  *     Context: Kernel Thread
6440  */
6441 
6442 static int
6443 sd_get_devid(struct sd_lun *un)
6444 {
6445 	struct dk_devid		*dkdevid;
6446 	ddi_devid_t		tmpid;
6447 	uint_t			*ip;
6448 	size_t			sz;
6449 	daddr_t			blk;
6450 	int			status;
6451 	int			chksum;
6452 	int			i;
6453 	size_t			buffer_size;
6454 
6455 	ASSERT(un != NULL);
6456 	ASSERT(mutex_owned(SD_MUTEX(un)));
6457 
6458 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
6459 	    un);
6460 
6461 	if (un->un_devid != NULL) {
6462 		return (0);
6463 	}
6464 
6465 	blk = sd_get_devid_block(un);
6466 	if (blk < 0)
6467 		return (EINVAL);
6468 
6469 	/*
6470 	 * Read and verify device id, stored in the reserved cylinders at the
6471 	 * end of the disk. Backup label is on the odd sectors of the last
6472 	 * track of the last cylinder. Device id will be on track of the next
6473 	 * to last cylinder.
6474 	 */
6475 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
6476 	mutex_exit(SD_MUTEX(un));
6477 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
6478 	status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk,
6479 	    SD_PATH_DIRECT);
6480 	if (status != 0) {
6481 		goto error;
6482 	}
6483 
6484 	/* Validate the revision */
6485 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
6486 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
6487 		status = EINVAL;
6488 		goto error;
6489 	}
6490 
6491 	/* Calculate the checksum */
6492 	chksum = 0;
6493 	ip = (uint_t *)dkdevid;
6494 	for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int));
6495 	    i++) {
6496 		chksum ^= ip[i];
6497 	}
6498 
6499 	/* Compare the checksums */
6500 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
6501 		status = EINVAL;
6502 		goto error;
6503 	}
6504 
6505 	/* Validate the device id */
6506 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
6507 		status = EINVAL;
6508 		goto error;
6509 	}
6510 
6511 	/*
6512 	 * Store the device id in the driver soft state
6513 	 */
6514 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
6515 	tmpid = kmem_alloc(sz, KM_SLEEP);
6516 
6517 	mutex_enter(SD_MUTEX(un));
6518 
6519 	un->un_devid = tmpid;
6520 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
6521 
6522 	kmem_free(dkdevid, buffer_size);
6523 
6524 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
6525 
6526 	return (status);
6527 error:
6528 	mutex_enter(SD_MUTEX(un));
6529 	kmem_free(dkdevid, buffer_size);
6530 	return (status);
6531 }
6532 
6533 
6534 /*
6535  *    Function: sd_create_devid
6536  *
6537  * Description: This routine will fabricate the device id and write it
6538  *		to the disk.
6539  *
6540  *   Arguments: un - driver soft state (unit) structure
6541  *
6542  * Return Code: value of the fabricated device id
6543  *
6544  *     Context: Kernel Thread
6545  */
6546 
6547 static ddi_devid_t
6548 sd_create_devid(struct sd_lun *un)
6549 {
6550 	ASSERT(un != NULL);
6551 
6552 	/* Fabricate the devid */
6553 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
6554 	    == DDI_FAILURE) {
6555 		return (NULL);
6556 	}
6557 
6558 	/* Write the devid to disk */
6559 	if (sd_write_deviceid(un) != 0) {
6560 		ddi_devid_free(un->un_devid);
6561 		un->un_devid = NULL;
6562 	}
6563 
6564 	return (un->un_devid);
6565 }
6566 
6567 
6568 /*
6569  *    Function: sd_write_deviceid
6570  *
6571  * Description: This routine will write the device id to the disk
6572  *		reserved sector.
6573  *
6574  *   Arguments: un - driver soft state (unit) structure
6575  *
6576  * Return Code: EINVAL
6577  *		value returned by sd_send_scsi_cmd
6578  *
6579  *     Context: Kernel Thread
6580  */
6581 
6582 static int
6583 sd_write_deviceid(struct sd_lun *un)
6584 {
6585 	struct dk_devid		*dkdevid;
6586 	daddr_t			blk;
6587 	uint_t			*ip, chksum;
6588 	int			status;
6589 	int			i;
6590 
6591 	ASSERT(mutex_owned(SD_MUTEX(un)));
6592 
6593 	blk = sd_get_devid_block(un);
6594 	if (blk < 0)
6595 		return (-1);
6596 	mutex_exit(SD_MUTEX(un));
6597 
6598 	/* Allocate the buffer */
6599 	dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
6600 
6601 	/* Fill in the revision */
6602 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
6603 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
6604 
6605 	/* Copy in the device id */
6606 	mutex_enter(SD_MUTEX(un));
6607 	bcopy(un->un_devid, &dkdevid->dkd_devid,
6608 	    ddi_devid_sizeof(un->un_devid));
6609 	mutex_exit(SD_MUTEX(un));
6610 
6611 	/* Calculate the checksum */
6612 	chksum = 0;
6613 	ip = (uint_t *)dkdevid;
6614 	for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int));
6615 	    i++) {
6616 		chksum ^= ip[i];
6617 	}
6618 
6619 	/* Fill-in checksum */
6620 	DKD_FORMCHKSUM(chksum, dkdevid);
6621 
6622 	/* Write the reserved sector */
6623 	status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk,
6624 	    SD_PATH_DIRECT);
6625 
6626 	kmem_free(dkdevid, un->un_sys_blocksize);
6627 
6628 	mutex_enter(SD_MUTEX(un));
6629 	return (status);
6630 }
6631 
6632 
6633 /*
6634  *    Function: sd_check_vpd_page_support
6635  *
6636  * Description: This routine sends an inquiry command with the EVPD bit set and
6637  *		a page code of 0x00 to the device. It is used to determine which
6638  *		vital product pages are availible to find the devid. We are
6639  *		looking for pages 0x83 or 0x80.  If we return a negative 1, the
6640  *		device does not support that command.
6641  *
6642  *   Arguments: un  - driver soft state (unit) structure
6643  *
6644  * Return Code: 0 - success
6645  *		1 - check condition
6646  *
6647  *     Context: This routine can sleep.
6648  */
6649 
6650 static int
6651 sd_check_vpd_page_support(struct sd_lun *un)
6652 {
6653 	uchar_t	*page_list	= NULL;
6654 	uchar_t	page_length	= 0xff;	/* Use max possible length */
6655 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
6656 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
6657 	int    	rval		= 0;
6658 	int	counter;
6659 
6660 	ASSERT(un != NULL);
6661 	ASSERT(mutex_owned(SD_MUTEX(un)));
6662 
6663 	mutex_exit(SD_MUTEX(un));
6664 
6665 	/*
6666 	 * We'll set the page length to the maximum to save figuring it out
6667 	 * with an additional call.
6668 	 */
6669 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
6670 
6671 	rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd,
6672 	    page_code, NULL);
6673 
6674 	mutex_enter(SD_MUTEX(un));
6675 
6676 	/*
6677 	 * Now we must validate that the device accepted the command, as some
6678 	 * drives do not support it.  If the drive does support it, we will
6679 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
6680 	 * not, we return -1.
6681 	 */
6682 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
6683 		/* Loop to find one of the 2 pages we need */
6684 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
6685 
6686 		/*
6687 		 * Pages are returned in ascending order, and 0x83 is what we
6688 		 * are hoping for.
6689 		 */
6690 		while ((page_list[counter] <= 0x83) &&
6691 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
6692 		    VPD_HEAD_OFFSET))) {
6693 			/*
6694 			 * Add 3 because page_list[3] is the number of
6695 			 * pages minus 3
6696 			 */
6697 
6698 			switch (page_list[counter]) {
6699 			case 0x00:
6700 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
6701 				break;
6702 			case 0x80:
6703 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
6704 				break;
6705 			case 0x81:
6706 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
6707 				break;
6708 			case 0x82:
6709 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
6710 				break;
6711 			case 0x83:
6712 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
6713 				break;
6714 			}
6715 			counter++;
6716 		}
6717 
6718 	} else {
6719 		rval = -1;
6720 
6721 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
6722 		    "sd_check_vpd_page_support: This drive does not implement "
6723 		    "VPD pages.\n");
6724 	}
6725 
6726 	kmem_free(page_list, page_length);
6727 
6728 	return (rval);
6729 }
6730 
6731 
6732 /*
6733  *    Function: sd_setup_pm
6734  *
6735  * Description: Initialize Power Management on the device
6736  *
6737  *     Context: Kernel Thread
6738  */
6739 
6740 static void
6741 sd_setup_pm(struct sd_lun *un, dev_info_t *devi)
6742 {
6743 	uint_t	log_page_size;
6744 	uchar_t	*log_page_data;
6745 	int	rval;
6746 
6747 	/*
6748 	 * Since we are called from attach, holding a mutex for
6749 	 * un is unnecessary. Because some of the routines called
6750 	 * from here require SD_MUTEX to not be held, assert this
6751 	 * right up front.
6752 	 */
6753 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6754 	/*
6755 	 * Since the sd device does not have the 'reg' property,
6756 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
6757 	 * The following code is to tell cpr that this device
6758 	 * DOES need to be suspended and resumed.
6759 	 */
6760 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
6761 	    "pm-hardware-state", "needs-suspend-resume");
6762 
6763 	/*
6764 	 * This complies with the new power management framework
6765 	 * for certain desktop machines. Create the pm_components
6766 	 * property as a string array property.
6767 	 */
6768 	if (un->un_f_pm_supported) {
6769 		/*
6770 		 * not all devices have a motor, try it first.
6771 		 * some devices may return ILLEGAL REQUEST, some
6772 		 * will hang
6773 		 * The following START_STOP_UNIT is used to check if target
6774 		 * device has a motor.
6775 		 */
6776 		un->un_f_start_stop_supported = TRUE;
6777 		if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
6778 		    SD_PATH_DIRECT) != 0) {
6779 			un->un_f_start_stop_supported = FALSE;
6780 		}
6781 
6782 		/*
6783 		 * create pm properties anyways otherwise the parent can't
6784 		 * go to sleep
6785 		 */
6786 		(void) sd_create_pm_components(devi, un);
6787 		un->un_f_pm_is_enabled = TRUE;
6788 		return;
6789 	}
6790 
6791 	if (!un->un_f_log_sense_supported) {
6792 		un->un_power_level = SD_SPINDLE_ON;
6793 		un->un_f_pm_is_enabled = FALSE;
6794 		return;
6795 	}
6796 
6797 	rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE);
6798 
6799 #ifdef	SDDEBUG
6800 	if (sd_force_pm_supported) {
6801 		/* Force a successful result */
6802 		rval = 1;
6803 	}
6804 #endif
6805 
6806 	/*
6807 	 * If the start-stop cycle counter log page is not supported
6808 	 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0)
6809 	 * then we should not create the pm_components property.
6810 	 */
6811 	if (rval == -1) {
6812 		/*
6813 		 * Error.
6814 		 * Reading log sense failed, most likely this is
6815 		 * an older drive that does not support log sense.
6816 		 * If this fails auto-pm is not supported.
6817 		 */
6818 		un->un_power_level = SD_SPINDLE_ON;
6819 		un->un_f_pm_is_enabled = FALSE;
6820 
6821 	} else if (rval == 0) {
6822 		/*
6823 		 * Page not found.
6824 		 * The start stop cycle counter is implemented as page
6825 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6826 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6827 		 */
6828 		if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) {
6829 			/*
6830 			 * Page found, use this one.
6831 			 */
6832 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6833 			un->un_f_pm_is_enabled = TRUE;
6834 		} else {
6835 			/*
6836 			 * Error or page not found.
6837 			 * auto-pm is not supported for this device.
6838 			 */
6839 			un->un_power_level = SD_SPINDLE_ON;
6840 			un->un_f_pm_is_enabled = FALSE;
6841 		}
6842 	} else {
6843 		/*
6844 		 * Page found, use it.
6845 		 */
6846 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6847 		un->un_f_pm_is_enabled = TRUE;
6848 	}
6849 
6850 
6851 	if (un->un_f_pm_is_enabled == TRUE) {
6852 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6853 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6854 
6855 		rval = sd_send_scsi_LOG_SENSE(un, log_page_data,
6856 		    log_page_size, un->un_start_stop_cycle_page,
6857 		    0x01, 0, SD_PATH_DIRECT);
6858 #ifdef	SDDEBUG
6859 		if (sd_force_pm_supported) {
6860 			/* Force a successful result */
6861 			rval = 0;
6862 		}
6863 #endif
6864 
6865 		/*
6866 		 * If the Log sense for Page( Start/stop cycle counter page)
6867 		 * succeeds, then power managment is supported and we can
6868 		 * enable auto-pm.
6869 		 */
6870 		if (rval == 0)  {
6871 			(void) sd_create_pm_components(devi, un);
6872 		} else {
6873 			un->un_power_level = SD_SPINDLE_ON;
6874 			un->un_f_pm_is_enabled = FALSE;
6875 		}
6876 
6877 		kmem_free(log_page_data, log_page_size);
6878 	}
6879 }
6880 
6881 
6882 /*
6883  *    Function: sd_create_pm_components
6884  *
6885  * Description: Initialize PM property.
6886  *
6887  *     Context: Kernel thread context
6888  */
6889 
6890 static void
6891 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6892 {
6893 	char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL };
6894 
6895 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6896 
6897 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6898 	    "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) {
6899 		/*
6900 		 * When components are initially created they are idle,
6901 		 * power up any non-removables.
6902 		 * Note: the return value of pm_raise_power can't be used
6903 		 * for determining if PM should be enabled for this device.
6904 		 * Even if you check the return values and remove this
6905 		 * property created above, the PM framework will not honor the
6906 		 * change after the first call to pm_raise_power. Hence,
6907 		 * removal of that property does not help if pm_raise_power
6908 		 * fails. In the case of removable media, the start/stop
6909 		 * will fail if the media is not present.
6910 		 */
6911 		if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6912 		    SD_SPINDLE_ON) == DDI_SUCCESS)) {
6913 			mutex_enter(SD_MUTEX(un));
6914 			un->un_power_level = SD_SPINDLE_ON;
6915 			mutex_enter(&un->un_pm_mutex);
6916 			/* Set to on and not busy. */
6917 			un->un_pm_count = 0;
6918 		} else {
6919 			mutex_enter(SD_MUTEX(un));
6920 			un->un_power_level = SD_SPINDLE_OFF;
6921 			mutex_enter(&un->un_pm_mutex);
6922 			/* Set to off. */
6923 			un->un_pm_count = -1;
6924 		}
6925 		mutex_exit(&un->un_pm_mutex);
6926 		mutex_exit(SD_MUTEX(un));
6927 	} else {
6928 		un->un_power_level = SD_SPINDLE_ON;
6929 		un->un_f_pm_is_enabled = FALSE;
6930 	}
6931 }
6932 
6933 
6934 /*
6935  *    Function: sd_ddi_suspend
6936  *
6937  * Description: Performs system power-down operations. This includes
6938  *		setting the drive state to indicate its suspended so
6939  *		that no new commands will be accepted. Also, wait for
6940  *		all commands that are in transport or queued to a timer
6941  *		for retry to complete. All timeout threads are cancelled.
6942  *
6943  * Return Code: DDI_FAILURE or DDI_SUCCESS
6944  *
6945  *     Context: Kernel thread context
6946  */
6947 
6948 static int
6949 sd_ddi_suspend(dev_info_t *devi)
6950 {
6951 	struct	sd_lun	*un;
6952 	clock_t		wait_cmds_complete;
6953 
6954 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6955 	if (un == NULL) {
6956 		return (DDI_FAILURE);
6957 	}
6958 
6959 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6960 
6961 	mutex_enter(SD_MUTEX(un));
6962 
6963 	/* Return success if the device is already suspended. */
6964 	if (un->un_state == SD_STATE_SUSPENDED) {
6965 		mutex_exit(SD_MUTEX(un));
6966 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6967 		    "device already suspended, exiting\n");
6968 		return (DDI_SUCCESS);
6969 	}
6970 
6971 	/* Return failure if the device is being used by HA */
6972 	if (un->un_resvd_status &
6973 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6974 		mutex_exit(SD_MUTEX(un));
6975 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6976 		    "device in use by HA, exiting\n");
6977 		return (DDI_FAILURE);
6978 	}
6979 
6980 	/*
6981 	 * Return failure if the device is in a resource wait
6982 	 * or power changing state.
6983 	 */
6984 	if ((un->un_state == SD_STATE_RWAIT) ||
6985 	    (un->un_state == SD_STATE_PM_CHANGING)) {
6986 		mutex_exit(SD_MUTEX(un));
6987 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6988 		    "device in resource wait state, exiting\n");
6989 		return (DDI_FAILURE);
6990 	}
6991 
6992 
6993 	un->un_save_state = un->un_last_state;
6994 	New_state(un, SD_STATE_SUSPENDED);
6995 
6996 	/*
6997 	 * Wait for all commands that are in transport or queued to a timer
6998 	 * for retry to complete.
6999 	 *
7000 	 * While waiting, no new commands will be accepted or sent because of
7001 	 * the new state we set above.
7002 	 *
7003 	 * Wait till current operation has completed. If we are in the resource
7004 	 * wait state (with an intr outstanding) then we need to wait till the
7005 	 * intr completes and starts the next cmd. We want to wait for
7006 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
7007 	 */
7008 	wait_cmds_complete = ddi_get_lbolt() +
7009 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
7010 
7011 	while (un->un_ncmds_in_transport != 0) {
7012 		/*
7013 		 * Fail if commands do not finish in the specified time.
7014 		 */
7015 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
7016 		    wait_cmds_complete) == -1) {
7017 			/*
7018 			 * Undo the state changes made above. Everything
7019 			 * must go back to it's original value.
7020 			 */
7021 			Restore_state(un);
7022 			un->un_last_state = un->un_save_state;
7023 			/* Wake up any threads that might be waiting. */
7024 			cv_broadcast(&un->un_suspend_cv);
7025 			mutex_exit(SD_MUTEX(un));
7026 			SD_ERROR(SD_LOG_IO_PM, un,
7027 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
7028 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
7029 			return (DDI_FAILURE);
7030 		}
7031 	}
7032 
7033 	/*
7034 	 * Cancel SCSI watch thread and timeouts, if any are active
7035 	 */
7036 
7037 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
7038 		opaque_t temp_token = un->un_swr_token;
7039 		mutex_exit(SD_MUTEX(un));
7040 		scsi_watch_suspend(temp_token);
7041 		mutex_enter(SD_MUTEX(un));
7042 	}
7043 
7044 	if (un->un_reset_throttle_timeid != NULL) {
7045 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
7046 		un->un_reset_throttle_timeid = NULL;
7047 		mutex_exit(SD_MUTEX(un));
7048 		(void) untimeout(temp_id);
7049 		mutex_enter(SD_MUTEX(un));
7050 	}
7051 
7052 	if (un->un_dcvb_timeid != NULL) {
7053 		timeout_id_t temp_id = un->un_dcvb_timeid;
7054 		un->un_dcvb_timeid = NULL;
7055 		mutex_exit(SD_MUTEX(un));
7056 		(void) untimeout(temp_id);
7057 		mutex_enter(SD_MUTEX(un));
7058 	}
7059 
7060 	mutex_enter(&un->un_pm_mutex);
7061 	if (un->un_pm_timeid != NULL) {
7062 		timeout_id_t temp_id = un->un_pm_timeid;
7063 		un->un_pm_timeid = NULL;
7064 		mutex_exit(&un->un_pm_mutex);
7065 		mutex_exit(SD_MUTEX(un));
7066 		(void) untimeout(temp_id);
7067 		mutex_enter(SD_MUTEX(un));
7068 	} else {
7069 		mutex_exit(&un->un_pm_mutex);
7070 	}
7071 
7072 	if (un->un_retry_timeid != NULL) {
7073 		timeout_id_t temp_id = un->un_retry_timeid;
7074 		un->un_retry_timeid = NULL;
7075 		mutex_exit(SD_MUTEX(un));
7076 		(void) untimeout(temp_id);
7077 		mutex_enter(SD_MUTEX(un));
7078 	}
7079 
7080 	if (un->un_direct_priority_timeid != NULL) {
7081 		timeout_id_t temp_id = un->un_direct_priority_timeid;
7082 		un->un_direct_priority_timeid = NULL;
7083 		mutex_exit(SD_MUTEX(un));
7084 		(void) untimeout(temp_id);
7085 		mutex_enter(SD_MUTEX(un));
7086 	}
7087 
7088 	if (un->un_f_is_fibre == TRUE) {
7089 		/*
7090 		 * Remove callbacks for insert and remove events
7091 		 */
7092 		if (un->un_insert_event != NULL) {
7093 			mutex_exit(SD_MUTEX(un));
7094 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
7095 			mutex_enter(SD_MUTEX(un));
7096 			un->un_insert_event = NULL;
7097 		}
7098 
7099 		if (un->un_remove_event != NULL) {
7100 			mutex_exit(SD_MUTEX(un));
7101 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
7102 			mutex_enter(SD_MUTEX(un));
7103 			un->un_remove_event = NULL;
7104 		}
7105 	}
7106 
7107 	mutex_exit(SD_MUTEX(un));
7108 
7109 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
7110 
7111 	return (DDI_SUCCESS);
7112 }
7113 
7114 
7115 /*
7116  *    Function: sd_ddi_pm_suspend
7117  *
7118  * Description: Set the drive state to low power.
7119  *		Someone else is required to actually change the drive
7120  *		power level.
7121  *
7122  *   Arguments: un - driver soft state (unit) structure
7123  *
7124  * Return Code: DDI_FAILURE or DDI_SUCCESS
7125  *
7126  *     Context: Kernel thread context
7127  */
7128 
7129 static int
7130 sd_ddi_pm_suspend(struct sd_lun *un)
7131 {
7132 	ASSERT(un != NULL);
7133 	SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n");
7134 
7135 	ASSERT(!mutex_owned(SD_MUTEX(un)));
7136 	mutex_enter(SD_MUTEX(un));
7137 
7138 	/*
7139 	 * Exit if power management is not enabled for this device, or if
7140 	 * the device is being used by HA.
7141 	 */
7142 	if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
7143 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
7144 		mutex_exit(SD_MUTEX(un));
7145 		SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n");
7146 		return (DDI_SUCCESS);
7147 	}
7148 
7149 	SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n",
7150 	    un->un_ncmds_in_driver);
7151 
7152 	/*
7153 	 * See if the device is not busy, ie.:
7154 	 *    - we have no commands in the driver for this device
7155 	 *    - not waiting for resources
7156 	 */
7157 	if ((un->un_ncmds_in_driver == 0) &&
7158 	    (un->un_state != SD_STATE_RWAIT)) {
7159 		/*
7160 		 * The device is not busy, so it is OK to go to low power state.
7161 		 * Indicate low power, but rely on someone else to actually
7162 		 * change it.
7163 		 */
7164 		mutex_enter(&un->un_pm_mutex);
7165 		un->un_pm_count = -1;
7166 		mutex_exit(&un->un_pm_mutex);
7167 		un->un_power_level = SD_SPINDLE_OFF;
7168 	}
7169 
7170 	mutex_exit(SD_MUTEX(un));
7171 
7172 	SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n");
7173 
7174 	return (DDI_SUCCESS);
7175 }
7176 
7177 
7178 /*
7179  *    Function: sd_ddi_resume
7180  *
7181  * Description: Performs system power-up operations..
7182  *
7183  * Return Code: DDI_SUCCESS
7184  *		DDI_FAILURE
7185  *
7186  *     Context: Kernel thread context
7187  */
7188 
7189 static int
7190 sd_ddi_resume(dev_info_t *devi)
7191 {
7192 	struct	sd_lun	*un;
7193 
7194 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
7195 	if (un == NULL) {
7196 		return (DDI_FAILURE);
7197 	}
7198 
7199 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
7200 
7201 	mutex_enter(SD_MUTEX(un));
7202 	Restore_state(un);
7203 
7204 	/*
7205 	 * Restore the state which was saved to give the
7206 	 * the right state in un_last_state
7207 	 */
7208 	un->un_last_state = un->un_save_state;
7209 	/*
7210 	 * Note: throttle comes back at full.
7211 	 * Also note: this MUST be done before calling pm_raise_power
7212 	 * otherwise the system can get hung in biowait. The scenario where
7213 	 * this'll happen is under cpr suspend. Writing of the system
7214 	 * state goes through sddump, which writes 0 to un_throttle. If
7215 	 * writing the system state then fails, example if the partition is
7216 	 * too small, then cpr attempts a resume. If throttle isn't restored
7217 	 * from the saved value until after calling pm_raise_power then
7218 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
7219 	 * in biowait.
7220 	 */
7221 	un->un_throttle = un->un_saved_throttle;
7222 
7223 	/*
7224 	 * The chance of failure is very rare as the only command done in power
7225 	 * entry point is START command when you transition from 0->1 or
7226 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
7227 	 * which suspend was done. Ignore the return value as the resume should
7228 	 * not be failed. In the case of removable media the media need not be
7229 	 * inserted and hence there is a chance that raise power will fail with
7230 	 * media not present.
7231 	 */
7232 	if (un->un_f_attach_spinup) {
7233 		mutex_exit(SD_MUTEX(un));
7234 		(void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON);
7235 		mutex_enter(SD_MUTEX(un));
7236 	}
7237 
7238 	/*
7239 	 * Don't broadcast to the suspend cv and therefore possibly
7240 	 * start I/O until after power has been restored.
7241 	 */
7242 	cv_broadcast(&un->un_suspend_cv);
7243 	cv_broadcast(&un->un_state_cv);
7244 
7245 	/* restart thread */
7246 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
7247 		scsi_watch_resume(un->un_swr_token);
7248 	}
7249 
7250 #if (defined(__fibre))
7251 	if (un->un_f_is_fibre == TRUE) {
7252 		/*
7253 		 * Add callbacks for insert and remove events
7254 		 */
7255 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
7256 			sd_init_event_callbacks(un);
7257 		}
7258 	}
7259 #endif
7260 
7261 	/*
7262 	 * Transport any pending commands to the target.
7263 	 *
7264 	 * If this is a low-activity device commands in queue will have to wait
7265 	 * until new commands come in, which may take awhile. Also, we
7266 	 * specifically don't check un_ncmds_in_transport because we know that
7267 	 * there really are no commands in progress after the unit was
7268 	 * suspended and we could have reached the throttle level, been
7269 	 * suspended, and have no new commands coming in for awhile. Highly
7270 	 * unlikely, but so is the low-activity disk scenario.
7271 	 */
7272 	ddi_xbuf_dispatch(un->un_xbuf_attr);
7273 
7274 	sd_start_cmds(un, NULL);
7275 	mutex_exit(SD_MUTEX(un));
7276 
7277 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
7278 
7279 	return (DDI_SUCCESS);
7280 }
7281 
7282 
7283 /*
7284  *    Function: sd_ddi_pm_resume
7285  *
7286  * Description: Set the drive state to powered on.
7287  *		Someone else is required to actually change the drive
7288  *		power level.
7289  *
7290  *   Arguments: un - driver soft state (unit) structure
7291  *
7292  * Return Code: DDI_SUCCESS
7293  *
7294  *     Context: Kernel thread context
7295  */
7296 
7297 static int
7298 sd_ddi_pm_resume(struct sd_lun *un)
7299 {
7300 	ASSERT(un != NULL);
7301 
7302 	ASSERT(!mutex_owned(SD_MUTEX(un)));
7303 	mutex_enter(SD_MUTEX(un));
7304 	un->un_power_level = SD_SPINDLE_ON;
7305 
7306 	ASSERT(!mutex_owned(&un->un_pm_mutex));
7307 	mutex_enter(&un->un_pm_mutex);
7308 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
7309 		un->un_pm_count++;
7310 		ASSERT(un->un_pm_count == 0);
7311 		/*
7312 		 * Note: no longer do the cv_broadcast on un_suspend_cv. The
7313 		 * un_suspend_cv is for a system resume, not a power management
7314 		 * device resume. (4297749)
7315 		 *	 cv_broadcast(&un->un_suspend_cv);
7316 		 */
7317 	}
7318 	mutex_exit(&un->un_pm_mutex);
7319 	mutex_exit(SD_MUTEX(un));
7320 
7321 	return (DDI_SUCCESS);
7322 }
7323 
7324 
7325 /*
7326  *    Function: sd_pm_idletimeout_handler
7327  *
7328  * Description: A timer routine that's active only while a device is busy.
7329  *		The purpose is to extend slightly the pm framework's busy
7330  *		view of the device to prevent busy/idle thrashing for
7331  *		back-to-back commands. Do this by comparing the current time
7332  *		to the time at which the last command completed and when the
7333  *		difference is greater than sd_pm_idletime, call
7334  *		pm_idle_component. In addition to indicating idle to the pm
7335  *		framework, update the chain type to again use the internal pm
7336  *		layers of the driver.
7337  *
7338  *   Arguments: arg - driver soft state (unit) structure
7339  *
7340  *     Context: Executes in a timeout(9F) thread context
7341  */
7342 
7343 static void
7344 sd_pm_idletimeout_handler(void *arg)
7345 {
7346 	struct sd_lun *un = arg;
7347 
7348 	time_t	now;
7349 
7350 	mutex_enter(&sd_detach_mutex);
7351 	if (un->un_detach_count != 0) {
7352 		/* Abort if the instance is detaching */
7353 		mutex_exit(&sd_detach_mutex);
7354 		return;
7355 	}
7356 	mutex_exit(&sd_detach_mutex);
7357 
7358 	now = ddi_get_time();
7359 	/*
7360 	 * Grab both mutexes, in the proper order, since we're accessing
7361 	 * both PM and softstate variables.
7362 	 */
7363 	mutex_enter(SD_MUTEX(un));
7364 	mutex_enter(&un->un_pm_mutex);
7365 	if (((now - un->un_pm_idle_time) > sd_pm_idletime) &&
7366 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
7367 		/*
7368 		 * Update the chain types.
7369 		 * This takes affect on the next new command received.
7370 		 */
7371 		if (un->un_f_non_devbsize_supported) {
7372 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7373 		} else {
7374 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7375 		}
7376 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7377 
7378 		SD_TRACE(SD_LOG_IO_PM, un,
7379 		    "sd_pm_idletimeout_handler: idling device\n");
7380 		(void) pm_idle_component(SD_DEVINFO(un), 0);
7381 		un->un_pm_idle_timeid = NULL;
7382 	} else {
7383 		un->un_pm_idle_timeid =
7384 			timeout(sd_pm_idletimeout_handler, un,
7385 			(drv_usectohz((clock_t)300000))); /* 300 ms. */
7386 	}
7387 	mutex_exit(&un->un_pm_mutex);
7388 	mutex_exit(SD_MUTEX(un));
7389 }
7390 
7391 
7392 /*
7393  *    Function: sd_pm_timeout_handler
7394  *
7395  * Description: Callback to tell framework we are idle.
7396  *
7397  *     Context: timeout(9f) thread context.
7398  */
7399 
7400 static void
7401 sd_pm_timeout_handler(void *arg)
7402 {
7403 	struct sd_lun *un = arg;
7404 
7405 	(void) pm_idle_component(SD_DEVINFO(un), 0);
7406 	mutex_enter(&un->un_pm_mutex);
7407 	un->un_pm_timeid = NULL;
7408 	mutex_exit(&un->un_pm_mutex);
7409 }
7410 
7411 
7412 /*
7413  *    Function: sdpower
7414  *
7415  * Description: PM entry point.
7416  *
7417  * Return Code: DDI_SUCCESS
7418  *		DDI_FAILURE
7419  *
7420  *     Context: Kernel thread context
7421  */
7422 
7423 static int
7424 sdpower(dev_info_t *devi, int component, int level)
7425 {
7426 	struct sd_lun	*un;
7427 	int		instance;
7428 	int		rval = DDI_SUCCESS;
7429 	uint_t		i, log_page_size, maxcycles, ncycles;
7430 	uchar_t		*log_page_data;
7431 	int		log_sense_page;
7432 	int		medium_present;
7433 	time_t		intvlp;
7434 	dev_t		dev;
7435 	struct pm_trans_data	sd_pm_tran_data;
7436 	uchar_t		save_state;
7437 	int		sval;
7438 	uchar_t		state_before_pm;
7439 	int		got_semaphore_here;
7440 
7441 	instance = ddi_get_instance(devi);
7442 
7443 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
7444 	    (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) ||
7445 	    component != 0) {
7446 		return (DDI_FAILURE);
7447 	}
7448 
7449 	dev = sd_make_device(SD_DEVINFO(un));
7450 
7451 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
7452 
7453 	/*
7454 	 * Must synchronize power down with close.
7455 	 * Attempt to decrement/acquire the open/close semaphore,
7456 	 * but do NOT wait on it. If it's not greater than zero,
7457 	 * ie. it can't be decremented without waiting, then
7458 	 * someone else, either open or close, already has it
7459 	 * and the try returns 0. Use that knowledge here to determine
7460 	 * if it's OK to change the device power level.
7461 	 * Also, only increment it on exit if it was decremented, ie. gotten,
7462 	 * here.
7463 	 */
7464 	got_semaphore_here = sema_tryp(&un->un_semoclose);
7465 
7466 	mutex_enter(SD_MUTEX(un));
7467 
7468 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
7469 	    un->un_ncmds_in_driver);
7470 
7471 	/*
7472 	 * If un_ncmds_in_driver is non-zero it indicates commands are
7473 	 * already being processed in the driver, or if the semaphore was
7474 	 * not gotten here it indicates an open or close is being processed.
7475 	 * At the same time somebody is requesting to go low power which
7476 	 * can't happen, therefore we need to return failure.
7477 	 */
7478 	if ((level == SD_SPINDLE_OFF) &&
7479 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
7480 		mutex_exit(SD_MUTEX(un));
7481 
7482 		if (got_semaphore_here != 0) {
7483 			sema_v(&un->un_semoclose);
7484 		}
7485 		SD_TRACE(SD_LOG_IO_PM, un,
7486 		    "sdpower: exit, device has queued cmds.\n");
7487 		return (DDI_FAILURE);
7488 	}
7489 
7490 	/*
7491 	 * if it is OFFLINE that means the disk is completely dead
7492 	 * in our case we have to put the disk in on or off by sending commands
7493 	 * Of course that will fail anyway so return back here.
7494 	 *
7495 	 * Power changes to a device that's OFFLINE or SUSPENDED
7496 	 * are not allowed.
7497 	 */
7498 	if ((un->un_state == SD_STATE_OFFLINE) ||
7499 	    (un->un_state == SD_STATE_SUSPENDED)) {
7500 		mutex_exit(SD_MUTEX(un));
7501 
7502 		if (got_semaphore_here != 0) {
7503 			sema_v(&un->un_semoclose);
7504 		}
7505 		SD_TRACE(SD_LOG_IO_PM, un,
7506 		    "sdpower: exit, device is off-line.\n");
7507 		return (DDI_FAILURE);
7508 	}
7509 
7510 	/*
7511 	 * Change the device's state to indicate it's power level
7512 	 * is being changed. Do this to prevent a power off in the
7513 	 * middle of commands, which is especially bad on devices
7514 	 * that are really powered off instead of just spun down.
7515 	 */
7516 	state_before_pm = un->un_state;
7517 	un->un_state = SD_STATE_PM_CHANGING;
7518 
7519 	mutex_exit(SD_MUTEX(un));
7520 
7521 	/*
7522 	 * If "pm-capable" property is set to TRUE by HBA drivers,
7523 	 * bypass the following checking, otherwise, check the log
7524 	 * sense information for this device
7525 	 */
7526 	if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) {
7527 		/*
7528 		 * Get the log sense information to understand whether the
7529 		 * the powercycle counts have gone beyond the threshhold.
7530 		 */
7531 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
7532 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
7533 
7534 		mutex_enter(SD_MUTEX(un));
7535 		log_sense_page = un->un_start_stop_cycle_page;
7536 		mutex_exit(SD_MUTEX(un));
7537 
7538 		rval = sd_send_scsi_LOG_SENSE(un, log_page_data,
7539 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
7540 #ifdef	SDDEBUG
7541 		if (sd_force_pm_supported) {
7542 			/* Force a successful result */
7543 			rval = 0;
7544 		}
7545 #endif
7546 		if (rval != 0) {
7547 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
7548 			    "Log Sense Failed\n");
7549 			kmem_free(log_page_data, log_page_size);
7550 			/* Cannot support power management on those drives */
7551 
7552 			if (got_semaphore_here != 0) {
7553 				sema_v(&un->un_semoclose);
7554 			}
7555 			/*
7556 			 * On exit put the state back to it's original value
7557 			 * and broadcast to anyone waiting for the power
7558 			 * change completion.
7559 			 */
7560 			mutex_enter(SD_MUTEX(un));
7561 			un->un_state = state_before_pm;
7562 			cv_broadcast(&un->un_suspend_cv);
7563 			mutex_exit(SD_MUTEX(un));
7564 			SD_TRACE(SD_LOG_IO_PM, un,
7565 			    "sdpower: exit, Log Sense Failed.\n");
7566 			return (DDI_FAILURE);
7567 		}
7568 
7569 		/*
7570 		 * From the page data - Convert the essential information to
7571 		 * pm_trans_data
7572 		 */
7573 		maxcycles =
7574 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
7575 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
7576 
7577 		sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
7578 
7579 		ncycles =
7580 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
7581 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
7582 
7583 		sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
7584 
7585 		for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
7586 			sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
7587 			    log_page_data[8+i];
7588 		}
7589 
7590 		kmem_free(log_page_data, log_page_size);
7591 
7592 		/*
7593 		 * Call pm_trans_check routine to get the Ok from
7594 		 * the global policy
7595 		 */
7596 
7597 		sd_pm_tran_data.format = DC_SCSI_FORMAT;
7598 		sd_pm_tran_data.un.scsi_cycles.flag = 0;
7599 
7600 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
7601 #ifdef	SDDEBUG
7602 		if (sd_force_pm_supported) {
7603 			/* Force a successful result */
7604 			rval = 1;
7605 		}
7606 #endif
7607 		switch (rval) {
7608 		case 0:
7609 			/*
7610 			 * Not Ok to Power cycle or error in parameters passed
7611 			 * Would have given the advised time to consider power
7612 			 * cycle. Based on the new intvlp parameter we are
7613 			 * supposed to pretend we are busy so that pm framework
7614 			 * will never call our power entry point. Because of
7615 			 * that install a timeout handler and wait for the
7616 			 * recommended time to elapse so that power management
7617 			 * can be effective again.
7618 			 *
7619 			 * To effect this behavior, call pm_busy_component to
7620 			 * indicate to the framework this device is busy.
7621 			 * By not adjusting un_pm_count the rest of PM in
7622 			 * the driver will function normally, and independant
7623 			 * of this but because the framework is told the device
7624 			 * is busy it won't attempt powering down until it gets
7625 			 * a matching idle. The timeout handler sends this.
7626 			 * Note: sd_pm_entry can't be called here to do this
7627 			 * because sdpower may have been called as a result
7628 			 * of a call to pm_raise_power from within sd_pm_entry.
7629 			 *
7630 			 * If a timeout handler is already active then
7631 			 * don't install another.
7632 			 */
7633 			mutex_enter(&un->un_pm_mutex);
7634 			if (un->un_pm_timeid == NULL) {
7635 				un->un_pm_timeid =
7636 				    timeout(sd_pm_timeout_handler,
7637 				    un, intvlp * drv_usectohz(1000000));
7638 				mutex_exit(&un->un_pm_mutex);
7639 				(void) pm_busy_component(SD_DEVINFO(un), 0);
7640 			} else {
7641 				mutex_exit(&un->un_pm_mutex);
7642 			}
7643 			if (got_semaphore_here != 0) {
7644 				sema_v(&un->un_semoclose);
7645 			}
7646 			/*
7647 			 * On exit put the state back to it's original value
7648 			 * and broadcast to anyone waiting for the power
7649 			 * change completion.
7650 			 */
7651 			mutex_enter(SD_MUTEX(un));
7652 			un->un_state = state_before_pm;
7653 			cv_broadcast(&un->un_suspend_cv);
7654 			mutex_exit(SD_MUTEX(un));
7655 
7656 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
7657 			    "trans check Failed, not ok to power cycle.\n");
7658 			return (DDI_FAILURE);
7659 
7660 		case -1:
7661 			if (got_semaphore_here != 0) {
7662 				sema_v(&un->un_semoclose);
7663 			}
7664 			/*
7665 			 * On exit put the state back to it's original value
7666 			 * and broadcast to anyone waiting for the power
7667 			 * change completion.
7668 			 */
7669 			mutex_enter(SD_MUTEX(un));
7670 			un->un_state = state_before_pm;
7671 			cv_broadcast(&un->un_suspend_cv);
7672 			mutex_exit(SD_MUTEX(un));
7673 			SD_TRACE(SD_LOG_IO_PM, un,
7674 			    "sdpower: exit, trans check command Failed.\n");
7675 			return (DDI_FAILURE);
7676 		}
7677 	}
7678 
7679 	if (level == SD_SPINDLE_OFF) {
7680 		/*
7681 		 * Save the last state... if the STOP FAILS we need it
7682 		 * for restoring
7683 		 */
7684 		mutex_enter(SD_MUTEX(un));
7685 		save_state = un->un_last_state;
7686 		/*
7687 		 * There must not be any cmds. getting processed
7688 		 * in the driver when we get here. Power to the
7689 		 * device is potentially going off.
7690 		 */
7691 		ASSERT(un->un_ncmds_in_driver == 0);
7692 		mutex_exit(SD_MUTEX(un));
7693 
7694 		/*
7695 		 * For now suspend the device completely before spindle is
7696 		 * turned off
7697 		 */
7698 		if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) {
7699 			if (got_semaphore_here != 0) {
7700 				sema_v(&un->un_semoclose);
7701 			}
7702 			/*
7703 			 * On exit put the state back to it's original value
7704 			 * and broadcast to anyone waiting for the power
7705 			 * change completion.
7706 			 */
7707 			mutex_enter(SD_MUTEX(un));
7708 			un->un_state = state_before_pm;
7709 			cv_broadcast(&un->un_suspend_cv);
7710 			mutex_exit(SD_MUTEX(un));
7711 			SD_TRACE(SD_LOG_IO_PM, un,
7712 			    "sdpower: exit, PM suspend Failed.\n");
7713 			return (DDI_FAILURE);
7714 		}
7715 	}
7716 
7717 	/*
7718 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
7719 	 * close, or strategy. Dump no long uses this routine, it uses it's
7720 	 * own code so it can be done in polled mode.
7721 	 */
7722 
7723 	medium_present = TRUE;
7724 
7725 	/*
7726 	 * When powering up, issue a TUR in case the device is at unit
7727 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
7728 	 * a deadlock on un_pm_busy_cv will occur.
7729 	 */
7730 	if (level == SD_SPINDLE_ON) {
7731 		(void) sd_send_scsi_TEST_UNIT_READY(un,
7732 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
7733 	}
7734 
7735 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
7736 	    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
7737 
7738 	sval = sd_send_scsi_START_STOP_UNIT(un,
7739 	    ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP),
7740 	    SD_PATH_DIRECT);
7741 	/* Command failed, check for media present. */
7742 	if ((sval == ENXIO) && un->un_f_has_removable_media) {
7743 		medium_present = FALSE;
7744 	}
7745 
7746 	/*
7747 	 * The conditions of interest here are:
7748 	 *   if a spindle off with media present fails,
7749 	 *	then restore the state and return an error.
7750 	 *   else if a spindle on fails,
7751 	 *	then return an error (there's no state to restore).
7752 	 * In all other cases we setup for the new state
7753 	 * and return success.
7754 	 */
7755 	switch (level) {
7756 	case SD_SPINDLE_OFF:
7757 		if ((medium_present == TRUE) && (sval != 0)) {
7758 			/* The stop command from above failed */
7759 			rval = DDI_FAILURE;
7760 			/*
7761 			 * The stop command failed, and we have media
7762 			 * present. Put the level back by calling the
7763 			 * sd_pm_resume() and set the state back to
7764 			 * it's previous value.
7765 			 */
7766 			(void) sd_ddi_pm_resume(un);
7767 			mutex_enter(SD_MUTEX(un));
7768 			un->un_last_state = save_state;
7769 			mutex_exit(SD_MUTEX(un));
7770 			break;
7771 		}
7772 		/*
7773 		 * The stop command from above succeeded.
7774 		 */
7775 		if (un->un_f_monitor_media_state) {
7776 			/*
7777 			 * Terminate watch thread in case of removable media
7778 			 * devices going into low power state. This is as per
7779 			 * the requirements of pm framework, otherwise commands
7780 			 * will be generated for the device (through watch
7781 			 * thread), even when the device is in low power state.
7782 			 */
7783 			mutex_enter(SD_MUTEX(un));
7784 			un->un_f_watcht_stopped = FALSE;
7785 			if (un->un_swr_token != NULL) {
7786 				opaque_t temp_token = un->un_swr_token;
7787 				un->un_f_watcht_stopped = TRUE;
7788 				un->un_swr_token = NULL;
7789 				mutex_exit(SD_MUTEX(un));
7790 				(void) scsi_watch_request_terminate(temp_token,
7791 				    SCSI_WATCH_TERMINATE_WAIT);
7792 			} else {
7793 				mutex_exit(SD_MUTEX(un));
7794 			}
7795 		}
7796 		break;
7797 
7798 	default:	/* The level requested is spindle on... */
7799 		/*
7800 		 * Legacy behavior: return success on a failed spinup
7801 		 * if there is no media in the drive.
7802 		 * Do this by looking at medium_present here.
7803 		 */
7804 		if ((sval != 0) && medium_present) {
7805 			/* The start command from above failed */
7806 			rval = DDI_FAILURE;
7807 			break;
7808 		}
7809 		/*
7810 		 * The start command from above succeeded
7811 		 * Resume the devices now that we have
7812 		 * started the disks
7813 		 */
7814 		(void) sd_ddi_pm_resume(un);
7815 
7816 		/*
7817 		 * Resume the watch thread since it was suspended
7818 		 * when the device went into low power mode.
7819 		 */
7820 		if (un->un_f_monitor_media_state) {
7821 			mutex_enter(SD_MUTEX(un));
7822 			if (un->un_f_watcht_stopped == TRUE) {
7823 				opaque_t temp_token;
7824 
7825 				un->un_f_watcht_stopped = FALSE;
7826 				mutex_exit(SD_MUTEX(un));
7827 				temp_token = scsi_watch_request_submit(
7828 				    SD_SCSI_DEVP(un),
7829 				    sd_check_media_time,
7830 				    SENSE_LENGTH, sd_media_watch_cb,
7831 				    (caddr_t)dev);
7832 				mutex_enter(SD_MUTEX(un));
7833 				un->un_swr_token = temp_token;
7834 			}
7835 			mutex_exit(SD_MUTEX(un));
7836 		}
7837 	}
7838 	if (got_semaphore_here != 0) {
7839 		sema_v(&un->un_semoclose);
7840 	}
7841 	/*
7842 	 * On exit put the state back to it's original value
7843 	 * and broadcast to anyone waiting for the power
7844 	 * change completion.
7845 	 */
7846 	mutex_enter(SD_MUTEX(un));
7847 	un->un_state = state_before_pm;
7848 	cv_broadcast(&un->un_suspend_cv);
7849 	mutex_exit(SD_MUTEX(un));
7850 
7851 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7852 
7853 	return (rval);
7854 }
7855 
7856 
7857 
7858 /*
7859  *    Function: sdattach
7860  *
7861  * Description: Driver's attach(9e) entry point function.
7862  *
7863  *   Arguments: devi - opaque device info handle
7864  *		cmd  - attach  type
7865  *
7866  * Return Code: DDI_SUCCESS
7867  *		DDI_FAILURE
7868  *
7869  *     Context: Kernel thread context
7870  */
7871 
7872 static int
7873 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7874 {
7875 	switch (cmd) {
7876 	case DDI_ATTACH:
7877 		return (sd_unit_attach(devi));
7878 	case DDI_RESUME:
7879 		return (sd_ddi_resume(devi));
7880 	default:
7881 		break;
7882 	}
7883 	return (DDI_FAILURE);
7884 }
7885 
7886 
7887 /*
7888  *    Function: sddetach
7889  *
7890  * Description: Driver's detach(9E) entry point function.
7891  *
7892  *   Arguments: devi - opaque device info handle
7893  *		cmd  - detach  type
7894  *
7895  * Return Code: DDI_SUCCESS
7896  *		DDI_FAILURE
7897  *
7898  *     Context: Kernel thread context
7899  */
7900 
7901 static int
7902 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7903 {
7904 	switch (cmd) {
7905 	case DDI_DETACH:
7906 		return (sd_unit_detach(devi));
7907 	case DDI_SUSPEND:
7908 		return (sd_ddi_suspend(devi));
7909 	default:
7910 		break;
7911 	}
7912 	return (DDI_FAILURE);
7913 }
7914 
7915 
7916 /*
7917  *     Function: sd_sync_with_callback
7918  *
7919  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7920  *		 state while the callback routine is active.
7921  *
7922  *    Arguments: un: softstate structure for the instance
7923  *
7924  *	Context: Kernel thread context
7925  */
7926 
7927 static void
7928 sd_sync_with_callback(struct sd_lun *un)
7929 {
7930 	ASSERT(un != NULL);
7931 
7932 	mutex_enter(SD_MUTEX(un));
7933 
7934 	ASSERT(un->un_in_callback >= 0);
7935 
7936 	while (un->un_in_callback > 0) {
7937 		mutex_exit(SD_MUTEX(un));
7938 		delay(2);
7939 		mutex_enter(SD_MUTEX(un));
7940 	}
7941 
7942 	mutex_exit(SD_MUTEX(un));
7943 }
7944 
7945 /*
7946  *    Function: sd_unit_attach
7947  *
7948  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7949  *		the soft state structure for the device and performs
7950  *		all necessary structure and device initializations.
7951  *
7952  *   Arguments: devi: the system's dev_info_t for the device.
7953  *
7954  * Return Code: DDI_SUCCESS if attach is successful.
7955  *		DDI_FAILURE if any part of the attach fails.
7956  *
7957  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7958  *		Kernel thread context only.  Can sleep.
7959  */
7960 
7961 static int
7962 sd_unit_attach(dev_info_t *devi)
7963 {
7964 	struct	scsi_device	*devp;
7965 	struct	sd_lun		*un;
7966 	char			*variantp;
7967 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
7968 	int	instance;
7969 	int	rval;
7970 	int	wc_enabled;
7971 	int	tgt;
7972 	uint64_t	capacity;
7973 	uint_t		lbasize;
7974 	dev_info_t	*pdip = ddi_get_parent(devi);
7975 
7976 	/*
7977 	 * Retrieve the target driver's private data area. This was set
7978 	 * up by the HBA.
7979 	 */
7980 	devp = ddi_get_driver_private(devi);
7981 
7982 	/*
7983 	 * Retrieve the target ID of the device.
7984 	 */
7985 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7986 	    SCSI_ADDR_PROP_TARGET, -1);
7987 
7988 	/*
7989 	 * Since we have no idea what state things were left in by the last
7990 	 * user of the device, set up some 'default' settings, ie. turn 'em
7991 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7992 	 * Do this before the scsi_probe, which sends an inquiry.
7993 	 * This is a fix for bug (4430280).
7994 	 * Of special importance is wide-xfer. The drive could have been left
7995 	 * in wide transfer mode by the last driver to communicate with it,
7996 	 * this includes us. If that's the case, and if the following is not
7997 	 * setup properly or we don't re-negotiate with the drive prior to
7998 	 * transferring data to/from the drive, it causes bus parity errors,
7999 	 * data overruns, and unexpected interrupts. This first occurred when
8000 	 * the fix for bug (4378686) was made.
8001 	 */
8002 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
8003 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
8004 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
8005 
8006 	/*
8007 	 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
8008 	 * on a target. Setting it per lun instance actually sets the
8009 	 * capability of this target, which affects those luns already
8010 	 * attached on the same target. So during attach, we can only disable
8011 	 * this capability only when no other lun has been attached on this
8012 	 * target. By doing this, we assume a target has the same tagged-qing
8013 	 * capability for every lun. The condition can be removed when HBA
8014 	 * is changed to support per lun based tagged-qing capability.
8015 	 */
8016 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8017 		(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
8018 	}
8019 
8020 	/*
8021 	 * Use scsi_probe() to issue an INQUIRY command to the device.
8022 	 * This call will allocate and fill in the scsi_inquiry structure
8023 	 * and point the sd_inq member of the scsi_device structure to it.
8024 	 * If the attach succeeds, then this memory will not be de-allocated
8025 	 * (via scsi_unprobe()) until the instance is detached.
8026 	 */
8027 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
8028 		goto probe_failed;
8029 	}
8030 
8031 	/*
8032 	 * Check the device type as specified in the inquiry data and
8033 	 * claim it if it is of a type that we support.
8034 	 */
8035 	switch (devp->sd_inq->inq_dtype) {
8036 	case DTYPE_DIRECT:
8037 		break;
8038 	case DTYPE_RODIRECT:
8039 		break;
8040 	case DTYPE_OPTICAL:
8041 		break;
8042 	case DTYPE_NOTPRESENT:
8043 	default:
8044 		/* Unsupported device type; fail the attach. */
8045 		goto probe_failed;
8046 	}
8047 
8048 	/*
8049 	 * Allocate the soft state structure for this unit.
8050 	 *
8051 	 * We rely upon this memory being set to all zeroes by
8052 	 * ddi_soft_state_zalloc().  We assume that any member of the
8053 	 * soft state structure that is not explicitly initialized by
8054 	 * this routine will have a value of zero.
8055 	 */
8056 	instance = ddi_get_instance(devp->sd_dev);
8057 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
8058 		goto probe_failed;
8059 	}
8060 
8061 	/*
8062 	 * Retrieve a pointer to the newly-allocated soft state.
8063 	 *
8064 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
8065 	 * was successful, unless something has gone horribly wrong and the
8066 	 * ddi's soft state internals are corrupt (in which case it is
8067 	 * probably better to halt here than just fail the attach....)
8068 	 */
8069 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
8070 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
8071 		    instance);
8072 		/*NOTREACHED*/
8073 	}
8074 
8075 	/*
8076 	 * Link the back ptr of the driver soft state to the scsi_device
8077 	 * struct for this lun.
8078 	 * Save a pointer to the softstate in the driver-private area of
8079 	 * the scsi_device struct.
8080 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
8081 	 * we first set un->un_sd below.
8082 	 */
8083 	un->un_sd = devp;
8084 	devp->sd_private = (opaque_t)un;
8085 
8086 	/*
8087 	 * The following must be after devp is stored in the soft state struct.
8088 	 */
8089 #ifdef SDDEBUG
8090 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8091 	    "%s_unit_attach: un:0x%p instance:%d\n",
8092 	    ddi_driver_name(devi), un, instance);
8093 #endif
8094 
8095 	/*
8096 	 * Set up the device type and node type (for the minor nodes).
8097 	 * By default we assume that the device can at least support the
8098 	 * Common Command Set. Call it a CD-ROM if it reports itself
8099 	 * as a RODIRECT device.
8100 	 */
8101 	switch (devp->sd_inq->inq_dtype) {
8102 	case DTYPE_RODIRECT:
8103 		un->un_node_type = DDI_NT_CD_CHAN;
8104 		un->un_ctype	 = CTYPE_CDROM;
8105 		break;
8106 	case DTYPE_OPTICAL:
8107 		un->un_node_type = DDI_NT_BLOCK_CHAN;
8108 		un->un_ctype	 = CTYPE_ROD;
8109 		break;
8110 	default:
8111 		un->un_node_type = DDI_NT_BLOCK_CHAN;
8112 		un->un_ctype	 = CTYPE_CCS;
8113 		break;
8114 	}
8115 
8116 	/*
8117 	 * Try to read the interconnect type from the HBA.
8118 	 *
8119 	 * Note: This driver is currently compiled as two binaries, a parallel
8120 	 * scsi version (sd) and a fibre channel version (ssd). All functional
8121 	 * differences are determined at compile time. In the future a single
8122 	 * binary will be provided and the inteconnect type will be used to
8123 	 * differentiate between fibre and parallel scsi behaviors. At that time
8124 	 * it will be necessary for all fibre channel HBAs to support this
8125 	 * property.
8126 	 *
8127 	 * set un_f_is_fiber to TRUE ( default fiber )
8128 	 */
8129 	un->un_f_is_fibre = TRUE;
8130 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
8131 	case INTERCONNECT_SSA:
8132 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
8133 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8134 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
8135 		break;
8136 	case INTERCONNECT_PARALLEL:
8137 		un->un_f_is_fibre = FALSE;
8138 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
8139 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8140 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
8141 		break;
8142 	case INTERCONNECT_SATA:
8143 		un->un_f_is_fibre = FALSE;
8144 		un->un_interconnect_type = SD_INTERCONNECT_SATA;
8145 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8146 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
8147 		break;
8148 	case INTERCONNECT_FIBRE:
8149 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
8150 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8151 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
8152 		break;
8153 	case INTERCONNECT_FABRIC:
8154 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
8155 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
8156 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8157 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
8158 		break;
8159 	default:
8160 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
8161 		/*
8162 		 * The HBA does not support the "interconnect-type" property
8163 		 * (or did not provide a recognized type).
8164 		 *
8165 		 * Note: This will be obsoleted when a single fibre channel
8166 		 * and parallel scsi driver is delivered. In the meantime the
8167 		 * interconnect type will be set to the platform default.If that
8168 		 * type is not parallel SCSI, it means that we should be
8169 		 * assuming "ssd" semantics. However, here this also means that
8170 		 * the FC HBA is not supporting the "interconnect-type" property
8171 		 * like we expect it to, so log this occurrence.
8172 		 */
8173 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
8174 		if (!SD_IS_PARALLEL_SCSI(un)) {
8175 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8176 			    "sd_unit_attach: un:0x%p Assuming "
8177 			    "INTERCONNECT_FIBRE\n", un);
8178 		} else {
8179 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8180 			    "sd_unit_attach: un:0x%p Assuming "
8181 			    "INTERCONNECT_PARALLEL\n", un);
8182 			un->un_f_is_fibre = FALSE;
8183 		}
8184 #else
8185 		/*
8186 		 * Note: This source will be implemented when a single fibre
8187 		 * channel and parallel scsi driver is delivered. The default
8188 		 * will be to assume that if a device does not support the
8189 		 * "interconnect-type" property it is a parallel SCSI HBA and
8190 		 * we will set the interconnect type for parallel scsi.
8191 		 */
8192 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
8193 		un->un_f_is_fibre = FALSE;
8194 #endif
8195 		break;
8196 	}
8197 
8198 	if (un->un_f_is_fibre == TRUE) {
8199 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
8200 			SCSI_VERSION_3) {
8201 			switch (un->un_interconnect_type) {
8202 			case SD_INTERCONNECT_FIBRE:
8203 			case SD_INTERCONNECT_SSA:
8204 				un->un_node_type = DDI_NT_BLOCK_WWN;
8205 				break;
8206 			default:
8207 				break;
8208 			}
8209 		}
8210 	}
8211 
8212 	/*
8213 	 * Initialize the Request Sense command for the target
8214 	 */
8215 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
8216 		goto alloc_rqs_failed;
8217 	}
8218 
8219 	/*
8220 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
8221 	 * with seperate binary for sd and ssd.
8222 	 *
8223 	 * x86 has 1 binary, un_retry_count is set base on connection type.
8224 	 * The hardcoded values will go away when Sparc uses 1 binary
8225 	 * for sd and ssd.  This hardcoded values need to match
8226 	 * SD_RETRY_COUNT in sddef.h
8227 	 * The value used is base on interconnect type.
8228 	 * fibre = 3, parallel = 5
8229 	 */
8230 #if defined(__i386) || defined(__amd64)
8231 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
8232 #else
8233 	un->un_retry_count = SD_RETRY_COUNT;
8234 #endif
8235 
8236 	/*
8237 	 * Set the per disk retry count to the default number of retries
8238 	 * for disks and CDROMs. This value can be overridden by the
8239 	 * disk property list or an entry in sd.conf.
8240 	 */
8241 	un->un_notready_retry_count =
8242 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
8243 			: DISK_NOT_READY_RETRY_COUNT(un);
8244 
8245 	/*
8246 	 * Set the busy retry count to the default value of un_retry_count.
8247 	 * This can be overridden by entries in sd.conf or the device
8248 	 * config table.
8249 	 */
8250 	un->un_busy_retry_count = un->un_retry_count;
8251 
8252 	/*
8253 	 * Init the reset threshold for retries.  This number determines
8254 	 * how many retries must be performed before a reset can be issued
8255 	 * (for certain error conditions). This can be overridden by entries
8256 	 * in sd.conf or the device config table.
8257 	 */
8258 	un->un_reset_retry_count = (un->un_retry_count / 2);
8259 
8260 	/*
8261 	 * Set the victim_retry_count to the default un_retry_count
8262 	 */
8263 	un->un_victim_retry_count = (2 * un->un_retry_count);
8264 
8265 	/*
8266 	 * Set the reservation release timeout to the default value of
8267 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
8268 	 * device config table.
8269 	 */
8270 	un->un_reserve_release_time = 5;
8271 
8272 	/*
8273 	 * Set up the default maximum transfer size. Note that this may
8274 	 * get updated later in the attach, when setting up default wide
8275 	 * operations for disks.
8276 	 */
8277 #if defined(__i386) || defined(__amd64)
8278 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
8279 #else
8280 	un->un_max_xfer_size = (uint_t)maxphys;
8281 #endif
8282 
8283 	/*
8284 	 * Get "allow bus device reset" property (defaults to "enabled" if
8285 	 * the property was not defined). This is to disable bus resets for
8286 	 * certain kinds of error recovery. Note: In the future when a run-time
8287 	 * fibre check is available the soft state flag should default to
8288 	 * enabled.
8289 	 */
8290 	if (un->un_f_is_fibre == TRUE) {
8291 		un->un_f_allow_bus_device_reset = TRUE;
8292 	} else {
8293 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8294 			"allow-bus-device-reset", 1) != 0) {
8295 			un->un_f_allow_bus_device_reset = TRUE;
8296 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8297 			"sd_unit_attach: un:0x%p Bus device reset enabled\n",
8298 				un);
8299 		} else {
8300 			un->un_f_allow_bus_device_reset = FALSE;
8301 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8302 			"sd_unit_attach: un:0x%p Bus device reset disabled\n",
8303 				un);
8304 		}
8305 	}
8306 
8307 	/*
8308 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
8309 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
8310 	 *
8311 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
8312 	 * property. The new "variant" property with a value of "atapi" has been
8313 	 * introduced so that future 'variants' of standard SCSI behavior (like
8314 	 * atapi) could be specified by the underlying HBA drivers by supplying
8315 	 * a new value for the "variant" property, instead of having to define a
8316 	 * new property.
8317 	 */
8318 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
8319 		un->un_f_cfg_is_atapi = TRUE;
8320 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8321 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
8322 	}
8323 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
8324 	    &variantp) == DDI_PROP_SUCCESS) {
8325 		if (strcmp(variantp, "atapi") == 0) {
8326 			un->un_f_cfg_is_atapi = TRUE;
8327 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8328 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
8329 		}
8330 		ddi_prop_free(variantp);
8331 	}
8332 
8333 	un->un_cmd_timeout	= SD_IO_TIME;
8334 
8335 	/* Info on current states, statuses, etc. (Updated frequently) */
8336 	un->un_state		= SD_STATE_NORMAL;
8337 	un->un_last_state	= SD_STATE_NORMAL;
8338 
8339 	/* Control & status info for command throttling */
8340 	un->un_throttle		= sd_max_throttle;
8341 	un->un_saved_throttle	= sd_max_throttle;
8342 	un->un_min_throttle	= sd_min_throttle;
8343 
8344 	if (un->un_f_is_fibre == TRUE) {
8345 		un->un_f_use_adaptive_throttle = TRUE;
8346 	} else {
8347 		un->un_f_use_adaptive_throttle = FALSE;
8348 	}
8349 
8350 	/* Removable media support. */
8351 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
8352 	un->un_mediastate		= DKIO_NONE;
8353 	un->un_specified_mediastate	= DKIO_NONE;
8354 
8355 	/* CVs for suspend/resume (PM or DR) */
8356 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
8357 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
8358 
8359 	/* Power management support. */
8360 	un->un_power_level = SD_SPINDLE_UNINIT;
8361 
8362 	cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
8363 	un->un_f_wcc_inprog = 0;
8364 
8365 	/*
8366 	 * The open/close semaphore is used to serialize threads executing
8367 	 * in the driver's open & close entry point routines for a given
8368 	 * instance.
8369 	 */
8370 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
8371 
8372 	/*
8373 	 * The conf file entry and softstate variable is a forceful override,
8374 	 * meaning a non-zero value must be entered to change the default.
8375 	 */
8376 	un->un_f_disksort_disabled = FALSE;
8377 
8378 	/*
8379 	 * Retrieve the properties from the static driver table or the driver
8380 	 * configuration file (.conf) for this unit and update the soft state
8381 	 * for the device as needed for the indicated properties.
8382 	 * Note: the property configuration needs to occur here as some of the
8383 	 * following routines may have dependancies on soft state flags set
8384 	 * as part of the driver property configuration.
8385 	 */
8386 	sd_read_unit_properties(un);
8387 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8388 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
8389 
8390 	/*
8391 	 * Only if a device has "hotpluggable" property, it is
8392 	 * treated as hotpluggable device. Otherwise, it is
8393 	 * regarded as non-hotpluggable one.
8394 	 */
8395 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
8396 	    -1) != -1) {
8397 		un->un_f_is_hotpluggable = TRUE;
8398 	}
8399 
8400 	/*
8401 	 * set unit's attributes(flags) according to "hotpluggable" and
8402 	 * RMB bit in INQUIRY data.
8403 	 */
8404 	sd_set_unit_attributes(un, devi);
8405 
8406 	/*
8407 	 * By default, we mark the capacity, lbasize, and geometry
8408 	 * as invalid. Only if we successfully read a valid capacity
8409 	 * will we update the un_blockcount and un_tgt_blocksize with the
8410 	 * valid values (the geometry will be validated later).
8411 	 */
8412 	un->un_f_blockcount_is_valid	= FALSE;
8413 	un->un_f_tgt_blocksize_is_valid	= FALSE;
8414 	un->un_f_geometry_is_valid	= FALSE;
8415 
8416 	/*
8417 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
8418 	 * otherwise.
8419 	 */
8420 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
8421 	un->un_blockcount = 0;
8422 
8423 	/*
8424 	 * Set up the per-instance info needed to determine the correct
8425 	 * CDBs and other info for issuing commands to the target.
8426 	 */
8427 	sd_init_cdb_limits(un);
8428 
8429 	/*
8430 	 * Set up the IO chains to use, based upon the target type.
8431 	 */
8432 	if (un->un_f_non_devbsize_supported) {
8433 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
8434 	} else {
8435 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
8436 	}
8437 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
8438 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
8439 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
8440 
8441 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
8442 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
8443 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
8444 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
8445 
8446 
8447 	if (ISCD(un)) {
8448 		un->un_additional_codes = sd_additional_codes;
8449 	} else {
8450 		un->un_additional_codes = NULL;
8451 	}
8452 
8453 	/*
8454 	 * Create the kstats here so they can be available for attach-time
8455 	 * routines that send commands to the unit (either polled or via
8456 	 * sd_send_scsi_cmd).
8457 	 *
8458 	 * Note: This is a critical sequence that needs to be maintained:
8459 	 *	1) Instantiate the kstats here, before any routines using the
8460 	 *	   iopath (i.e. sd_send_scsi_cmd).
8461 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8462 	 *	   stats (sd_set_pstats), following sd_validate_geometry(),
8463 	 *	   sd_register_devid(), and sd_cache_control().
8464 	 */
8465 
8466 	un->un_stats = kstat_create(sd_label, instance,
8467 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
8468 	if (un->un_stats != NULL) {
8469 		un->un_stats->ks_lock = SD_MUTEX(un);
8470 		kstat_install(un->un_stats);
8471 	}
8472 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8473 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
8474 
8475 	sd_create_errstats(un, instance);
8476 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8477 	    "sd_unit_attach: un:0x%p errstats created\n", un);
8478 
8479 	/*
8480 	 * The following if/else code was relocated here from below as part
8481 	 * of the fix for bug (4430280). However with the default setup added
8482 	 * on entry to this routine, it's no longer absolutely necessary for
8483 	 * this to be before the call to sd_spin_up_unit.
8484 	 */
8485 	if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
8486 		/*
8487 		 * If SCSI-2 tagged queueing is supported by the target
8488 		 * and by the host adapter then we will enable it.
8489 		 */
8490 		un->un_tagflags = 0;
8491 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) &&
8492 		    (devp->sd_inq->inq_cmdque) &&
8493 		    (un->un_f_arq_enabled == TRUE)) {
8494 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
8495 			    1, 1) == 1) {
8496 				un->un_tagflags = FLAG_STAG;
8497 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8498 				    "sd_unit_attach: un:0x%p tag queueing "
8499 				    "enabled\n", un);
8500 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
8501 			    "untagged-qing", 0) == 1) {
8502 				un->un_f_opt_queueing = TRUE;
8503 				un->un_saved_throttle = un->un_throttle =
8504 				    min(un->un_throttle, 3);
8505 			} else {
8506 				un->un_f_opt_queueing = FALSE;
8507 				un->un_saved_throttle = un->un_throttle = 1;
8508 			}
8509 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
8510 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
8511 			/* The Host Adapter supports internal queueing. */
8512 			un->un_f_opt_queueing = TRUE;
8513 			un->un_saved_throttle = un->un_throttle =
8514 			    min(un->un_throttle, 3);
8515 		} else {
8516 			un->un_f_opt_queueing = FALSE;
8517 			un->un_saved_throttle = un->un_throttle = 1;
8518 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8519 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
8520 		}
8521 
8522 		/*
8523 		 * Enable large transfers for SATA/SAS drives
8524 		 */
8525 		if (SD_IS_SERIAL(un)) {
8526 			un->un_max_xfer_size =
8527 			    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8528 			    sd_max_xfer_size, SD_MAX_XFER_SIZE);
8529 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8530 			    "sd_unit_attach: un:0x%p max transfer "
8531 			    "size=0x%x\n", un, un->un_max_xfer_size);
8532 
8533 		}
8534 
8535 		/* Setup or tear down default wide operations for disks */
8536 
8537 		/*
8538 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
8539 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
8540 		 * system and be set to different values. In the future this
8541 		 * code may need to be updated when the ssd module is
8542 		 * obsoleted and removed from the system. (4299588)
8543 		 */
8544 		if (SD_IS_PARALLEL_SCSI(un) &&
8545 		    (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
8546 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
8547 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
8548 			    1, 1) == 1) {
8549 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8550 				    "sd_unit_attach: un:0x%p Wide Transfer "
8551 				    "enabled\n", un);
8552 			}
8553 
8554 			/*
8555 			 * If tagged queuing has also been enabled, then
8556 			 * enable large xfers
8557 			 */
8558 			if (un->un_saved_throttle == sd_max_throttle) {
8559 				un->un_max_xfer_size =
8560 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8561 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
8562 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8563 				    "sd_unit_attach: un:0x%p max transfer "
8564 				    "size=0x%x\n", un, un->un_max_xfer_size);
8565 			}
8566 		} else {
8567 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
8568 			    0, 1) == 1) {
8569 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8570 				    "sd_unit_attach: un:0x%p "
8571 				    "Wide Transfer disabled\n", un);
8572 			}
8573 		}
8574 	} else {
8575 		un->un_tagflags = FLAG_STAG;
8576 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
8577 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
8578 	}
8579 
8580 	/*
8581 	 * If this target supports LUN reset, try to enable it.
8582 	 */
8583 	if (un->un_f_lun_reset_enabled) {
8584 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
8585 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
8586 			    "un:0x%p lun_reset capability set\n", un);
8587 		} else {
8588 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
8589 			    "un:0x%p lun-reset capability not set\n", un);
8590 		}
8591 	}
8592 
8593 	/*
8594 	 * At this point in the attach, we have enough info in the
8595 	 * soft state to be able to issue commands to the target.
8596 	 *
8597 	 * All command paths used below MUST issue their commands as
8598 	 * SD_PATH_DIRECT. This is important as intermediate layers
8599 	 * are not all initialized yet (such as PM).
8600 	 */
8601 
8602 	/*
8603 	 * Send a TEST UNIT READY command to the device. This should clear
8604 	 * any outstanding UNIT ATTENTION that may be present.
8605 	 *
8606 	 * Note: Don't check for success, just track if there is a reservation,
8607 	 * this is a throw away command to clear any unit attentions.
8608 	 *
8609 	 * Note: This MUST be the first command issued to the target during
8610 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
8611 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
8612 	 * with attempts at spinning up a device with no media.
8613 	 */
8614 	if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) {
8615 		reservation_flag = SD_TARGET_IS_RESERVED;
8616 	}
8617 
8618 	/*
8619 	 * If the device is NOT a removable media device, attempt to spin
8620 	 * it up (using the START_STOP_UNIT command) and read its capacity
8621 	 * (using the READ CAPACITY command).  Note, however, that either
8622 	 * of these could fail and in some cases we would continue with
8623 	 * the attach despite the failure (see below).
8624 	 */
8625 	if (un->un_f_descr_format_supported) {
8626 		switch (sd_spin_up_unit(un)) {
8627 		case 0:
8628 			/*
8629 			 * Spin-up was successful; now try to read the
8630 			 * capacity.  If successful then save the results
8631 			 * and mark the capacity & lbasize as valid.
8632 			 */
8633 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8634 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
8635 
8636 			switch (sd_send_scsi_READ_CAPACITY(un, &capacity,
8637 			    &lbasize, SD_PATH_DIRECT)) {
8638 			case 0: {
8639 				if (capacity > DK_MAX_BLOCKS) {
8640 #ifdef _LP64
8641 					if (capacity + 1 >
8642 					    SD_GROUP1_MAX_ADDRESS) {
8643 						/*
8644 						 * Enable descriptor format
8645 						 * sense data so that we can
8646 						 * get 64 bit sense data
8647 						 * fields.
8648 						 */
8649 						sd_enable_descr_sense(un);
8650 					}
8651 #else
8652 					/* 32-bit kernels can't handle this */
8653 					scsi_log(SD_DEVINFO(un),
8654 					    sd_label, CE_WARN,
8655 					    "disk has %llu blocks, which "
8656 					    "is too large for a 32-bit "
8657 					    "kernel", capacity);
8658 
8659 #if defined(__i386) || defined(__amd64)
8660 					/*
8661 					 * Refer to comments related to off-by-1
8662 					 * at the header of this file.
8663 					 * 1TB disk was treated as (1T - 512)B
8664 					 * in the past, so that it might has
8665 					 * valid VTOC and solaris partitions,
8666 					 * we have to allow it to continue to
8667 					 * work.
8668 					 */
8669 					if (capacity -1 > DK_MAX_BLOCKS)
8670 #endif
8671 					goto spinup_failed;
8672 #endif
8673 				}
8674 
8675 				/*
8676 				 * Here it's not necessary to check the case:
8677 				 * the capacity of the device is bigger than
8678 				 * what the max hba cdb can support. Because
8679 				 * sd_send_scsi_READ_CAPACITY will retrieve
8680 				 * the capacity by sending USCSI command, which
8681 				 * is constrained by the max hba cdb. Actually,
8682 				 * sd_send_scsi_READ_CAPACITY will return
8683 				 * EINVAL when using bigger cdb than required
8684 				 * cdb length. Will handle this case in
8685 				 * "case EINVAL".
8686 				 */
8687 
8688 				/*
8689 				 * The following relies on
8690 				 * sd_send_scsi_READ_CAPACITY never
8691 				 * returning 0 for capacity and/or lbasize.
8692 				 */
8693 				sd_update_block_info(un, lbasize, capacity);
8694 
8695 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8696 				    "sd_unit_attach: un:0x%p capacity = %ld "
8697 				    "blocks; lbasize= %ld.\n", un,
8698 				    un->un_blockcount, un->un_tgt_blocksize);
8699 
8700 				break;
8701 			}
8702 			case EINVAL:
8703 				/*
8704 				 * In the case where the max-cdb-length property
8705 				 * is smaller than the required CDB length for
8706 				 * a SCSI device, a target driver can fail to
8707 				 * attach to that device.
8708 				 */
8709 				scsi_log(SD_DEVINFO(un),
8710 				    sd_label, CE_WARN,
8711 				    "disk capacity is too large "
8712 				    "for current cdb length");
8713 				goto spinup_failed;
8714 			case EACCES:
8715 				/*
8716 				 * Should never get here if the spin-up
8717 				 * succeeded, but code it in anyway.
8718 				 * From here, just continue with the attach...
8719 				 */
8720 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8721 				    "sd_unit_attach: un:0x%p "
8722 				    "sd_send_scsi_READ_CAPACITY "
8723 				    "returned reservation conflict\n", un);
8724 				reservation_flag = SD_TARGET_IS_RESERVED;
8725 				break;
8726 			default:
8727 				/*
8728 				 * Likewise, should never get here if the
8729 				 * spin-up succeeded. Just continue with
8730 				 * the attach...
8731 				 */
8732 				break;
8733 			}
8734 			break;
8735 		case EACCES:
8736 			/*
8737 			 * Device is reserved by another host.  In this case
8738 			 * we could not spin it up or read the capacity, but
8739 			 * we continue with the attach anyway.
8740 			 */
8741 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8742 			    "sd_unit_attach: un:0x%p spin-up reservation "
8743 			    "conflict.\n", un);
8744 			reservation_flag = SD_TARGET_IS_RESERVED;
8745 			break;
8746 		default:
8747 			/* Fail the attach if the spin-up failed. */
8748 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8749 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8750 			goto spinup_failed;
8751 		}
8752 	}
8753 
8754 	/*
8755 	 * Check to see if this is a MMC drive
8756 	 */
8757 	if (ISCD(un)) {
8758 		sd_set_mmc_caps(un);
8759 	}
8760 
8761 	/*
8762 	 * Create the minor nodes for the device.
8763 	 * Note: If we want to support fdisk on both sparc and intel, this will
8764 	 * have to separate out the notion that VTOC8 is always sparc, and
8765 	 * VTOC16 is always intel (tho these can be the defaults).  The vtoc
8766 	 * type will have to be determined at run-time, and the fdisk
8767 	 * partitioning will have to have been read & set up before we
8768 	 * create the minor nodes. (any other inits (such as kstats) that
8769 	 * also ought to be done before creating the minor nodes?) (Doesn't
8770 	 * setting up the minor nodes kind of imply that we're ready to
8771 	 * handle an open from userland?)
8772 	 */
8773 	if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) {
8774 		goto create_minor_nodes_failed;
8775 	}
8776 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8777 	    "sd_unit_attach: un:0x%p minor nodes created\n", un);
8778 
8779 	/*
8780 	 * Add a zero-length attribute to tell the world we support
8781 	 * kernel ioctls (for layered drivers)
8782 	 */
8783 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8784 	    DDI_KERNEL_IOCTL, NULL, 0);
8785 
8786 	/*
8787 	 * Add a boolean property to tell the world we support
8788 	 * the B_FAILFAST flag (for layered drivers)
8789 	 */
8790 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8791 	    "ddi-failfast-supported", NULL, 0);
8792 
8793 	/*
8794 	 * Initialize power management
8795 	 */
8796 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8797 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8798 	sd_setup_pm(un, devi);
8799 	if (un->un_f_pm_is_enabled == FALSE) {
8800 		/*
8801 		 * For performance, point to a jump table that does
8802 		 * not include pm.
8803 		 * The direct and priority chains don't change with PM.
8804 		 *
8805 		 * Note: this is currently done based on individual device
8806 		 * capabilities. When an interface for determining system
8807 		 * power enabled state becomes available, or when additional
8808 		 * layers are added to the command chain, these values will
8809 		 * have to be re-evaluated for correctness.
8810 		 */
8811 		if (un->un_f_non_devbsize_supported) {
8812 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8813 		} else {
8814 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8815 		}
8816 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8817 	}
8818 
8819 	/*
8820 	 * This property is set to 0 by HA software to avoid retries
8821 	 * on a reserved disk. (The preferred property name is
8822 	 * "retry-on-reservation-conflict") (1189689)
8823 	 *
8824 	 * Note: The use of a global here can have unintended consequences. A
8825 	 * per instance variable is preferrable to match the capabilities of
8826 	 * different underlying hba's (4402600)
8827 	 */
8828 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8829 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8830 	    sd_retry_on_reservation_conflict);
8831 	if (sd_retry_on_reservation_conflict != 0) {
8832 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8833 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8834 		    sd_retry_on_reservation_conflict);
8835 	}
8836 
8837 	/* Set up options for QFULL handling. */
8838 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8839 	    "qfull-retries", -1)) != -1) {
8840 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8841 		    rval, 1);
8842 	}
8843 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8844 	    "qfull-retry-interval", -1)) != -1) {
8845 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8846 		    rval, 1);
8847 	}
8848 
8849 	/*
8850 	 * This just prints a message that announces the existence of the
8851 	 * device. The message is always printed in the system logfile, but
8852 	 * only appears on the console if the system is booted with the
8853 	 * -v (verbose) argument.
8854 	 */
8855 	ddi_report_dev(devi);
8856 
8857 	/*
8858 	 * The framework calls driver attach routines single-threaded
8859 	 * for a given instance.  However we still acquire SD_MUTEX here
8860 	 * because this required for calling the sd_validate_geometry()
8861 	 * and sd_register_devid() functions.
8862 	 */
8863 	mutex_enter(SD_MUTEX(un));
8864 	un->un_f_geometry_is_valid = FALSE;
8865 	un->un_mediastate = DKIO_NONE;
8866 	un->un_reserved = -1;
8867 
8868 	/*
8869 	 * Read and validate the device's geometry (ie, disk label)
8870 	 * A new unformatted drive will not have a valid geometry, but
8871 	 * the driver needs to successfully attach to this device so
8872 	 * the drive can be formatted via ioctls.
8873 	 */
8874 	if (((sd_validate_geometry(un, SD_PATH_DIRECT) ==
8875 	    ENOTSUP)) &&
8876 	    (un->un_blockcount < DK_MAX_BLOCKS)) {
8877 		/*
8878 		 * We found a small disk with an EFI label on it;
8879 		 * we need to fix up the minor nodes accordingly.
8880 		 */
8881 		ddi_remove_minor_node(devi, "h");
8882 		ddi_remove_minor_node(devi, "h,raw");
8883 		(void) ddi_create_minor_node(devi, "wd",
8884 		    S_IFBLK,
8885 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8886 		    un->un_node_type, NULL);
8887 		(void) ddi_create_minor_node(devi, "wd,raw",
8888 		    S_IFCHR,
8889 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8890 		    un->un_node_type, NULL);
8891 	}
8892 #if defined(__i386) || defined(__amd64)
8893 	else if (un->un_f_capacity_adjusted == 1) {
8894 		/*
8895 		 * Refer to comments related to off-by-1 at the
8896 		 * header of this file.
8897 		 * Adjust minor node for 1TB disk.
8898 		 */
8899 		ddi_remove_minor_node(devi, "wd");
8900 		ddi_remove_minor_node(devi, "wd,raw");
8901 		(void) ddi_create_minor_node(devi, "h",
8902 		    S_IFBLK,
8903 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8904 		    un->un_node_type, NULL);
8905 		(void) ddi_create_minor_node(devi, "h,raw",
8906 		    S_IFCHR,
8907 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8908 		    un->un_node_type, NULL);
8909 	}
8910 #endif
8911 	/*
8912 	 * Read and initialize the devid for the unit.
8913 	 */
8914 	ASSERT(un->un_errstats != NULL);
8915 	if (un->un_f_devid_supported) {
8916 		sd_register_devid(un, devi, reservation_flag);
8917 	}
8918 	mutex_exit(SD_MUTEX(un));
8919 
8920 #if (defined(__fibre))
8921 	/*
8922 	 * Register callbacks for fibre only.  You can't do this soley
8923 	 * on the basis of the devid_type because this is hba specific.
8924 	 * We need to query our hba capabilities to find out whether to
8925 	 * register or not.
8926 	 */
8927 	if (un->un_f_is_fibre) {
8928 	    if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8929 		sd_init_event_callbacks(un);
8930 		SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8931 		    "sd_unit_attach: un:0x%p event callbacks inserted", un);
8932 	    }
8933 	}
8934 #endif
8935 
8936 	if (un->un_f_opt_disable_cache == TRUE) {
8937 		/*
8938 		 * Disable both read cache and write cache.  This is
8939 		 * the historic behavior of the keywords in the config file.
8940 		 */
8941 		if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8942 		    0) {
8943 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8944 			    "sd_unit_attach: un:0x%p Could not disable "
8945 			    "caching", un);
8946 			goto devid_failed;
8947 		}
8948 	}
8949 
8950 	/*
8951 	 * Check the value of the WCE bit now and
8952 	 * set un_f_write_cache_enabled accordingly.
8953 	 */
8954 	(void) sd_get_write_cache_enabled(un, &wc_enabled);
8955 	mutex_enter(SD_MUTEX(un));
8956 	un->un_f_write_cache_enabled = (wc_enabled != 0);
8957 	mutex_exit(SD_MUTEX(un));
8958 
8959 	/*
8960 	 * Set the pstat and error stat values here, so data obtained during the
8961 	 * previous attach-time routines is available.
8962 	 *
8963 	 * Note: This is a critical sequence that needs to be maintained:
8964 	 *	1) Instantiate the kstats before any routines using the iopath
8965 	 *	   (i.e. sd_send_scsi_cmd).
8966 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8967 	 *	   stats (sd_set_pstats)here, following sd_validate_geometry(),
8968 	 *	   sd_register_devid(), and sd_cache_control().
8969 	 */
8970 	if (un->un_f_pkstats_enabled) {
8971 		sd_set_pstats(un);
8972 		SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8973 		    "sd_unit_attach: un:0x%p pstats created and set\n", un);
8974 	}
8975 
8976 	sd_set_errstats(un);
8977 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8978 	    "sd_unit_attach: un:0x%p errstats set\n", un);
8979 
8980 	/*
8981 	 * Find out what type of reservation this disk supports.
8982 	 */
8983 	switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) {
8984 	case 0:
8985 		/*
8986 		 * SCSI-3 reservations are supported.
8987 		 */
8988 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8989 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8990 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8991 		break;
8992 	case ENOTSUP:
8993 		/*
8994 		 * The PERSISTENT RESERVE IN command would not be recognized by
8995 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8996 		 */
8997 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8998 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8999 		un->un_reservation_type = SD_SCSI2_RESERVATION;
9000 		break;
9001 	default:
9002 		/*
9003 		 * default to SCSI-3 reservations
9004 		 */
9005 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
9006 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
9007 		un->un_reservation_type = SD_SCSI3_RESERVATION;
9008 		break;
9009 	}
9010 
9011 	/*
9012 	 * After successfully attaching an instance, we record the information
9013 	 * of how many luns have been attached on the relative target and
9014 	 * controller for parallel SCSI. This information is used when sd tries
9015 	 * to set the tagged queuing capability in HBA.
9016 	 */
9017 	if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9018 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
9019 	}
9020 
9021 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
9022 	    "sd_unit_attach: un:0x%p exit success\n", un);
9023 
9024 	return (DDI_SUCCESS);
9025 
9026 	/*
9027 	 * An error occurred during the attach; clean up & return failure.
9028 	 */
9029 
9030 devid_failed:
9031 
9032 setup_pm_failed:
9033 	ddi_remove_minor_node(devi, NULL);
9034 
9035 create_minor_nodes_failed:
9036 	/*
9037 	 * Cleanup from the scsi_ifsetcap() calls (437868)
9038 	 */
9039 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
9040 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
9041 
9042 	/*
9043 	 * Refer to the comments of setting tagged-qing in the beginning of
9044 	 * sd_unit_attach. We can only disable tagged queuing when there is
9045 	 * no lun attached on the target.
9046 	 */
9047 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
9048 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
9049 	}
9050 
9051 	if (un->un_f_is_fibre == FALSE) {
9052 	    (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
9053 	}
9054 
9055 spinup_failed:
9056 
9057 	mutex_enter(SD_MUTEX(un));
9058 
9059 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
9060 	if (un->un_direct_priority_timeid != NULL) {
9061 		timeout_id_t temp_id = un->un_direct_priority_timeid;
9062 		un->un_direct_priority_timeid = NULL;
9063 		mutex_exit(SD_MUTEX(un));
9064 		(void) untimeout(temp_id);
9065 		mutex_enter(SD_MUTEX(un));
9066 	}
9067 
9068 	/* Cancel any pending start/stop timeouts */
9069 	if (un->un_startstop_timeid != NULL) {
9070 		timeout_id_t temp_id = un->un_startstop_timeid;
9071 		un->un_startstop_timeid = NULL;
9072 		mutex_exit(SD_MUTEX(un));
9073 		(void) untimeout(temp_id);
9074 		mutex_enter(SD_MUTEX(un));
9075 	}
9076 
9077 	/* Cancel any pending reset-throttle timeouts */
9078 	if (un->un_reset_throttle_timeid != NULL) {
9079 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
9080 		un->un_reset_throttle_timeid = NULL;
9081 		mutex_exit(SD_MUTEX(un));
9082 		(void) untimeout(temp_id);
9083 		mutex_enter(SD_MUTEX(un));
9084 	}
9085 
9086 	/* Cancel any pending retry timeouts */
9087 	if (un->un_retry_timeid != NULL) {
9088 		timeout_id_t temp_id = un->un_retry_timeid;
9089 		un->un_retry_timeid = NULL;
9090 		mutex_exit(SD_MUTEX(un));
9091 		(void) untimeout(temp_id);
9092 		mutex_enter(SD_MUTEX(un));
9093 	}
9094 
9095 	/* Cancel any pending delayed cv broadcast timeouts */
9096 	if (un->un_dcvb_timeid != NULL) {
9097 		timeout_id_t temp_id = un->un_dcvb_timeid;
9098 		un->un_dcvb_timeid = NULL;
9099 		mutex_exit(SD_MUTEX(un));
9100 		(void) untimeout(temp_id);
9101 		mutex_enter(SD_MUTEX(un));
9102 	}
9103 
9104 	mutex_exit(SD_MUTEX(un));
9105 
9106 	/* There should not be any in-progress I/O so ASSERT this check */
9107 	ASSERT(un->un_ncmds_in_transport == 0);
9108 	ASSERT(un->un_ncmds_in_driver == 0);
9109 
9110 	/* Do not free the softstate if the callback routine is active */
9111 	sd_sync_with_callback(un);
9112 
9113 	/*
9114 	 * Partition stats apparently are not used with removables. These would
9115 	 * not have been created during attach, so no need to clean them up...
9116 	 */
9117 	if (un->un_stats != NULL) {
9118 		kstat_delete(un->un_stats);
9119 		un->un_stats = NULL;
9120 	}
9121 	if (un->un_errstats != NULL) {
9122 		kstat_delete(un->un_errstats);
9123 		un->un_errstats = NULL;
9124 	}
9125 
9126 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
9127 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
9128 
9129 	ddi_prop_remove_all(devi);
9130 	sema_destroy(&un->un_semoclose);
9131 	cv_destroy(&un->un_state_cv);
9132 
9133 getrbuf_failed:
9134 
9135 	sd_free_rqs(un);
9136 
9137 alloc_rqs_failed:
9138 
9139 	devp->sd_private = NULL;
9140 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
9141 
9142 get_softstate_failed:
9143 	/*
9144 	 * Note: the man pages are unclear as to whether or not doing a
9145 	 * ddi_soft_state_free(sd_state, instance) is the right way to
9146 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
9147 	 * ddi_get_soft_state() fails.  The implication seems to be
9148 	 * that the get_soft_state cannot fail if the zalloc succeeds.
9149 	 */
9150 	ddi_soft_state_free(sd_state, instance);
9151 
9152 probe_failed:
9153 	scsi_unprobe(devp);
9154 #ifdef SDDEBUG
9155 	if ((sd_component_mask & SD_LOG_ATTACH_DETACH) &&
9156 	    (sd_level_mask & SD_LOGMASK_TRACE)) {
9157 		cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n",
9158 		    (void *)un);
9159 	}
9160 #endif
9161 	return (DDI_FAILURE);
9162 }
9163 
9164 
9165 /*
9166  *    Function: sd_unit_detach
9167  *
9168  * Description: Performs DDI_DETACH processing for sddetach().
9169  *
9170  * Return Code: DDI_SUCCESS
9171  *		DDI_FAILURE
9172  *
9173  *     Context: Kernel thread context
9174  */
9175 
9176 static int
9177 sd_unit_detach(dev_info_t *devi)
9178 {
9179 	struct scsi_device	*devp;
9180 	struct sd_lun		*un;
9181 	int			i;
9182 	int			tgt;
9183 	dev_t			dev;
9184 	dev_info_t		*pdip = ddi_get_parent(devi);
9185 	int			instance = ddi_get_instance(devi);
9186 
9187 	mutex_enter(&sd_detach_mutex);
9188 
9189 	/*
9190 	 * Fail the detach for any of the following:
9191 	 *  - Unable to get the sd_lun struct for the instance
9192 	 *  - A layered driver has an outstanding open on the instance
9193 	 *  - Another thread is already detaching this instance
9194 	 *  - Another thread is currently performing an open
9195 	 */
9196 	devp = ddi_get_driver_private(devi);
9197 	if ((devp == NULL) ||
9198 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
9199 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
9200 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
9201 		mutex_exit(&sd_detach_mutex);
9202 		return (DDI_FAILURE);
9203 	}
9204 
9205 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
9206 
9207 	/*
9208 	 * Mark this instance as currently in a detach, to inhibit any
9209 	 * opens from a layered driver.
9210 	 */
9211 	un->un_detach_count++;
9212 	mutex_exit(&sd_detach_mutex);
9213 
9214 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
9215 	    SCSI_ADDR_PROP_TARGET, -1);
9216 
9217 	dev = sd_make_device(SD_DEVINFO(un));
9218 
9219 	_NOTE(COMPETING_THREADS_NOW);
9220 
9221 	mutex_enter(SD_MUTEX(un));
9222 
9223 	/*
9224 	 * Fail the detach if there are any outstanding layered
9225 	 * opens on this device.
9226 	 */
9227 	for (i = 0; i < NDKMAP; i++) {
9228 		if (un->un_ocmap.lyropen[i] != 0) {
9229 			goto err_notclosed;
9230 		}
9231 	}
9232 
9233 	/*
9234 	 * Verify there are NO outstanding commands issued to this device.
9235 	 * ie, un_ncmds_in_transport == 0.
9236 	 * It's possible to have outstanding commands through the physio
9237 	 * code path, even though everything's closed.
9238 	 */
9239 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
9240 	    (un->un_direct_priority_timeid != NULL) ||
9241 	    (un->un_state == SD_STATE_RWAIT)) {
9242 		mutex_exit(SD_MUTEX(un));
9243 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9244 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
9245 		goto err_stillbusy;
9246 	}
9247 
9248 	/*
9249 	 * If we have the device reserved, release the reservation.
9250 	 */
9251 	if ((un->un_resvd_status & SD_RESERVE) &&
9252 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
9253 		mutex_exit(SD_MUTEX(un));
9254 		/*
9255 		 * Note: sd_reserve_release sends a command to the device
9256 		 * via the sd_ioctlcmd() path, and can sleep.
9257 		 */
9258 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
9259 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9260 			    "sd_dr_detach: Cannot release reservation \n");
9261 		}
9262 	} else {
9263 		mutex_exit(SD_MUTEX(un));
9264 	}
9265 
9266 	/*
9267 	 * Untimeout any reserve recover, throttle reset, restart unit
9268 	 * and delayed broadcast timeout threads. Protect the timeout pointer
9269 	 * from getting nulled by their callback functions.
9270 	 */
9271 	mutex_enter(SD_MUTEX(un));
9272 	if (un->un_resvd_timeid != NULL) {
9273 		timeout_id_t temp_id = un->un_resvd_timeid;
9274 		un->un_resvd_timeid = NULL;
9275 		mutex_exit(SD_MUTEX(un));
9276 		(void) untimeout(temp_id);
9277 		mutex_enter(SD_MUTEX(un));
9278 	}
9279 
9280 	if (un->un_reset_throttle_timeid != NULL) {
9281 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
9282 		un->un_reset_throttle_timeid = NULL;
9283 		mutex_exit(SD_MUTEX(un));
9284 		(void) untimeout(temp_id);
9285 		mutex_enter(SD_MUTEX(un));
9286 	}
9287 
9288 	if (un->un_startstop_timeid != NULL) {
9289 		timeout_id_t temp_id = un->un_startstop_timeid;
9290 		un->un_startstop_timeid = NULL;
9291 		mutex_exit(SD_MUTEX(un));
9292 		(void) untimeout(temp_id);
9293 		mutex_enter(SD_MUTEX(un));
9294 	}
9295 
9296 	if (un->un_dcvb_timeid != NULL) {
9297 		timeout_id_t temp_id = un->un_dcvb_timeid;
9298 		un->un_dcvb_timeid = NULL;
9299 		mutex_exit(SD_MUTEX(un));
9300 		(void) untimeout(temp_id);
9301 	} else {
9302 		mutex_exit(SD_MUTEX(un));
9303 	}
9304 
9305 	/* Remove any pending reservation reclaim requests for this device */
9306 	sd_rmv_resv_reclaim_req(dev);
9307 
9308 	mutex_enter(SD_MUTEX(un));
9309 
9310 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
9311 	if (un->un_direct_priority_timeid != NULL) {
9312 		timeout_id_t temp_id = un->un_direct_priority_timeid;
9313 		un->un_direct_priority_timeid = NULL;
9314 		mutex_exit(SD_MUTEX(un));
9315 		(void) untimeout(temp_id);
9316 		mutex_enter(SD_MUTEX(un));
9317 	}
9318 
9319 	/* Cancel any active multi-host disk watch thread requests */
9320 	if (un->un_mhd_token != NULL) {
9321 		mutex_exit(SD_MUTEX(un));
9322 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
9323 		if (scsi_watch_request_terminate(un->un_mhd_token,
9324 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
9325 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9326 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
9327 			/*
9328 			 * Note: We are returning here after having removed
9329 			 * some driver timeouts above. This is consistent with
9330 			 * the legacy implementation but perhaps the watch
9331 			 * terminate call should be made with the wait flag set.
9332 			 */
9333 			goto err_stillbusy;
9334 		}
9335 		mutex_enter(SD_MUTEX(un));
9336 		un->un_mhd_token = NULL;
9337 	}
9338 
9339 	if (un->un_swr_token != NULL) {
9340 		mutex_exit(SD_MUTEX(un));
9341 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
9342 		if (scsi_watch_request_terminate(un->un_swr_token,
9343 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
9344 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9345 			    "sd_dr_detach: Cannot cancel swr watch request\n");
9346 			/*
9347 			 * Note: We are returning here after having removed
9348 			 * some driver timeouts above. This is consistent with
9349 			 * the legacy implementation but perhaps the watch
9350 			 * terminate call should be made with the wait flag set.
9351 			 */
9352 			goto err_stillbusy;
9353 		}
9354 		mutex_enter(SD_MUTEX(un));
9355 		un->un_swr_token = NULL;
9356 	}
9357 
9358 	mutex_exit(SD_MUTEX(un));
9359 
9360 	/*
9361 	 * Clear any scsi_reset_notifies. We clear the reset notifies
9362 	 * if we have not registered one.
9363 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
9364 	 */
9365 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
9366 	    sd_mhd_reset_notify_cb, (caddr_t)un);
9367 
9368 	/*
9369 	 * protect the timeout pointers from getting nulled by
9370 	 * their callback functions during the cancellation process.
9371 	 * In such a scenario untimeout can be invoked with a null value.
9372 	 */
9373 	_NOTE(NO_COMPETING_THREADS_NOW);
9374 
9375 	mutex_enter(&un->un_pm_mutex);
9376 	if (un->un_pm_idle_timeid != NULL) {
9377 		timeout_id_t temp_id = un->un_pm_idle_timeid;
9378 		un->un_pm_idle_timeid = NULL;
9379 		mutex_exit(&un->un_pm_mutex);
9380 
9381 		/*
9382 		 * Timeout is active; cancel it.
9383 		 * Note that it'll never be active on a device
9384 		 * that does not support PM therefore we don't
9385 		 * have to check before calling pm_idle_component.
9386 		 */
9387 		(void) untimeout(temp_id);
9388 		(void) pm_idle_component(SD_DEVINFO(un), 0);
9389 		mutex_enter(&un->un_pm_mutex);
9390 	}
9391 
9392 	/*
9393 	 * Check whether there is already a timeout scheduled for power
9394 	 * management. If yes then don't lower the power here, that's.
9395 	 * the timeout handler's job.
9396 	 */
9397 	if (un->un_pm_timeid != NULL) {
9398 		timeout_id_t temp_id = un->un_pm_timeid;
9399 		un->un_pm_timeid = NULL;
9400 		mutex_exit(&un->un_pm_mutex);
9401 		/*
9402 		 * Timeout is active; cancel it.
9403 		 * Note that it'll never be active on a device
9404 		 * that does not support PM therefore we don't
9405 		 * have to check before calling pm_idle_component.
9406 		 */
9407 		(void) untimeout(temp_id);
9408 		(void) pm_idle_component(SD_DEVINFO(un), 0);
9409 
9410 	} else {
9411 		mutex_exit(&un->un_pm_mutex);
9412 		if ((un->un_f_pm_is_enabled == TRUE) &&
9413 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) !=
9414 		    DDI_SUCCESS)) {
9415 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9416 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
9417 			/*
9418 			 * Fix for bug: 4297749, item # 13
9419 			 * The above test now includes a check to see if PM is
9420 			 * supported by this device before call
9421 			 * pm_lower_power().
9422 			 * Note, the following is not dead code. The call to
9423 			 * pm_lower_power above will generate a call back into
9424 			 * our sdpower routine which might result in a timeout
9425 			 * handler getting activated. Therefore the following
9426 			 * code is valid and necessary.
9427 			 */
9428 			mutex_enter(&un->un_pm_mutex);
9429 			if (un->un_pm_timeid != NULL) {
9430 				timeout_id_t temp_id = un->un_pm_timeid;
9431 				un->un_pm_timeid = NULL;
9432 				mutex_exit(&un->un_pm_mutex);
9433 				(void) untimeout(temp_id);
9434 				(void) pm_idle_component(SD_DEVINFO(un), 0);
9435 			} else {
9436 				mutex_exit(&un->un_pm_mutex);
9437 			}
9438 		}
9439 	}
9440 
9441 	/*
9442 	 * Cleanup from the scsi_ifsetcap() calls (437868)
9443 	 * Relocated here from above to be after the call to
9444 	 * pm_lower_power, which was getting errors.
9445 	 */
9446 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
9447 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
9448 
9449 	/*
9450 	 * Currently, tagged queuing is supported per target based by HBA.
9451 	 * Setting this per lun instance actually sets the capability of this
9452 	 * target in HBA, which affects those luns already attached on the
9453 	 * same target. So during detach, we can only disable this capability
9454 	 * only when this is the only lun left on this target. By doing
9455 	 * this, we assume a target has the same tagged queuing capability
9456 	 * for every lun. The condition can be removed when HBA is changed to
9457 	 * support per lun based tagged queuing capability.
9458 	 */
9459 	if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
9460 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
9461 	}
9462 
9463 	if (un->un_f_is_fibre == FALSE) {
9464 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
9465 	}
9466 
9467 	/*
9468 	 * Remove any event callbacks, fibre only
9469 	 */
9470 	if (un->un_f_is_fibre == TRUE) {
9471 		if ((un->un_insert_event != NULL) &&
9472 			(ddi_remove_event_handler(un->un_insert_cb_id) !=
9473 				DDI_SUCCESS)) {
9474 			/*
9475 			 * Note: We are returning here after having done
9476 			 * substantial cleanup above. This is consistent
9477 			 * with the legacy implementation but this may not
9478 			 * be the right thing to do.
9479 			 */
9480 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9481 				"sd_dr_detach: Cannot cancel insert event\n");
9482 			goto err_remove_event;
9483 		}
9484 		un->un_insert_event = NULL;
9485 
9486 		if ((un->un_remove_event != NULL) &&
9487 			(ddi_remove_event_handler(un->un_remove_cb_id) !=
9488 				DDI_SUCCESS)) {
9489 			/*
9490 			 * Note: We are returning here after having done
9491 			 * substantial cleanup above. This is consistent
9492 			 * with the legacy implementation but this may not
9493 			 * be the right thing to do.
9494 			 */
9495 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9496 				"sd_dr_detach: Cannot cancel remove event\n");
9497 			goto err_remove_event;
9498 		}
9499 		un->un_remove_event = NULL;
9500 	}
9501 
9502 	/* Do not free the softstate if the callback routine is active */
9503 	sd_sync_with_callback(un);
9504 
9505 	/*
9506 	 * Hold the detach mutex here, to make sure that no other threads ever
9507 	 * can access a (partially) freed soft state structure.
9508 	 */
9509 	mutex_enter(&sd_detach_mutex);
9510 
9511 	/*
9512 	 * Clean up the soft state struct.
9513 	 * Cleanup is done in reverse order of allocs/inits.
9514 	 * At this point there should be no competing threads anymore.
9515 	 */
9516 
9517 	/* Unregister and free device id. */
9518 	ddi_devid_unregister(devi);
9519 	if (un->un_devid) {
9520 		ddi_devid_free(un->un_devid);
9521 		un->un_devid = NULL;
9522 	}
9523 
9524 	/*
9525 	 * Destroy wmap cache if it exists.
9526 	 */
9527 	if (un->un_wm_cache != NULL) {
9528 		kmem_cache_destroy(un->un_wm_cache);
9529 		un->un_wm_cache = NULL;
9530 	}
9531 
9532 	/* Remove minor nodes */
9533 	ddi_remove_minor_node(devi, NULL);
9534 
9535 	/*
9536 	 * kstat cleanup is done in detach for all device types (4363169).
9537 	 * We do not want to fail detach if the device kstats are not deleted
9538 	 * since there is a confusion about the devo_refcnt for the device.
9539 	 * We just delete the kstats and let detach complete successfully.
9540 	 */
9541 	if (un->un_stats != NULL) {
9542 		kstat_delete(un->un_stats);
9543 		un->un_stats = NULL;
9544 	}
9545 	if (un->un_errstats != NULL) {
9546 		kstat_delete(un->un_errstats);
9547 		un->un_errstats = NULL;
9548 	}
9549 
9550 	/* Remove partition stats */
9551 	if (un->un_f_pkstats_enabled) {
9552 		for (i = 0; i < NSDMAP; i++) {
9553 			if (un->un_pstats[i] != NULL) {
9554 				kstat_delete(un->un_pstats[i]);
9555 				un->un_pstats[i] = NULL;
9556 			}
9557 		}
9558 	}
9559 
9560 	/* Remove xbuf registration */
9561 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
9562 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
9563 
9564 	/* Remove driver properties */
9565 	ddi_prop_remove_all(devi);
9566 
9567 	mutex_destroy(&un->un_pm_mutex);
9568 	cv_destroy(&un->un_pm_busy_cv);
9569 
9570 	cv_destroy(&un->un_wcc_cv);
9571 
9572 	/* Open/close semaphore */
9573 	sema_destroy(&un->un_semoclose);
9574 
9575 	/* Removable media condvar. */
9576 	cv_destroy(&un->un_state_cv);
9577 
9578 	/* Suspend/resume condvar. */
9579 	cv_destroy(&un->un_suspend_cv);
9580 	cv_destroy(&un->un_disk_busy_cv);
9581 
9582 	sd_free_rqs(un);
9583 
9584 	/* Free up soft state */
9585 	devp->sd_private = NULL;
9586 	bzero(un, sizeof (struct sd_lun));
9587 	ddi_soft_state_free(sd_state, instance);
9588 
9589 	mutex_exit(&sd_detach_mutex);
9590 
9591 	/* This frees up the INQUIRY data associated with the device. */
9592 	scsi_unprobe(devp);
9593 
9594 	/*
9595 	 * After successfully detaching an instance, we update the information
9596 	 * of how many luns have been attached in the relative target and
9597 	 * controller for parallel SCSI. This information is used when sd tries
9598 	 * to set the tagged queuing capability in HBA.
9599 	 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
9600 	 * check if the device is parallel SCSI. However, we don't need to
9601 	 * check here because we've already checked during attach. No device
9602 	 * that is not parallel SCSI is in the chain.
9603 	 */
9604 	if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9605 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
9606 	}
9607 
9608 	return (DDI_SUCCESS);
9609 
9610 err_notclosed:
9611 	mutex_exit(SD_MUTEX(un));
9612 
9613 err_stillbusy:
9614 	_NOTE(NO_COMPETING_THREADS_NOW);
9615 
9616 err_remove_event:
9617 	mutex_enter(&sd_detach_mutex);
9618 	un->un_detach_count--;
9619 	mutex_exit(&sd_detach_mutex);
9620 
9621 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9622 	return (DDI_FAILURE);
9623 }
9624 
9625 
9626 /*
9627  * Driver minor node structure and data table
9628  */
9629 struct driver_minor_data {
9630 	char	*name;
9631 	minor_t	minor;
9632 	int	type;
9633 };
9634 
9635 static struct driver_minor_data sd_minor_data[] = {
9636 	{"a", 0, S_IFBLK},
9637 	{"b", 1, S_IFBLK},
9638 	{"c", 2, S_IFBLK},
9639 	{"d", 3, S_IFBLK},
9640 	{"e", 4, S_IFBLK},
9641 	{"f", 5, S_IFBLK},
9642 	{"g", 6, S_IFBLK},
9643 	{"h", 7, S_IFBLK},
9644 #if defined(_SUNOS_VTOC_16)
9645 	{"i", 8, S_IFBLK},
9646 	{"j", 9, S_IFBLK},
9647 	{"k", 10, S_IFBLK},
9648 	{"l", 11, S_IFBLK},
9649 	{"m", 12, S_IFBLK},
9650 	{"n", 13, S_IFBLK},
9651 	{"o", 14, S_IFBLK},
9652 	{"p", 15, S_IFBLK},
9653 #endif			/* defined(_SUNOS_VTOC_16) */
9654 #if defined(_FIRMWARE_NEEDS_FDISK)
9655 	{"q", 16, S_IFBLK},
9656 	{"r", 17, S_IFBLK},
9657 	{"s", 18, S_IFBLK},
9658 	{"t", 19, S_IFBLK},
9659 	{"u", 20, S_IFBLK},
9660 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9661 	{"a,raw", 0, S_IFCHR},
9662 	{"b,raw", 1, S_IFCHR},
9663 	{"c,raw", 2, S_IFCHR},
9664 	{"d,raw", 3, S_IFCHR},
9665 	{"e,raw", 4, S_IFCHR},
9666 	{"f,raw", 5, S_IFCHR},
9667 	{"g,raw", 6, S_IFCHR},
9668 	{"h,raw", 7, S_IFCHR},
9669 #if defined(_SUNOS_VTOC_16)
9670 	{"i,raw", 8, S_IFCHR},
9671 	{"j,raw", 9, S_IFCHR},
9672 	{"k,raw", 10, S_IFCHR},
9673 	{"l,raw", 11, S_IFCHR},
9674 	{"m,raw", 12, S_IFCHR},
9675 	{"n,raw", 13, S_IFCHR},
9676 	{"o,raw", 14, S_IFCHR},
9677 	{"p,raw", 15, S_IFCHR},
9678 #endif			/* defined(_SUNOS_VTOC_16) */
9679 #if defined(_FIRMWARE_NEEDS_FDISK)
9680 	{"q,raw", 16, S_IFCHR},
9681 	{"r,raw", 17, S_IFCHR},
9682 	{"s,raw", 18, S_IFCHR},
9683 	{"t,raw", 19, S_IFCHR},
9684 	{"u,raw", 20, S_IFCHR},
9685 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9686 	{0}
9687 };
9688 
9689 static struct driver_minor_data sd_minor_data_efi[] = {
9690 	{"a", 0, S_IFBLK},
9691 	{"b", 1, S_IFBLK},
9692 	{"c", 2, S_IFBLK},
9693 	{"d", 3, S_IFBLK},
9694 	{"e", 4, S_IFBLK},
9695 	{"f", 5, S_IFBLK},
9696 	{"g", 6, S_IFBLK},
9697 	{"wd", 7, S_IFBLK},
9698 #if defined(_FIRMWARE_NEEDS_FDISK)
9699 	{"q", 16, S_IFBLK},
9700 	{"r", 17, S_IFBLK},
9701 	{"s", 18, S_IFBLK},
9702 	{"t", 19, S_IFBLK},
9703 	{"u", 20, S_IFBLK},
9704 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9705 	{"a,raw", 0, S_IFCHR},
9706 	{"b,raw", 1, S_IFCHR},
9707 	{"c,raw", 2, S_IFCHR},
9708 	{"d,raw", 3, S_IFCHR},
9709 	{"e,raw", 4, S_IFCHR},
9710 	{"f,raw", 5, S_IFCHR},
9711 	{"g,raw", 6, S_IFCHR},
9712 	{"wd,raw", 7, S_IFCHR},
9713 #if defined(_FIRMWARE_NEEDS_FDISK)
9714 	{"q,raw", 16, S_IFCHR},
9715 	{"r,raw", 17, S_IFCHR},
9716 	{"s,raw", 18, S_IFCHR},
9717 	{"t,raw", 19, S_IFCHR},
9718 	{"u,raw", 20, S_IFCHR},
9719 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9720 	{0}
9721 };
9722 
9723 
9724 /*
9725  *    Function: sd_create_minor_nodes
9726  *
9727  * Description: Create the minor device nodes for the instance.
9728  *
9729  *   Arguments: un - driver soft state (unit) structure
9730  *		devi - pointer to device info structure
9731  *
9732  * Return Code: DDI_SUCCESS
9733  *		DDI_FAILURE
9734  *
9735  *     Context: Kernel thread context
9736  */
9737 
9738 static int
9739 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi)
9740 {
9741 	struct driver_minor_data	*dmdp;
9742 	struct scsi_device		*devp;
9743 	int				instance;
9744 	char				name[48];
9745 
9746 	ASSERT(un != NULL);
9747 	devp = ddi_get_driver_private(devi);
9748 	instance = ddi_get_instance(devp->sd_dev);
9749 
9750 	/*
9751 	 * Create all the minor nodes for this target.
9752 	 */
9753 	if (un->un_blockcount > DK_MAX_BLOCKS)
9754 		dmdp = sd_minor_data_efi;
9755 	else
9756 		dmdp = sd_minor_data;
9757 	while (dmdp->name != NULL) {
9758 
9759 		(void) sprintf(name, "%s", dmdp->name);
9760 
9761 		if (ddi_create_minor_node(devi, name, dmdp->type,
9762 		    (instance << SDUNIT_SHIFT) | dmdp->minor,
9763 		    un->un_node_type, NULL) == DDI_FAILURE) {
9764 			/*
9765 			 * Clean up any nodes that may have been created, in
9766 			 * case this fails in the middle of the loop.
9767 			 */
9768 			ddi_remove_minor_node(devi, NULL);
9769 			return (DDI_FAILURE);
9770 		}
9771 		dmdp++;
9772 	}
9773 
9774 	return (DDI_SUCCESS);
9775 }
9776 
9777 
9778 /*
9779  *    Function: sd_create_errstats
9780  *
9781  * Description: This routine instantiates the device error stats.
9782  *
9783  *		Note: During attach the stats are instantiated first so they are
9784  *		available for attach-time routines that utilize the driver
9785  *		iopath to send commands to the device. The stats are initialized
9786  *		separately so data obtained during some attach-time routines is
9787  *		available. (4362483)
9788  *
9789  *   Arguments: un - driver soft state (unit) structure
9790  *		instance - driver instance
9791  *
9792  *     Context: Kernel thread context
9793  */
9794 
9795 static void
9796 sd_create_errstats(struct sd_lun *un, int instance)
9797 {
9798 	struct	sd_errstats	*stp;
9799 	char	kstatmodule_err[KSTAT_STRLEN];
9800 	char	kstatname[KSTAT_STRLEN];
9801 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9802 
9803 	ASSERT(un != NULL);
9804 
9805 	if (un->un_errstats != NULL) {
9806 		return;
9807 	}
9808 
9809 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9810 	    "%serr", sd_label);
9811 	(void) snprintf(kstatname, sizeof (kstatname),
9812 	    "%s%d,err", sd_label, instance);
9813 
9814 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9815 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9816 
9817 	if (un->un_errstats == NULL) {
9818 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9819 		    "sd_create_errstats: Failed kstat_create\n");
9820 		return;
9821 	}
9822 
9823 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9824 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9825 	    KSTAT_DATA_UINT32);
9826 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9827 	    KSTAT_DATA_UINT32);
9828 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9829 	    KSTAT_DATA_UINT32);
9830 	kstat_named_init(&stp->sd_vid,		"Vendor",
9831 	    KSTAT_DATA_CHAR);
9832 	kstat_named_init(&stp->sd_pid,		"Product",
9833 	    KSTAT_DATA_CHAR);
9834 	kstat_named_init(&stp->sd_revision,	"Revision",
9835 	    KSTAT_DATA_CHAR);
9836 	kstat_named_init(&stp->sd_serial,	"Serial No",
9837 	    KSTAT_DATA_CHAR);
9838 	kstat_named_init(&stp->sd_capacity,	"Size",
9839 	    KSTAT_DATA_ULONGLONG);
9840 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9841 	    KSTAT_DATA_UINT32);
9842 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9843 	    KSTAT_DATA_UINT32);
9844 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9845 	    KSTAT_DATA_UINT32);
9846 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9847 	    KSTAT_DATA_UINT32);
9848 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9849 	    KSTAT_DATA_UINT32);
9850 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9851 	    KSTAT_DATA_UINT32);
9852 
9853 	un->un_errstats->ks_private = un;
9854 	un->un_errstats->ks_update  = nulldev;
9855 
9856 	kstat_install(un->un_errstats);
9857 }
9858 
9859 
9860 /*
9861  *    Function: sd_set_errstats
9862  *
9863  * Description: This routine sets the value of the vendor id, product id,
9864  *		revision, serial number, and capacity device error stats.
9865  *
9866  *		Note: During attach the stats are instantiated first so they are
9867  *		available for attach-time routines that utilize the driver
9868  *		iopath to send commands to the device. The stats are initialized
9869  *		separately so data obtained during some attach-time routines is
9870  *		available. (4362483)
9871  *
9872  *   Arguments: un - driver soft state (unit) structure
9873  *
9874  *     Context: Kernel thread context
9875  */
9876 
9877 static void
9878 sd_set_errstats(struct sd_lun *un)
9879 {
9880 	struct	sd_errstats	*stp;
9881 
9882 	ASSERT(un != NULL);
9883 	ASSERT(un->un_errstats != NULL);
9884 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9885 	ASSERT(stp != NULL);
9886 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9887 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9888 	(void) strncpy(stp->sd_revision.value.c,
9889 	    un->un_sd->sd_inq->inq_revision, 4);
9890 
9891 	/*
9892 	 * All the errstats are persistent across detach/attach,
9893 	 * so reset all the errstats here in case of the hot
9894 	 * replacement of disk drives, except for not changed
9895 	 * Sun qualified drives.
9896 	 */
9897 	if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9898 	    (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9899 	    sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9900 		stp->sd_softerrs.value.ui32 = 0;
9901 		stp->sd_harderrs.value.ui32 = 0;
9902 		stp->sd_transerrs.value.ui32 = 0;
9903 		stp->sd_rq_media_err.value.ui32 = 0;
9904 		stp->sd_rq_ntrdy_err.value.ui32 = 0;
9905 		stp->sd_rq_nodev_err.value.ui32 = 0;
9906 		stp->sd_rq_recov_err.value.ui32 = 0;
9907 		stp->sd_rq_illrq_err.value.ui32 = 0;
9908 		stp->sd_rq_pfa_err.value.ui32 = 0;
9909 	}
9910 
9911 	/*
9912 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9913 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9914 	 * (4376302))
9915 	 */
9916 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9917 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9918 		    sizeof (SD_INQUIRY(un)->inq_serial));
9919 	}
9920 
9921 	if (un->un_f_blockcount_is_valid != TRUE) {
9922 		/*
9923 		 * Set capacity error stat to 0 for no media. This ensures
9924 		 * a valid capacity is displayed in response to 'iostat -E'
9925 		 * when no media is present in the device.
9926 		 */
9927 		stp->sd_capacity.value.ui64 = 0;
9928 	} else {
9929 		/*
9930 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9931 		 * capacity.
9932 		 *
9933 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9934 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9935 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9936 		 */
9937 		stp->sd_capacity.value.ui64 = (uint64_t)
9938 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9939 	}
9940 }
9941 
9942 
9943 /*
9944  *    Function: sd_set_pstats
9945  *
9946  * Description: This routine instantiates and initializes the partition
9947  *              stats for each partition with more than zero blocks.
9948  *		(4363169)
9949  *
9950  *   Arguments: un - driver soft state (unit) structure
9951  *
9952  *     Context: Kernel thread context
9953  */
9954 
9955 static void
9956 sd_set_pstats(struct sd_lun *un)
9957 {
9958 	char	kstatname[KSTAT_STRLEN];
9959 	int	instance;
9960 	int	i;
9961 
9962 	ASSERT(un != NULL);
9963 
9964 	instance = ddi_get_instance(SD_DEVINFO(un));
9965 
9966 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
9967 	for (i = 0; i < NSDMAP; i++) {
9968 		if ((un->un_pstats[i] == NULL) &&
9969 		    (un->un_map[i].dkl_nblk != 0)) {
9970 			(void) snprintf(kstatname, sizeof (kstatname),
9971 			    "%s%d,%s", sd_label, instance,
9972 			    sd_minor_data[i].name);
9973 			un->un_pstats[i] = kstat_create(sd_label,
9974 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
9975 			    1, KSTAT_FLAG_PERSISTENT);
9976 			if (un->un_pstats[i] != NULL) {
9977 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9978 				kstat_install(un->un_pstats[i]);
9979 			}
9980 		}
9981 	}
9982 }
9983 
9984 
9985 #if (defined(__fibre))
9986 /*
9987  *    Function: sd_init_event_callbacks
9988  *
9989  * Description: This routine initializes the insertion and removal event
9990  *		callbacks. (fibre only)
9991  *
9992  *   Arguments: un - driver soft state (unit) structure
9993  *
9994  *     Context: Kernel thread context
9995  */
9996 
9997 static void
9998 sd_init_event_callbacks(struct sd_lun *un)
9999 {
10000 	ASSERT(un != NULL);
10001 
10002 	if ((un->un_insert_event == NULL) &&
10003 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
10004 	    &un->un_insert_event) == DDI_SUCCESS)) {
10005 		/*
10006 		 * Add the callback for an insertion event
10007 		 */
10008 		(void) ddi_add_event_handler(SD_DEVINFO(un),
10009 		    un->un_insert_event, sd_event_callback, (void *)un,
10010 		    &(un->un_insert_cb_id));
10011 	}
10012 
10013 	if ((un->un_remove_event == NULL) &&
10014 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
10015 	    &un->un_remove_event) == DDI_SUCCESS)) {
10016 		/*
10017 		 * Add the callback for a removal event
10018 		 */
10019 		(void) ddi_add_event_handler(SD_DEVINFO(un),
10020 		    un->un_remove_event, sd_event_callback, (void *)un,
10021 		    &(un->un_remove_cb_id));
10022 	}
10023 }
10024 
10025 
10026 /*
10027  *    Function: sd_event_callback
10028  *
10029  * Description: This routine handles insert/remove events (photon). The
10030  *		state is changed to OFFLINE which can be used to supress
10031  *		error msgs. (fibre only)
10032  *
10033  *   Arguments: un - driver soft state (unit) structure
10034  *
10035  *     Context: Callout thread context
10036  */
10037 /* ARGSUSED */
10038 static void
10039 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
10040     void *bus_impldata)
10041 {
10042 	struct sd_lun *un = (struct sd_lun *)arg;
10043 
10044 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
10045 	if (event == un->un_insert_event) {
10046 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
10047 		mutex_enter(SD_MUTEX(un));
10048 		if (un->un_state == SD_STATE_OFFLINE) {
10049 			if (un->un_last_state != SD_STATE_SUSPENDED) {
10050 				un->un_state = un->un_last_state;
10051 			} else {
10052 				/*
10053 				 * We have gone through SUSPEND/RESUME while
10054 				 * we were offline. Restore the last state
10055 				 */
10056 				un->un_state = un->un_save_state;
10057 			}
10058 		}
10059 		mutex_exit(SD_MUTEX(un));
10060 
10061 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
10062 	} else if (event == un->un_remove_event) {
10063 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
10064 		mutex_enter(SD_MUTEX(un));
10065 		/*
10066 		 * We need to handle an event callback that occurs during
10067 		 * the suspend operation, since we don't prevent it.
10068 		 */
10069 		if (un->un_state != SD_STATE_OFFLINE) {
10070 			if (un->un_state != SD_STATE_SUSPENDED) {
10071 				New_state(un, SD_STATE_OFFLINE);
10072 			} else {
10073 				un->un_last_state = SD_STATE_OFFLINE;
10074 			}
10075 		}
10076 		mutex_exit(SD_MUTEX(un));
10077 	} else {
10078 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
10079 		    "!Unknown event\n");
10080 	}
10081 
10082 }
10083 #endif
10084 
10085 /*
10086  *    Function: sd_cache_control()
10087  *
10088  * Description: This routine is the driver entry point for setting
10089  *		read and write caching by modifying the WCE (write cache
10090  *		enable) and RCD (read cache disable) bits of mode
10091  *		page 8 (MODEPAGE_CACHING).
10092  *
10093  *   Arguments: un - driver soft state (unit) structure
10094  *		rcd_flag - flag for controlling the read cache
10095  *		wce_flag - flag for controlling the write cache
10096  *
10097  * Return Code: EIO
10098  *		code returned by sd_send_scsi_MODE_SENSE and
10099  *		sd_send_scsi_MODE_SELECT
10100  *
10101  *     Context: Kernel Thread
10102  */
10103 
10104 static int
10105 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag)
10106 {
10107 	struct mode_caching	*mode_caching_page;
10108 	uchar_t			*header;
10109 	size_t			buflen;
10110 	int			hdrlen;
10111 	int			bd_len;
10112 	int			rval = 0;
10113 	struct mode_header_grp2	*mhp;
10114 
10115 	ASSERT(un != NULL);
10116 
10117 	/*
10118 	 * Do a test unit ready, otherwise a mode sense may not work if this
10119 	 * is the first command sent to the device after boot.
10120 	 */
10121 	(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
10122 
10123 	if (un->un_f_cfg_is_atapi == TRUE) {
10124 		hdrlen = MODE_HEADER_LENGTH_GRP2;
10125 	} else {
10126 		hdrlen = MODE_HEADER_LENGTH;
10127 	}
10128 
10129 	/*
10130 	 * Allocate memory for the retrieved mode page and its headers.  Set
10131 	 * a pointer to the page itself.  Use mode_cache_scsi3 to insure
10132 	 * we get all of the mode sense data otherwise, the mode select
10133 	 * will fail.  mode_cache_scsi3 is a superset of mode_caching.
10134 	 */
10135 	buflen = hdrlen + MODE_BLK_DESC_LENGTH +
10136 		sizeof (struct mode_cache_scsi3);
10137 
10138 	header = kmem_zalloc(buflen, KM_SLEEP);
10139 
10140 	/* Get the information from the device. */
10141 	if (un->un_f_cfg_is_atapi == TRUE) {
10142 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen,
10143 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
10144 	} else {
10145 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
10146 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
10147 	}
10148 	if (rval != 0) {
10149 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
10150 		    "sd_cache_control: Mode Sense Failed\n");
10151 		kmem_free(header, buflen);
10152 		return (rval);
10153 	}
10154 
10155 	/*
10156 	 * Determine size of Block Descriptors in order to locate
10157 	 * the mode page data. ATAPI devices return 0, SCSI devices
10158 	 * should return MODE_BLK_DESC_LENGTH.
10159 	 */
10160 	if (un->un_f_cfg_is_atapi == TRUE) {
10161 		mhp	= (struct mode_header_grp2 *)header;
10162 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
10163 	} else {
10164 		bd_len  = ((struct mode_header *)header)->bdesc_length;
10165 	}
10166 
10167 	if (bd_len > MODE_BLK_DESC_LENGTH) {
10168 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10169 		    "sd_cache_control: Mode Sense returned invalid "
10170 		    "block descriptor length\n");
10171 		kmem_free(header, buflen);
10172 		return (EIO);
10173 	}
10174 
10175 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
10176 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
10177 		SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense"
10178 		    " caching page code mismatch %d\n",
10179 		    mode_caching_page->mode_page.code);
10180 		kmem_free(header, buflen);
10181 		return (EIO);
10182 	}
10183 
10184 	/* Check the relevant bits on successful mode sense. */
10185 	if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
10186 	    (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
10187 	    (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
10188 	    (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
10189 
10190 		size_t sbuflen;
10191 		uchar_t save_pg;
10192 
10193 		/*
10194 		 * Construct select buffer length based on the
10195 		 * length of the sense data returned.
10196 		 */
10197 		sbuflen =  hdrlen + MODE_BLK_DESC_LENGTH +
10198 				sizeof (struct mode_page) +
10199 				(int)mode_caching_page->mode_page.length;
10200 
10201 		/*
10202 		 * Set the caching bits as requested.
10203 		 */
10204 		if (rcd_flag == SD_CACHE_ENABLE)
10205 			mode_caching_page->rcd = 0;
10206 		else if (rcd_flag == SD_CACHE_DISABLE)
10207 			mode_caching_page->rcd = 1;
10208 
10209 		if (wce_flag == SD_CACHE_ENABLE)
10210 			mode_caching_page->wce = 1;
10211 		else if (wce_flag == SD_CACHE_DISABLE)
10212 			mode_caching_page->wce = 0;
10213 
10214 		/*
10215 		 * Save the page if the mode sense says the
10216 		 * drive supports it.
10217 		 */
10218 		save_pg = mode_caching_page->mode_page.ps ?
10219 				SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
10220 
10221 		/* Clear reserved bits before mode select. */
10222 		mode_caching_page->mode_page.ps = 0;
10223 
10224 		/*
10225 		 * Clear out mode header for mode select.
10226 		 * The rest of the retrieved page will be reused.
10227 		 */
10228 		bzero(header, hdrlen);
10229 
10230 		if (un->un_f_cfg_is_atapi == TRUE) {
10231 			mhp = (struct mode_header_grp2 *)header;
10232 			mhp->bdesc_length_hi = bd_len >> 8;
10233 			mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff;
10234 		} else {
10235 			((struct mode_header *)header)->bdesc_length = bd_len;
10236 		}
10237 
10238 		/* Issue mode select to change the cache settings */
10239 		if (un->un_f_cfg_is_atapi == TRUE) {
10240 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header,
10241 			    sbuflen, save_pg, SD_PATH_DIRECT);
10242 		} else {
10243 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
10244 			    sbuflen, save_pg, SD_PATH_DIRECT);
10245 		}
10246 	}
10247 
10248 	kmem_free(header, buflen);
10249 	return (rval);
10250 }
10251 
10252 
10253 /*
10254  *    Function: sd_get_write_cache_enabled()
10255  *
10256  * Description: This routine is the driver entry point for determining if
10257  *		write caching is enabled.  It examines the WCE (write cache
10258  *		enable) bits of mode page 8 (MODEPAGE_CACHING).
10259  *
10260  *   Arguments: un - driver soft state (unit) structure
10261  *   		is_enabled - pointer to int where write cache enabled state
10262  *   			is returned (non-zero -> write cache enabled)
10263  *
10264  *
10265  * Return Code: EIO
10266  *		code returned by sd_send_scsi_MODE_SENSE
10267  *
10268  *     Context: Kernel Thread
10269  *
10270  * NOTE: If ioctl is added to disable write cache, this sequence should
10271  * be followed so that no locking is required for accesses to
10272  * un->un_f_write_cache_enabled:
10273  * 	do mode select to clear wce
10274  * 	do synchronize cache to flush cache
10275  * 	set un->un_f_write_cache_enabled = FALSE
10276  *
10277  * Conversely, an ioctl to enable the write cache should be done
10278  * in this order:
10279  * 	set un->un_f_write_cache_enabled = TRUE
10280  * 	do mode select to set wce
10281  */
10282 
10283 static int
10284 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled)
10285 {
10286 	struct mode_caching	*mode_caching_page;
10287 	uchar_t			*header;
10288 	size_t			buflen;
10289 	int			hdrlen;
10290 	int			bd_len;
10291 	int			rval = 0;
10292 
10293 	ASSERT(un != NULL);
10294 	ASSERT(is_enabled != NULL);
10295 
10296 	/* in case of error, flag as enabled */
10297 	*is_enabled = TRUE;
10298 
10299 	/*
10300 	 * Do a test unit ready, otherwise a mode sense may not work if this
10301 	 * is the first command sent to the device after boot.
10302 	 */
10303 	(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
10304 
10305 	if (un->un_f_cfg_is_atapi == TRUE) {
10306 		hdrlen = MODE_HEADER_LENGTH_GRP2;
10307 	} else {
10308 		hdrlen = MODE_HEADER_LENGTH;
10309 	}
10310 
10311 	/*
10312 	 * Allocate memory for the retrieved mode page and its headers.  Set
10313 	 * a pointer to the page itself.
10314 	 */
10315 	buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
10316 	header = kmem_zalloc(buflen, KM_SLEEP);
10317 
10318 	/* Get the information from the device. */
10319 	if (un->un_f_cfg_is_atapi == TRUE) {
10320 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen,
10321 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
10322 	} else {
10323 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
10324 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
10325 	}
10326 	if (rval != 0) {
10327 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
10328 		    "sd_get_write_cache_enabled: Mode Sense Failed\n");
10329 		kmem_free(header, buflen);
10330 		return (rval);
10331 	}
10332 
10333 	/*
10334 	 * Determine size of Block Descriptors in order to locate
10335 	 * the mode page data. ATAPI devices return 0, SCSI devices
10336 	 * should return MODE_BLK_DESC_LENGTH.
10337 	 */
10338 	if (un->un_f_cfg_is_atapi == TRUE) {
10339 		struct mode_header_grp2	*mhp;
10340 		mhp	= (struct mode_header_grp2 *)header;
10341 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
10342 	} else {
10343 		bd_len  = ((struct mode_header *)header)->bdesc_length;
10344 	}
10345 
10346 	if (bd_len > MODE_BLK_DESC_LENGTH) {
10347 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10348 		    "sd_get_write_cache_enabled: Mode Sense returned invalid "
10349 		    "block descriptor length\n");
10350 		kmem_free(header, buflen);
10351 		return (EIO);
10352 	}
10353 
10354 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
10355 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
10356 		SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense"
10357 		    " caching page code mismatch %d\n",
10358 		    mode_caching_page->mode_page.code);
10359 		kmem_free(header, buflen);
10360 		return (EIO);
10361 	}
10362 	*is_enabled = mode_caching_page->wce;
10363 
10364 	kmem_free(header, buflen);
10365 	return (0);
10366 }
10367 
10368 
10369 /*
10370  *    Function: sd_make_device
10371  *
10372  * Description: Utility routine to return the Solaris device number from
10373  *		the data in the device's dev_info structure.
10374  *
10375  * Return Code: The Solaris device number
10376  *
10377  *     Context: Any
10378  */
10379 
10380 static dev_t
10381 sd_make_device(dev_info_t *devi)
10382 {
10383 	return (makedevice(ddi_name_to_major(ddi_get_name(devi)),
10384 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
10385 }
10386 
10387 
10388 /*
10389  *    Function: sd_pm_entry
10390  *
10391  * Description: Called at the start of a new command to manage power
10392  *		and busy status of a device. This includes determining whether
10393  *		the current power state of the device is sufficient for
10394  *		performing the command or whether it must be changed.
10395  *		The PM framework is notified appropriately.
10396  *		Only with a return status of DDI_SUCCESS will the
10397  *		component be busy to the framework.
10398  *
10399  *		All callers of sd_pm_entry must check the return status
10400  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
10401  *		of DDI_FAILURE indicates the device failed to power up.
10402  *		In this case un_pm_count has been adjusted so the result
10403  *		on exit is still powered down, ie. count is less than 0.
10404  *		Calling sd_pm_exit with this count value hits an ASSERT.
10405  *
10406  * Return Code: DDI_SUCCESS or DDI_FAILURE
10407  *
10408  *     Context: Kernel thread context.
10409  */
10410 
10411 static int
10412 sd_pm_entry(struct sd_lun *un)
10413 {
10414 	int return_status = DDI_SUCCESS;
10415 
10416 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10417 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10418 
10419 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
10420 
10421 	if (un->un_f_pm_is_enabled == FALSE) {
10422 		SD_TRACE(SD_LOG_IO_PM, un,
10423 		    "sd_pm_entry: exiting, PM not enabled\n");
10424 		return (return_status);
10425 	}
10426 
10427 	/*
10428 	 * Just increment a counter if PM is enabled. On the transition from
10429 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
10430 	 * the count with each IO and mark the device as idle when the count
10431 	 * hits 0.
10432 	 *
10433 	 * If the count is less than 0 the device is powered down. If a powered
10434 	 * down device is successfully powered up then the count must be
10435 	 * incremented to reflect the power up. Note that it'll get incremented
10436 	 * a second time to become busy.
10437 	 *
10438 	 * Because the following has the potential to change the device state
10439 	 * and must release the un_pm_mutex to do so, only one thread can be
10440 	 * allowed through at a time.
10441 	 */
10442 
10443 	mutex_enter(&un->un_pm_mutex);
10444 	while (un->un_pm_busy == TRUE) {
10445 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
10446 	}
10447 	un->un_pm_busy = TRUE;
10448 
10449 	if (un->un_pm_count < 1) {
10450 
10451 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
10452 
10453 		/*
10454 		 * Indicate we are now busy so the framework won't attempt to
10455 		 * power down the device. This call will only fail if either
10456 		 * we passed a bad component number or the device has no
10457 		 * components. Neither of these should ever happen.
10458 		 */
10459 		mutex_exit(&un->un_pm_mutex);
10460 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
10461 		ASSERT(return_status == DDI_SUCCESS);
10462 
10463 		mutex_enter(&un->un_pm_mutex);
10464 
10465 		if (un->un_pm_count < 0) {
10466 			mutex_exit(&un->un_pm_mutex);
10467 
10468 			SD_TRACE(SD_LOG_IO_PM, un,
10469 			    "sd_pm_entry: power up component\n");
10470 
10471 			/*
10472 			 * pm_raise_power will cause sdpower to be called
10473 			 * which brings the device power level to the
10474 			 * desired state, ON in this case. If successful,
10475 			 * un_pm_count and un_power_level will be updated
10476 			 * appropriately.
10477 			 */
10478 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
10479 			    SD_SPINDLE_ON);
10480 
10481 			mutex_enter(&un->un_pm_mutex);
10482 
10483 			if (return_status != DDI_SUCCESS) {
10484 				/*
10485 				 * Power up failed.
10486 				 * Idle the device and adjust the count
10487 				 * so the result on exit is that we're
10488 				 * still powered down, ie. count is less than 0.
10489 				 */
10490 				SD_TRACE(SD_LOG_IO_PM, un,
10491 				    "sd_pm_entry: power up failed,"
10492 				    " idle the component\n");
10493 
10494 				(void) pm_idle_component(SD_DEVINFO(un), 0);
10495 				un->un_pm_count--;
10496 			} else {
10497 				/*
10498 				 * Device is powered up, verify the
10499 				 * count is non-negative.
10500 				 * This is debug only.
10501 				 */
10502 				ASSERT(un->un_pm_count == 0);
10503 			}
10504 		}
10505 
10506 		if (return_status == DDI_SUCCESS) {
10507 			/*
10508 			 * For performance, now that the device has been tagged
10509 			 * as busy, and it's known to be powered up, update the
10510 			 * chain types to use jump tables that do not include
10511 			 * pm. This significantly lowers the overhead and
10512 			 * therefore improves performance.
10513 			 */
10514 
10515 			mutex_exit(&un->un_pm_mutex);
10516 			mutex_enter(SD_MUTEX(un));
10517 			SD_TRACE(SD_LOG_IO_PM, un,
10518 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
10519 			    un->un_uscsi_chain_type);
10520 
10521 			if (un->un_f_non_devbsize_supported) {
10522 				un->un_buf_chain_type =
10523 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
10524 			} else {
10525 				un->un_buf_chain_type =
10526 				    SD_CHAIN_INFO_DISK_NO_PM;
10527 			}
10528 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
10529 
10530 			SD_TRACE(SD_LOG_IO_PM, un,
10531 			    "             changed  uscsi_chain_type to   %d\n",
10532 			    un->un_uscsi_chain_type);
10533 			mutex_exit(SD_MUTEX(un));
10534 			mutex_enter(&un->un_pm_mutex);
10535 
10536 			if (un->un_pm_idle_timeid == NULL) {
10537 				/* 300 ms. */
10538 				un->un_pm_idle_timeid =
10539 				    timeout(sd_pm_idletimeout_handler, un,
10540 				    (drv_usectohz((clock_t)300000)));
10541 				/*
10542 				 * Include an extra call to busy which keeps the
10543 				 * device busy with-respect-to the PM layer
10544 				 * until the timer fires, at which time it'll
10545 				 * get the extra idle call.
10546 				 */
10547 				(void) pm_busy_component(SD_DEVINFO(un), 0);
10548 			}
10549 		}
10550 	}
10551 	un->un_pm_busy = FALSE;
10552 	/* Next... */
10553 	cv_signal(&un->un_pm_busy_cv);
10554 
10555 	un->un_pm_count++;
10556 
10557 	SD_TRACE(SD_LOG_IO_PM, un,
10558 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
10559 
10560 	mutex_exit(&un->un_pm_mutex);
10561 
10562 	return (return_status);
10563 }
10564 
10565 
10566 /*
10567  *    Function: sd_pm_exit
10568  *
10569  * Description: Called at the completion of a command to manage busy
10570  *		status for the device. If the device becomes idle the
10571  *		PM framework is notified.
10572  *
10573  *     Context: Kernel thread context
10574  */
10575 
10576 static void
10577 sd_pm_exit(struct sd_lun *un)
10578 {
10579 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10580 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10581 
10582 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10583 
10584 	/*
10585 	 * After attach the following flag is only read, so don't
10586 	 * take the penalty of acquiring a mutex for it.
10587 	 */
10588 	if (un->un_f_pm_is_enabled == TRUE) {
10589 
10590 		mutex_enter(&un->un_pm_mutex);
10591 		un->un_pm_count--;
10592 
10593 		SD_TRACE(SD_LOG_IO_PM, un,
10594 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10595 
10596 		ASSERT(un->un_pm_count >= 0);
10597 		if (un->un_pm_count == 0) {
10598 			mutex_exit(&un->un_pm_mutex);
10599 
10600 			SD_TRACE(SD_LOG_IO_PM, un,
10601 			    "sd_pm_exit: idle component\n");
10602 
10603 			(void) pm_idle_component(SD_DEVINFO(un), 0);
10604 
10605 		} else {
10606 			mutex_exit(&un->un_pm_mutex);
10607 		}
10608 	}
10609 
10610 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10611 }
10612 
10613 
10614 /*
10615  *    Function: sdopen
10616  *
10617  * Description: Driver's open(9e) entry point function.
10618  *
10619  *   Arguments: dev_i   - pointer to device number
10620  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10621  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10622  *		cred_p  - user credential pointer
10623  *
10624  * Return Code: EINVAL
10625  *		ENXIO
10626  *		EIO
10627  *		EROFS
10628  *		EBUSY
10629  *
10630  *     Context: Kernel thread context
10631  */
10632 /* ARGSUSED */
10633 static int
10634 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10635 {
10636 	struct sd_lun	*un;
10637 	int		nodelay;
10638 	int		part;
10639 	uint64_t	partmask;
10640 	int		instance;
10641 	dev_t		dev;
10642 	int		rval = EIO;
10643 
10644 	/* Validate the open type */
10645 	if (otyp >= OTYPCNT) {
10646 		return (EINVAL);
10647 	}
10648 
10649 	dev = *dev_p;
10650 	instance = SDUNIT(dev);
10651 	mutex_enter(&sd_detach_mutex);
10652 
10653 	/*
10654 	 * Fail the open if there is no softstate for the instance, or
10655 	 * if another thread somewhere is trying to detach the instance.
10656 	 */
10657 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10658 	    (un->un_detach_count != 0)) {
10659 		mutex_exit(&sd_detach_mutex);
10660 		/*
10661 		 * The probe cache only needs to be cleared when open (9e) fails
10662 		 * with ENXIO (4238046).
10663 		 */
10664 		/*
10665 		 * un-conditionally clearing probe cache is ok with
10666 		 * separate sd/ssd binaries
10667 		 * x86 platform can be an issue with both parallel
10668 		 * and fibre in 1 binary
10669 		 */
10670 		sd_scsi_clear_probe_cache();
10671 		return (ENXIO);
10672 	}
10673 
10674 	/*
10675 	 * The un_layer_count is to prevent another thread in specfs from
10676 	 * trying to detach the instance, which can happen when we are
10677 	 * called from a higher-layer driver instead of thru specfs.
10678 	 * This will not be needed when DDI provides a layered driver
10679 	 * interface that allows specfs to know that an instance is in
10680 	 * use by a layered driver & should not be detached.
10681 	 *
10682 	 * Note: the semantics for layered driver opens are exactly one
10683 	 * close for every open.
10684 	 */
10685 	if (otyp == OTYP_LYR) {
10686 		un->un_layer_count++;
10687 	}
10688 
10689 	/*
10690 	 * Keep a count of the current # of opens in progress. This is because
10691 	 * some layered drivers try to call us as a regular open. This can
10692 	 * cause problems that we cannot prevent, however by keeping this count
10693 	 * we can at least keep our open and detach routines from racing against
10694 	 * each other under such conditions.
10695 	 */
10696 	un->un_opens_in_progress++;
10697 	mutex_exit(&sd_detach_mutex);
10698 
10699 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
10700 	part	 = SDPART(dev);
10701 	partmask = 1 << part;
10702 
10703 	/*
10704 	 * We use a semaphore here in order to serialize
10705 	 * open and close requests on the device.
10706 	 */
10707 	sema_p(&un->un_semoclose);
10708 
10709 	mutex_enter(SD_MUTEX(un));
10710 
10711 	/*
10712 	 * All device accesses go thru sdstrategy() where we check
10713 	 * on suspend status but there could be a scsi_poll command,
10714 	 * which bypasses sdstrategy(), so we need to check pm
10715 	 * status.
10716 	 */
10717 
10718 	if (!nodelay) {
10719 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10720 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10721 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10722 		}
10723 
10724 		mutex_exit(SD_MUTEX(un));
10725 		if (sd_pm_entry(un) != DDI_SUCCESS) {
10726 			rval = EIO;
10727 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10728 			    "sdopen: sd_pm_entry failed\n");
10729 			goto open_failed_with_pm;
10730 		}
10731 		mutex_enter(SD_MUTEX(un));
10732 	}
10733 
10734 	/* check for previous exclusive open */
10735 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10736 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10737 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10738 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10739 
10740 	if (un->un_exclopen & (partmask)) {
10741 		goto excl_open_fail;
10742 	}
10743 
10744 	if (flag & FEXCL) {
10745 		int i;
10746 		if (un->un_ocmap.lyropen[part]) {
10747 			goto excl_open_fail;
10748 		}
10749 		for (i = 0; i < (OTYPCNT - 1); i++) {
10750 			if (un->un_ocmap.regopen[i] & (partmask)) {
10751 				goto excl_open_fail;
10752 			}
10753 		}
10754 	}
10755 
10756 	/*
10757 	 * Check the write permission if this is a removable media device,
10758 	 * NDELAY has not been set, and writable permission is requested.
10759 	 *
10760 	 * Note: If NDELAY was set and this is write-protected media the WRITE
10761 	 * attempt will fail with EIO as part of the I/O processing. This is a
10762 	 * more permissive implementation that allows the open to succeed and
10763 	 * WRITE attempts to fail when appropriate.
10764 	 */
10765 	if (un->un_f_chk_wp_open) {
10766 		if ((flag & FWRITE) && (!nodelay)) {
10767 			mutex_exit(SD_MUTEX(un));
10768 			/*
10769 			 * Defer the check for write permission on writable
10770 			 * DVD drive till sdstrategy and will not fail open even
10771 			 * if FWRITE is set as the device can be writable
10772 			 * depending upon the media and the media can change
10773 			 * after the call to open().
10774 			 */
10775 			if (un->un_f_dvdram_writable_device == FALSE) {
10776 				if (ISCD(un) || sr_check_wp(dev)) {
10777 				rval = EROFS;
10778 				mutex_enter(SD_MUTEX(un));
10779 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10780 				    "write to cd or write protected media\n");
10781 				goto open_fail;
10782 				}
10783 			}
10784 			mutex_enter(SD_MUTEX(un));
10785 		}
10786 	}
10787 
10788 	/*
10789 	 * If opening in NDELAY/NONBLOCK mode, just return.
10790 	 * Check if disk is ready and has a valid geometry later.
10791 	 */
10792 	if (!nodelay) {
10793 		mutex_exit(SD_MUTEX(un));
10794 		rval = sd_ready_and_valid(un);
10795 		mutex_enter(SD_MUTEX(un));
10796 		/*
10797 		 * Fail if device is not ready or if the number of disk
10798 		 * blocks is zero or negative for non CD devices.
10799 		 */
10800 		if ((rval != SD_READY_VALID) ||
10801 		    (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) {
10802 			rval = un->un_f_has_removable_media ? ENXIO : EIO;
10803 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10804 			    "device not ready or invalid disk block value\n");
10805 			goto open_fail;
10806 		}
10807 #if defined(__i386) || defined(__amd64)
10808 	} else {
10809 		uchar_t *cp;
10810 		/*
10811 		 * x86 requires special nodelay handling, so that p0 is
10812 		 * always defined and accessible.
10813 		 * Invalidate geometry only if device is not already open.
10814 		 */
10815 		cp = &un->un_ocmap.chkd[0];
10816 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10817 			if (*cp != (uchar_t)0) {
10818 			    break;
10819 			}
10820 			cp++;
10821 		}
10822 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10823 			un->un_f_geometry_is_valid = FALSE;
10824 		}
10825 
10826 #endif
10827 	}
10828 
10829 	if (otyp == OTYP_LYR) {
10830 		un->un_ocmap.lyropen[part]++;
10831 	} else {
10832 		un->un_ocmap.regopen[otyp] |= partmask;
10833 	}
10834 
10835 	/* Set up open and exclusive open flags */
10836 	if (flag & FEXCL) {
10837 		un->un_exclopen |= (partmask);
10838 	}
10839 
10840 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10841 	    "open of part %d type %d\n", part, otyp);
10842 
10843 	mutex_exit(SD_MUTEX(un));
10844 	if (!nodelay) {
10845 		sd_pm_exit(un);
10846 	}
10847 
10848 	sema_v(&un->un_semoclose);
10849 
10850 	mutex_enter(&sd_detach_mutex);
10851 	un->un_opens_in_progress--;
10852 	mutex_exit(&sd_detach_mutex);
10853 
10854 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10855 	return (DDI_SUCCESS);
10856 
10857 excl_open_fail:
10858 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10859 	rval = EBUSY;
10860 
10861 open_fail:
10862 	mutex_exit(SD_MUTEX(un));
10863 
10864 	/*
10865 	 * On a failed open we must exit the pm management.
10866 	 */
10867 	if (!nodelay) {
10868 		sd_pm_exit(un);
10869 	}
10870 open_failed_with_pm:
10871 	sema_v(&un->un_semoclose);
10872 
10873 	mutex_enter(&sd_detach_mutex);
10874 	un->un_opens_in_progress--;
10875 	if (otyp == OTYP_LYR) {
10876 		un->un_layer_count--;
10877 	}
10878 	mutex_exit(&sd_detach_mutex);
10879 
10880 	return (rval);
10881 }
10882 
10883 
10884 /*
10885  *    Function: sdclose
10886  *
10887  * Description: Driver's close(9e) entry point function.
10888  *
10889  *   Arguments: dev    - device number
10890  *		flag   - file status flag, informational only
10891  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10892  *		cred_p - user credential pointer
10893  *
10894  * Return Code: ENXIO
10895  *
10896  *     Context: Kernel thread context
10897  */
10898 /* ARGSUSED */
10899 static int
10900 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10901 {
10902 	struct sd_lun	*un;
10903 	uchar_t		*cp;
10904 	int		part;
10905 	int		nodelay;
10906 	int		rval = 0;
10907 
10908 	/* Validate the open type */
10909 	if (otyp >= OTYPCNT) {
10910 		return (ENXIO);
10911 	}
10912 
10913 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10914 		return (ENXIO);
10915 	}
10916 
10917 	part = SDPART(dev);
10918 	nodelay = flag & (FNDELAY | FNONBLOCK);
10919 
10920 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10921 	    "sdclose: close of part %d type %d\n", part, otyp);
10922 
10923 	/*
10924 	 * We use a semaphore here in order to serialize
10925 	 * open and close requests on the device.
10926 	 */
10927 	sema_p(&un->un_semoclose);
10928 
10929 	mutex_enter(SD_MUTEX(un));
10930 
10931 	/* Don't proceed if power is being changed. */
10932 	while (un->un_state == SD_STATE_PM_CHANGING) {
10933 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10934 	}
10935 
10936 	if (un->un_exclopen & (1 << part)) {
10937 		un->un_exclopen &= ~(1 << part);
10938 	}
10939 
10940 	/* Update the open partition map */
10941 	if (otyp == OTYP_LYR) {
10942 		un->un_ocmap.lyropen[part] -= 1;
10943 	} else {
10944 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10945 	}
10946 
10947 	cp = &un->un_ocmap.chkd[0];
10948 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10949 		if (*cp != NULL) {
10950 			break;
10951 		}
10952 		cp++;
10953 	}
10954 
10955 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10956 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10957 
10958 		/*
10959 		 * We avoid persistance upon the last close, and set
10960 		 * the throttle back to the maximum.
10961 		 */
10962 		un->un_throttle = un->un_saved_throttle;
10963 
10964 		if (un->un_state == SD_STATE_OFFLINE) {
10965 			if (un->un_f_is_fibre == FALSE) {
10966 				scsi_log(SD_DEVINFO(un), sd_label,
10967 					CE_WARN, "offline\n");
10968 			}
10969 			un->un_f_geometry_is_valid = FALSE;
10970 
10971 		} else {
10972 			/*
10973 			 * Flush any outstanding writes in NVRAM cache.
10974 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10975 			 * cmd, it may not work for non-Pluto devices.
10976 			 * SYNCHRONIZE CACHE is not required for removables,
10977 			 * except DVD-RAM drives.
10978 			 *
10979 			 * Also note: because SYNCHRONIZE CACHE is currently
10980 			 * the only command issued here that requires the
10981 			 * drive be powered up, only do the power up before
10982 			 * sending the Sync Cache command. If additional
10983 			 * commands are added which require a powered up
10984 			 * drive, the following sequence may have to change.
10985 			 *
10986 			 * And finally, note that parallel SCSI on SPARC
10987 			 * only issues a Sync Cache to DVD-RAM, a newly
10988 			 * supported device.
10989 			 */
10990 #if defined(__i386) || defined(__amd64)
10991 			if (un->un_f_sync_cache_supported ||
10992 			    un->un_f_dvdram_writable_device == TRUE) {
10993 #else
10994 			if (un->un_f_dvdram_writable_device == TRUE) {
10995 #endif
10996 				mutex_exit(SD_MUTEX(un));
10997 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10998 					rval =
10999 					    sd_send_scsi_SYNCHRONIZE_CACHE(un,
11000 					    NULL);
11001 					/* ignore error if not supported */
11002 					if (rval == ENOTSUP) {
11003 						rval = 0;
11004 					} else if (rval != 0) {
11005 						rval = EIO;
11006 					}
11007 					sd_pm_exit(un);
11008 				} else {
11009 					rval = EIO;
11010 				}
11011 				mutex_enter(SD_MUTEX(un));
11012 			}
11013 
11014 			/*
11015 			 * For devices which supports DOOR_LOCK, send an ALLOW
11016 			 * MEDIA REMOVAL command, but don't get upset if it
11017 			 * fails. We need to raise the power of the drive before
11018 			 * we can call sd_send_scsi_DOORLOCK()
11019 			 */
11020 			if (un->un_f_doorlock_supported) {
11021 				mutex_exit(SD_MUTEX(un));
11022 				if (sd_pm_entry(un) == DDI_SUCCESS) {
11023 					rval = sd_send_scsi_DOORLOCK(un,
11024 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
11025 
11026 					sd_pm_exit(un);
11027 					if (ISCD(un) && (rval != 0) &&
11028 					    (nodelay != 0)) {
11029 						rval = ENXIO;
11030 					}
11031 				} else {
11032 					rval = EIO;
11033 				}
11034 				mutex_enter(SD_MUTEX(un));
11035 			}
11036 
11037 			/*
11038 			 * If a device has removable media, invalidate all
11039 			 * parameters related to media, such as geometry,
11040 			 * blocksize, and blockcount.
11041 			 */
11042 			if (un->un_f_has_removable_media) {
11043 				sr_ejected(un);
11044 			}
11045 
11046 			/*
11047 			 * Destroy the cache (if it exists) which was
11048 			 * allocated for the write maps since this is
11049 			 * the last close for this media.
11050 			 */
11051 			if (un->un_wm_cache) {
11052 				/*
11053 				 * Check if there are pending commands.
11054 				 * and if there are give a warning and
11055 				 * do not destroy the cache.
11056 				 */
11057 				if (un->un_ncmds_in_driver > 0) {
11058 					scsi_log(SD_DEVINFO(un),
11059 					    sd_label, CE_WARN,
11060 					    "Unable to clean up memory "
11061 					    "because of pending I/O\n");
11062 				} else {
11063 					kmem_cache_destroy(
11064 					    un->un_wm_cache);
11065 					un->un_wm_cache = NULL;
11066 				}
11067 			}
11068 		}
11069 	}
11070 
11071 	mutex_exit(SD_MUTEX(un));
11072 	sema_v(&un->un_semoclose);
11073 
11074 	if (otyp == OTYP_LYR) {
11075 		mutex_enter(&sd_detach_mutex);
11076 		/*
11077 		 * The detach routine may run when the layer count
11078 		 * drops to zero.
11079 		 */
11080 		un->un_layer_count--;
11081 		mutex_exit(&sd_detach_mutex);
11082 	}
11083 
11084 	return (rval);
11085 }
11086 
11087 
11088 /*
11089  *    Function: sd_ready_and_valid
11090  *
11091  * Description: Test if device is ready and has a valid geometry.
11092  *
11093  *   Arguments: dev - device number
11094  *		un  - driver soft state (unit) structure
11095  *
11096  * Return Code: SD_READY_VALID		ready and valid label
11097  *		SD_READY_NOT_VALID	ready, geom ops never applicable
11098  *		SD_NOT_READY_VALID	not ready, no label
11099  *		SD_RESERVED_BY_OTHERS	reservation conflict
11100  *
11101  *     Context: Never called at interrupt context.
11102  */
11103 
11104 static int
11105 sd_ready_and_valid(struct sd_lun *un)
11106 {
11107 	struct sd_errstats	*stp;
11108 	uint64_t		capacity;
11109 	uint_t			lbasize;
11110 	int			rval = SD_READY_VALID;
11111 	char			name_str[48];
11112 
11113 	ASSERT(un != NULL);
11114 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11115 
11116 	mutex_enter(SD_MUTEX(un));
11117 	/*
11118 	 * If a device has removable media, we must check if media is
11119 	 * ready when checking if this device is ready and valid.
11120 	 */
11121 	if (un->un_f_has_removable_media) {
11122 		mutex_exit(SD_MUTEX(un));
11123 		if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) {
11124 			rval = SD_NOT_READY_VALID;
11125 			mutex_enter(SD_MUTEX(un));
11126 			goto done;
11127 		}
11128 
11129 		mutex_enter(SD_MUTEX(un));
11130 		if ((un->un_f_geometry_is_valid == FALSE) ||
11131 		    (un->un_f_blockcount_is_valid == FALSE) ||
11132 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
11133 
11134 			/* capacity has to be read every open. */
11135 			mutex_exit(SD_MUTEX(un));
11136 			if (sd_send_scsi_READ_CAPACITY(un, &capacity,
11137 			    &lbasize, SD_PATH_DIRECT) != 0) {
11138 				mutex_enter(SD_MUTEX(un));
11139 				un->un_f_geometry_is_valid = FALSE;
11140 				rval = SD_NOT_READY_VALID;
11141 				goto done;
11142 			} else {
11143 				mutex_enter(SD_MUTEX(un));
11144 				sd_update_block_info(un, lbasize, capacity);
11145 			}
11146 		}
11147 
11148 		/*
11149 		 * Check if the media in the device is writable or not.
11150 		 */
11151 		if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) {
11152 			sd_check_for_writable_cd(un);
11153 		}
11154 
11155 	} else {
11156 		/*
11157 		 * Do a test unit ready to clear any unit attention from non-cd
11158 		 * devices.
11159 		 */
11160 		mutex_exit(SD_MUTEX(un));
11161 		(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
11162 		mutex_enter(SD_MUTEX(un));
11163 	}
11164 
11165 
11166 	/*
11167 	 * If this is a non 512 block device, allocate space for
11168 	 * the wmap cache. This is being done here since every time
11169 	 * a media is changed this routine will be called and the
11170 	 * block size is a function of media rather than device.
11171 	 */
11172 	if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) {
11173 		if (!(un->un_wm_cache)) {
11174 			(void) snprintf(name_str, sizeof (name_str),
11175 			    "%s%d_cache",
11176 			    ddi_driver_name(SD_DEVINFO(un)),
11177 			    ddi_get_instance(SD_DEVINFO(un)));
11178 			un->un_wm_cache = kmem_cache_create(
11179 			    name_str, sizeof (struct sd_w_map),
11180 			    8, sd_wm_cache_constructor,
11181 			    sd_wm_cache_destructor, NULL,
11182 			    (void *)un, NULL, 0);
11183 			if (!(un->un_wm_cache)) {
11184 					rval = ENOMEM;
11185 					goto done;
11186 			}
11187 		}
11188 	}
11189 
11190 	if (un->un_state == SD_STATE_NORMAL) {
11191 		/*
11192 		 * If the target is not yet ready here (defined by a TUR
11193 		 * failure), invalidate the geometry and print an 'offline'
11194 		 * message. This is a legacy message, as the state of the
11195 		 * target is not actually changed to SD_STATE_OFFLINE.
11196 		 *
11197 		 * If the TUR fails for EACCES (Reservation Conflict),
11198 		 * SD_RESERVED_BY_OTHERS will be returned to indicate
11199 		 * reservation conflict. If the TUR fails for other
11200 		 * reasons, SD_NOT_READY_VALID will be returned.
11201 		 */
11202 		int err;
11203 
11204 		mutex_exit(SD_MUTEX(un));
11205 		err = sd_send_scsi_TEST_UNIT_READY(un, 0);
11206 		mutex_enter(SD_MUTEX(un));
11207 
11208 		if (err != 0) {
11209 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
11210 			    "offline or reservation conflict\n");
11211 			un->un_f_geometry_is_valid = FALSE;
11212 			if (err == EACCES) {
11213 				rval = SD_RESERVED_BY_OTHERS;
11214 			} else {
11215 				rval = SD_NOT_READY_VALID;
11216 			}
11217 			goto done;
11218 		}
11219 	}
11220 
11221 	if (un->un_f_format_in_progress == FALSE) {
11222 		/*
11223 		 * Note: sd_validate_geometry may return TRUE, but that does
11224 		 * not necessarily mean un_f_geometry_is_valid == TRUE!
11225 		 */
11226 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
11227 		if (rval == ENOTSUP) {
11228 			if (un->un_f_geometry_is_valid == TRUE)
11229 				rval = 0;
11230 			else {
11231 				rval = SD_READY_NOT_VALID;
11232 				goto done;
11233 			}
11234 		}
11235 		if (rval != 0) {
11236 			/*
11237 			 * We don't check the validity of geometry for
11238 			 * CDROMs. Also we assume we have a good label
11239 			 * even if sd_validate_geometry returned ENOMEM.
11240 			 */
11241 			if (!ISCD(un) && rval != ENOMEM) {
11242 				rval = SD_NOT_READY_VALID;
11243 				goto done;
11244 			}
11245 		}
11246 	}
11247 
11248 	/*
11249 	 * If this device supports DOOR_LOCK command, try and send
11250 	 * this command to PREVENT MEDIA REMOVAL, but don't get upset
11251 	 * if it fails. For a CD, however, it is an error
11252 	 */
11253 	if (un->un_f_doorlock_supported) {
11254 		mutex_exit(SD_MUTEX(un));
11255 		if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
11256 		    SD_PATH_DIRECT) != 0) && ISCD(un)) {
11257 			rval = SD_NOT_READY_VALID;
11258 			mutex_enter(SD_MUTEX(un));
11259 			goto done;
11260 		}
11261 		mutex_enter(SD_MUTEX(un));
11262 	}
11263 
11264 	/* The state has changed, inform the media watch routines */
11265 	un->un_mediastate = DKIO_INSERTED;
11266 	cv_broadcast(&un->un_state_cv);
11267 	rval = SD_READY_VALID;
11268 
11269 done:
11270 
11271 	/*
11272 	 * Initialize the capacity kstat value, if no media previously
11273 	 * (capacity kstat is 0) and a media has been inserted
11274 	 * (un_blockcount > 0).
11275 	 */
11276 	if (un->un_errstats != NULL) {
11277 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
11278 		if ((stp->sd_capacity.value.ui64 == 0) &&
11279 		    (un->un_f_blockcount_is_valid == TRUE)) {
11280 			stp->sd_capacity.value.ui64 =
11281 			    (uint64_t)((uint64_t)un->un_blockcount *
11282 			    un->un_sys_blocksize);
11283 		}
11284 	}
11285 
11286 	mutex_exit(SD_MUTEX(un));
11287 	return (rval);
11288 }
11289 
11290 
11291 /*
11292  *    Function: sdmin
11293  *
11294  * Description: Routine to limit the size of a data transfer. Used in
11295  *		conjunction with physio(9F).
11296  *
11297  *   Arguments: bp - pointer to the indicated buf(9S) struct.
11298  *
11299  *     Context: Kernel thread context.
11300  */
11301 
11302 static void
11303 sdmin(struct buf *bp)
11304 {
11305 	struct sd_lun	*un;
11306 	int		instance;
11307 
11308 	instance = SDUNIT(bp->b_edev);
11309 
11310 	un = ddi_get_soft_state(sd_state, instance);
11311 	ASSERT(un != NULL);
11312 
11313 	if (bp->b_bcount > un->un_max_xfer_size) {
11314 		bp->b_bcount = un->un_max_xfer_size;
11315 	}
11316 }
11317 
11318 
11319 /*
11320  *    Function: sdread
11321  *
11322  * Description: Driver's read(9e) entry point function.
11323  *
11324  *   Arguments: dev   - device number
11325  *		uio   - structure pointer describing where data is to be stored
11326  *			in user's space
11327  *		cred_p  - user credential pointer
11328  *
11329  * Return Code: ENXIO
11330  *		EIO
11331  *		EINVAL
11332  *		value returned by physio
11333  *
11334  *     Context: Kernel thread context.
11335  */
11336 /* ARGSUSED */
11337 static int
11338 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
11339 {
11340 	struct sd_lun	*un = NULL;
11341 	int		secmask;
11342 	int		err;
11343 
11344 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11345 		return (ENXIO);
11346 	}
11347 
11348 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11349 
11350 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11351 		mutex_enter(SD_MUTEX(un));
11352 		/*
11353 		 * Because the call to sd_ready_and_valid will issue I/O we
11354 		 * must wait here if either the device is suspended or
11355 		 * if it's power level is changing.
11356 		 */
11357 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11358 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11359 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11360 		}
11361 		un->un_ncmds_in_driver++;
11362 		mutex_exit(SD_MUTEX(un));
11363 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11364 			mutex_enter(SD_MUTEX(un));
11365 			un->un_ncmds_in_driver--;
11366 			ASSERT(un->un_ncmds_in_driver >= 0);
11367 			mutex_exit(SD_MUTEX(un));
11368 			return (EIO);
11369 		}
11370 		mutex_enter(SD_MUTEX(un));
11371 		un->un_ncmds_in_driver--;
11372 		ASSERT(un->un_ncmds_in_driver >= 0);
11373 		mutex_exit(SD_MUTEX(un));
11374 	}
11375 
11376 	/*
11377 	 * Read requests are restricted to multiples of the system block size.
11378 	 */
11379 	secmask = un->un_sys_blocksize - 1;
11380 
11381 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11382 		SD_ERROR(SD_LOG_READ_WRITE, un,
11383 		    "sdread: file offset not modulo %d\n",
11384 		    un->un_sys_blocksize);
11385 		err = EINVAL;
11386 	} else if (uio->uio_iov->iov_len & (secmask)) {
11387 		SD_ERROR(SD_LOG_READ_WRITE, un,
11388 		    "sdread: transfer length not modulo %d\n",
11389 		    un->un_sys_blocksize);
11390 		err = EINVAL;
11391 	} else {
11392 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
11393 	}
11394 	return (err);
11395 }
11396 
11397 
11398 /*
11399  *    Function: sdwrite
11400  *
11401  * Description: Driver's write(9e) entry point function.
11402  *
11403  *   Arguments: dev   - device number
11404  *		uio   - structure pointer describing where data is stored in
11405  *			user's space
11406  *		cred_p  - user credential pointer
11407  *
11408  * Return Code: ENXIO
11409  *		EIO
11410  *		EINVAL
11411  *		value returned by physio
11412  *
11413  *     Context: Kernel thread context.
11414  */
11415 /* ARGSUSED */
11416 static int
11417 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
11418 {
11419 	struct sd_lun	*un = NULL;
11420 	int		secmask;
11421 	int		err;
11422 
11423 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11424 		return (ENXIO);
11425 	}
11426 
11427 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11428 
11429 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11430 		mutex_enter(SD_MUTEX(un));
11431 		/*
11432 		 * Because the call to sd_ready_and_valid will issue I/O we
11433 		 * must wait here if either the device is suspended or
11434 		 * if it's power level is changing.
11435 		 */
11436 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11437 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11438 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11439 		}
11440 		un->un_ncmds_in_driver++;
11441 		mutex_exit(SD_MUTEX(un));
11442 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11443 			mutex_enter(SD_MUTEX(un));
11444 			un->un_ncmds_in_driver--;
11445 			ASSERT(un->un_ncmds_in_driver >= 0);
11446 			mutex_exit(SD_MUTEX(un));
11447 			return (EIO);
11448 		}
11449 		mutex_enter(SD_MUTEX(un));
11450 		un->un_ncmds_in_driver--;
11451 		ASSERT(un->un_ncmds_in_driver >= 0);
11452 		mutex_exit(SD_MUTEX(un));
11453 	}
11454 
11455 	/*
11456 	 * Write requests are restricted to multiples of the system block size.
11457 	 */
11458 	secmask = un->un_sys_blocksize - 1;
11459 
11460 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11461 		SD_ERROR(SD_LOG_READ_WRITE, un,
11462 		    "sdwrite: file offset not modulo %d\n",
11463 		    un->un_sys_blocksize);
11464 		err = EINVAL;
11465 	} else if (uio->uio_iov->iov_len & (secmask)) {
11466 		SD_ERROR(SD_LOG_READ_WRITE, un,
11467 		    "sdwrite: transfer length not modulo %d\n",
11468 		    un->un_sys_blocksize);
11469 		err = EINVAL;
11470 	} else {
11471 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11472 	}
11473 	return (err);
11474 }
11475 
11476 
11477 /*
11478  *    Function: sdaread
11479  *
11480  * Description: Driver's aread(9e) entry point function.
11481  *
11482  *   Arguments: dev   - device number
11483  *		aio   - structure pointer describing where data is to be stored
11484  *		cred_p  - user credential pointer
11485  *
11486  * Return Code: ENXIO
11487  *		EIO
11488  *		EINVAL
11489  *		value returned by aphysio
11490  *
11491  *     Context: Kernel thread context.
11492  */
11493 /* ARGSUSED */
11494 static int
11495 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11496 {
11497 	struct sd_lun	*un = NULL;
11498 	struct uio	*uio = aio->aio_uio;
11499 	int		secmask;
11500 	int		err;
11501 
11502 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11503 		return (ENXIO);
11504 	}
11505 
11506 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11507 
11508 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11509 		mutex_enter(SD_MUTEX(un));
11510 		/*
11511 		 * Because the call to sd_ready_and_valid will issue I/O we
11512 		 * must wait here if either the device is suspended or
11513 		 * if it's power level is changing.
11514 		 */
11515 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11516 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11517 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11518 		}
11519 		un->un_ncmds_in_driver++;
11520 		mutex_exit(SD_MUTEX(un));
11521 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11522 			mutex_enter(SD_MUTEX(un));
11523 			un->un_ncmds_in_driver--;
11524 			ASSERT(un->un_ncmds_in_driver >= 0);
11525 			mutex_exit(SD_MUTEX(un));
11526 			return (EIO);
11527 		}
11528 		mutex_enter(SD_MUTEX(un));
11529 		un->un_ncmds_in_driver--;
11530 		ASSERT(un->un_ncmds_in_driver >= 0);
11531 		mutex_exit(SD_MUTEX(un));
11532 	}
11533 
11534 	/*
11535 	 * Read requests are restricted to multiples of the system block size.
11536 	 */
11537 	secmask = un->un_sys_blocksize - 1;
11538 
11539 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11540 		SD_ERROR(SD_LOG_READ_WRITE, un,
11541 		    "sdaread: file offset not modulo %d\n",
11542 		    un->un_sys_blocksize);
11543 		err = EINVAL;
11544 	} else if (uio->uio_iov->iov_len & (secmask)) {
11545 		SD_ERROR(SD_LOG_READ_WRITE, un,
11546 		    "sdaread: transfer length not modulo %d\n",
11547 		    un->un_sys_blocksize);
11548 		err = EINVAL;
11549 	} else {
11550 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11551 	}
11552 	return (err);
11553 }
11554 
11555 
11556 /*
11557  *    Function: sdawrite
11558  *
11559  * Description: Driver's awrite(9e) entry point function.
11560  *
11561  *   Arguments: dev   - device number
11562  *		aio   - structure pointer describing where data is stored
11563  *		cred_p  - user credential pointer
11564  *
11565  * Return Code: ENXIO
11566  *		EIO
11567  *		EINVAL
11568  *		value returned by aphysio
11569  *
11570  *     Context: Kernel thread context.
11571  */
11572 /* ARGSUSED */
11573 static int
11574 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11575 {
11576 	struct sd_lun	*un = NULL;
11577 	struct uio	*uio = aio->aio_uio;
11578 	int		secmask;
11579 	int		err;
11580 
11581 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11582 		return (ENXIO);
11583 	}
11584 
11585 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11586 
11587 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11588 		mutex_enter(SD_MUTEX(un));
11589 		/*
11590 		 * Because the call to sd_ready_and_valid will issue I/O we
11591 		 * must wait here if either the device is suspended or
11592 		 * if it's power level is changing.
11593 		 */
11594 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11595 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11596 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11597 		}
11598 		un->un_ncmds_in_driver++;
11599 		mutex_exit(SD_MUTEX(un));
11600 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11601 			mutex_enter(SD_MUTEX(un));
11602 			un->un_ncmds_in_driver--;
11603 			ASSERT(un->un_ncmds_in_driver >= 0);
11604 			mutex_exit(SD_MUTEX(un));
11605 			return (EIO);
11606 		}
11607 		mutex_enter(SD_MUTEX(un));
11608 		un->un_ncmds_in_driver--;
11609 		ASSERT(un->un_ncmds_in_driver >= 0);
11610 		mutex_exit(SD_MUTEX(un));
11611 	}
11612 
11613 	/*
11614 	 * Write requests are restricted to multiples of the system block size.
11615 	 */
11616 	secmask = un->un_sys_blocksize - 1;
11617 
11618 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11619 		SD_ERROR(SD_LOG_READ_WRITE, un,
11620 		    "sdawrite: file offset not modulo %d\n",
11621 		    un->un_sys_blocksize);
11622 		err = EINVAL;
11623 	} else if (uio->uio_iov->iov_len & (secmask)) {
11624 		SD_ERROR(SD_LOG_READ_WRITE, un,
11625 		    "sdawrite: transfer length not modulo %d\n",
11626 		    un->un_sys_blocksize);
11627 		err = EINVAL;
11628 	} else {
11629 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11630 	}
11631 	return (err);
11632 }
11633 
11634 
11635 
11636 
11637 
11638 /*
11639  * Driver IO processing follows the following sequence:
11640  *
11641  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11642  *         |                |                     ^
11643  *         v                v                     |
11644  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11645  *         |                |                     |                   |
11646  *         v                |                     |                   |
11647  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11648  *         |                |                     ^                   ^
11649  *         v                v                     |                   |
11650  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11651  *         |                |                     |                   |
11652  *     +---+                |                     +------------+      +-------+
11653  *     |                    |                                  |              |
11654  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11655  *     |                    v                                  |              |
11656  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11657  *     |                    |                                  ^              |
11658  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11659  *     |                    v                                  |              |
11660  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11661  *     |                    |                                  ^              |
11662  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11663  *     |                    v                                  |              |
11664  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11665  *     |                    |                                  ^              |
11666  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11667  *     |                    v                                  |              |
11668  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11669  *     |                    |                                  ^              |
11670  *     |                    |                                  |              |
11671  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11672  *                          |                           ^
11673  *                          v                           |
11674  *                   sd_core_iostart()                  |
11675  *                          |                           |
11676  *                          |                           +------>(*destroypkt)()
11677  *                          +-> sd_start_cmds() <-+     |           |
11678  *                          |                     |     |           v
11679  *                          |                     |     |  scsi_destroy_pkt(9F)
11680  *                          |                     |     |
11681  *                          +->(*initpkt)()       +- sdintr()
11682  *                          |  |                        |  |
11683  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11684  *                          |  +-> scsi_setup_cdb(9F)   |
11685  *                          |                           |
11686  *                          +--> scsi_transport(9F)     |
11687  *                                     |                |
11688  *                                     +----> SCSA ---->+
11689  *
11690  *
11691  * This code is based upon the following presumtions:
11692  *
11693  *   - iostart and iodone functions operate on buf(9S) structures. These
11694  *     functions perform the necessary operations on the buf(9S) and pass
11695  *     them along to the next function in the chain by using the macros
11696  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11697  *     (for iodone side functions).
11698  *
11699  *   - The iostart side functions may sleep. The iodone side functions
11700  *     are called under interrupt context and may NOT sleep. Therefore
11701  *     iodone side functions also may not call iostart side functions.
11702  *     (NOTE: iostart side functions should NOT sleep for memory, as
11703  *     this could result in deadlock.)
11704  *
11705  *   - An iostart side function may call its corresponding iodone side
11706  *     function directly (if necessary).
11707  *
11708  *   - In the event of an error, an iostart side function can return a buf(9S)
11709  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11710  *     b_error in the usual way of course).
11711  *
11712  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11713  *     requests to the iostart side functions.  The iostart side functions in
11714  *     this case would be called under the context of a taskq thread, so it's
11715  *     OK for them to block/sleep/spin in this case.
11716  *
11717  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11718  *     pass them along to the next function in the chain.  The corresponding
11719  *     iodone side functions must coalesce the "shadow" bufs and return
11720  *     the "original" buf to the next higher layer.
11721  *
11722  *   - The b_private field of the buf(9S) struct holds a pointer to
11723  *     an sd_xbuf struct, which contains information needed to
11724  *     construct the scsi_pkt for the command.
11725  *
11726  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11727  *     layer must acquire & release the SD_MUTEX(un) as needed.
11728  */
11729 
11730 
11731 /*
11732  * Create taskq for all targets in the system. This is created at
11733  * _init(9E) and destroyed at _fini(9E).
11734  *
11735  * Note: here we set the minalloc to a reasonably high number to ensure that
11736  * we will have an adequate supply of task entries available at interrupt time.
11737  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11738  * sd_create_taskq().  Since we do not want to sleep for allocations at
11739  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11740  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11741  * requests any one instant in time.
11742  */
11743 #define	SD_TASKQ_NUMTHREADS	8
11744 #define	SD_TASKQ_MINALLOC	256
11745 #define	SD_TASKQ_MAXALLOC	256
11746 
11747 static taskq_t	*sd_tq = NULL;
11748 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11749 
11750 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11751 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11752 
11753 /*
11754  * The following task queue is being created for the write part of
11755  * read-modify-write of non-512 block size devices.
11756  * Limit the number of threads to 1 for now. This number has been choosen
11757  * considering the fact that it applies only to dvd ram drives/MO drives
11758  * currently. Performance for which is not main criteria at this stage.
11759  * Note: It needs to be explored if we can use a single taskq in future
11760  */
11761 #define	SD_WMR_TASKQ_NUMTHREADS	1
11762 static taskq_t	*sd_wmr_tq = NULL;
11763 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11764 
11765 /*
11766  *    Function: sd_taskq_create
11767  *
11768  * Description: Create taskq thread(s) and preallocate task entries
11769  *
11770  * Return Code: Returns a pointer to the allocated taskq_t.
11771  *
11772  *     Context: Can sleep. Requires blockable context.
11773  *
11774  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11775  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
11776  *		- taskq_create() will block for memory, also it will panic
11777  *		  if it cannot create the requested number of threads.
11778  *		- Currently taskq_create() creates threads that cannot be
11779  *		  swapped.
11780  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
11781  *		  supply of taskq entries at interrupt time (ie, so that we
11782  *		  do not have to sleep for memory)
11783  */
11784 
11785 static void
11786 sd_taskq_create(void)
11787 {
11788 	char	taskq_name[TASKQ_NAMELEN];
11789 
11790 	ASSERT(sd_tq == NULL);
11791 	ASSERT(sd_wmr_tq == NULL);
11792 
11793 	(void) snprintf(taskq_name, sizeof (taskq_name),
11794 	    "%s_drv_taskq", sd_label);
11795 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11796 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11797 	    TASKQ_PREPOPULATE));
11798 
11799 	(void) snprintf(taskq_name, sizeof (taskq_name),
11800 	    "%s_rmw_taskq", sd_label);
11801 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11802 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11803 	    TASKQ_PREPOPULATE));
11804 }
11805 
11806 
11807 /*
11808  *    Function: sd_taskq_delete
11809  *
11810  * Description: Complementary cleanup routine for sd_taskq_create().
11811  *
11812  *     Context: Kernel thread context.
11813  */
11814 
11815 static void
11816 sd_taskq_delete(void)
11817 {
11818 	ASSERT(sd_tq != NULL);
11819 	ASSERT(sd_wmr_tq != NULL);
11820 	taskq_destroy(sd_tq);
11821 	taskq_destroy(sd_wmr_tq);
11822 	sd_tq = NULL;
11823 	sd_wmr_tq = NULL;
11824 }
11825 
11826 
11827 /*
11828  *    Function: sdstrategy
11829  *
11830  * Description: Driver's strategy (9E) entry point function.
11831  *
11832  *   Arguments: bp - pointer to buf(9S)
11833  *
11834  * Return Code: Always returns zero
11835  *
11836  *     Context: Kernel thread context.
11837  */
11838 
11839 static int
11840 sdstrategy(struct buf *bp)
11841 {
11842 	struct sd_lun *un;
11843 
11844 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11845 	if (un == NULL) {
11846 		bioerror(bp, EIO);
11847 		bp->b_resid = bp->b_bcount;
11848 		biodone(bp);
11849 		return (0);
11850 	}
11851 	/* As was done in the past, fail new cmds. if state is dumping. */
11852 	if (un->un_state == SD_STATE_DUMPING) {
11853 		bioerror(bp, ENXIO);
11854 		bp->b_resid = bp->b_bcount;
11855 		biodone(bp);
11856 		return (0);
11857 	}
11858 
11859 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11860 
11861 	/*
11862 	 * Commands may sneak in while we released the mutex in
11863 	 * DDI_SUSPEND, we should block new commands. However, old
11864 	 * commands that are still in the driver at this point should
11865 	 * still be allowed to drain.
11866 	 */
11867 	mutex_enter(SD_MUTEX(un));
11868 	/*
11869 	 * Must wait here if either the device is suspended or
11870 	 * if it's power level is changing.
11871 	 */
11872 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11873 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11874 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11875 	}
11876 
11877 	un->un_ncmds_in_driver++;
11878 
11879 	/*
11880 	 * atapi: Since we are running the CD for now in PIO mode we need to
11881 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11882 	 * the HBA's init_pkt routine.
11883 	 */
11884 	if (un->un_f_cfg_is_atapi == TRUE) {
11885 		mutex_exit(SD_MUTEX(un));
11886 		bp_mapin(bp);
11887 		mutex_enter(SD_MUTEX(un));
11888 	}
11889 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11890 	    un->un_ncmds_in_driver);
11891 
11892 	mutex_exit(SD_MUTEX(un));
11893 
11894 	/*
11895 	 * This will (eventually) allocate the sd_xbuf area and
11896 	 * call sd_xbuf_strategy().  We just want to return the
11897 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11898 	 * imized tail call which saves us a stack frame.
11899 	 */
11900 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11901 }
11902 
11903 
11904 /*
11905  *    Function: sd_xbuf_strategy
11906  *
11907  * Description: Function for initiating IO operations via the
11908  *		ddi_xbuf_qstrategy() mechanism.
11909  *
11910  *     Context: Kernel thread context.
11911  */
11912 
11913 static void
11914 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11915 {
11916 	struct sd_lun *un = arg;
11917 
11918 	ASSERT(bp != NULL);
11919 	ASSERT(xp != NULL);
11920 	ASSERT(un != NULL);
11921 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11922 
11923 	/*
11924 	 * Initialize the fields in the xbuf and save a pointer to the
11925 	 * xbuf in bp->b_private.
11926 	 */
11927 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11928 
11929 	/* Send the buf down the iostart chain */
11930 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11931 }
11932 
11933 
11934 /*
11935  *    Function: sd_xbuf_init
11936  *
11937  * Description: Prepare the given sd_xbuf struct for use.
11938  *
11939  *   Arguments: un - ptr to softstate
11940  *		bp - ptr to associated buf(9S)
11941  *		xp - ptr to associated sd_xbuf
11942  *		chain_type - IO chain type to use:
11943  *			SD_CHAIN_NULL
11944  *			SD_CHAIN_BUFIO
11945  *			SD_CHAIN_USCSI
11946  *			SD_CHAIN_DIRECT
11947  *			SD_CHAIN_DIRECT_PRIORITY
11948  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11949  *			initialization; may be NULL if none.
11950  *
11951  *     Context: Kernel thread context
11952  */
11953 
11954 static void
11955 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11956 	uchar_t chain_type, void *pktinfop)
11957 {
11958 	int index;
11959 
11960 	ASSERT(un != NULL);
11961 	ASSERT(bp != NULL);
11962 	ASSERT(xp != NULL);
11963 
11964 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11965 	    bp, chain_type);
11966 
11967 	xp->xb_un	= un;
11968 	xp->xb_pktp	= NULL;
11969 	xp->xb_pktinfo	= pktinfop;
11970 	xp->xb_private	= bp->b_private;
11971 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
11972 
11973 	/*
11974 	 * Set up the iostart and iodone chain indexes in the xbuf, based
11975 	 * upon the specified chain type to use.
11976 	 */
11977 	switch (chain_type) {
11978 	case SD_CHAIN_NULL:
11979 		/*
11980 		 * Fall thru to just use the values for the buf type, even
11981 		 * tho for the NULL chain these values will never be used.
11982 		 */
11983 		/* FALLTHRU */
11984 	case SD_CHAIN_BUFIO:
11985 		index = un->un_buf_chain_type;
11986 		break;
11987 	case SD_CHAIN_USCSI:
11988 		index = un->un_uscsi_chain_type;
11989 		break;
11990 	case SD_CHAIN_DIRECT:
11991 		index = un->un_direct_chain_type;
11992 		break;
11993 	case SD_CHAIN_DIRECT_PRIORITY:
11994 		index = un->un_priority_chain_type;
11995 		break;
11996 	default:
11997 		/* We're really broken if we ever get here... */
11998 		panic("sd_xbuf_init: illegal chain type!");
11999 		/*NOTREACHED*/
12000 	}
12001 
12002 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
12003 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
12004 
12005 	/*
12006 	 * It might be a bit easier to simply bzero the entire xbuf above,
12007 	 * but it turns out that since we init a fair number of members anyway,
12008 	 * we save a fair number cycles by doing explicit assignment of zero.
12009 	 */
12010 	xp->xb_pkt_flags	= 0;
12011 	xp->xb_dma_resid	= 0;
12012 	xp->xb_retry_count	= 0;
12013 	xp->xb_victim_retry_count = 0;
12014 	xp->xb_ua_retry_count	= 0;
12015 	xp->xb_sense_bp		= NULL;
12016 	xp->xb_sense_status	= 0;
12017 	xp->xb_sense_state	= 0;
12018 	xp->xb_sense_resid	= 0;
12019 
12020 	bp->b_private	= xp;
12021 	bp->b_flags	&= ~(B_DONE | B_ERROR);
12022 	bp->b_resid	= 0;
12023 	bp->av_forw	= NULL;
12024 	bp->av_back	= NULL;
12025 	bioerror(bp, 0);
12026 
12027 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
12028 }
12029 
12030 
12031 /*
12032  *    Function: sd_uscsi_strategy
12033  *
12034  * Description: Wrapper for calling into the USCSI chain via physio(9F)
12035  *
12036  *   Arguments: bp - buf struct ptr
12037  *
12038  * Return Code: Always returns 0
12039  *
12040  *     Context: Kernel thread context
12041  */
12042 
12043 static int
12044 sd_uscsi_strategy(struct buf *bp)
12045 {
12046 	struct sd_lun		*un;
12047 	struct sd_uscsi_info	*uip;
12048 	struct sd_xbuf		*xp;
12049 	uchar_t			chain_type;
12050 
12051 	ASSERT(bp != NULL);
12052 
12053 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
12054 	if (un == NULL) {
12055 		bioerror(bp, EIO);
12056 		bp->b_resid = bp->b_bcount;
12057 		biodone(bp);
12058 		return (0);
12059 	}
12060 
12061 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12062 
12063 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
12064 
12065 	mutex_enter(SD_MUTEX(un));
12066 	/*
12067 	 * atapi: Since we are running the CD for now in PIO mode we need to
12068 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
12069 	 * the HBA's init_pkt routine.
12070 	 */
12071 	if (un->un_f_cfg_is_atapi == TRUE) {
12072 		mutex_exit(SD_MUTEX(un));
12073 		bp_mapin(bp);
12074 		mutex_enter(SD_MUTEX(un));
12075 	}
12076 	un->un_ncmds_in_driver++;
12077 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
12078 	    un->un_ncmds_in_driver);
12079 	mutex_exit(SD_MUTEX(un));
12080 
12081 	/*
12082 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
12083 	 */
12084 	ASSERT(bp->b_private != NULL);
12085 	uip = (struct sd_uscsi_info *)bp->b_private;
12086 
12087 	switch (uip->ui_flags) {
12088 	case SD_PATH_DIRECT:
12089 		chain_type = SD_CHAIN_DIRECT;
12090 		break;
12091 	case SD_PATH_DIRECT_PRIORITY:
12092 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
12093 		break;
12094 	default:
12095 		chain_type = SD_CHAIN_USCSI;
12096 		break;
12097 	}
12098 
12099 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
12100 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
12101 
12102 	/* Use the index obtained within xbuf_init */
12103 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
12104 
12105 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
12106 
12107 	return (0);
12108 }
12109 
12110 
12111 /*
12112  * These routines perform raw i/o operations.
12113  */
12114 /*ARGSUSED*/
12115 static void
12116 sduscsimin(struct buf *bp)
12117 {
12118 	/*
12119 	 * do not break up because the CDB count would then
12120 	 * be incorrect and data underruns would result (incomplete
12121 	 * read/writes which would be retried and then failed, see
12122 	 * sdintr().
12123 	 */
12124 }
12125 
12126 
12127 
12128 /*
12129  *    Function: sd_send_scsi_cmd
12130  *
12131  * Description: Runs a USCSI command for user (when called thru sdioctl),
12132  *		or for the driver
12133  *
12134  *   Arguments: dev - the dev_t for the device
12135  *		incmd - ptr to a valid uscsi_cmd struct
12136  *		cdbspace - UIO_USERSPACE or UIO_SYSSPACE
12137  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
12138  *		rqbufspace - UIO_USERSPACE or UIO_SYSSPACE
12139  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
12140  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
12141  *			to use the USCSI "direct" chain and bypass the normal
12142  *			command waitq.
12143  *
12144  * Return Code: 0 -  successful completion of the given command
12145  *		EIO - scsi_reset() failed, or see biowait()/physio() codes.
12146  *		ENXIO  - soft state not found for specified dev
12147  *		EINVAL
12148  *		EFAULT - copyin/copyout error
12149  *		return code of biowait(9F) or physio(9F):
12150  *			EIO - IO error, caller may check incmd->uscsi_status
12151  *			ENXIO
12152  *			EACCES - reservation conflict
12153  *
12154  *     Context: Waits for command to complete. Can sleep.
12155  */
12156 
12157 static int
12158 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd,
12159 	enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace,
12160 	int path_flag)
12161 {
12162 	struct sd_uscsi_info	*uip;
12163 	struct uscsi_cmd	*uscmd;
12164 	struct sd_lun	*un;
12165 	struct buf	*bp;
12166 	int	rval;
12167 	int	flags;
12168 
12169 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
12170 	if (un == NULL) {
12171 		return (ENXIO);
12172 	}
12173 
12174 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12175 
12176 #ifdef SDDEBUG
12177 	switch (dataspace) {
12178 	case UIO_USERSPACE:
12179 		SD_TRACE(SD_LOG_IO, un,
12180 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un);
12181 		break;
12182 	case UIO_SYSSPACE:
12183 		SD_TRACE(SD_LOG_IO, un,
12184 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un);
12185 		break;
12186 	default:
12187 		SD_TRACE(SD_LOG_IO, un,
12188 		    "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un);
12189 		break;
12190 	}
12191 #endif
12192 
12193 	/*
12194 	 * Perform resets directly; no need to generate a command to do it.
12195 	 */
12196 	if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) {
12197 		flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ?
12198 		    RESET_ALL : RESET_TARGET;
12199 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n");
12200 		if (scsi_reset(SD_ADDRESS(un), flags) == 0) {
12201 			/* Reset attempt was unsuccessful */
12202 			SD_TRACE(SD_LOG_IO, un,
12203 			    "sd_send_scsi_cmd: reset: failure\n");
12204 			return (EIO);
12205 		}
12206 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n");
12207 		return (0);
12208 	}
12209 
12210 	/* Perfunctory sanity check... */
12211 	if (incmd->uscsi_cdblen <= 0) {
12212 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12213 		    "invalid uscsi_cdblen, returning EINVAL\n");
12214 		return (EINVAL);
12215 	} else if (incmd->uscsi_cdblen > un->un_max_hba_cdb) {
12216 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12217 		    "unsupported uscsi_cdblen, returning EINVAL\n");
12218 		return (EINVAL);
12219 	}
12220 
12221 	/*
12222 	 * In order to not worry about where the uscsi structure came from
12223 	 * (or where the cdb it points to came from) we're going to make
12224 	 * kmem_alloc'd copies of them here. This will also allow reference
12225 	 * to the data they contain long after this process has gone to
12226 	 * sleep and its kernel stack has been unmapped, etc.
12227 	 *
12228 	 * First get some memory for the uscsi_cmd struct and copy the
12229 	 * contents of the given uscsi_cmd struct into it.
12230 	 */
12231 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
12232 	bcopy(incmd, uscmd, sizeof (struct uscsi_cmd));
12233 
12234 	SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd",
12235 	    (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX);
12236 
12237 	/*
12238 	 * Now get some space for the CDB, and copy the given CDB into
12239 	 * it. Use ddi_copyin() in case the data is in user space.
12240 	 */
12241 	uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP);
12242 	flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0;
12243 	if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb,
12244 	    (uint_t)incmd->uscsi_cdblen, flags) != 0) {
12245 		kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen);
12246 		kmem_free(uscmd, sizeof (struct uscsi_cmd));
12247 		return (EFAULT);
12248 	}
12249 
12250 	SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB",
12251 	    (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX);
12252 
12253 	bp = getrbuf(KM_SLEEP);
12254 
12255 	/*
12256 	 * Allocate an sd_uscsi_info struct and fill it with the info
12257 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
12258 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
12259 	 * since we allocate the buf here in this function, we do not
12260 	 * need to preserve the prior contents of b_private.
12261 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12262 	 */
12263 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
12264 	uip->ui_flags = path_flag;
12265 	uip->ui_cmdp  = uscmd;
12266 	bp->b_private = uip;
12267 
12268 	/*
12269 	 * Initialize Request Sense buffering, if requested.
12270 	 */
12271 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
12272 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
12273 		/*
12274 		 * Here uscmd->uscsi_rqbuf currently points to the caller's
12275 		 * buffer, but we replace this with a kernel buffer that
12276 		 * we allocate to use with the sense data. The sense data
12277 		 * (if present) gets copied into this new buffer before the
12278 		 * command is completed.  Then we copy the sense data from
12279 		 * our allocated buf into the caller's buffer below. Note
12280 		 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used
12281 		 * below to perform the copy back to the caller's buf.
12282 		 */
12283 		uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
12284 		if (rqbufspace == UIO_USERSPACE) {
12285 			uscmd->uscsi_rqlen   = SENSE_LENGTH;
12286 			uscmd->uscsi_rqresid = SENSE_LENGTH;
12287 		} else {
12288 			uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen);
12289 			uscmd->uscsi_rqlen   = rlen;
12290 			uscmd->uscsi_rqresid = rlen;
12291 		}
12292 	} else {
12293 		uscmd->uscsi_rqbuf = NULL;
12294 		uscmd->uscsi_rqlen   = 0;
12295 		uscmd->uscsi_rqresid = 0;
12296 	}
12297 
12298 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p  rqlen:%d\n",
12299 	    uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen);
12300 
12301 	if (un->un_f_is_fibre == FALSE) {
12302 		/*
12303 		 * Force asynchronous mode, if necessary.  Doing this here
12304 		 * has the unfortunate effect of running other queued
12305 		 * commands async also, but since the main purpose of this
12306 		 * capability is downloading new drive firmware, we can
12307 		 * probably live with it.
12308 		 */
12309 		if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) {
12310 			if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1)
12311 				== 1) {
12312 				if (scsi_ifsetcap(SD_ADDRESS(un),
12313 					    "synchronous", 0, 1) == 1) {
12314 					SD_TRACE(SD_LOG_IO, un,
12315 					"sd_send_scsi_cmd: forced async ok\n");
12316 				} else {
12317 					SD_TRACE(SD_LOG_IO, un,
12318 					"sd_send_scsi_cmd:\
12319 					forced async failed\n");
12320 					rval = EINVAL;
12321 					goto done;
12322 				}
12323 			}
12324 		}
12325 
12326 		/*
12327 		 * Re-enable synchronous mode, if requested
12328 		 */
12329 		if (uscmd->uscsi_flags & USCSI_SYNC) {
12330 			if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1)
12331 				== 0) {
12332 				int i = scsi_ifsetcap(SD_ADDRESS(un),
12333 						"synchronous", 1, 1);
12334 				SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12335 					"re-enabled sync %s\n",
12336 					(i == 1) ? "ok" : "failed");
12337 			}
12338 		}
12339 	}
12340 
12341 	/*
12342 	 * Commands sent with priority are intended for error recovery
12343 	 * situations, and do not have retries performed.
12344 	 */
12345 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12346 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12347 	}
12348 
12349 	/*
12350 	 * If we're going to do actual I/O, let physio do all the right things
12351 	 */
12352 	if (uscmd->uscsi_buflen != 0) {
12353 		struct iovec	aiov;
12354 		struct uio	auio;
12355 		struct uio	*uio = &auio;
12356 
12357 		bzero(&auio, sizeof (struct uio));
12358 		bzero(&aiov, sizeof (struct iovec));
12359 		aiov.iov_base = uscmd->uscsi_bufaddr;
12360 		aiov.iov_len  = uscmd->uscsi_buflen;
12361 		uio->uio_iov  = &aiov;
12362 
12363 		uio->uio_iovcnt  = 1;
12364 		uio->uio_resid   = uscmd->uscsi_buflen;
12365 		uio->uio_segflg  = dataspace;
12366 
12367 		/*
12368 		 * physio() will block here until the command completes....
12369 		 */
12370 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n");
12371 
12372 		rval = physio(sd_uscsi_strategy, bp, dev,
12373 		    ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE),
12374 		    sduscsimin, uio);
12375 
12376 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12377 		    "returned from physio with 0x%x\n", rval);
12378 
12379 	} else {
12380 		/*
12381 		 * We have to mimic what physio would do here! Argh!
12382 		 */
12383 		bp->b_flags  = B_BUSY |
12384 		    ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE);
12385 		bp->b_edev   = dev;
12386 		bp->b_dev    = cmpdev(dev);	/* maybe unnecessary? */
12387 		bp->b_bcount = 0;
12388 		bp->b_blkno  = 0;
12389 
12390 		SD_TRACE(SD_LOG_IO, un,
12391 		    "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n");
12392 
12393 		(void) sd_uscsi_strategy(bp);
12394 
12395 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n");
12396 
12397 		rval = biowait(bp);
12398 
12399 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12400 		    "returned from  biowait with 0x%x\n", rval);
12401 	}
12402 
12403 done:
12404 
12405 #ifdef SDDEBUG
12406 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12407 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
12408 	    uscmd->uscsi_status, uscmd->uscsi_resid);
12409 	if (uscmd->uscsi_bufaddr != NULL) {
12410 		SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12411 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
12412 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12413 		if (dataspace == UIO_SYSSPACE) {
12414 			SD_DUMP_MEMORY(un, SD_LOG_IO,
12415 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
12416 			    uscmd->uscsi_buflen, SD_LOG_HEX);
12417 		}
12418 	}
12419 #endif
12420 
12421 	/*
12422 	 * Get the status and residual to return to the caller.
12423 	 */
12424 	incmd->uscsi_status = uscmd->uscsi_status;
12425 	incmd->uscsi_resid  = uscmd->uscsi_resid;
12426 
12427 	/*
12428 	 * If the caller wants sense data, copy back whatever sense data
12429 	 * we may have gotten, and update the relevant rqsense info.
12430 	 */
12431 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
12432 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
12433 
12434 		int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid;
12435 		rqlen = min(((int)incmd->uscsi_rqlen), rqlen);
12436 
12437 		/* Update the Request Sense status and resid */
12438 		incmd->uscsi_rqresid  = incmd->uscsi_rqlen - rqlen;
12439 		incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus;
12440 
12441 		SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12442 		    "uscsi_rqstatus: 0x%02x  uscsi_rqresid:0x%x\n",
12443 		    incmd->uscsi_rqstatus, incmd->uscsi_rqresid);
12444 
12445 		/* Copy out the sense data for user processes */
12446 		if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) {
12447 			int flags =
12448 			    (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL;
12449 			if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf,
12450 			    rqlen, flags) != 0) {
12451 				rval = EFAULT;
12452 			}
12453 			/*
12454 			 * Note: Can't touch incmd->uscsi_rqbuf so use
12455 			 * uscmd->uscsi_rqbuf instead. They're the same.
12456 			 */
12457 			SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12458 			    "incmd->uscsi_rqbuf: 0x%p  rqlen:%d\n",
12459 			    incmd->uscsi_rqbuf, rqlen);
12460 			SD_DUMP_MEMORY(un, SD_LOG_IO, "rq",
12461 			    (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX);
12462 		}
12463 	}
12464 
12465 	/*
12466 	 * Free allocated resources and return; mapout the buf in case it was
12467 	 * mapped in by a lower layer.
12468 	 */
12469 	bp_mapout(bp);
12470 	freerbuf(bp);
12471 	kmem_free(uip, sizeof (struct sd_uscsi_info));
12472 	if (uscmd->uscsi_rqbuf != NULL) {
12473 		kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
12474 	}
12475 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
12476 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
12477 
12478 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n");
12479 
12480 	return (rval);
12481 }
12482 
12483 
12484 /*
12485  *    Function: sd_buf_iodone
12486  *
12487  * Description: Frees the sd_xbuf & returns the buf to its originator.
12488  *
12489  *     Context: May be called from interrupt context.
12490  */
12491 /* ARGSUSED */
12492 static void
12493 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12494 {
12495 	struct sd_xbuf *xp;
12496 
12497 	ASSERT(un != NULL);
12498 	ASSERT(bp != NULL);
12499 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12500 
12501 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12502 
12503 	xp = SD_GET_XBUF(bp);
12504 	ASSERT(xp != NULL);
12505 
12506 	mutex_enter(SD_MUTEX(un));
12507 
12508 	/*
12509 	 * Grab time when the cmd completed.
12510 	 * This is used for determining if the system has been
12511 	 * idle long enough to make it idle to the PM framework.
12512 	 * This is for lowering the overhead, and therefore improving
12513 	 * performance per I/O operation.
12514 	 */
12515 	un->un_pm_idle_time = ddi_get_time();
12516 
12517 	un->un_ncmds_in_driver--;
12518 	ASSERT(un->un_ncmds_in_driver >= 0);
12519 	SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12520 	    un->un_ncmds_in_driver);
12521 
12522 	mutex_exit(SD_MUTEX(un));
12523 
12524 	ddi_xbuf_done(bp, un->un_xbuf_attr);	/* xbuf is gone after this */
12525 	biodone(bp);				/* bp is gone after this */
12526 
12527 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12528 }
12529 
12530 
12531 /*
12532  *    Function: sd_uscsi_iodone
12533  *
12534  * Description: Frees the sd_xbuf & returns the buf to its originator.
12535  *
12536  *     Context: May be called from interrupt context.
12537  */
12538 /* ARGSUSED */
12539 static void
12540 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12541 {
12542 	struct sd_xbuf *xp;
12543 
12544 	ASSERT(un != NULL);
12545 	ASSERT(bp != NULL);
12546 
12547 	xp = SD_GET_XBUF(bp);
12548 	ASSERT(xp != NULL);
12549 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12550 
12551 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12552 
12553 	bp->b_private = xp->xb_private;
12554 
12555 	mutex_enter(SD_MUTEX(un));
12556 
12557 	/*
12558 	 * Grab time when the cmd completed.
12559 	 * This is used for determining if the system has been
12560 	 * idle long enough to make it idle to the PM framework.
12561 	 * This is for lowering the overhead, and therefore improving
12562 	 * performance per I/O operation.
12563 	 */
12564 	un->un_pm_idle_time = ddi_get_time();
12565 
12566 	un->un_ncmds_in_driver--;
12567 	ASSERT(un->un_ncmds_in_driver >= 0);
12568 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12569 	    un->un_ncmds_in_driver);
12570 
12571 	mutex_exit(SD_MUTEX(un));
12572 
12573 	kmem_free(xp, sizeof (struct sd_xbuf));
12574 	biodone(bp);
12575 
12576 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12577 }
12578 
12579 
12580 /*
12581  *    Function: sd_mapblockaddr_iostart
12582  *
12583  * Description: Verify request lies withing the partition limits for
12584  *		the indicated minor device.  Issue "overrun" buf if
12585  *		request would exceed partition range.  Converts
12586  *		partition-relative block address to absolute.
12587  *
12588  *     Context: Can sleep
12589  *
12590  *      Issues: This follows what the old code did, in terms of accessing
12591  *		some of the partition info in the unit struct without holding
12592  *		the mutext.  This is a general issue, if the partition info
12593  *		can be altered while IO is in progress... as soon as we send
12594  *		a buf, its partitioning can be invalid before it gets to the
12595  *		device.  Probably the right fix is to move partitioning out
12596  *		of the driver entirely.
12597  */
12598 
12599 static void
12600 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12601 {
12602 	daddr_t	nblocks;	/* #blocks in the given partition */
12603 	daddr_t	blocknum;	/* Block number specified by the buf */
12604 	size_t	requested_nblocks;
12605 	size_t	available_nblocks;
12606 	int	partition;
12607 	diskaddr_t	partition_offset;
12608 	struct sd_xbuf *xp;
12609 
12610 
12611 	ASSERT(un != NULL);
12612 	ASSERT(bp != NULL);
12613 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12614 
12615 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12616 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12617 
12618 	xp = SD_GET_XBUF(bp);
12619 	ASSERT(xp != NULL);
12620 
12621 	/*
12622 	 * If the geometry is not indicated as valid, attempt to access
12623 	 * the unit & verify the geometry/label. This can be the case for
12624 	 * removable-media devices, of if the device was opened in
12625 	 * NDELAY/NONBLOCK mode.
12626 	 */
12627 	if ((un->un_f_geometry_is_valid != TRUE) &&
12628 	    (sd_ready_and_valid(un) != SD_READY_VALID)) {
12629 		/*
12630 		 * For removable devices it is possible to start an I/O
12631 		 * without a media by opening the device in nodelay mode.
12632 		 * Also for writable CDs there can be many scenarios where
12633 		 * there is no geometry yet but volume manager is trying to
12634 		 * issue a read() just because it can see TOC on the CD. So
12635 		 * do not print a message for removables.
12636 		 */
12637 		if (!un->un_f_has_removable_media) {
12638 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12639 			    "i/o to invalid geometry\n");
12640 		}
12641 		bioerror(bp, EIO);
12642 		bp->b_resid = bp->b_bcount;
12643 		SD_BEGIN_IODONE(index, un, bp);
12644 		return;
12645 	}
12646 
12647 	partition = SDPART(bp->b_edev);
12648 
12649 	/* #blocks in partition */
12650 	nblocks = un->un_map[partition].dkl_nblk;    /* #blocks in partition */
12651 
12652 	/* Use of a local variable potentially improves performance slightly */
12653 	partition_offset = un->un_offset[partition];
12654 
12655 	/*
12656 	 * blocknum is the starting block number of the request. At this
12657 	 * point it is still relative to the start of the minor device.
12658 	 */
12659 	blocknum = xp->xb_blkno;
12660 
12661 	/*
12662 	 * Legacy: If the starting block number is one past the last block
12663 	 * in the partition, do not set B_ERROR in the buf.
12664 	 */
12665 	if (blocknum == nblocks)  {
12666 		goto error_exit;
12667 	}
12668 
12669 	/*
12670 	 * Confirm that the first block of the request lies within the
12671 	 * partition limits. Also the requested number of bytes must be
12672 	 * a multiple of the system block size.
12673 	 */
12674 	if ((blocknum < 0) || (blocknum >= nblocks) ||
12675 	    ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) {
12676 		bp->b_flags |= B_ERROR;
12677 		goto error_exit;
12678 	}
12679 
12680 	/*
12681 	 * If the requsted # blocks exceeds the available # blocks, that
12682 	 * is an overrun of the partition.
12683 	 */
12684 	requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount);
12685 	available_nblocks = (size_t)(nblocks - blocknum);
12686 	ASSERT(nblocks >= blocknum);
12687 
12688 	if (requested_nblocks > available_nblocks) {
12689 		/*
12690 		 * Allocate an "overrun" buf to allow the request to proceed
12691 		 * for the amount of space available in the partition. The
12692 		 * amount not transferred will be added into the b_resid
12693 		 * when the operation is complete. The overrun buf
12694 		 * replaces the original buf here, and the original buf
12695 		 * is saved inside the overrun buf, for later use.
12696 		 */
12697 		size_t resid = SD_SYSBLOCKS2BYTES(un,
12698 		    (offset_t)(requested_nblocks - available_nblocks));
12699 		size_t count = bp->b_bcount - resid;
12700 		/*
12701 		 * Note: count is an unsigned entity thus it'll NEVER
12702 		 * be less than 0 so ASSERT the original values are
12703 		 * correct.
12704 		 */
12705 		ASSERT(bp->b_bcount >= resid);
12706 
12707 		bp = sd_bioclone_alloc(bp, count, blocknum,
12708 			(int (*)(struct buf *)) sd_mapblockaddr_iodone);
12709 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12710 		ASSERT(xp != NULL);
12711 	}
12712 
12713 	/* At this point there should be no residual for this buf. */
12714 	ASSERT(bp->b_resid == 0);
12715 
12716 	/* Convert the block number to an absolute address. */
12717 	xp->xb_blkno += partition_offset;
12718 
12719 	SD_NEXT_IOSTART(index, un, bp);
12720 
12721 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12722 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12723 
12724 	return;
12725 
12726 error_exit:
12727 	bp->b_resid = bp->b_bcount;
12728 	SD_BEGIN_IODONE(index, un, bp);
12729 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12730 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12731 }
12732 
12733 
12734 /*
12735  *    Function: sd_mapblockaddr_iodone
12736  *
12737  * Description: Completion-side processing for partition management.
12738  *
12739  *     Context: May be called under interrupt context
12740  */
12741 
12742 static void
12743 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12744 {
12745 	/* int	partition; */	/* Not used, see below. */
12746 	ASSERT(un != NULL);
12747 	ASSERT(bp != NULL);
12748 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12749 
12750 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12751 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12752 
12753 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12754 		/*
12755 		 * We have an "overrun" buf to deal with...
12756 		 */
12757 		struct sd_xbuf	*xp;
12758 		struct buf	*obp;	/* ptr to the original buf */
12759 
12760 		xp = SD_GET_XBUF(bp);
12761 		ASSERT(xp != NULL);
12762 
12763 		/* Retrieve the pointer to the original buf */
12764 		obp = (struct buf *)xp->xb_private;
12765 		ASSERT(obp != NULL);
12766 
12767 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12768 		bioerror(obp, bp->b_error);
12769 
12770 		sd_bioclone_free(bp);
12771 
12772 		/*
12773 		 * Get back the original buf.
12774 		 * Note that since the restoration of xb_blkno below
12775 		 * was removed, the sd_xbuf is not needed.
12776 		 */
12777 		bp = obp;
12778 		/*
12779 		 * xp = SD_GET_XBUF(bp);
12780 		 * ASSERT(xp != NULL);
12781 		 */
12782 	}
12783 
12784 	/*
12785 	 * Convert sd->xb_blkno back to a minor-device relative value.
12786 	 * Note: this has been commented out, as it is not needed in the
12787 	 * current implementation of the driver (ie, since this function
12788 	 * is at the top of the layering chains, so the info will be
12789 	 * discarded) and it is in the "hot" IO path.
12790 	 *
12791 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
12792 	 * xp->xb_blkno -= un->un_offset[partition];
12793 	 */
12794 
12795 	SD_NEXT_IODONE(index, un, bp);
12796 
12797 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12798 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12799 }
12800 
12801 
12802 /*
12803  *    Function: sd_mapblocksize_iostart
12804  *
12805  * Description: Convert between system block size (un->un_sys_blocksize)
12806  *		and target block size (un->un_tgt_blocksize).
12807  *
12808  *     Context: Can sleep to allocate resources.
12809  *
12810  * Assumptions: A higher layer has already performed any partition validation,
12811  *		and converted the xp->xb_blkno to an absolute value relative
12812  *		to the start of the device.
12813  *
12814  *		It is also assumed that the higher layer has implemented
12815  *		an "overrun" mechanism for the case where the request would
12816  *		read/write beyond the end of a partition.  In this case we
12817  *		assume (and ASSERT) that bp->b_resid == 0.
12818  *
12819  *		Note: The implementation for this routine assumes the target
12820  *		block size remains constant between allocation and transport.
12821  */
12822 
12823 static void
12824 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12825 {
12826 	struct sd_mapblocksize_info	*bsp;
12827 	struct sd_xbuf			*xp;
12828 	offset_t first_byte;
12829 	daddr_t	start_block, end_block;
12830 	daddr_t	request_bytes;
12831 	ushort_t is_aligned = FALSE;
12832 
12833 	ASSERT(un != NULL);
12834 	ASSERT(bp != NULL);
12835 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12836 	ASSERT(bp->b_resid == 0);
12837 
12838 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12839 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12840 
12841 	/*
12842 	 * For a non-writable CD, a write request is an error
12843 	 */
12844 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12845 	    (un->un_f_mmc_writable_media == FALSE)) {
12846 		bioerror(bp, EIO);
12847 		bp->b_resid = bp->b_bcount;
12848 		SD_BEGIN_IODONE(index, un, bp);
12849 		return;
12850 	}
12851 
12852 	/*
12853 	 * We do not need a shadow buf if the device is using
12854 	 * un->un_sys_blocksize as its block size or if bcount == 0.
12855 	 * In this case there is no layer-private data block allocated.
12856 	 */
12857 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
12858 	    (bp->b_bcount == 0)) {
12859 		goto done;
12860 	}
12861 
12862 #if defined(__i386) || defined(__amd64)
12863 	/* We do not support non-block-aligned transfers for ROD devices */
12864 	ASSERT(!ISROD(un));
12865 #endif
12866 
12867 	xp = SD_GET_XBUF(bp);
12868 	ASSERT(xp != NULL);
12869 
12870 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12871 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12872 	    un->un_tgt_blocksize, un->un_sys_blocksize);
12873 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12874 	    "request start block:0x%x\n", xp->xb_blkno);
12875 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12876 	    "request len:0x%x\n", bp->b_bcount);
12877 
12878 	/*
12879 	 * Allocate the layer-private data area for the mapblocksize layer.
12880 	 * Layers are allowed to use the xp_private member of the sd_xbuf
12881 	 * struct to store the pointer to their layer-private data block, but
12882 	 * each layer also has the responsibility of restoring the prior
12883 	 * contents of xb_private before returning the buf/xbuf to the
12884 	 * higher layer that sent it.
12885 	 *
12886 	 * Here we save the prior contents of xp->xb_private into the
12887 	 * bsp->mbs_oprivate field of our layer-private data area. This value
12888 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12889 	 * the layer-private area and returning the buf/xbuf to the layer
12890 	 * that sent it.
12891 	 *
12892 	 * Note that here we use kmem_zalloc for the allocation as there are
12893 	 * parts of the mapblocksize code that expect certain fields to be
12894 	 * zero unless explicitly set to a required value.
12895 	 */
12896 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12897 	bsp->mbs_oprivate = xp->xb_private;
12898 	xp->xb_private = bsp;
12899 
12900 	/*
12901 	 * This treats the data on the disk (target) as an array of bytes.
12902 	 * first_byte is the byte offset, from the beginning of the device,
12903 	 * to the location of the request. This is converted from a
12904 	 * un->un_sys_blocksize block address to a byte offset, and then back
12905 	 * to a block address based upon a un->un_tgt_blocksize block size.
12906 	 *
12907 	 * xp->xb_blkno should be absolute upon entry into this function,
12908 	 * but, but it is based upon partitions that use the "system"
12909 	 * block size. It must be adjusted to reflect the block size of
12910 	 * the target.
12911 	 *
12912 	 * Note that end_block is actually the block that follows the last
12913 	 * block of the request, but that's what is needed for the computation.
12914 	 */
12915 	first_byte  = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12916 	start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12917 	end_block   = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) /
12918 	    un->un_tgt_blocksize;
12919 
12920 	/* request_bytes is rounded up to a multiple of the target block size */
12921 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12922 
12923 	/*
12924 	 * See if the starting address of the request and the request
12925 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12926 	 * then we do not need to allocate a shadow buf to handle the request.
12927 	 */
12928 	if (((first_byte   % un->un_tgt_blocksize) == 0) &&
12929 	    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12930 		is_aligned = TRUE;
12931 	}
12932 
12933 	if ((bp->b_flags & B_READ) == 0) {
12934 		/*
12935 		 * Lock the range for a write operation. An aligned request is
12936 		 * considered a simple write; otherwise the request must be a
12937 		 * read-modify-write.
12938 		 */
12939 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12940 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12941 	}
12942 
12943 	/*
12944 	 * Alloc a shadow buf if the request is not aligned. Also, this is
12945 	 * where the READ command is generated for a read-modify-write. (The
12946 	 * write phase is deferred until after the read completes.)
12947 	 */
12948 	if (is_aligned == FALSE) {
12949 
12950 		struct sd_mapblocksize_info	*shadow_bsp;
12951 		struct sd_xbuf	*shadow_xp;
12952 		struct buf	*shadow_bp;
12953 
12954 		/*
12955 		 * Allocate the shadow buf and it associated xbuf. Note that
12956 		 * after this call the xb_blkno value in both the original
12957 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12958 		 * same: absolute relative to the start of the device, and
12959 		 * adjusted for the target block size. The b_blkno in the
12960 		 * shadow buf will also be set to this value. We should never
12961 		 * change b_blkno in the original bp however.
12962 		 *
12963 		 * Note also that the shadow buf will always need to be a
12964 		 * READ command, regardless of whether the incoming command
12965 		 * is a READ or a WRITE.
12966 		 */
12967 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
12968 		    xp->xb_blkno,
12969 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
12970 
12971 		shadow_xp = SD_GET_XBUF(shadow_bp);
12972 
12973 		/*
12974 		 * Allocate the layer-private data for the shadow buf.
12975 		 * (No need to preserve xb_private in the shadow xbuf.)
12976 		 */
12977 		shadow_xp->xb_private = shadow_bsp =
12978 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12979 
12980 		/*
12981 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
12982 		 * to figure out where the start of the user data is (based upon
12983 		 * the system block size) in the data returned by the READ
12984 		 * command (which will be based upon the target blocksize). Note
12985 		 * that this is only really used if the request is unaligned.
12986 		 */
12987 		bsp->mbs_copy_offset = (ssize_t)(first_byte -
12988 		    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
12989 		ASSERT((bsp->mbs_copy_offset >= 0) &&
12990 		    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
12991 
12992 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
12993 
12994 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
12995 
12996 		/* Transfer the wmap (if any) to the shadow buf */
12997 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
12998 		bsp->mbs_wmp = NULL;
12999 
13000 		/*
13001 		 * The shadow buf goes on from here in place of the
13002 		 * original buf.
13003 		 */
13004 		shadow_bsp->mbs_orig_bp = bp;
13005 		bp = shadow_bp;
13006 	}
13007 
13008 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13009 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
13010 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13011 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
13012 	    request_bytes);
13013 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13014 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
13015 
13016 done:
13017 	SD_NEXT_IOSTART(index, un, bp);
13018 
13019 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13020 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
13021 }
13022 
13023 
13024 /*
13025  *    Function: sd_mapblocksize_iodone
13026  *
13027  * Description: Completion side processing for block-size mapping.
13028  *
13029  *     Context: May be called under interrupt context
13030  */
13031 
13032 static void
13033 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
13034 {
13035 	struct sd_mapblocksize_info	*bsp;
13036 	struct sd_xbuf	*xp;
13037 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
13038 	struct buf	*orig_bp;	/* ptr to the original buf */
13039 	offset_t	shadow_end;
13040 	offset_t	request_end;
13041 	offset_t	shadow_start;
13042 	ssize_t		copy_offset;
13043 	size_t		copy_length;
13044 	size_t		shortfall;
13045 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
13046 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
13047 
13048 	ASSERT(un != NULL);
13049 	ASSERT(bp != NULL);
13050 
13051 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13052 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13053 
13054 	/*
13055 	 * There is no shadow buf or layer-private data if the target is
13056 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
13057 	 */
13058 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
13059 	    (bp->b_bcount == 0)) {
13060 		goto exit;
13061 	}
13062 
13063 	xp = SD_GET_XBUF(bp);
13064 	ASSERT(xp != NULL);
13065 
13066 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
13067 	bsp = xp->xb_private;
13068 
13069 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13070 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13071 
13072 	if (is_write) {
13073 		/*
13074 		 * For a WRITE request we must free up the block range that
13075 		 * we have locked up.  This holds regardless of whether this is
13076 		 * an aligned write request or a read-modify-write request.
13077 		 */
13078 		sd_range_unlock(un, bsp->mbs_wmp);
13079 		bsp->mbs_wmp = NULL;
13080 	}
13081 
13082 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13083 		/*
13084 		 * An aligned read or write command will have no shadow buf;
13085 		 * there is not much else to do with it.
13086 		 */
13087 		goto done;
13088 	}
13089 
13090 	orig_bp = bsp->mbs_orig_bp;
13091 	ASSERT(orig_bp != NULL);
13092 	orig_xp = SD_GET_XBUF(orig_bp);
13093 	ASSERT(orig_xp != NULL);
13094 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13095 
13096 	if (!is_write && has_wmap) {
13097 		/*
13098 		 * A READ with a wmap means this is the READ phase of a
13099 		 * read-modify-write. If an error occurred on the READ then
13100 		 * we do not proceed with the WRITE phase or copy any data.
13101 		 * Just release the write maps and return with an error.
13102 		 */
13103 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13104 			orig_bp->b_resid = orig_bp->b_bcount;
13105 			bioerror(orig_bp, bp->b_error);
13106 			sd_range_unlock(un, bsp->mbs_wmp);
13107 			goto freebuf_done;
13108 		}
13109 	}
13110 
13111 	/*
13112 	 * Here is where we set up to copy the data from the shadow buf
13113 	 * into the space associated with the original buf.
13114 	 *
13115 	 * To deal with the conversion between block sizes, these
13116 	 * computations treat the data as an array of bytes, with the
13117 	 * first byte (byte 0) corresponding to the first byte in the
13118 	 * first block on the disk.
13119 	 */
13120 
13121 	/*
13122 	 * shadow_start and shadow_len indicate the location and size of
13123 	 * the data returned with the shadow IO request.
13124 	 */
13125 	shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13126 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
13127 
13128 	/*
13129 	 * copy_offset gives the offset (in bytes) from the start of the first
13130 	 * block of the READ request to the beginning of the data.  We retrieve
13131 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13132 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
13133 	 * data to be copied (in bytes).
13134 	 */
13135 	copy_offset  = bsp->mbs_copy_offset;
13136 	ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize));
13137 	copy_length  = orig_bp->b_bcount;
13138 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
13139 
13140 	/*
13141 	 * Set up the resid and error fields of orig_bp as appropriate.
13142 	 */
13143 	if (shadow_end >= request_end) {
13144 		/* We got all the requested data; set resid to zero */
13145 		orig_bp->b_resid = 0;
13146 	} else {
13147 		/*
13148 		 * We failed to get enough data to fully satisfy the original
13149 		 * request. Just copy back whatever data we got and set
13150 		 * up the residual and error code as required.
13151 		 *
13152 		 * 'shortfall' is the amount by which the data received with the
13153 		 * shadow buf has "fallen short" of the requested amount.
13154 		 */
13155 		shortfall = (size_t)(request_end - shadow_end);
13156 
13157 		if (shortfall > orig_bp->b_bcount) {
13158 			/*
13159 			 * We did not get enough data to even partially
13160 			 * fulfill the original request.  The residual is
13161 			 * equal to the amount requested.
13162 			 */
13163 			orig_bp->b_resid = orig_bp->b_bcount;
13164 		} else {
13165 			/*
13166 			 * We did not get all the data that we requested
13167 			 * from the device, but we will try to return what
13168 			 * portion we did get.
13169 			 */
13170 			orig_bp->b_resid = shortfall;
13171 		}
13172 		ASSERT(copy_length >= orig_bp->b_resid);
13173 		copy_length  -= orig_bp->b_resid;
13174 	}
13175 
13176 	/* Propagate the error code from the shadow buf to the original buf */
13177 	bioerror(orig_bp, bp->b_error);
13178 
13179 	if (is_write) {
13180 		goto freebuf_done;	/* No data copying for a WRITE */
13181 	}
13182 
13183 	if (has_wmap) {
13184 		/*
13185 		 * This is a READ command from the READ phase of a
13186 		 * read-modify-write request. We have to copy the data given
13187 		 * by the user OVER the data returned by the READ command,
13188 		 * then convert the command from a READ to a WRITE and send
13189 		 * it back to the target.
13190 		 */
13191 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13192 		    copy_length);
13193 
13194 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
13195 
13196 		/*
13197 		 * Dispatch the WRITE command to the taskq thread, which
13198 		 * will in turn send the command to the target. When the
13199 		 * WRITE command completes, we (sd_mapblocksize_iodone())
13200 		 * will get called again as part of the iodone chain
13201 		 * processing for it. Note that we will still be dealing
13202 		 * with the shadow buf at that point.
13203 		 */
13204 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13205 		    KM_NOSLEEP) != 0) {
13206 			/*
13207 			 * Dispatch was successful so we are done. Return
13208 			 * without going any higher up the iodone chain. Do
13209 			 * not free up any layer-private data until after the
13210 			 * WRITE completes.
13211 			 */
13212 			return;
13213 		}
13214 
13215 		/*
13216 		 * Dispatch of the WRITE command failed; set up the error
13217 		 * condition and send this IO back up the iodone chain.
13218 		 */
13219 		bioerror(orig_bp, EIO);
13220 		orig_bp->b_resid = orig_bp->b_bcount;
13221 
13222 	} else {
13223 		/*
13224 		 * This is a regular READ request (ie, not a RMW). Copy the
13225 		 * data from the shadow buf into the original buf. The
13226 		 * copy_offset compensates for any "misalignment" between the
13227 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
13228 		 * original buf (with its un->un_sys_blocksize blocks).
13229 		 */
13230 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13231 		    copy_length);
13232 	}
13233 
13234 freebuf_done:
13235 
13236 	/*
13237 	 * At this point we still have both the shadow buf AND the original
13238 	 * buf to deal with, as well as the layer-private data area in each.
13239 	 * Local variables are as follows:
13240 	 *
13241 	 * bp -- points to shadow buf
13242 	 * xp -- points to xbuf of shadow buf
13243 	 * bsp -- points to layer-private data area of shadow buf
13244 	 * orig_bp -- points to original buf
13245 	 *
13246 	 * First free the shadow buf and its associated xbuf, then free the
13247 	 * layer-private data area from the shadow buf. There is no need to
13248 	 * restore xb_private in the shadow xbuf.
13249 	 */
13250 	sd_shadow_buf_free(bp);
13251 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13252 
13253 	/*
13254 	 * Now update the local variables to point to the original buf, xbuf,
13255 	 * and layer-private area.
13256 	 */
13257 	bp = orig_bp;
13258 	xp = SD_GET_XBUF(bp);
13259 	ASSERT(xp != NULL);
13260 	ASSERT(xp == orig_xp);
13261 	bsp = xp->xb_private;
13262 	ASSERT(bsp != NULL);
13263 
13264 done:
13265 	/*
13266 	 * Restore xb_private to whatever it was set to by the next higher
13267 	 * layer in the chain, then free the layer-private data area.
13268 	 */
13269 	xp->xb_private = bsp->mbs_oprivate;
13270 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13271 
13272 exit:
13273 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13274 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13275 
13276 	SD_NEXT_IODONE(index, un, bp);
13277 }
13278 
13279 
13280 /*
13281  *    Function: sd_checksum_iostart
13282  *
13283  * Description: A stub function for a layer that's currently not used.
13284  *		For now just a placeholder.
13285  *
13286  *     Context: Kernel thread context
13287  */
13288 
13289 static void
13290 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13291 {
13292 	ASSERT(un != NULL);
13293 	ASSERT(bp != NULL);
13294 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13295 	SD_NEXT_IOSTART(index, un, bp);
13296 }
13297 
13298 
13299 /*
13300  *    Function: sd_checksum_iodone
13301  *
13302  * Description: A stub function for a layer that's currently not used.
13303  *		For now just a placeholder.
13304  *
13305  *     Context: May be called under interrupt context
13306  */
13307 
13308 static void
13309 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13310 {
13311 	ASSERT(un != NULL);
13312 	ASSERT(bp != NULL);
13313 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13314 	SD_NEXT_IODONE(index, un, bp);
13315 }
13316 
13317 
13318 /*
13319  *    Function: sd_checksum_uscsi_iostart
13320  *
13321  * Description: A stub function for a layer that's currently not used.
13322  *		For now just a placeholder.
13323  *
13324  *     Context: Kernel thread context
13325  */
13326 
13327 static void
13328 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13329 {
13330 	ASSERT(un != NULL);
13331 	ASSERT(bp != NULL);
13332 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13333 	SD_NEXT_IOSTART(index, un, bp);
13334 }
13335 
13336 
13337 /*
13338  *    Function: sd_checksum_uscsi_iodone
13339  *
13340  * Description: A stub function for a layer that's currently not used.
13341  *		For now just a placeholder.
13342  *
13343  *     Context: May be called under interrupt context
13344  */
13345 
13346 static void
13347 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13348 {
13349 	ASSERT(un != NULL);
13350 	ASSERT(bp != NULL);
13351 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13352 	SD_NEXT_IODONE(index, un, bp);
13353 }
13354 
13355 
13356 /*
13357  *    Function: sd_pm_iostart
13358  *
13359  * Description: iostart-side routine for Power mangement.
13360  *
13361  *     Context: Kernel thread context
13362  */
13363 
13364 static void
13365 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13366 {
13367 	ASSERT(un != NULL);
13368 	ASSERT(bp != NULL);
13369 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13370 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13371 
13372 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13373 
13374 	if (sd_pm_entry(un) != DDI_SUCCESS) {
13375 		/*
13376 		 * Set up to return the failed buf back up the 'iodone'
13377 		 * side of the calling chain.
13378 		 */
13379 		bioerror(bp, EIO);
13380 		bp->b_resid = bp->b_bcount;
13381 
13382 		SD_BEGIN_IODONE(index, un, bp);
13383 
13384 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13385 		return;
13386 	}
13387 
13388 	SD_NEXT_IOSTART(index, un, bp);
13389 
13390 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13391 }
13392 
13393 
13394 /*
13395  *    Function: sd_pm_iodone
13396  *
13397  * Description: iodone-side routine for power mangement.
13398  *
13399  *     Context: may be called from interrupt context
13400  */
13401 
13402 static void
13403 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13404 {
13405 	ASSERT(un != NULL);
13406 	ASSERT(bp != NULL);
13407 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13408 
13409 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13410 
13411 	/*
13412 	 * After attach the following flag is only read, so don't
13413 	 * take the penalty of acquiring a mutex for it.
13414 	 */
13415 	if (un->un_f_pm_is_enabled == TRUE) {
13416 		sd_pm_exit(un);
13417 	}
13418 
13419 	SD_NEXT_IODONE(index, un, bp);
13420 
13421 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13422 }
13423 
13424 
13425 /*
13426  *    Function: sd_core_iostart
13427  *
13428  * Description: Primary driver function for enqueuing buf(9S) structs from
13429  *		the system and initiating IO to the target device
13430  *
13431  *     Context: Kernel thread context. Can sleep.
13432  *
13433  * Assumptions:  - The given xp->xb_blkno is absolute
13434  *		   (ie, relative to the start of the device).
13435  *		 - The IO is to be done using the native blocksize of
13436  *		   the device, as specified in un->un_tgt_blocksize.
13437  */
13438 /* ARGSUSED */
13439 static void
13440 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13441 {
13442 	struct sd_xbuf *xp;
13443 
13444 	ASSERT(un != NULL);
13445 	ASSERT(bp != NULL);
13446 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13447 	ASSERT(bp->b_resid == 0);
13448 
13449 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13450 
13451 	xp = SD_GET_XBUF(bp);
13452 	ASSERT(xp != NULL);
13453 
13454 	mutex_enter(SD_MUTEX(un));
13455 
13456 	/*
13457 	 * If we are currently in the failfast state, fail any new IO
13458 	 * that has B_FAILFAST set, then return.
13459 	 */
13460 	if ((bp->b_flags & B_FAILFAST) &&
13461 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13462 		mutex_exit(SD_MUTEX(un));
13463 		bioerror(bp, EIO);
13464 		bp->b_resid = bp->b_bcount;
13465 		SD_BEGIN_IODONE(index, un, bp);
13466 		return;
13467 	}
13468 
13469 	if (SD_IS_DIRECT_PRIORITY(xp)) {
13470 		/*
13471 		 * Priority command -- transport it immediately.
13472 		 *
13473 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13474 		 * because all direct priority commands should be associated
13475 		 * with error recovery actions which we don't want to retry.
13476 		 */
13477 		sd_start_cmds(un, bp);
13478 	} else {
13479 		/*
13480 		 * Normal command -- add it to the wait queue, then start
13481 		 * transporting commands from the wait queue.
13482 		 */
13483 		sd_add_buf_to_waitq(un, bp);
13484 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13485 		sd_start_cmds(un, NULL);
13486 	}
13487 
13488 	mutex_exit(SD_MUTEX(un));
13489 
13490 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13491 }
13492 
13493 
13494 /*
13495  *    Function: sd_init_cdb_limits
13496  *
13497  * Description: This is to handle scsi_pkt initialization differences
13498  *		between the driver platforms.
13499  *
13500  *		Legacy behaviors:
13501  *
13502  *		If the block number or the sector count exceeds the
13503  *		capabilities of a Group 0 command, shift over to a
13504  *		Group 1 command. We don't blindly use Group 1
13505  *		commands because a) some drives (CDC Wren IVs) get a
13506  *		bit confused, and b) there is probably a fair amount
13507  *		of speed difference for a target to receive and decode
13508  *		a 10 byte command instead of a 6 byte command.
13509  *
13510  *		The xfer time difference of 6 vs 10 byte CDBs is
13511  *		still significant so this code is still worthwhile.
13512  *		10 byte CDBs are very inefficient with the fas HBA driver
13513  *		and older disks. Each CDB byte took 1 usec with some
13514  *		popular disks.
13515  *
13516  *     Context: Must be called at attach time
13517  */
13518 
13519 static void
13520 sd_init_cdb_limits(struct sd_lun *un)
13521 {
13522 	int hba_cdb_limit;
13523 
13524 	/*
13525 	 * Use CDB_GROUP1 commands for most devices except for
13526 	 * parallel SCSI fixed drives in which case we get better
13527 	 * performance using CDB_GROUP0 commands (where applicable).
13528 	 */
13529 	un->un_mincdb = SD_CDB_GROUP1;
13530 #if !defined(__fibre)
13531 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13532 	    !un->un_f_has_removable_media) {
13533 		un->un_mincdb = SD_CDB_GROUP0;
13534 	}
13535 #endif
13536 
13537 	/*
13538 	 * Try to read the max-cdb-length supported by HBA.
13539 	 */
13540 	un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13541 	if (0 >= un->un_max_hba_cdb) {
13542 		un->un_max_hba_cdb = CDB_GROUP4;
13543 		hba_cdb_limit = SD_CDB_GROUP4;
13544 	} else if (0 < un->un_max_hba_cdb &&
13545 	    un->un_max_hba_cdb < CDB_GROUP1) {
13546 		hba_cdb_limit = SD_CDB_GROUP0;
13547 	} else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13548 	    un->un_max_hba_cdb < CDB_GROUP5) {
13549 		hba_cdb_limit = SD_CDB_GROUP1;
13550 	} else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13551 	    un->un_max_hba_cdb < CDB_GROUP4) {
13552 		hba_cdb_limit = SD_CDB_GROUP5;
13553 	} else {
13554 		hba_cdb_limit = SD_CDB_GROUP4;
13555 	}
13556 
13557 	/*
13558 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13559 	 * commands for fixed disks unless we are building for a 32 bit
13560 	 * kernel.
13561 	 */
13562 #ifdef _LP64
13563 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13564 	    min(hba_cdb_limit, SD_CDB_GROUP4);
13565 #else
13566 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13567 	    min(hba_cdb_limit, SD_CDB_GROUP1);
13568 #endif
13569 
13570 	/*
13571 	 * x86 systems require the PKT_DMA_PARTIAL flag
13572 	 */
13573 #if defined(__x86)
13574 	un->un_pkt_flags = PKT_DMA_PARTIAL;
13575 #else
13576 	un->un_pkt_flags = 0;
13577 #endif
13578 
13579 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13580 	    ? sizeof (struct scsi_arq_status) : 1);
13581 	un->un_cmd_timeout = (ushort_t)sd_io_time;
13582 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13583 }
13584 
13585 
13586 /*
13587  *    Function: sd_initpkt_for_buf
13588  *
13589  * Description: Allocate and initialize for transport a scsi_pkt struct,
13590  *		based upon the info specified in the given buf struct.
13591  *
13592  *		Assumes the xb_blkno in the request is absolute (ie,
13593  *		relative to the start of the device (NOT partition!).
13594  *		Also assumes that the request is using the native block
13595  *		size of the device (as returned by the READ CAPACITY
13596  *		command).
13597  *
13598  * Return Code: SD_PKT_ALLOC_SUCCESS
13599  *		SD_PKT_ALLOC_FAILURE
13600  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13601  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13602  *
13603  *     Context: Kernel thread and may be called from software interrupt context
13604  *		as part of a sdrunout callback. This function may not block or
13605  *		call routines that block
13606  */
13607 
13608 static int
13609 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13610 {
13611 	struct sd_xbuf	*xp;
13612 	struct scsi_pkt *pktp = NULL;
13613 	struct sd_lun	*un;
13614 	size_t		blockcount;
13615 	daddr_t		startblock;
13616 	int		rval;
13617 	int		cmd_flags;
13618 
13619 	ASSERT(bp != NULL);
13620 	ASSERT(pktpp != NULL);
13621 	xp = SD_GET_XBUF(bp);
13622 	ASSERT(xp != NULL);
13623 	un = SD_GET_UN(bp);
13624 	ASSERT(un != NULL);
13625 	ASSERT(mutex_owned(SD_MUTEX(un)));
13626 	ASSERT(bp->b_resid == 0);
13627 
13628 	SD_TRACE(SD_LOG_IO_CORE, un,
13629 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13630 
13631 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13632 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13633 		/*
13634 		 * Already have a scsi_pkt -- just need DMA resources.
13635 		 * We must recompute the CDB in case the mapping returns
13636 		 * a nonzero pkt_resid.
13637 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13638 		 * that is being retried, the unmap/remap of the DMA resouces
13639 		 * will result in the entire transfer starting over again
13640 		 * from the very first block.
13641 		 */
13642 		ASSERT(xp->xb_pktp != NULL);
13643 		pktp = xp->xb_pktp;
13644 	} else {
13645 		pktp = NULL;
13646 	}
13647 #endif /* __i386 || __amd64 */
13648 
13649 	startblock = xp->xb_blkno;	/* Absolute block num. */
13650 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13651 
13652 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13653 
13654 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13655 
13656 #else
13657 
13658 	cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags;
13659 
13660 #endif
13661 
13662 	/*
13663 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13664 	 * call scsi_init_pkt, and build the CDB.
13665 	 */
13666 	rval = sd_setup_rw_pkt(un, &pktp, bp,
13667 	    cmd_flags, sdrunout, (caddr_t)un,
13668 	    startblock, blockcount);
13669 
13670 	if (rval == 0) {
13671 		/*
13672 		 * Success.
13673 		 *
13674 		 * If partial DMA is being used and required for this transfer.
13675 		 * set it up here.
13676 		 */
13677 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13678 		    (pktp->pkt_resid != 0)) {
13679 
13680 			/*
13681 			 * Save the CDB length and pkt_resid for the
13682 			 * next xfer
13683 			 */
13684 			xp->xb_dma_resid = pktp->pkt_resid;
13685 
13686 			/* rezero resid */
13687 			pktp->pkt_resid = 0;
13688 
13689 		} else {
13690 			xp->xb_dma_resid = 0;
13691 		}
13692 
13693 		pktp->pkt_flags = un->un_tagflags;
13694 		pktp->pkt_time  = un->un_cmd_timeout;
13695 		pktp->pkt_comp  = sdintr;
13696 
13697 		pktp->pkt_private = bp;
13698 		*pktpp = pktp;
13699 
13700 		SD_TRACE(SD_LOG_IO_CORE, un,
13701 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13702 
13703 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13704 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13705 #endif
13706 
13707 		return (SD_PKT_ALLOC_SUCCESS);
13708 
13709 	}
13710 
13711 	/*
13712 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13713 	 * from sd_setup_rw_pkt.
13714 	 */
13715 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13716 
13717 	if (rval == SD_PKT_ALLOC_FAILURE) {
13718 		*pktpp = NULL;
13719 		/*
13720 		 * Set the driver state to RWAIT to indicate the driver
13721 		 * is waiting on resource allocations. The driver will not
13722 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13723 		 */
13724 		New_state(un, SD_STATE_RWAIT);
13725 
13726 		SD_ERROR(SD_LOG_IO_CORE, un,
13727 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13728 
13729 		if ((bp->b_flags & B_ERROR) != 0) {
13730 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13731 		}
13732 		return (SD_PKT_ALLOC_FAILURE);
13733 	} else {
13734 		/*
13735 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13736 		 *
13737 		 * This should never happen.  Maybe someone messed with the
13738 		 * kernel's minphys?
13739 		 */
13740 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13741 		    "Request rejected: too large for CDB: "
13742 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13743 		SD_ERROR(SD_LOG_IO_CORE, un,
13744 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13745 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13746 
13747 	}
13748 }
13749 
13750 
13751 /*
13752  *    Function: sd_destroypkt_for_buf
13753  *
13754  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13755  *
13756  *     Context: Kernel thread or interrupt context
13757  */
13758 
13759 static void
13760 sd_destroypkt_for_buf(struct buf *bp)
13761 {
13762 	ASSERT(bp != NULL);
13763 	ASSERT(SD_GET_UN(bp) != NULL);
13764 
13765 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13766 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13767 
13768 	ASSERT(SD_GET_PKTP(bp) != NULL);
13769 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13770 
13771 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13772 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13773 }
13774 
13775 /*
13776  *    Function: sd_setup_rw_pkt
13777  *
13778  * Description: Determines appropriate CDB group for the requested LBA
13779  *		and transfer length, calls scsi_init_pkt, and builds
13780  *		the CDB.  Do not use for partial DMA transfers except
13781  *		for the initial transfer since the CDB size must
13782  *		remain constant.
13783  *
13784  *     Context: Kernel thread and may be called from software interrupt
13785  *		context as part of a sdrunout callback. This function may not
13786  *		block or call routines that block
13787  */
13788 
13789 
13790 int
13791 sd_setup_rw_pkt(struct sd_lun *un,
13792     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13793     int (*callback)(caddr_t), caddr_t callback_arg,
13794     diskaddr_t lba, uint32_t blockcount)
13795 {
13796 	struct scsi_pkt *return_pktp;
13797 	union scsi_cdb *cdbp;
13798 	struct sd_cdbinfo *cp = NULL;
13799 	int i;
13800 
13801 	/*
13802 	 * See which size CDB to use, based upon the request.
13803 	 */
13804 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13805 
13806 		/*
13807 		 * Check lba and block count against sd_cdbtab limits.
13808 		 * In the partial DMA case, we have to use the same size
13809 		 * CDB for all the transfers.  Check lba + blockcount
13810 		 * against the max LBA so we know that segment of the
13811 		 * transfer can use the CDB we select.
13812 		 */
13813 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13814 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13815 
13816 			/*
13817 			 * The command will fit into the CDB type
13818 			 * specified by sd_cdbtab[i].
13819 			 */
13820 			cp = sd_cdbtab + i;
13821 
13822 			/*
13823 			 * Call scsi_init_pkt so we can fill in the
13824 			 * CDB.
13825 			 */
13826 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13827 			    bp, cp->sc_grpcode, un->un_status_len, 0,
13828 			    flags, callback, callback_arg);
13829 
13830 			if (return_pktp != NULL) {
13831 
13832 				/*
13833 				 * Return new value of pkt
13834 				 */
13835 				*pktpp = return_pktp;
13836 
13837 				/*
13838 				 * To be safe, zero the CDB insuring there is
13839 				 * no leftover data from a previous command.
13840 				 */
13841 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13842 
13843 				/*
13844 				 * Handle partial DMA mapping
13845 				 */
13846 				if (return_pktp->pkt_resid != 0) {
13847 
13848 					/*
13849 					 * Not going to xfer as many blocks as
13850 					 * originally expected
13851 					 */
13852 					blockcount -=
13853 					    SD_BYTES2TGTBLOCKS(un,
13854 						return_pktp->pkt_resid);
13855 				}
13856 
13857 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13858 
13859 				/*
13860 				 * Set command byte based on the CDB
13861 				 * type we matched.
13862 				 */
13863 				cdbp->scc_cmd = cp->sc_grpmask |
13864 				    ((bp->b_flags & B_READ) ?
13865 					SCMD_READ : SCMD_WRITE);
13866 
13867 				SD_FILL_SCSI1_LUN(un, return_pktp);
13868 
13869 				/*
13870 				 * Fill in LBA and length
13871 				 */
13872 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13873 				    (cp->sc_grpcode == CDB_GROUP4) ||
13874 				    (cp->sc_grpcode == CDB_GROUP0) ||
13875 				    (cp->sc_grpcode == CDB_GROUP5));
13876 
13877 				if (cp->sc_grpcode == CDB_GROUP1) {
13878 					FORMG1ADDR(cdbp, lba);
13879 					FORMG1COUNT(cdbp, blockcount);
13880 					return (0);
13881 				} else if (cp->sc_grpcode == CDB_GROUP4) {
13882 					FORMG4LONGADDR(cdbp, lba);
13883 					FORMG4COUNT(cdbp, blockcount);
13884 					return (0);
13885 				} else if (cp->sc_grpcode == CDB_GROUP0) {
13886 					FORMG0ADDR(cdbp, lba);
13887 					FORMG0COUNT(cdbp, blockcount);
13888 					return (0);
13889 				} else if (cp->sc_grpcode == CDB_GROUP5) {
13890 					FORMG5ADDR(cdbp, lba);
13891 					FORMG5COUNT(cdbp, blockcount);
13892 					return (0);
13893 				}
13894 
13895 				/*
13896 				 * It should be impossible to not match one
13897 				 * of the CDB types above, so we should never
13898 				 * reach this point.  Set the CDB command byte
13899 				 * to test-unit-ready to avoid writing
13900 				 * to somewhere we don't intend.
13901 				 */
13902 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13903 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13904 			} else {
13905 				/*
13906 				 * Couldn't get scsi_pkt
13907 				 */
13908 				return (SD_PKT_ALLOC_FAILURE);
13909 			}
13910 		}
13911 	}
13912 
13913 	/*
13914 	 * None of the available CDB types were suitable.  This really
13915 	 * should never happen:  on a 64 bit system we support
13916 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
13917 	 * and on a 32 bit system we will refuse to bind to a device
13918 	 * larger than 2TB so addresses will never be larger than 32 bits.
13919 	 */
13920 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13921 }
13922 
13923 #if defined(__i386) || defined(__amd64)
13924 /*
13925  *    Function: sd_setup_next_rw_pkt
13926  *
13927  * Description: Setup packet for partial DMA transfers, except for the
13928  * 		initial transfer.  sd_setup_rw_pkt should be used for
13929  *		the initial transfer.
13930  *
13931  *     Context: Kernel thread and may be called from interrupt context.
13932  */
13933 
13934 int
13935 sd_setup_next_rw_pkt(struct sd_lun *un,
13936     struct scsi_pkt *pktp, struct buf *bp,
13937     diskaddr_t lba, uint32_t blockcount)
13938 {
13939 	uchar_t com;
13940 	union scsi_cdb *cdbp;
13941 	uchar_t cdb_group_id;
13942 
13943 	ASSERT(pktp != NULL);
13944 	ASSERT(pktp->pkt_cdbp != NULL);
13945 
13946 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13947 	com = cdbp->scc_cmd;
13948 	cdb_group_id = CDB_GROUPID(com);
13949 
13950 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13951 	    (cdb_group_id == CDB_GROUPID_1) ||
13952 	    (cdb_group_id == CDB_GROUPID_4) ||
13953 	    (cdb_group_id == CDB_GROUPID_5));
13954 
13955 	/*
13956 	 * Move pkt to the next portion of the xfer.
13957 	 * func is NULL_FUNC so we do not have to release
13958 	 * the disk mutex here.
13959 	 */
13960 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13961 	    NULL_FUNC, NULL) == pktp) {
13962 		/* Success.  Handle partial DMA */
13963 		if (pktp->pkt_resid != 0) {
13964 			blockcount -=
13965 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
13966 		}
13967 
13968 		cdbp->scc_cmd = com;
13969 		SD_FILL_SCSI1_LUN(un, pktp);
13970 		if (cdb_group_id == CDB_GROUPID_1) {
13971 			FORMG1ADDR(cdbp, lba);
13972 			FORMG1COUNT(cdbp, blockcount);
13973 			return (0);
13974 		} else if (cdb_group_id == CDB_GROUPID_4) {
13975 			FORMG4LONGADDR(cdbp, lba);
13976 			FORMG4COUNT(cdbp, blockcount);
13977 			return (0);
13978 		} else if (cdb_group_id == CDB_GROUPID_0) {
13979 			FORMG0ADDR(cdbp, lba);
13980 			FORMG0COUNT(cdbp, blockcount);
13981 			return (0);
13982 		} else if (cdb_group_id == CDB_GROUPID_5) {
13983 			FORMG5ADDR(cdbp, lba);
13984 			FORMG5COUNT(cdbp, blockcount);
13985 			return (0);
13986 		}
13987 
13988 		/* Unreachable */
13989 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13990 	}
13991 
13992 	/*
13993 	 * Error setting up next portion of cmd transfer.
13994 	 * Something is definitely very wrong and this
13995 	 * should not happen.
13996 	 */
13997 	return (SD_PKT_ALLOC_FAILURE);
13998 }
13999 #endif /* defined(__i386) || defined(__amd64) */
14000 
14001 /*
14002  *    Function: sd_initpkt_for_uscsi
14003  *
14004  * Description: Allocate and initialize for transport a scsi_pkt struct,
14005  *		based upon the info specified in the given uscsi_cmd struct.
14006  *
14007  * Return Code: SD_PKT_ALLOC_SUCCESS
14008  *		SD_PKT_ALLOC_FAILURE
14009  *		SD_PKT_ALLOC_FAILURE_NO_DMA
14010  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
14011  *
14012  *     Context: Kernel thread and may be called from software interrupt context
14013  *		as part of a sdrunout callback. This function may not block or
14014  *		call routines that block
14015  */
14016 
14017 static int
14018 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
14019 {
14020 	struct uscsi_cmd *uscmd;
14021 	struct sd_xbuf	*xp;
14022 	struct scsi_pkt	*pktp;
14023 	struct sd_lun	*un;
14024 	uint32_t	flags = 0;
14025 
14026 	ASSERT(bp != NULL);
14027 	ASSERT(pktpp != NULL);
14028 	xp = SD_GET_XBUF(bp);
14029 	ASSERT(xp != NULL);
14030 	un = SD_GET_UN(bp);
14031 	ASSERT(un != NULL);
14032 	ASSERT(mutex_owned(SD_MUTEX(un)));
14033 
14034 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14035 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14036 	ASSERT(uscmd != NULL);
14037 
14038 	SD_TRACE(SD_LOG_IO_CORE, un,
14039 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
14040 
14041 	/*
14042 	 * Allocate the scsi_pkt for the command.
14043 	 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
14044 	 *	 during scsi_init_pkt time and will continue to use the
14045 	 *	 same path as long as the same scsi_pkt is used without
14046 	 *	 intervening scsi_dma_free(). Since uscsi command does
14047 	 *	 not call scsi_dmafree() before retry failed command, it
14048 	 *	 is necessary to make sure PKT_DMA_PARTIAL flag is NOT
14049 	 *	 set such that scsi_vhci can use other available path for
14050 	 *	 retry. Besides, ucsci command does not allow DMA breakup,
14051 	 *	 so there is no need to set PKT_DMA_PARTIAL flag.
14052 	 */
14053 	pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14054 	    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14055 	    sizeof (struct scsi_arq_status), 0,
14056 	    (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14057 	    sdrunout, (caddr_t)un);
14058 
14059 	if (pktp == NULL) {
14060 		*pktpp = NULL;
14061 		/*
14062 		 * Set the driver state to RWAIT to indicate the driver
14063 		 * is waiting on resource allocations. The driver will not
14064 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
14065 		 */
14066 		New_state(un, SD_STATE_RWAIT);
14067 
14068 		SD_ERROR(SD_LOG_IO_CORE, un,
14069 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14070 
14071 		if ((bp->b_flags & B_ERROR) != 0) {
14072 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14073 		}
14074 		return (SD_PKT_ALLOC_FAILURE);
14075 	}
14076 
14077 	/*
14078 	 * We do not do DMA breakup for USCSI commands, so return failure
14079 	 * here if all the needed DMA resources were not allocated.
14080 	 */
14081 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14082 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14083 		scsi_destroy_pkt(pktp);
14084 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14085 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14086 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14087 	}
14088 
14089 	/* Init the cdb from the given uscsi struct */
14090 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14091 	    uscmd->uscsi_cdb[0], 0, 0, 0);
14092 
14093 	SD_FILL_SCSI1_LUN(un, pktp);
14094 
14095 	/*
14096 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
14097 	 * for listing of the supported flags.
14098 	 */
14099 
14100 	if (uscmd->uscsi_flags & USCSI_SILENT) {
14101 		flags |= FLAG_SILENT;
14102 	}
14103 
14104 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14105 		flags |= FLAG_DIAGNOSE;
14106 	}
14107 
14108 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14109 		flags |= FLAG_ISOLATE;
14110 	}
14111 
14112 	if (un->un_f_is_fibre == FALSE) {
14113 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14114 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14115 		}
14116 	}
14117 
14118 	/*
14119 	 * Set the pkt flags here so we save time later.
14120 	 * Note: These flags are NOT in the uscsi man page!!!
14121 	 */
14122 	if (uscmd->uscsi_flags & USCSI_HEAD) {
14123 		flags |= FLAG_HEAD;
14124 	}
14125 
14126 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
14127 		flags |= FLAG_NOINTR;
14128 	}
14129 
14130 	/*
14131 	 * For tagged queueing, things get a bit complicated.
14132 	 * Check first for head of queue and last for ordered queue.
14133 	 * If neither head nor order, use the default driver tag flags.
14134 	 */
14135 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14136 		if (uscmd->uscsi_flags & USCSI_HTAG) {
14137 			flags |= FLAG_HTAG;
14138 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
14139 			flags |= FLAG_OTAG;
14140 		} else {
14141 			flags |= un->un_tagflags & FLAG_TAGMASK;
14142 		}
14143 	}
14144 
14145 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
14146 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14147 	}
14148 
14149 	pktp->pkt_flags = flags;
14150 
14151 	/* Copy the caller's CDB into the pkt... */
14152 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14153 
14154 	if (uscmd->uscsi_timeout == 0) {
14155 		pktp->pkt_time = un->un_uscsi_timeout;
14156 	} else {
14157 		pktp->pkt_time = uscmd->uscsi_timeout;
14158 	}
14159 
14160 	/* need it later to identify USCSI request in sdintr */
14161 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
14162 
14163 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
14164 
14165 	pktp->pkt_private = bp;
14166 	pktp->pkt_comp = sdintr;
14167 	*pktpp = pktp;
14168 
14169 	SD_TRACE(SD_LOG_IO_CORE, un,
14170 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14171 
14172 	return (SD_PKT_ALLOC_SUCCESS);
14173 }
14174 
14175 
14176 /*
14177  *    Function: sd_destroypkt_for_uscsi
14178  *
14179  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14180  *		IOs.. Also saves relevant info into the associated uscsi_cmd
14181  *		struct.
14182  *
14183  *     Context: May be called under interrupt context
14184  */
14185 
14186 static void
14187 sd_destroypkt_for_uscsi(struct buf *bp)
14188 {
14189 	struct uscsi_cmd *uscmd;
14190 	struct sd_xbuf	*xp;
14191 	struct scsi_pkt	*pktp;
14192 	struct sd_lun	*un;
14193 
14194 	ASSERT(bp != NULL);
14195 	xp = SD_GET_XBUF(bp);
14196 	ASSERT(xp != NULL);
14197 	un = SD_GET_UN(bp);
14198 	ASSERT(un != NULL);
14199 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14200 	pktp = SD_GET_PKTP(bp);
14201 	ASSERT(pktp != NULL);
14202 
14203 	SD_TRACE(SD_LOG_IO_CORE, un,
14204 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14205 
14206 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14207 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14208 	ASSERT(uscmd != NULL);
14209 
14210 	/* Save the status and the residual into the uscsi_cmd struct */
14211 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14212 	uscmd->uscsi_resid  = bp->b_resid;
14213 
14214 	/*
14215 	 * If enabled, copy any saved sense data into the area specified
14216 	 * by the uscsi command.
14217 	 */
14218 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14219 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14220 		/*
14221 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
14222 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14223 		 */
14224 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
14225 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
14226 		bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH);
14227 	}
14228 
14229 	/* We are done with the scsi_pkt; free it now */
14230 	ASSERT(SD_GET_PKTP(bp) != NULL);
14231 	scsi_destroy_pkt(SD_GET_PKTP(bp));
14232 
14233 	SD_TRACE(SD_LOG_IO_CORE, un,
14234 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14235 }
14236 
14237 
14238 /*
14239  *    Function: sd_bioclone_alloc
14240  *
14241  * Description: Allocate a buf(9S) and init it as per the given buf
14242  *		and the various arguments.  The associated sd_xbuf
14243  *		struct is (nearly) duplicated.  The struct buf *bp
14244  *		argument is saved in new_xp->xb_private.
14245  *
14246  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14247  *		datalen - size of data area for the shadow bp
14248  *		blkno - starting LBA
14249  *		func - function pointer for b_iodone in the shadow buf. (May
14250  *			be NULL if none.)
14251  *
14252  * Return Code: Pointer to allocates buf(9S) struct
14253  *
14254  *     Context: Can sleep.
14255  */
14256 
14257 static struct buf *
14258 sd_bioclone_alloc(struct buf *bp, size_t datalen,
14259 	daddr_t blkno, int (*func)(struct buf *))
14260 {
14261 	struct	sd_lun	*un;
14262 	struct	sd_xbuf	*xp;
14263 	struct	sd_xbuf	*new_xp;
14264 	struct	buf	*new_bp;
14265 
14266 	ASSERT(bp != NULL);
14267 	xp = SD_GET_XBUF(bp);
14268 	ASSERT(xp != NULL);
14269 	un = SD_GET_UN(bp);
14270 	ASSERT(un != NULL);
14271 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14272 
14273 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14274 	    NULL, KM_SLEEP);
14275 
14276 	new_bp->b_lblkno	= blkno;
14277 
14278 	/*
14279 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14280 	 * original xbuf into it.
14281 	 */
14282 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14283 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14284 
14285 	/*
14286 	 * The given bp is automatically saved in the xb_private member
14287 	 * of the new xbuf.  Callers are allowed to depend on this.
14288 	 */
14289 	new_xp->xb_private = bp;
14290 
14291 	new_bp->b_private  = new_xp;
14292 
14293 	return (new_bp);
14294 }
14295 
14296 /*
14297  *    Function: sd_shadow_buf_alloc
14298  *
14299  * Description: Allocate a buf(9S) and init it as per the given buf
14300  *		and the various arguments.  The associated sd_xbuf
14301  *		struct is (nearly) duplicated.  The struct buf *bp
14302  *		argument is saved in new_xp->xb_private.
14303  *
14304  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14305  *		datalen - size of data area for the shadow bp
14306  *		bflags - B_READ or B_WRITE (pseudo flag)
14307  *		blkno - starting LBA
14308  *		func - function pointer for b_iodone in the shadow buf. (May
14309  *			be NULL if none.)
14310  *
14311  * Return Code: Pointer to allocates buf(9S) struct
14312  *
14313  *     Context: Can sleep.
14314  */
14315 
14316 static struct buf *
14317 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14318 	daddr_t blkno, int (*func)(struct buf *))
14319 {
14320 	struct	sd_lun	*un;
14321 	struct	sd_xbuf	*xp;
14322 	struct	sd_xbuf	*new_xp;
14323 	struct	buf	*new_bp;
14324 
14325 	ASSERT(bp != NULL);
14326 	xp = SD_GET_XBUF(bp);
14327 	ASSERT(xp != NULL);
14328 	un = SD_GET_UN(bp);
14329 	ASSERT(un != NULL);
14330 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14331 
14332 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14333 		bp_mapin(bp);
14334 	}
14335 
14336 	bflags &= (B_READ | B_WRITE);
14337 #if defined(__i386) || defined(__amd64)
14338 	new_bp = getrbuf(KM_SLEEP);
14339 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14340 	new_bp->b_bcount = datalen;
14341 	new_bp->b_flags = bflags |
14342 	    (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14343 #else
14344 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14345 	    datalen, bflags, SLEEP_FUNC, NULL);
14346 #endif
14347 	new_bp->av_forw	= NULL;
14348 	new_bp->av_back	= NULL;
14349 	new_bp->b_dev	= bp->b_dev;
14350 	new_bp->b_blkno	= blkno;
14351 	new_bp->b_iodone = func;
14352 	new_bp->b_edev	= bp->b_edev;
14353 	new_bp->b_resid	= 0;
14354 
14355 	/* We need to preserve the B_FAILFAST flag */
14356 	if (bp->b_flags & B_FAILFAST) {
14357 		new_bp->b_flags |= B_FAILFAST;
14358 	}
14359 
14360 	/*
14361 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14362 	 * original xbuf into it.
14363 	 */
14364 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14365 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14366 
14367 	/* Need later to copy data between the shadow buf & original buf! */
14368 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14369 
14370 	/*
14371 	 * The given bp is automatically saved in the xb_private member
14372 	 * of the new xbuf.  Callers are allowed to depend on this.
14373 	 */
14374 	new_xp->xb_private = bp;
14375 
14376 	new_bp->b_private  = new_xp;
14377 
14378 	return (new_bp);
14379 }
14380 
14381 /*
14382  *    Function: sd_bioclone_free
14383  *
14384  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14385  *		in the larger than partition operation.
14386  *
14387  *     Context: May be called under interrupt context
14388  */
14389 
14390 static void
14391 sd_bioclone_free(struct buf *bp)
14392 {
14393 	struct sd_xbuf	*xp;
14394 
14395 	ASSERT(bp != NULL);
14396 	xp = SD_GET_XBUF(bp);
14397 	ASSERT(xp != NULL);
14398 
14399 	/*
14400 	 * Call bp_mapout() before freeing the buf,  in case a lower
14401 	 * layer or HBA  had done a bp_mapin().  we must do this here
14402 	 * as we are the "originator" of the shadow buf.
14403 	 */
14404 	bp_mapout(bp);
14405 
14406 	/*
14407 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14408 	 * never gets confused by a stale value in this field. (Just a little
14409 	 * extra defensiveness here.)
14410 	 */
14411 	bp->b_iodone = NULL;
14412 
14413 	freerbuf(bp);
14414 
14415 	kmem_free(xp, sizeof (struct sd_xbuf));
14416 }
14417 
14418 /*
14419  *    Function: sd_shadow_buf_free
14420  *
14421  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14422  *
14423  *     Context: May be called under interrupt context
14424  */
14425 
14426 static void
14427 sd_shadow_buf_free(struct buf *bp)
14428 {
14429 	struct sd_xbuf	*xp;
14430 
14431 	ASSERT(bp != NULL);
14432 	xp = SD_GET_XBUF(bp);
14433 	ASSERT(xp != NULL);
14434 
14435 #if defined(__sparc)
14436 	/*
14437 	 * Call bp_mapout() before freeing the buf,  in case a lower
14438 	 * layer or HBA  had done a bp_mapin().  we must do this here
14439 	 * as we are the "originator" of the shadow buf.
14440 	 */
14441 	bp_mapout(bp);
14442 #endif
14443 
14444 	/*
14445 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14446 	 * never gets confused by a stale value in this field. (Just a little
14447 	 * extra defensiveness here.)
14448 	 */
14449 	bp->b_iodone = NULL;
14450 
14451 #if defined(__i386) || defined(__amd64)
14452 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
14453 	freerbuf(bp);
14454 #else
14455 	scsi_free_consistent_buf(bp);
14456 #endif
14457 
14458 	kmem_free(xp, sizeof (struct sd_xbuf));
14459 }
14460 
14461 
14462 /*
14463  *    Function: sd_print_transport_rejected_message
14464  *
14465  * Description: This implements the ludicrously complex rules for printing
14466  *		a "transport rejected" message.  This is to address the
14467  *		specific problem of having a flood of this error message
14468  *		produced when a failover occurs.
14469  *
14470  *     Context: Any.
14471  */
14472 
14473 static void
14474 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14475 	int code)
14476 {
14477 	ASSERT(un != NULL);
14478 	ASSERT(mutex_owned(SD_MUTEX(un)));
14479 	ASSERT(xp != NULL);
14480 
14481 	/*
14482 	 * Print the "transport rejected" message under the following
14483 	 * conditions:
14484 	 *
14485 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14486 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14487 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14488 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14489 	 *   scsi_transport(9F) (which indicates that the target might have
14490 	 *   gone off-line).  This uses the un->un_tran_fatal_count
14491 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14492 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14493 	 *   from scsi_transport().
14494 	 *
14495 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14496 	 * the preceeding cases in order for the message to be printed.
14497 	 */
14498 	if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) {
14499 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14500 		    (code != TRAN_FATAL_ERROR) ||
14501 		    (un->un_tran_fatal_count == 1)) {
14502 			switch (code) {
14503 			case TRAN_BADPKT:
14504 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14505 				    "transport rejected bad packet\n");
14506 				break;
14507 			case TRAN_FATAL_ERROR:
14508 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14509 				    "transport rejected fatal error\n");
14510 				break;
14511 			default:
14512 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14513 				    "transport rejected (%d)\n", code);
14514 				break;
14515 			}
14516 		}
14517 	}
14518 }
14519 
14520 
14521 /*
14522  *    Function: sd_add_buf_to_waitq
14523  *
14524  * Description: Add the given buf(9S) struct to the wait queue for the
14525  *		instance.  If sorting is enabled, then the buf is added
14526  *		to the queue via an elevator sort algorithm (a la
14527  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14528  *		If sorting is not enabled, then the buf is just added
14529  *		to the end of the wait queue.
14530  *
14531  * Return Code: void
14532  *
14533  *     Context: Does not sleep/block, therefore technically can be called
14534  *		from any context.  However if sorting is enabled then the
14535  *		execution time is indeterminate, and may take long if
14536  *		the wait queue grows large.
14537  */
14538 
14539 static void
14540 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14541 {
14542 	struct buf *ap;
14543 
14544 	ASSERT(bp != NULL);
14545 	ASSERT(un != NULL);
14546 	ASSERT(mutex_owned(SD_MUTEX(un)));
14547 
14548 	/* If the queue is empty, add the buf as the only entry & return. */
14549 	if (un->un_waitq_headp == NULL) {
14550 		ASSERT(un->un_waitq_tailp == NULL);
14551 		un->un_waitq_headp = un->un_waitq_tailp = bp;
14552 		bp->av_forw = NULL;
14553 		return;
14554 	}
14555 
14556 	ASSERT(un->un_waitq_tailp != NULL);
14557 
14558 	/*
14559 	 * If sorting is disabled, just add the buf to the tail end of
14560 	 * the wait queue and return.
14561 	 */
14562 	if (un->un_f_disksort_disabled) {
14563 		un->un_waitq_tailp->av_forw = bp;
14564 		un->un_waitq_tailp = bp;
14565 		bp->av_forw = NULL;
14566 		return;
14567 	}
14568 
14569 	/*
14570 	 * Sort thru the list of requests currently on the wait queue
14571 	 * and add the new buf request at the appropriate position.
14572 	 *
14573 	 * The un->un_waitq_headp is an activity chain pointer on which
14574 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14575 	 * first queue holds those requests which are positioned after
14576 	 * the current SD_GET_BLKNO() (in the first request); the second holds
14577 	 * requests which came in after their SD_GET_BLKNO() number was passed.
14578 	 * Thus we implement a one way scan, retracting after reaching
14579 	 * the end of the drive to the first request on the second
14580 	 * queue, at which time it becomes the first queue.
14581 	 * A one-way scan is natural because of the way UNIX read-ahead
14582 	 * blocks are allocated.
14583 	 *
14584 	 * If we lie after the first request, then we must locate the
14585 	 * second request list and add ourselves to it.
14586 	 */
14587 	ap = un->un_waitq_headp;
14588 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14589 		while (ap->av_forw != NULL) {
14590 			/*
14591 			 * Look for an "inversion" in the (normally
14592 			 * ascending) block numbers. This indicates
14593 			 * the start of the second request list.
14594 			 */
14595 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14596 				/*
14597 				 * Search the second request list for the
14598 				 * first request at a larger block number.
14599 				 * We go before that; however if there is
14600 				 * no such request, we go at the end.
14601 				 */
14602 				do {
14603 					if (SD_GET_BLKNO(bp) <
14604 					    SD_GET_BLKNO(ap->av_forw)) {
14605 						goto insert;
14606 					}
14607 					ap = ap->av_forw;
14608 				} while (ap->av_forw != NULL);
14609 				goto insert;		/* after last */
14610 			}
14611 			ap = ap->av_forw;
14612 		}
14613 
14614 		/*
14615 		 * No inversions... we will go after the last, and
14616 		 * be the first request in the second request list.
14617 		 */
14618 		goto insert;
14619 	}
14620 
14621 	/*
14622 	 * Request is at/after the current request...
14623 	 * sort in the first request list.
14624 	 */
14625 	while (ap->av_forw != NULL) {
14626 		/*
14627 		 * We want to go after the current request (1) if
14628 		 * there is an inversion after it (i.e. it is the end
14629 		 * of the first request list), or (2) if the next
14630 		 * request is a larger block no. than our request.
14631 		 */
14632 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14633 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14634 			goto insert;
14635 		}
14636 		ap = ap->av_forw;
14637 	}
14638 
14639 	/*
14640 	 * Neither a second list nor a larger request, therefore
14641 	 * we go at the end of the first list (which is the same
14642 	 * as the end of the whole schebang).
14643 	 */
14644 insert:
14645 	bp->av_forw = ap->av_forw;
14646 	ap->av_forw = bp;
14647 
14648 	/*
14649 	 * If we inserted onto the tail end of the waitq, make sure the
14650 	 * tail pointer is updated.
14651 	 */
14652 	if (ap == un->un_waitq_tailp) {
14653 		un->un_waitq_tailp = bp;
14654 	}
14655 }
14656 
14657 
14658 /*
14659  *    Function: sd_start_cmds
14660  *
14661  * Description: Remove and transport cmds from the driver queues.
14662  *
14663  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14664  *
14665  *		immed_bp - ptr to a buf to be transported immediately. Only
14666  *		the immed_bp is transported; bufs on the waitq are not
14667  *		processed and the un_retry_bp is not checked.  If immed_bp is
14668  *		NULL, then normal queue processing is performed.
14669  *
14670  *     Context: May be called from kernel thread context, interrupt context,
14671  *		or runout callback context. This function may not block or
14672  *		call routines that block.
14673  */
14674 
14675 static void
14676 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14677 {
14678 	struct	sd_xbuf	*xp;
14679 	struct	buf	*bp;
14680 	void	(*statp)(kstat_io_t *);
14681 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14682 	void	(*saved_statp)(kstat_io_t *);
14683 #endif
14684 	int	rval;
14685 
14686 	ASSERT(un != NULL);
14687 	ASSERT(mutex_owned(SD_MUTEX(un)));
14688 	ASSERT(un->un_ncmds_in_transport >= 0);
14689 	ASSERT(un->un_throttle >= 0);
14690 
14691 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14692 
14693 	do {
14694 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14695 		saved_statp = NULL;
14696 #endif
14697 
14698 		/*
14699 		 * If we are syncing or dumping, fail the command to
14700 		 * avoid recursively calling back into scsi_transport().
14701 		 * The dump I/O itself uses a separate code path so this
14702 		 * only prevents non-dump I/O from being sent while dumping.
14703 		 * File system sync takes place before dumping begins.
14704 		 * During panic, filesystem I/O is allowed provided
14705 		 * un_in_callback is <= 1.  This is to prevent recursion
14706 		 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14707 		 * sd_start_cmds and so on.  See panic.c for more information
14708 		 * about the states the system can be in during panic.
14709 		 */
14710 		if ((un->un_state == SD_STATE_DUMPING) ||
14711 		    (ddi_in_panic() && (un->un_in_callback > 1))) {
14712 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14713 			    "sd_start_cmds: panicking\n");
14714 			goto exit;
14715 		}
14716 
14717 		if ((bp = immed_bp) != NULL) {
14718 			/*
14719 			 * We have a bp that must be transported immediately.
14720 			 * It's OK to transport the immed_bp here without doing
14721 			 * the throttle limit check because the immed_bp is
14722 			 * always used in a retry/recovery case. This means
14723 			 * that we know we are not at the throttle limit by
14724 			 * virtue of the fact that to get here we must have
14725 			 * already gotten a command back via sdintr(). This also
14726 			 * relies on (1) the command on un_retry_bp preventing
14727 			 * further commands from the waitq from being issued;
14728 			 * and (2) the code in sd_retry_command checking the
14729 			 * throttle limit before issuing a delayed or immediate
14730 			 * retry. This holds even if the throttle limit is
14731 			 * currently ratcheted down from its maximum value.
14732 			 */
14733 			statp = kstat_runq_enter;
14734 			if (bp == un->un_retry_bp) {
14735 				ASSERT((un->un_retry_statp == NULL) ||
14736 				    (un->un_retry_statp == kstat_waitq_enter) ||
14737 				    (un->un_retry_statp ==
14738 				    kstat_runq_back_to_waitq));
14739 				/*
14740 				 * If the waitq kstat was incremented when
14741 				 * sd_set_retry_bp() queued this bp for a retry,
14742 				 * then we must set up statp so that the waitq
14743 				 * count will get decremented correctly below.
14744 				 * Also we must clear un->un_retry_statp to
14745 				 * ensure that we do not act on a stale value
14746 				 * in this field.
14747 				 */
14748 				if ((un->un_retry_statp == kstat_waitq_enter) ||
14749 				    (un->un_retry_statp ==
14750 				    kstat_runq_back_to_waitq)) {
14751 					statp = kstat_waitq_to_runq;
14752 				}
14753 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14754 				saved_statp = un->un_retry_statp;
14755 #endif
14756 				un->un_retry_statp = NULL;
14757 
14758 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14759 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14760 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
14761 				    un, un->un_retry_bp, un->un_throttle,
14762 				    un->un_ncmds_in_transport);
14763 			} else {
14764 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14765 				    "processing priority bp:0x%p\n", bp);
14766 			}
14767 
14768 		} else if ((bp = un->un_waitq_headp) != NULL) {
14769 			/*
14770 			 * A command on the waitq is ready to go, but do not
14771 			 * send it if:
14772 			 *
14773 			 * (1) the throttle limit has been reached, or
14774 			 * (2) a retry is pending, or
14775 			 * (3) a START_STOP_UNIT callback pending, or
14776 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14777 			 *	command is pending.
14778 			 *
14779 			 * For all of these conditions, IO processing will
14780 			 * restart after the condition is cleared.
14781 			 */
14782 			if (un->un_ncmds_in_transport >= un->un_throttle) {
14783 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14784 				    "sd_start_cmds: exiting, "
14785 				    "throttle limit reached!\n");
14786 				goto exit;
14787 			}
14788 			if (un->un_retry_bp != NULL) {
14789 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14790 				    "sd_start_cmds: exiting, retry pending!\n");
14791 				goto exit;
14792 			}
14793 			if (un->un_startstop_timeid != NULL) {
14794 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14795 				    "sd_start_cmds: exiting, "
14796 				    "START_STOP pending!\n");
14797 				goto exit;
14798 			}
14799 			if (un->un_direct_priority_timeid != NULL) {
14800 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14801 				    "sd_start_cmds: exiting, "
14802 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14803 				goto exit;
14804 			}
14805 
14806 			/* Dequeue the command */
14807 			un->un_waitq_headp = bp->av_forw;
14808 			if (un->un_waitq_headp == NULL) {
14809 				un->un_waitq_tailp = NULL;
14810 			}
14811 			bp->av_forw = NULL;
14812 			statp = kstat_waitq_to_runq;
14813 			SD_TRACE(SD_LOG_IO_CORE, un,
14814 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14815 
14816 		} else {
14817 			/* No work to do so bail out now */
14818 			SD_TRACE(SD_LOG_IO_CORE, un,
14819 			    "sd_start_cmds: no more work, exiting!\n");
14820 			goto exit;
14821 		}
14822 
14823 		/*
14824 		 * Reset the state to normal. This is the mechanism by which
14825 		 * the state transitions from either SD_STATE_RWAIT or
14826 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14827 		 * If state is SD_STATE_PM_CHANGING then this command is
14828 		 * part of the device power control and the state must
14829 		 * not be put back to normal. Doing so would would
14830 		 * allow new commands to proceed when they shouldn't,
14831 		 * the device may be going off.
14832 		 */
14833 		if ((un->un_state != SD_STATE_SUSPENDED) &&
14834 		    (un->un_state != SD_STATE_PM_CHANGING)) {
14835 			New_state(un, SD_STATE_NORMAL);
14836 		    }
14837 
14838 		xp = SD_GET_XBUF(bp);
14839 		ASSERT(xp != NULL);
14840 
14841 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14842 		/*
14843 		 * Allocate the scsi_pkt if we need one, or attach DMA
14844 		 * resources if we have a scsi_pkt that needs them. The
14845 		 * latter should only occur for commands that are being
14846 		 * retried.
14847 		 */
14848 		if ((xp->xb_pktp == NULL) ||
14849 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14850 #else
14851 		if (xp->xb_pktp == NULL) {
14852 #endif
14853 			/*
14854 			 * There is no scsi_pkt allocated for this buf. Call
14855 			 * the initpkt function to allocate & init one.
14856 			 *
14857 			 * The scsi_init_pkt runout callback functionality is
14858 			 * implemented as follows:
14859 			 *
14860 			 * 1) The initpkt function always calls
14861 			 *    scsi_init_pkt(9F) with sdrunout specified as the
14862 			 *    callback routine.
14863 			 * 2) A successful packet allocation is initialized and
14864 			 *    the I/O is transported.
14865 			 * 3) The I/O associated with an allocation resource
14866 			 *    failure is left on its queue to be retried via
14867 			 *    runout or the next I/O.
14868 			 * 4) The I/O associated with a DMA error is removed
14869 			 *    from the queue and failed with EIO. Processing of
14870 			 *    the transport queues is also halted to be
14871 			 *    restarted via runout or the next I/O.
14872 			 * 5) The I/O associated with a CDB size or packet
14873 			 *    size error is removed from the queue and failed
14874 			 *    with EIO. Processing of the transport queues is
14875 			 *    continued.
14876 			 *
14877 			 * Note: there is no interface for canceling a runout
14878 			 * callback. To prevent the driver from detaching or
14879 			 * suspending while a runout is pending the driver
14880 			 * state is set to SD_STATE_RWAIT
14881 			 *
14882 			 * Note: using the scsi_init_pkt callback facility can
14883 			 * result in an I/O request persisting at the head of
14884 			 * the list which cannot be satisfied even after
14885 			 * multiple retries. In the future the driver may
14886 			 * implement some kind of maximum runout count before
14887 			 * failing an I/O.
14888 			 *
14889 			 * Note: the use of funcp below may seem superfluous,
14890 			 * but it helps warlock figure out the correct
14891 			 * initpkt function calls (see [s]sd.wlcmd).
14892 			 */
14893 			struct scsi_pkt	*pktp;
14894 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14895 
14896 			ASSERT(bp != un->un_rqs_bp);
14897 
14898 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
14899 			switch ((*funcp)(bp, &pktp)) {
14900 			case  SD_PKT_ALLOC_SUCCESS:
14901 				xp->xb_pktp = pktp;
14902 				SD_TRACE(SD_LOG_IO_CORE, un,
14903 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14904 				    pktp);
14905 				goto got_pkt;
14906 
14907 			case SD_PKT_ALLOC_FAILURE:
14908 				/*
14909 				 * Temporary (hopefully) resource depletion.
14910 				 * Since retries and RQS commands always have a
14911 				 * scsi_pkt allocated, these cases should never
14912 				 * get here. So the only cases this needs to
14913 				 * handle is a bp from the waitq (which we put
14914 				 * back onto the waitq for sdrunout), or a bp
14915 				 * sent as an immed_bp (which we just fail).
14916 				 */
14917 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14918 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14919 
14920 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14921 
14922 				if (bp == immed_bp) {
14923 					/*
14924 					 * If SD_XB_DMA_FREED is clear, then
14925 					 * this is a failure to allocate a
14926 					 * scsi_pkt, and we must fail the
14927 					 * command.
14928 					 */
14929 					if ((xp->xb_pkt_flags &
14930 					    SD_XB_DMA_FREED) == 0) {
14931 						break;
14932 					}
14933 
14934 					/*
14935 					 * If this immediate command is NOT our
14936 					 * un_retry_bp, then we must fail it.
14937 					 */
14938 					if (bp != un->un_retry_bp) {
14939 						break;
14940 					}
14941 
14942 					/*
14943 					 * We get here if this cmd is our
14944 					 * un_retry_bp that was DMAFREED, but
14945 					 * scsi_init_pkt() failed to reallocate
14946 					 * DMA resources when we attempted to
14947 					 * retry it. This can happen when an
14948 					 * mpxio failover is in progress, but
14949 					 * we don't want to just fail the
14950 					 * command in this case.
14951 					 *
14952 					 * Use timeout(9F) to restart it after
14953 					 * a 100ms delay.  We don't want to
14954 					 * let sdrunout() restart it, because
14955 					 * sdrunout() is just supposed to start
14956 					 * commands that are sitting on the
14957 					 * wait queue.  The un_retry_bp stays
14958 					 * set until the command completes, but
14959 					 * sdrunout can be called many times
14960 					 * before that happens.  Since sdrunout
14961 					 * cannot tell if the un_retry_bp is
14962 					 * already in the transport, it could
14963 					 * end up calling scsi_transport() for
14964 					 * the un_retry_bp multiple times.
14965 					 *
14966 					 * Also: don't schedule the callback
14967 					 * if some other callback is already
14968 					 * pending.
14969 					 */
14970 					if (un->un_retry_statp == NULL) {
14971 						/*
14972 						 * restore the kstat pointer to
14973 						 * keep kstat counts coherent
14974 						 * when we do retry the command.
14975 						 */
14976 						un->un_retry_statp =
14977 						    saved_statp;
14978 					}
14979 
14980 					if ((un->un_startstop_timeid == NULL) &&
14981 					    (un->un_retry_timeid == NULL) &&
14982 					    (un->un_direct_priority_timeid ==
14983 					    NULL)) {
14984 
14985 						un->un_retry_timeid =
14986 						    timeout(
14987 						    sd_start_retry_command,
14988 						    un, SD_RESTART_TIMEOUT);
14989 					}
14990 					goto exit;
14991 				}
14992 
14993 #else
14994 				if (bp == immed_bp) {
14995 					break;	/* Just fail the command */
14996 				}
14997 #endif
14998 
14999 				/* Add the buf back to the head of the waitq */
15000 				bp->av_forw = un->un_waitq_headp;
15001 				un->un_waitq_headp = bp;
15002 				if (un->un_waitq_tailp == NULL) {
15003 					un->un_waitq_tailp = bp;
15004 				}
15005 				goto exit;
15006 
15007 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
15008 				/*
15009 				 * HBA DMA resource failure. Fail the command
15010 				 * and continue processing of the queues.
15011 				 */
15012 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15013 				    "sd_start_cmds: "
15014 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15015 				break;
15016 
15017 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15018 				/*
15019 				 * Note:x86: Partial DMA mapping not supported
15020 				 * for USCSI commands, and all the needed DMA
15021 				 * resources were not allocated.
15022 				 */
15023 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15024 				    "sd_start_cmds: "
15025 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15026 				break;
15027 
15028 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15029 				/*
15030 				 * Note:x86: Request cannot fit into CDB based
15031 				 * on lba and len.
15032 				 */
15033 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15034 				    "sd_start_cmds: "
15035 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15036 				break;
15037 
15038 			default:
15039 				/* Should NEVER get here! */
15040 				panic("scsi_initpkt error");
15041 				/*NOTREACHED*/
15042 			}
15043 
15044 			/*
15045 			 * Fatal error in allocating a scsi_pkt for this buf.
15046 			 * Update kstats & return the buf with an error code.
15047 			 * We must use sd_return_failed_command_no_restart() to
15048 			 * avoid a recursive call back into sd_start_cmds().
15049 			 * However this also means that we must keep processing
15050 			 * the waitq here in order to avoid stalling.
15051 			 */
15052 			if (statp == kstat_waitq_to_runq) {
15053 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15054 			}
15055 			sd_return_failed_command_no_restart(un, bp, EIO);
15056 			if (bp == immed_bp) {
15057 				/* immed_bp is gone by now, so clear this */
15058 				immed_bp = NULL;
15059 			}
15060 			continue;
15061 		}
15062 got_pkt:
15063 		if (bp == immed_bp) {
15064 			/* goto the head of the class.... */
15065 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15066 		}
15067 
15068 		un->un_ncmds_in_transport++;
15069 		SD_UPDATE_KSTATS(un, statp, bp);
15070 
15071 		/*
15072 		 * Call scsi_transport() to send the command to the target.
15073 		 * According to SCSA architecture, we must drop the mutex here
15074 		 * before calling scsi_transport() in order to avoid deadlock.
15075 		 * Note that the scsi_pkt's completion routine can be executed
15076 		 * (from interrupt context) even before the call to
15077 		 * scsi_transport() returns.
15078 		 */
15079 		SD_TRACE(SD_LOG_IO_CORE, un,
15080 		    "sd_start_cmds: calling scsi_transport()\n");
15081 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15082 
15083 		mutex_exit(SD_MUTEX(un));
15084 		rval = scsi_transport(xp->xb_pktp);
15085 		mutex_enter(SD_MUTEX(un));
15086 
15087 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15088 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
15089 
15090 		switch (rval) {
15091 		case TRAN_ACCEPT:
15092 			/* Clear this with every pkt accepted by the HBA */
15093 			un->un_tran_fatal_count = 0;
15094 			break;	/* Success; try the next cmd (if any) */
15095 
15096 		case TRAN_BUSY:
15097 			un->un_ncmds_in_transport--;
15098 			ASSERT(un->un_ncmds_in_transport >= 0);
15099 
15100 			/*
15101 			 * Don't retry request sense, the sense data
15102 			 * is lost when another request is sent.
15103 			 * Free up the rqs buf and retry
15104 			 * the original failed cmd.  Update kstat.
15105 			 */
15106 			if (bp == un->un_rqs_bp) {
15107 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15108 				bp = sd_mark_rqs_idle(un, xp);
15109 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15110 					NULL, NULL, EIO, SD_BSY_TIMEOUT / 500,
15111 					kstat_waitq_enter);
15112 				goto exit;
15113 			}
15114 
15115 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15116 			/*
15117 			 * Free the DMA resources for the  scsi_pkt. This will
15118 			 * allow mpxio to select another path the next time
15119 			 * we call scsi_transport() with this scsi_pkt.
15120 			 * See sdintr() for the rationalization behind this.
15121 			 */
15122 			if ((un->un_f_is_fibre == TRUE) &&
15123 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15124 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15125 				scsi_dmafree(xp->xb_pktp);
15126 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15127 			}
15128 #endif
15129 
15130 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15131 				/*
15132 				 * Commands that are SD_PATH_DIRECT_PRIORITY
15133 				 * are for error recovery situations. These do
15134 				 * not use the normal command waitq, so if they
15135 				 * get a TRAN_BUSY we cannot put them back onto
15136 				 * the waitq for later retry. One possible
15137 				 * problem is that there could already be some
15138 				 * other command on un_retry_bp that is waiting
15139 				 * for this one to complete, so we would be
15140 				 * deadlocked if we put this command back onto
15141 				 * the waitq for later retry (since un_retry_bp
15142 				 * must complete before the driver gets back to
15143 				 * commands on the waitq).
15144 				 *
15145 				 * To avoid deadlock we must schedule a callback
15146 				 * that will restart this command after a set
15147 				 * interval.  This should keep retrying for as
15148 				 * long as the underlying transport keeps
15149 				 * returning TRAN_BUSY (just like for other
15150 				 * commands).  Use the same timeout interval as
15151 				 * for the ordinary TRAN_BUSY retry.
15152 				 */
15153 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15154 				    "sd_start_cmds: scsi_transport() returned "
15155 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15156 
15157 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15158 				un->un_direct_priority_timeid =
15159 				    timeout(sd_start_direct_priority_command,
15160 				    bp, SD_BSY_TIMEOUT / 500);
15161 
15162 				goto exit;
15163 			}
15164 
15165 			/*
15166 			 * For TRAN_BUSY, we want to reduce the throttle value,
15167 			 * unless we are retrying a command.
15168 			 */
15169 			if (bp != un->un_retry_bp) {
15170 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15171 			}
15172 
15173 			/*
15174 			 * Set up the bp to be tried again 10 ms later.
15175 			 * Note:x86: Is there a timeout value in the sd_lun
15176 			 * for this condition?
15177 			 */
15178 			sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500,
15179 				kstat_runq_back_to_waitq);
15180 			goto exit;
15181 
15182 		case TRAN_FATAL_ERROR:
15183 			un->un_tran_fatal_count++;
15184 			/* FALLTHRU */
15185 
15186 		case TRAN_BADPKT:
15187 		default:
15188 			un->un_ncmds_in_transport--;
15189 			ASSERT(un->un_ncmds_in_transport >= 0);
15190 
15191 			/*
15192 			 * If this is our REQUEST SENSE command with a
15193 			 * transport error, we must get back the pointers
15194 			 * to the original buf, and mark the REQUEST
15195 			 * SENSE command as "available".
15196 			 */
15197 			if (bp == un->un_rqs_bp) {
15198 				bp = sd_mark_rqs_idle(un, xp);
15199 				xp = SD_GET_XBUF(bp);
15200 			} else {
15201 				/*
15202 				 * Legacy behavior: do not update transport
15203 				 * error count for request sense commands.
15204 				 */
15205 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
15206 			}
15207 
15208 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15209 			sd_print_transport_rejected_message(un, xp, rval);
15210 
15211 			/*
15212 			 * We must use sd_return_failed_command_no_restart() to
15213 			 * avoid a recursive call back into sd_start_cmds().
15214 			 * However this also means that we must keep processing
15215 			 * the waitq here in order to avoid stalling.
15216 			 */
15217 			sd_return_failed_command_no_restart(un, bp, EIO);
15218 
15219 			/*
15220 			 * Notify any threads waiting in sd_ddi_suspend() that
15221 			 * a command completion has occurred.
15222 			 */
15223 			if (un->un_state == SD_STATE_SUSPENDED) {
15224 				cv_broadcast(&un->un_disk_busy_cv);
15225 			}
15226 
15227 			if (bp == immed_bp) {
15228 				/* immed_bp is gone by now, so clear this */
15229 				immed_bp = NULL;
15230 			}
15231 			break;
15232 		}
15233 
15234 	} while (immed_bp == NULL);
15235 
15236 exit:
15237 	ASSERT(mutex_owned(SD_MUTEX(un)));
15238 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15239 }
15240 
15241 
15242 /*
15243  *    Function: sd_return_command
15244  *
15245  * Description: Returns a command to its originator (with or without an
15246  *		error).  Also starts commands waiting to be transported
15247  *		to the target.
15248  *
15249  *     Context: May be called from interrupt, kernel, or timeout context
15250  */
15251 
15252 static void
15253 sd_return_command(struct sd_lun *un, struct buf *bp)
15254 {
15255 	struct sd_xbuf *xp;
15256 #if defined(__i386) || defined(__amd64)
15257 	struct scsi_pkt *pktp;
15258 #endif
15259 
15260 	ASSERT(bp != NULL);
15261 	ASSERT(un != NULL);
15262 	ASSERT(mutex_owned(SD_MUTEX(un)));
15263 	ASSERT(bp != un->un_rqs_bp);
15264 	xp = SD_GET_XBUF(bp);
15265 	ASSERT(xp != NULL);
15266 
15267 #if defined(__i386) || defined(__amd64)
15268 	pktp = SD_GET_PKTP(bp);
15269 #endif
15270 
15271 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15272 
15273 #if defined(__i386) || defined(__amd64)
15274 	/*
15275 	 * Note:x86: check for the "sdrestart failed" case.
15276 	 */
15277 	if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15278 		(geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15279 		(xp->xb_pktp->pkt_resid == 0)) {
15280 
15281 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15282 			/*
15283 			 * Successfully set up next portion of cmd
15284 			 * transfer, try sending it
15285 			 */
15286 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15287 			    NULL, NULL, 0, (clock_t)0, NULL);
15288 			sd_start_cmds(un, NULL);
15289 			return;	/* Note:x86: need a return here? */
15290 		}
15291 	}
15292 #endif
15293 
15294 	/*
15295 	 * If this is the failfast bp, clear it from un_failfast_bp. This
15296 	 * can happen if upon being re-tried the failfast bp either
15297 	 * succeeded or encountered another error (possibly even a different
15298 	 * error than the one that precipitated the failfast state, but in
15299 	 * that case it would have had to exhaust retries as well). Regardless,
15300 	 * this should not occur whenever the instance is in the active
15301 	 * failfast state.
15302 	 */
15303 	if (bp == un->un_failfast_bp) {
15304 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15305 		un->un_failfast_bp = NULL;
15306 	}
15307 
15308 	/*
15309 	 * Clear the failfast state upon successful completion of ANY cmd.
15310 	 */
15311 	if (bp->b_error == 0) {
15312 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15313 	}
15314 
15315 	/*
15316 	 * This is used if the command was retried one or more times. Show that
15317 	 * we are done with it, and allow processing of the waitq to resume.
15318 	 */
15319 	if (bp == un->un_retry_bp) {
15320 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15321 		    "sd_return_command: un:0x%p: "
15322 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15323 		un->un_retry_bp = NULL;
15324 		un->un_retry_statp = NULL;
15325 	}
15326 
15327 	SD_UPDATE_RDWR_STATS(un, bp);
15328 	SD_UPDATE_PARTITION_STATS(un, bp);
15329 
15330 	switch (un->un_state) {
15331 	case SD_STATE_SUSPENDED:
15332 		/*
15333 		 * Notify any threads waiting in sd_ddi_suspend() that
15334 		 * a command completion has occurred.
15335 		 */
15336 		cv_broadcast(&un->un_disk_busy_cv);
15337 		break;
15338 	default:
15339 		sd_start_cmds(un, NULL);
15340 		break;
15341 	}
15342 
15343 	/* Return this command up the iodone chain to its originator. */
15344 	mutex_exit(SD_MUTEX(un));
15345 
15346 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15347 	xp->xb_pktp = NULL;
15348 
15349 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15350 
15351 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15352 	mutex_enter(SD_MUTEX(un));
15353 
15354 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15355 }
15356 
15357 
15358 /*
15359  *    Function: sd_return_failed_command
15360  *
15361  * Description: Command completion when an error occurred.
15362  *
15363  *     Context: May be called from interrupt context
15364  */
15365 
15366 static void
15367 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15368 {
15369 	ASSERT(bp != NULL);
15370 	ASSERT(un != NULL);
15371 	ASSERT(mutex_owned(SD_MUTEX(un)));
15372 
15373 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15374 	    "sd_return_failed_command: entry\n");
15375 
15376 	/*
15377 	 * b_resid could already be nonzero due to a partial data
15378 	 * transfer, so do not change it here.
15379 	 */
15380 	SD_BIOERROR(bp, errcode);
15381 
15382 	sd_return_command(un, bp);
15383 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15384 	    "sd_return_failed_command: exit\n");
15385 }
15386 
15387 
15388 /*
15389  *    Function: sd_return_failed_command_no_restart
15390  *
15391  * Description: Same as sd_return_failed_command, but ensures that no
15392  *		call back into sd_start_cmds will be issued.
15393  *
15394  *     Context: May be called from interrupt context
15395  */
15396 
15397 static void
15398 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15399 	int errcode)
15400 {
15401 	struct sd_xbuf *xp;
15402 
15403 	ASSERT(bp != NULL);
15404 	ASSERT(un != NULL);
15405 	ASSERT(mutex_owned(SD_MUTEX(un)));
15406 	xp = SD_GET_XBUF(bp);
15407 	ASSERT(xp != NULL);
15408 	ASSERT(errcode != 0);
15409 
15410 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15411 	    "sd_return_failed_command_no_restart: entry\n");
15412 
15413 	/*
15414 	 * b_resid could already be nonzero due to a partial data
15415 	 * transfer, so do not change it here.
15416 	 */
15417 	SD_BIOERROR(bp, errcode);
15418 
15419 	/*
15420 	 * If this is the failfast bp, clear it. This can happen if the
15421 	 * failfast bp encounterd a fatal error when we attempted to
15422 	 * re-try it (such as a scsi_transport(9F) failure).  However
15423 	 * we should NOT be in an active failfast state if the failfast
15424 	 * bp is not NULL.
15425 	 */
15426 	if (bp == un->un_failfast_bp) {
15427 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15428 		un->un_failfast_bp = NULL;
15429 	}
15430 
15431 	if (bp == un->un_retry_bp) {
15432 		/*
15433 		 * This command was retried one or more times. Show that we are
15434 		 * done with it, and allow processing of the waitq to resume.
15435 		 */
15436 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15437 		    "sd_return_failed_command_no_restart: "
15438 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15439 		un->un_retry_bp = NULL;
15440 		un->un_retry_statp = NULL;
15441 	}
15442 
15443 	SD_UPDATE_RDWR_STATS(un, bp);
15444 	SD_UPDATE_PARTITION_STATS(un, bp);
15445 
15446 	mutex_exit(SD_MUTEX(un));
15447 
15448 	if (xp->xb_pktp != NULL) {
15449 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15450 		xp->xb_pktp = NULL;
15451 	}
15452 
15453 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15454 
15455 	mutex_enter(SD_MUTEX(un));
15456 
15457 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15458 	    "sd_return_failed_command_no_restart: exit\n");
15459 }
15460 
15461 
15462 /*
15463  *    Function: sd_retry_command
15464  *
15465  * Description: queue up a command for retry, or (optionally) fail it
15466  *		if retry counts are exhausted.
15467  *
15468  *   Arguments: un - Pointer to the sd_lun struct for the target.
15469  *
15470  *		bp - Pointer to the buf for the command to be retried.
15471  *
15472  *		retry_check_flag - Flag to see which (if any) of the retry
15473  *		   counts should be decremented/checked. If the indicated
15474  *		   retry count is exhausted, then the command will not be
15475  *		   retried; it will be failed instead. This should use a
15476  *		   value equal to one of the following:
15477  *
15478  *			SD_RETRIES_NOCHECK
15479  *			SD_RESD_RETRIES_STANDARD
15480  *			SD_RETRIES_VICTIM
15481  *
15482  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15483  *		   if the check should be made to see of FLAG_ISOLATE is set
15484  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
15485  *		   not retried, it is simply failed.
15486  *
15487  *		user_funcp - Ptr to function to call before dispatching the
15488  *		   command. May be NULL if no action needs to be performed.
15489  *		   (Primarily intended for printing messages.)
15490  *
15491  *		user_arg - Optional argument to be passed along to
15492  *		   the user_funcp call.
15493  *
15494  *		failure_code - errno return code to set in the bp if the
15495  *		   command is going to be failed.
15496  *
15497  *		retry_delay - Retry delay interval in (clock_t) units. May
15498  *		   be zero which indicates that the retry should be retried
15499  *		   immediately (ie, without an intervening delay).
15500  *
15501  *		statp - Ptr to kstat function to be updated if the command
15502  *		   is queued for a delayed retry. May be NULL if no kstat
15503  *		   update is desired.
15504  *
15505  *     Context: May be called from interupt context.
15506  */
15507 
15508 static void
15509 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15510 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
15511 	code), void *user_arg, int failure_code,  clock_t retry_delay,
15512 	void (*statp)(kstat_io_t *))
15513 {
15514 	struct sd_xbuf	*xp;
15515 	struct scsi_pkt	*pktp;
15516 
15517 	ASSERT(un != NULL);
15518 	ASSERT(mutex_owned(SD_MUTEX(un)));
15519 	ASSERT(bp != NULL);
15520 	xp = SD_GET_XBUF(bp);
15521 	ASSERT(xp != NULL);
15522 	pktp = SD_GET_PKTP(bp);
15523 	ASSERT(pktp != NULL);
15524 
15525 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15526 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15527 
15528 	/*
15529 	 * If we are syncing or dumping, fail the command to avoid
15530 	 * recursively calling back into scsi_transport().
15531 	 */
15532 	if (ddi_in_panic()) {
15533 		goto fail_command_no_log;
15534 	}
15535 
15536 	/*
15537 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15538 	 * log an error and fail the command.
15539 	 */
15540 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15541 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15542 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
15543 		sd_dump_memory(un, SD_LOG_IO, "CDB",
15544 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15545 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15546 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15547 		goto fail_command;
15548 	}
15549 
15550 	/*
15551 	 * If we are suspended, then put the command onto head of the
15552 	 * wait queue since we don't want to start more commands.
15553 	 */
15554 	switch (un->un_state) {
15555 	case SD_STATE_SUSPENDED:
15556 	case SD_STATE_DUMPING:
15557 		bp->av_forw = un->un_waitq_headp;
15558 		un->un_waitq_headp = bp;
15559 		if (un->un_waitq_tailp == NULL) {
15560 			un->un_waitq_tailp = bp;
15561 		}
15562 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15563 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15564 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15565 		return;
15566 	default:
15567 		break;
15568 	}
15569 
15570 	/*
15571 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
15572 	 * is set; if it is then we do not want to retry the command.
15573 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15574 	 */
15575 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15576 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15577 			goto fail_command;
15578 		}
15579 	}
15580 
15581 
15582 	/*
15583 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15584 	 * command timeout or a selection timeout has occurred. This means
15585 	 * that we were unable to establish an kind of communication with
15586 	 * the target, and subsequent retries and/or commands are likely
15587 	 * to encounter similar results and take a long time to complete.
15588 	 *
15589 	 * If this is a failfast error condition, we need to update the
15590 	 * failfast state, even if this bp does not have B_FAILFAST set.
15591 	 */
15592 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
15593 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15594 			ASSERT(un->un_failfast_bp == NULL);
15595 			/*
15596 			 * If we are already in the active failfast state, and
15597 			 * another failfast error condition has been detected,
15598 			 * then fail this command if it has B_FAILFAST set.
15599 			 * If B_FAILFAST is clear, then maintain the legacy
15600 			 * behavior of retrying heroically, even tho this will
15601 			 * take a lot more time to fail the command.
15602 			 */
15603 			if (bp->b_flags & B_FAILFAST) {
15604 				goto fail_command;
15605 			}
15606 		} else {
15607 			/*
15608 			 * We're not in the active failfast state, but we
15609 			 * have a failfast error condition, so we must begin
15610 			 * transition to the next state. We do this regardless
15611 			 * of whether or not this bp has B_FAILFAST set.
15612 			 */
15613 			if (un->un_failfast_bp == NULL) {
15614 				/*
15615 				 * This is the first bp to meet a failfast
15616 				 * condition so save it on un_failfast_bp &
15617 				 * do normal retry processing. Do not enter
15618 				 * active failfast state yet. This marks
15619 				 * entry into the "failfast pending" state.
15620 				 */
15621 				un->un_failfast_bp = bp;
15622 
15623 			} else if (un->un_failfast_bp == bp) {
15624 				/*
15625 				 * This is the second time *this* bp has
15626 				 * encountered a failfast error condition,
15627 				 * so enter active failfast state & flush
15628 				 * queues as appropriate.
15629 				 */
15630 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
15631 				un->un_failfast_bp = NULL;
15632 				sd_failfast_flushq(un);
15633 
15634 				/*
15635 				 * Fail this bp now if B_FAILFAST set;
15636 				 * otherwise continue with retries. (It would
15637 				 * be pretty ironic if this bp succeeded on a
15638 				 * subsequent retry after we just flushed all
15639 				 * the queues).
15640 				 */
15641 				if (bp->b_flags & B_FAILFAST) {
15642 					goto fail_command;
15643 				}
15644 
15645 #if !defined(lint) && !defined(__lint)
15646 			} else {
15647 				/*
15648 				 * If neither of the preceeding conditionals
15649 				 * was true, it means that there is some
15650 				 * *other* bp that has met an inital failfast
15651 				 * condition and is currently either being
15652 				 * retried or is waiting to be retried. In
15653 				 * that case we should perform normal retry
15654 				 * processing on *this* bp, since there is a
15655 				 * chance that the current failfast condition
15656 				 * is transient and recoverable. If that does
15657 				 * not turn out to be the case, then retries
15658 				 * will be cleared when the wait queue is
15659 				 * flushed anyway.
15660 				 */
15661 #endif
15662 			}
15663 		}
15664 	} else {
15665 		/*
15666 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
15667 		 * likely were able to at least establish some level of
15668 		 * communication with the target and subsequent commands
15669 		 * and/or retries are likely to get through to the target,
15670 		 * In this case we want to be aggressive about clearing
15671 		 * the failfast state. Note that this does not affect
15672 		 * the "failfast pending" condition.
15673 		 */
15674 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15675 	}
15676 
15677 
15678 	/*
15679 	 * Check the specified retry count to see if we can still do
15680 	 * any retries with this pkt before we should fail it.
15681 	 */
15682 	switch (retry_check_flag & SD_RETRIES_MASK) {
15683 	case SD_RETRIES_VICTIM:
15684 		/*
15685 		 * Check the victim retry count. If exhausted, then fall
15686 		 * thru & check against the standard retry count.
15687 		 */
15688 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15689 			/* Increment count & proceed with the retry */
15690 			xp->xb_victim_retry_count++;
15691 			break;
15692 		}
15693 		/* Victim retries exhausted, fall back to std. retries... */
15694 		/* FALLTHRU */
15695 
15696 	case SD_RETRIES_STANDARD:
15697 		if (xp->xb_retry_count >= un->un_retry_count) {
15698 			/* Retries exhausted, fail the command */
15699 			SD_TRACE(SD_LOG_IO_CORE, un,
15700 			    "sd_retry_command: retries exhausted!\n");
15701 			/*
15702 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
15703 			 * commands with nonzero pkt_resid.
15704 			 */
15705 			if ((pktp->pkt_reason == CMD_CMPLT) &&
15706 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15707 			    (pktp->pkt_resid != 0)) {
15708 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15709 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15710 					SD_UPDATE_B_RESID(bp, pktp);
15711 				}
15712 			}
15713 			goto fail_command;
15714 		}
15715 		xp->xb_retry_count++;
15716 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15717 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15718 		break;
15719 
15720 	case SD_RETRIES_UA:
15721 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15722 			/* Retries exhausted, fail the command */
15723 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15724 			    "Unit Attention retries exhausted. "
15725 			    "Check the target.\n");
15726 			goto fail_command;
15727 		}
15728 		xp->xb_ua_retry_count++;
15729 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15730 		    "sd_retry_command: retry count:%d\n",
15731 			xp->xb_ua_retry_count);
15732 		break;
15733 
15734 	case SD_RETRIES_BUSY:
15735 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
15736 			/* Retries exhausted, fail the command */
15737 			SD_TRACE(SD_LOG_IO_CORE, un,
15738 			    "sd_retry_command: retries exhausted!\n");
15739 			goto fail_command;
15740 		}
15741 		xp->xb_retry_count++;
15742 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15743 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15744 		break;
15745 
15746 	case SD_RETRIES_NOCHECK:
15747 	default:
15748 		/* No retry count to check. Just proceed with the retry */
15749 		break;
15750 	}
15751 
15752 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15753 
15754 	/*
15755 	 * If we were given a zero timeout, we must attempt to retry the
15756 	 * command immediately (ie, without a delay).
15757 	 */
15758 	if (retry_delay == 0) {
15759 		/*
15760 		 * Check some limiting conditions to see if we can actually
15761 		 * do the immediate retry.  If we cannot, then we must
15762 		 * fall back to queueing up a delayed retry.
15763 		 */
15764 		if (un->un_ncmds_in_transport >= un->un_throttle) {
15765 			/*
15766 			 * We are at the throttle limit for the target,
15767 			 * fall back to delayed retry.
15768 			 */
15769 			retry_delay = SD_BSY_TIMEOUT;
15770 			statp = kstat_waitq_enter;
15771 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15772 			    "sd_retry_command: immed. retry hit "
15773 			    "throttle!\n");
15774 		} else {
15775 			/*
15776 			 * We're clear to proceed with the immediate retry.
15777 			 * First call the user-provided function (if any)
15778 			 */
15779 			if (user_funcp != NULL) {
15780 				(*user_funcp)(un, bp, user_arg,
15781 				    SD_IMMEDIATE_RETRY_ISSUED);
15782 #ifdef __lock_lint
15783 				sd_print_incomplete_msg(un, bp, user_arg,
15784 				    SD_IMMEDIATE_RETRY_ISSUED);
15785 				sd_print_cmd_incomplete_msg(un, bp, user_arg,
15786 				    SD_IMMEDIATE_RETRY_ISSUED);
15787 				sd_print_sense_failed_msg(un, bp, user_arg,
15788 				    SD_IMMEDIATE_RETRY_ISSUED);
15789 #endif
15790 			}
15791 
15792 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15793 			    "sd_retry_command: issuing immediate retry\n");
15794 
15795 			/*
15796 			 * Call sd_start_cmds() to transport the command to
15797 			 * the target.
15798 			 */
15799 			sd_start_cmds(un, bp);
15800 
15801 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15802 			    "sd_retry_command exit\n");
15803 			return;
15804 		}
15805 	}
15806 
15807 	/*
15808 	 * Set up to retry the command after a delay.
15809 	 * First call the user-provided function (if any)
15810 	 */
15811 	if (user_funcp != NULL) {
15812 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15813 	}
15814 
15815 	sd_set_retry_bp(un, bp, retry_delay, statp);
15816 
15817 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15818 	return;
15819 
15820 fail_command:
15821 
15822 	if (user_funcp != NULL) {
15823 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15824 	}
15825 
15826 fail_command_no_log:
15827 
15828 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15829 	    "sd_retry_command: returning failed command\n");
15830 
15831 	sd_return_failed_command(un, bp, failure_code);
15832 
15833 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15834 }
15835 
15836 
15837 /*
15838  *    Function: sd_set_retry_bp
15839  *
15840  * Description: Set up the given bp for retry.
15841  *
15842  *   Arguments: un - ptr to associated softstate
15843  *		bp - ptr to buf(9S) for the command
15844  *		retry_delay - time interval before issuing retry (may be 0)
15845  *		statp - optional pointer to kstat function
15846  *
15847  *     Context: May be called under interrupt context
15848  */
15849 
15850 static void
15851 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15852 	void (*statp)(kstat_io_t *))
15853 {
15854 	ASSERT(un != NULL);
15855 	ASSERT(mutex_owned(SD_MUTEX(un)));
15856 	ASSERT(bp != NULL);
15857 
15858 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15859 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15860 
15861 	/*
15862 	 * Indicate that the command is being retried. This will not allow any
15863 	 * other commands on the wait queue to be transported to the target
15864 	 * until this command has been completed (success or failure). The
15865 	 * "retry command" is not transported to the target until the given
15866 	 * time delay expires, unless the user specified a 0 retry_delay.
15867 	 *
15868 	 * Note: the timeout(9F) callback routine is what actually calls
15869 	 * sd_start_cmds() to transport the command, with the exception of a
15870 	 * zero retry_delay. The only current implementor of a zero retry delay
15871 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
15872 	 */
15873 	if (un->un_retry_bp == NULL) {
15874 		ASSERT(un->un_retry_statp == NULL);
15875 		un->un_retry_bp = bp;
15876 
15877 		/*
15878 		 * If the user has not specified a delay the command should
15879 		 * be queued and no timeout should be scheduled.
15880 		 */
15881 		if (retry_delay == 0) {
15882 			/*
15883 			 * Save the kstat pointer that will be used in the
15884 			 * call to SD_UPDATE_KSTATS() below, so that
15885 			 * sd_start_cmds() can correctly decrement the waitq
15886 			 * count when it is time to transport this command.
15887 			 */
15888 			un->un_retry_statp = statp;
15889 			goto done;
15890 		}
15891 	}
15892 
15893 	if (un->un_retry_bp == bp) {
15894 		/*
15895 		 * Save the kstat pointer that will be used in the call to
15896 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
15897 		 * correctly decrement the waitq count when it is time to
15898 		 * transport this command.
15899 		 */
15900 		un->un_retry_statp = statp;
15901 
15902 		/*
15903 		 * Schedule a timeout if:
15904 		 *   1) The user has specified a delay.
15905 		 *   2) There is not a START_STOP_UNIT callback pending.
15906 		 *
15907 		 * If no delay has been specified, then it is up to the caller
15908 		 * to ensure that IO processing continues without stalling.
15909 		 * Effectively, this means that the caller will issue the
15910 		 * required call to sd_start_cmds(). The START_STOP_UNIT
15911 		 * callback does this after the START STOP UNIT command has
15912 		 * completed. In either of these cases we should not schedule
15913 		 * a timeout callback here.  Also don't schedule the timeout if
15914 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
15915 		 */
15916 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
15917 		    (un->un_direct_priority_timeid == NULL)) {
15918 			un->un_retry_timeid =
15919 			    timeout(sd_start_retry_command, un, retry_delay);
15920 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15921 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
15922 			    " bp:0x%p un_retry_timeid:0x%p\n",
15923 			    un, bp, un->un_retry_timeid);
15924 		}
15925 	} else {
15926 		/*
15927 		 * We only get in here if there is already another command
15928 		 * waiting to be retried.  In this case, we just put the
15929 		 * given command onto the wait queue, so it can be transported
15930 		 * after the current retry command has completed.
15931 		 *
15932 		 * Also we have to make sure that if the command at the head
15933 		 * of the wait queue is the un_failfast_bp, that we do not
15934 		 * put ahead of it any other commands that are to be retried.
15935 		 */
15936 		if ((un->un_failfast_bp != NULL) &&
15937 		    (un->un_failfast_bp == un->un_waitq_headp)) {
15938 			/*
15939 			 * Enqueue this command AFTER the first command on
15940 			 * the wait queue (which is also un_failfast_bp).
15941 			 */
15942 			bp->av_forw = un->un_waitq_headp->av_forw;
15943 			un->un_waitq_headp->av_forw = bp;
15944 			if (un->un_waitq_headp == un->un_waitq_tailp) {
15945 				un->un_waitq_tailp = bp;
15946 			}
15947 		} else {
15948 			/* Enqueue this command at the head of the waitq. */
15949 			bp->av_forw = un->un_waitq_headp;
15950 			un->un_waitq_headp = bp;
15951 			if (un->un_waitq_tailp == NULL) {
15952 				un->un_waitq_tailp = bp;
15953 			}
15954 		}
15955 
15956 		if (statp == NULL) {
15957 			statp = kstat_waitq_enter;
15958 		}
15959 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15960 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
15961 	}
15962 
15963 done:
15964 	if (statp != NULL) {
15965 		SD_UPDATE_KSTATS(un, statp, bp);
15966 	}
15967 
15968 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15969 	    "sd_set_retry_bp: exit un:0x%p\n", un);
15970 }
15971 
15972 
15973 /*
15974  *    Function: sd_start_retry_command
15975  *
15976  * Description: Start the command that has been waiting on the target's
15977  *		retry queue.  Called from timeout(9F) context after the
15978  *		retry delay interval has expired.
15979  *
15980  *   Arguments: arg - pointer to associated softstate for the device.
15981  *
15982  *     Context: timeout(9F) thread context.  May not sleep.
15983  */
15984 
15985 static void
15986 sd_start_retry_command(void *arg)
15987 {
15988 	struct sd_lun *un = arg;
15989 
15990 	ASSERT(un != NULL);
15991 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15992 
15993 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15994 	    "sd_start_retry_command: entry\n");
15995 
15996 	mutex_enter(SD_MUTEX(un));
15997 
15998 	un->un_retry_timeid = NULL;
15999 
16000 	if (un->un_retry_bp != NULL) {
16001 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16002 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16003 		    un, un->un_retry_bp);
16004 		sd_start_cmds(un, un->un_retry_bp);
16005 	}
16006 
16007 	mutex_exit(SD_MUTEX(un));
16008 
16009 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16010 	    "sd_start_retry_command: exit\n");
16011 }
16012 
16013 
16014 /*
16015  *    Function: sd_start_direct_priority_command
16016  *
16017  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16018  *		received TRAN_BUSY when we called scsi_transport() to send it
16019  *		to the underlying HBA. This function is called from timeout(9F)
16020  *		context after the delay interval has expired.
16021  *
16022  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
16023  *
16024  *     Context: timeout(9F) thread context.  May not sleep.
16025  */
16026 
16027 static void
16028 sd_start_direct_priority_command(void *arg)
16029 {
16030 	struct buf	*priority_bp = arg;
16031 	struct sd_lun	*un;
16032 
16033 	ASSERT(priority_bp != NULL);
16034 	un = SD_GET_UN(priority_bp);
16035 	ASSERT(un != NULL);
16036 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16037 
16038 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16039 	    "sd_start_direct_priority_command: entry\n");
16040 
16041 	mutex_enter(SD_MUTEX(un));
16042 	un->un_direct_priority_timeid = NULL;
16043 	sd_start_cmds(un, priority_bp);
16044 	mutex_exit(SD_MUTEX(un));
16045 
16046 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16047 	    "sd_start_direct_priority_command: exit\n");
16048 }
16049 
16050 
16051 /*
16052  *    Function: sd_send_request_sense_command
16053  *
16054  * Description: Sends a REQUEST SENSE command to the target
16055  *
16056  *     Context: May be called from interrupt context.
16057  */
16058 
16059 static void
16060 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16061 	struct scsi_pkt *pktp)
16062 {
16063 	ASSERT(bp != NULL);
16064 	ASSERT(un != NULL);
16065 	ASSERT(mutex_owned(SD_MUTEX(un)));
16066 
16067 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16068 	    "entry: buf:0x%p\n", bp);
16069 
16070 	/*
16071 	 * If we are syncing or dumping, then fail the command to avoid a
16072 	 * recursive callback into scsi_transport(). Also fail the command
16073 	 * if we are suspended (legacy behavior).
16074 	 */
16075 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16076 	    (un->un_state == SD_STATE_DUMPING)) {
16077 		sd_return_failed_command(un, bp, EIO);
16078 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16079 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
16080 		return;
16081 	}
16082 
16083 	/*
16084 	 * Retry the failed command and don't issue the request sense if:
16085 	 *    1) the sense buf is busy
16086 	 *    2) we have 1 or more outstanding commands on the target
16087 	 *    (the sense data will be cleared or invalidated any way)
16088 	 *
16089 	 * Note: There could be an issue with not checking a retry limit here,
16090 	 * the problem is determining which retry limit to check.
16091 	 */
16092 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16093 		/* Don't retry if the command is flagged as non-retryable */
16094 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16095 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16096 			    NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter);
16097 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16098 			    "sd_send_request_sense_command: "
16099 			    "at full throttle, retrying exit\n");
16100 		} else {
16101 			sd_return_failed_command(un, bp, EIO);
16102 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16103 			    "sd_send_request_sense_command: "
16104 			    "at full throttle, non-retryable exit\n");
16105 		}
16106 		return;
16107 	}
16108 
16109 	sd_mark_rqs_busy(un, bp);
16110 	sd_start_cmds(un, un->un_rqs_bp);
16111 
16112 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16113 	    "sd_send_request_sense_command: exit\n");
16114 }
16115 
16116 
16117 /*
16118  *    Function: sd_mark_rqs_busy
16119  *
16120  * Description: Indicate that the request sense bp for this instance is
16121  *		in use.
16122  *
16123  *     Context: May be called under interrupt context
16124  */
16125 
16126 static void
16127 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16128 {
16129 	struct sd_xbuf	*sense_xp;
16130 
16131 	ASSERT(un != NULL);
16132 	ASSERT(bp != NULL);
16133 	ASSERT(mutex_owned(SD_MUTEX(un)));
16134 	ASSERT(un->un_sense_isbusy == 0);
16135 
16136 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16137 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16138 
16139 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16140 	ASSERT(sense_xp != NULL);
16141 
16142 	SD_INFO(SD_LOG_IO, un,
16143 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16144 
16145 	ASSERT(sense_xp->xb_pktp != NULL);
16146 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16147 	    == (FLAG_SENSING | FLAG_HEAD));
16148 
16149 	un->un_sense_isbusy = 1;
16150 	un->un_rqs_bp->b_resid = 0;
16151 	sense_xp->xb_pktp->pkt_resid  = 0;
16152 	sense_xp->xb_pktp->pkt_reason = 0;
16153 
16154 	/* So we can get back the bp at interrupt time! */
16155 	sense_xp->xb_sense_bp = bp;
16156 
16157 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16158 
16159 	/*
16160 	 * Mark this buf as awaiting sense data. (This is already set in
16161 	 * the pkt_flags for the RQS packet.)
16162 	 */
16163 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16164 
16165 	sense_xp->xb_retry_count	= 0;
16166 	sense_xp->xb_victim_retry_count = 0;
16167 	sense_xp->xb_ua_retry_count	= 0;
16168 	sense_xp->xb_dma_resid  = 0;
16169 
16170 	/* Clean up the fields for auto-request sense */
16171 	sense_xp->xb_sense_status = 0;
16172 	sense_xp->xb_sense_state  = 0;
16173 	sense_xp->xb_sense_resid  = 0;
16174 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16175 
16176 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16177 }
16178 
16179 
16180 /*
16181  *    Function: sd_mark_rqs_idle
16182  *
16183  * Description: SD_MUTEX must be held continuously through this routine
16184  *		to prevent reuse of the rqs struct before the caller can
16185  *		complete it's processing.
16186  *
16187  * Return Code: Pointer to the RQS buf
16188  *
16189  *     Context: May be called under interrupt context
16190  */
16191 
16192 static struct buf *
16193 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16194 {
16195 	struct buf *bp;
16196 	ASSERT(un != NULL);
16197 	ASSERT(sense_xp != NULL);
16198 	ASSERT(mutex_owned(SD_MUTEX(un)));
16199 	ASSERT(un->un_sense_isbusy != 0);
16200 
16201 	un->un_sense_isbusy = 0;
16202 	bp = sense_xp->xb_sense_bp;
16203 	sense_xp->xb_sense_bp = NULL;
16204 
16205 	/* This pkt is no longer interested in getting sense data */
16206 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16207 
16208 	return (bp);
16209 }
16210 
16211 
16212 
16213 /*
16214  *    Function: sd_alloc_rqs
16215  *
16216  * Description: Set up the unit to receive auto request sense data
16217  *
16218  * Return Code: DDI_SUCCESS or DDI_FAILURE
16219  *
16220  *     Context: Called under attach(9E) context
16221  */
16222 
16223 static int
16224 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16225 {
16226 	struct sd_xbuf *xp;
16227 
16228 	ASSERT(un != NULL);
16229 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16230 	ASSERT(un->un_rqs_bp == NULL);
16231 	ASSERT(un->un_rqs_pktp == NULL);
16232 
16233 	/*
16234 	 * First allocate the required buf and scsi_pkt structs, then set up
16235 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
16236 	 */
16237 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16238 	    SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16239 	if (un->un_rqs_bp == NULL) {
16240 		return (DDI_FAILURE);
16241 	}
16242 
16243 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16244 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16245 
16246 	if (un->un_rqs_pktp == NULL) {
16247 		sd_free_rqs(un);
16248 		return (DDI_FAILURE);
16249 	}
16250 
16251 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16252 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16253 	    SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
16254 
16255 	SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16256 
16257 	/* Set up the other needed members in the ARQ scsi_pkt. */
16258 	un->un_rqs_pktp->pkt_comp   = sdintr;
16259 	un->un_rqs_pktp->pkt_time   = sd_io_time;
16260 	un->un_rqs_pktp->pkt_flags |=
16261 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
16262 
16263 	/*
16264 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16265 	 * provide any intpkt, destroypkt routines as we take care of
16266 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16267 	 */
16268 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16269 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16270 	xp->xb_pktp = un->un_rqs_pktp;
16271 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
16272 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16273 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16274 
16275 	/*
16276 	 * Save the pointer to the request sense private bp so it can
16277 	 * be retrieved in sdintr.
16278 	 */
16279 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16280 	ASSERT(un->un_rqs_bp->b_private == xp);
16281 
16282 	/*
16283 	 * See if the HBA supports auto-request sense for the specified
16284 	 * target/lun. If it does, then try to enable it (if not already
16285 	 * enabled).
16286 	 *
16287 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16288 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16289 	 * return success.  However, in both of these cases ARQ is always
16290 	 * enabled and scsi_ifgetcap will always return true. The best approach
16291 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16292 	 *
16293 	 * The 3rd case is the HBA (adp) always return enabled on
16294 	 * scsi_ifgetgetcap even when it's not enable, the best approach
16295 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16296 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16297 	 */
16298 
16299 	if (un->un_f_is_fibre == TRUE) {
16300 		un->un_f_arq_enabled = TRUE;
16301 	} else {
16302 #if defined(__i386) || defined(__amd64)
16303 		/*
16304 		 * Circumvent the Adaptec bug, remove this code when
16305 		 * the bug is fixed
16306 		 */
16307 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16308 #endif
16309 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16310 		case 0:
16311 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16312 				"sd_alloc_rqs: HBA supports ARQ\n");
16313 			/*
16314 			 * ARQ is supported by this HBA but currently is not
16315 			 * enabled. Attempt to enable it and if successful then
16316 			 * mark this instance as ARQ enabled.
16317 			 */
16318 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16319 				== 1) {
16320 				/* Successfully enabled ARQ in the HBA */
16321 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16322 					"sd_alloc_rqs: ARQ enabled\n");
16323 				un->un_f_arq_enabled = TRUE;
16324 			} else {
16325 				/* Could not enable ARQ in the HBA */
16326 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16327 				"sd_alloc_rqs: failed ARQ enable\n");
16328 				un->un_f_arq_enabled = FALSE;
16329 			}
16330 			break;
16331 		case 1:
16332 			/*
16333 			 * ARQ is supported by this HBA and is already enabled.
16334 			 * Just mark ARQ as enabled for this instance.
16335 			 */
16336 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16337 				"sd_alloc_rqs: ARQ already enabled\n");
16338 			un->un_f_arq_enabled = TRUE;
16339 			break;
16340 		default:
16341 			/*
16342 			 * ARQ is not supported by this HBA; disable it for this
16343 			 * instance.
16344 			 */
16345 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16346 				"sd_alloc_rqs: HBA does not support ARQ\n");
16347 			un->un_f_arq_enabled = FALSE;
16348 			break;
16349 		}
16350 	}
16351 
16352 	return (DDI_SUCCESS);
16353 }
16354 
16355 
16356 /*
16357  *    Function: sd_free_rqs
16358  *
16359  * Description: Cleanup for the pre-instance RQS command.
16360  *
16361  *     Context: Kernel thread context
16362  */
16363 
16364 static void
16365 sd_free_rqs(struct sd_lun *un)
16366 {
16367 	ASSERT(un != NULL);
16368 
16369 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16370 
16371 	/*
16372 	 * If consistent memory is bound to a scsi_pkt, the pkt
16373 	 * has to be destroyed *before* freeing the consistent memory.
16374 	 * Don't change the sequence of this operations.
16375 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
16376 	 * after it was freed in scsi_free_consistent_buf().
16377 	 */
16378 	if (un->un_rqs_pktp != NULL) {
16379 		scsi_destroy_pkt(un->un_rqs_pktp);
16380 		un->un_rqs_pktp = NULL;
16381 	}
16382 
16383 	if (un->un_rqs_bp != NULL) {
16384 		kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf));
16385 		scsi_free_consistent_buf(un->un_rqs_bp);
16386 		un->un_rqs_bp = NULL;
16387 	}
16388 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16389 }
16390 
16391 
16392 
16393 /*
16394  *    Function: sd_reduce_throttle
16395  *
16396  * Description: Reduces the maximun # of outstanding commands on a
16397  *		target to the current number of outstanding commands.
16398  *		Queues a tiemout(9F) callback to restore the limit
16399  *		after a specified interval has elapsed.
16400  *		Typically used when we get a TRAN_BUSY return code
16401  *		back from scsi_transport().
16402  *
16403  *   Arguments: un - ptr to the sd_lun softstate struct
16404  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16405  *
16406  *     Context: May be called from interrupt context
16407  */
16408 
16409 static void
16410 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16411 {
16412 	ASSERT(un != NULL);
16413 	ASSERT(mutex_owned(SD_MUTEX(un)));
16414 	ASSERT(un->un_ncmds_in_transport >= 0);
16415 
16416 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16417 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16418 	    un, un->un_throttle, un->un_ncmds_in_transport);
16419 
16420 	if (un->un_throttle > 1) {
16421 		if (un->un_f_use_adaptive_throttle == TRUE) {
16422 			switch (throttle_type) {
16423 			case SD_THROTTLE_TRAN_BUSY:
16424 				if (un->un_busy_throttle == 0) {
16425 					un->un_busy_throttle = un->un_throttle;
16426 				}
16427 				break;
16428 			case SD_THROTTLE_QFULL:
16429 				un->un_busy_throttle = 0;
16430 				break;
16431 			default:
16432 				ASSERT(FALSE);
16433 			}
16434 
16435 			if (un->un_ncmds_in_transport > 0) {
16436 			    un->un_throttle = un->un_ncmds_in_transport;
16437 			}
16438 
16439 		} else {
16440 			if (un->un_ncmds_in_transport == 0) {
16441 				un->un_throttle = 1;
16442 			} else {
16443 				un->un_throttle = un->un_ncmds_in_transport;
16444 			}
16445 		}
16446 	}
16447 
16448 	/* Reschedule the timeout if none is currently active */
16449 	if (un->un_reset_throttle_timeid == NULL) {
16450 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16451 		    un, SD_THROTTLE_RESET_INTERVAL);
16452 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16453 		    "sd_reduce_throttle: timeout scheduled!\n");
16454 	}
16455 
16456 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16457 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16458 }
16459 
16460 
16461 
16462 /*
16463  *    Function: sd_restore_throttle
16464  *
16465  * Description: Callback function for timeout(9F).  Resets the current
16466  *		value of un->un_throttle to its default.
16467  *
16468  *   Arguments: arg - pointer to associated softstate for the device.
16469  *
16470  *     Context: May be called from interrupt context
16471  */
16472 
16473 static void
16474 sd_restore_throttle(void *arg)
16475 {
16476 	struct sd_lun	*un = arg;
16477 
16478 	ASSERT(un != NULL);
16479 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16480 
16481 	mutex_enter(SD_MUTEX(un));
16482 
16483 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16484 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16485 
16486 	un->un_reset_throttle_timeid = NULL;
16487 
16488 	if (un->un_f_use_adaptive_throttle == TRUE) {
16489 		/*
16490 		 * If un_busy_throttle is nonzero, then it contains the
16491 		 * value that un_throttle was when we got a TRAN_BUSY back
16492 		 * from scsi_transport(). We want to revert back to this
16493 		 * value.
16494 		 *
16495 		 * In the QFULL case, the throttle limit will incrementally
16496 		 * increase until it reaches max throttle.
16497 		 */
16498 		if (un->un_busy_throttle > 0) {
16499 			un->un_throttle = un->un_busy_throttle;
16500 			un->un_busy_throttle = 0;
16501 		} else {
16502 			/*
16503 			 * increase throttle by 10% open gate slowly, schedule
16504 			 * another restore if saved throttle has not been
16505 			 * reached
16506 			 */
16507 			short throttle;
16508 			if (sd_qfull_throttle_enable) {
16509 				throttle = un->un_throttle +
16510 				    max((un->un_throttle / 10), 1);
16511 				un->un_throttle =
16512 				    (throttle < un->un_saved_throttle) ?
16513 				    throttle : un->un_saved_throttle;
16514 				if (un->un_throttle < un->un_saved_throttle) {
16515 				    un->un_reset_throttle_timeid =
16516 					timeout(sd_restore_throttle,
16517 					un, SD_QFULL_THROTTLE_RESET_INTERVAL);
16518 				}
16519 			}
16520 		}
16521 
16522 		/*
16523 		 * If un_throttle has fallen below the low-water mark, we
16524 		 * restore the maximum value here (and allow it to ratchet
16525 		 * down again if necessary).
16526 		 */
16527 		if (un->un_throttle < un->un_min_throttle) {
16528 			un->un_throttle = un->un_saved_throttle;
16529 		}
16530 	} else {
16531 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16532 		    "restoring limit from 0x%x to 0x%x\n",
16533 		    un->un_throttle, un->un_saved_throttle);
16534 		un->un_throttle = un->un_saved_throttle;
16535 	}
16536 
16537 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16538 	    "sd_restore_throttle: calling sd_start_cmds!\n");
16539 
16540 	sd_start_cmds(un, NULL);
16541 
16542 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16543 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16544 	    un, un->un_throttle);
16545 
16546 	mutex_exit(SD_MUTEX(un));
16547 
16548 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16549 }
16550 
16551 /*
16552  *    Function: sdrunout
16553  *
16554  * Description: Callback routine for scsi_init_pkt when a resource allocation
16555  *		fails.
16556  *
16557  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16558  *		soft state instance.
16559  *
16560  * Return Code: The scsi_init_pkt routine allows for the callback function to
16561  *		return a 0 indicating the callback should be rescheduled or a 1
16562  *		indicating not to reschedule. This routine always returns 1
16563  *		because the driver always provides a callback function to
16564  *		scsi_init_pkt. This results in a callback always being scheduled
16565  *		(via the scsi_init_pkt callback implementation) if a resource
16566  *		failure occurs.
16567  *
16568  *     Context: This callback function may not block or call routines that block
16569  *
16570  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16571  *		request persisting at the head of the list which cannot be
16572  *		satisfied even after multiple retries. In the future the driver
16573  *		may implement some time of maximum runout count before failing
16574  *		an I/O.
16575  */
16576 
16577 static int
16578 sdrunout(caddr_t arg)
16579 {
16580 	struct sd_lun	*un = (struct sd_lun *)arg;
16581 
16582 	ASSERT(un != NULL);
16583 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16584 
16585 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16586 
16587 	mutex_enter(SD_MUTEX(un));
16588 	sd_start_cmds(un, NULL);
16589 	mutex_exit(SD_MUTEX(un));
16590 	/*
16591 	 * This callback routine always returns 1 (i.e. do not reschedule)
16592 	 * because we always specify sdrunout as the callback handler for
16593 	 * scsi_init_pkt inside the call to sd_start_cmds.
16594 	 */
16595 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16596 	return (1);
16597 }
16598 
16599 
16600 /*
16601  *    Function: sdintr
16602  *
16603  * Description: Completion callback routine for scsi_pkt(9S) structs
16604  *		sent to the HBA driver via scsi_transport(9F).
16605  *
16606  *     Context: Interrupt context
16607  */
16608 
16609 static void
16610 sdintr(struct scsi_pkt *pktp)
16611 {
16612 	struct buf	*bp;
16613 	struct sd_xbuf	*xp;
16614 	struct sd_lun	*un;
16615 
16616 	ASSERT(pktp != NULL);
16617 	bp = (struct buf *)pktp->pkt_private;
16618 	ASSERT(bp != NULL);
16619 	xp = SD_GET_XBUF(bp);
16620 	ASSERT(xp != NULL);
16621 	ASSERT(xp->xb_pktp != NULL);
16622 	un = SD_GET_UN(bp);
16623 	ASSERT(un != NULL);
16624 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16625 
16626 #ifdef SD_FAULT_INJECTION
16627 
16628 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16629 	/* SD FaultInjection */
16630 	sd_faultinjection(pktp);
16631 
16632 #endif /* SD_FAULT_INJECTION */
16633 
16634 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16635 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
16636 
16637 	mutex_enter(SD_MUTEX(un));
16638 
16639 	/* Reduce the count of the #commands currently in transport */
16640 	un->un_ncmds_in_transport--;
16641 	ASSERT(un->un_ncmds_in_transport >= 0);
16642 
16643 	/* Increment counter to indicate that the callback routine is active */
16644 	un->un_in_callback++;
16645 
16646 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16647 
16648 #ifdef	SDDEBUG
16649 	if (bp == un->un_retry_bp) {
16650 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16651 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16652 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
16653 	}
16654 #endif
16655 
16656 	/*
16657 	 * If pkt_reason is CMD_DEV_GONE, just fail the command
16658 	 */
16659 	if (pktp->pkt_reason == CMD_DEV_GONE) {
16660 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16661 			    "Device is gone\n");
16662 		sd_return_failed_command(un, bp, EIO);
16663 		goto exit;
16664 	}
16665 
16666 	/*
16667 	 * First see if the pkt has auto-request sense data with it....
16668 	 * Look at the packet state first so we don't take a performance
16669 	 * hit looking at the arq enabled flag unless absolutely necessary.
16670 	 */
16671 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16672 	    (un->un_f_arq_enabled == TRUE)) {
16673 		/*
16674 		 * The HBA did an auto request sense for this command so check
16675 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16676 		 * driver command that should not be retried.
16677 		 */
16678 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16679 			/*
16680 			 * Save the relevant sense info into the xp for the
16681 			 * original cmd.
16682 			 */
16683 			struct scsi_arq_status *asp;
16684 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16685 			xp->xb_sense_status =
16686 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
16687 			xp->xb_sense_state  = asp->sts_rqpkt_state;
16688 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16689 			bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16690 			    min(sizeof (struct scsi_extended_sense),
16691 			    SENSE_LENGTH));
16692 
16693 			/* fail the command */
16694 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16695 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
16696 			sd_return_failed_command(un, bp, EIO);
16697 			goto exit;
16698 		}
16699 
16700 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16701 		/*
16702 		 * We want to either retry or fail this command, so free
16703 		 * the DMA resources here.  If we retry the command then
16704 		 * the DMA resources will be reallocated in sd_start_cmds().
16705 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16706 		 * causes the *entire* transfer to start over again from the
16707 		 * beginning of the request, even for PARTIAL chunks that
16708 		 * have already transferred successfully.
16709 		 */
16710 		if ((un->un_f_is_fibre == TRUE) &&
16711 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16712 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16713 			scsi_dmafree(pktp);
16714 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16715 		}
16716 #endif
16717 
16718 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16719 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
16720 
16721 		sd_handle_auto_request_sense(un, bp, xp, pktp);
16722 		goto exit;
16723 	}
16724 
16725 	/* Next see if this is the REQUEST SENSE pkt for the instance */
16726 	if (pktp->pkt_flags & FLAG_SENSING)  {
16727 		/* This pktp is from the unit's REQUEST_SENSE command */
16728 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16729 		    "sdintr: sd_handle_request_sense\n");
16730 		sd_handle_request_sense(un, bp, xp, pktp);
16731 		goto exit;
16732 	}
16733 
16734 	/*
16735 	 * Check to see if the command successfully completed as requested;
16736 	 * this is the most common case (and also the hot performance path).
16737 	 *
16738 	 * Requirements for successful completion are:
16739 	 * pkt_reason is CMD_CMPLT and packet status is status good.
16740 	 * In addition:
16741 	 * - A residual of zero indicates successful completion no matter what
16742 	 *   the command is.
16743 	 * - If the residual is not zero and the command is not a read or
16744 	 *   write, then it's still defined as successful completion. In other
16745 	 *   words, if the command is a read or write the residual must be
16746 	 *   zero for successful completion.
16747 	 * - If the residual is not zero and the command is a read or
16748 	 *   write, and it's a USCSICMD, then it's still defined as
16749 	 *   successful completion.
16750 	 */
16751 	if ((pktp->pkt_reason == CMD_CMPLT) &&
16752 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16753 
16754 		/*
16755 		 * Since this command is returned with a good status, we
16756 		 * can reset the count for Sonoma failover.
16757 		 */
16758 		un->un_sonoma_failure_count = 0;
16759 
16760 		/*
16761 		 * Return all USCSI commands on good status
16762 		 */
16763 		if (pktp->pkt_resid == 0) {
16764 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16765 			    "sdintr: returning command for resid == 0\n");
16766 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
16767 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
16768 			SD_UPDATE_B_RESID(bp, pktp);
16769 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16770 			    "sdintr: returning command for resid != 0\n");
16771 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16772 			SD_UPDATE_B_RESID(bp, pktp);
16773 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16774 				"sdintr: returning uscsi command\n");
16775 		} else {
16776 			goto not_successful;
16777 		}
16778 		sd_return_command(un, bp);
16779 
16780 		/*
16781 		 * Decrement counter to indicate that the callback routine
16782 		 * is done.
16783 		 */
16784 		un->un_in_callback--;
16785 		ASSERT(un->un_in_callback >= 0);
16786 		mutex_exit(SD_MUTEX(un));
16787 
16788 		return;
16789 	}
16790 
16791 not_successful:
16792 
16793 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16794 	/*
16795 	 * The following is based upon knowledge of the underlying transport
16796 	 * and its use of DMA resources.  This code should be removed when
16797 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
16798 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
16799 	 * and sd_start_cmds().
16800 	 *
16801 	 * Free any DMA resources associated with this command if there
16802 	 * is a chance it could be retried or enqueued for later retry.
16803 	 * If we keep the DMA binding then mpxio cannot reissue the
16804 	 * command on another path whenever a path failure occurs.
16805 	 *
16806 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
16807 	 * causes the *entire* transfer to start over again from the
16808 	 * beginning of the request, even for PARTIAL chunks that
16809 	 * have already transferred successfully.
16810 	 *
16811 	 * This is only done for non-uscsi commands (and also skipped for the
16812 	 * driver's internal RQS command). Also just do this for Fibre Channel
16813 	 * devices as these are the only ones that support mpxio.
16814 	 */
16815 	if ((un->un_f_is_fibre == TRUE) &&
16816 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16817 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16818 		scsi_dmafree(pktp);
16819 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16820 	}
16821 #endif
16822 
16823 	/*
16824 	 * The command did not successfully complete as requested so check
16825 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16826 	 * driver command that should not be retried so just return. If
16827 	 * FLAG_DIAGNOSE is not set the error will be processed below.
16828 	 */
16829 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16830 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16831 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
16832 		/*
16833 		 * Issue a request sense if a check condition caused the error
16834 		 * (we handle the auto request sense case above), otherwise
16835 		 * just fail the command.
16836 		 */
16837 		if ((pktp->pkt_reason == CMD_CMPLT) &&
16838 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
16839 			sd_send_request_sense_command(un, bp, pktp);
16840 		} else {
16841 			sd_return_failed_command(un, bp, EIO);
16842 		}
16843 		goto exit;
16844 	}
16845 
16846 	/*
16847 	 * The command did not successfully complete as requested so process
16848 	 * the error, retry, and/or attempt recovery.
16849 	 */
16850 	switch (pktp->pkt_reason) {
16851 	case CMD_CMPLT:
16852 		switch (SD_GET_PKT_STATUS(pktp)) {
16853 		case STATUS_GOOD:
16854 			/*
16855 			 * The command completed successfully with a non-zero
16856 			 * residual
16857 			 */
16858 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16859 			    "sdintr: STATUS_GOOD \n");
16860 			sd_pkt_status_good(un, bp, xp, pktp);
16861 			break;
16862 
16863 		case STATUS_CHECK:
16864 		case STATUS_TERMINATED:
16865 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16866 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
16867 			sd_pkt_status_check_condition(un, bp, xp, pktp);
16868 			break;
16869 
16870 		case STATUS_BUSY:
16871 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16872 			    "sdintr: STATUS_BUSY\n");
16873 			sd_pkt_status_busy(un, bp, xp, pktp);
16874 			break;
16875 
16876 		case STATUS_RESERVATION_CONFLICT:
16877 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16878 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
16879 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
16880 			break;
16881 
16882 		case STATUS_QFULL:
16883 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16884 			    "sdintr: STATUS_QFULL\n");
16885 			sd_pkt_status_qfull(un, bp, xp, pktp);
16886 			break;
16887 
16888 		case STATUS_MET:
16889 		case STATUS_INTERMEDIATE:
16890 		case STATUS_SCSI2:
16891 		case STATUS_INTERMEDIATE_MET:
16892 		case STATUS_ACA_ACTIVE:
16893 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16894 			    "Unexpected SCSI status received: 0x%x\n",
16895 			    SD_GET_PKT_STATUS(pktp));
16896 			sd_return_failed_command(un, bp, EIO);
16897 			break;
16898 
16899 		default:
16900 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16901 			    "Invalid SCSI status received: 0x%x\n",
16902 			    SD_GET_PKT_STATUS(pktp));
16903 			sd_return_failed_command(un, bp, EIO);
16904 			break;
16905 
16906 		}
16907 		break;
16908 
16909 	case CMD_INCOMPLETE:
16910 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16911 		    "sdintr:  CMD_INCOMPLETE\n");
16912 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
16913 		break;
16914 	case CMD_TRAN_ERR:
16915 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16916 		    "sdintr: CMD_TRAN_ERR\n");
16917 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
16918 		break;
16919 	case CMD_RESET:
16920 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16921 		    "sdintr: CMD_RESET \n");
16922 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
16923 		break;
16924 	case CMD_ABORTED:
16925 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16926 		    "sdintr: CMD_ABORTED \n");
16927 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
16928 		break;
16929 	case CMD_TIMEOUT:
16930 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16931 		    "sdintr: CMD_TIMEOUT\n");
16932 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
16933 		break;
16934 	case CMD_UNX_BUS_FREE:
16935 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16936 		    "sdintr: CMD_UNX_BUS_FREE \n");
16937 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
16938 		break;
16939 	case CMD_TAG_REJECT:
16940 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16941 		    "sdintr: CMD_TAG_REJECT\n");
16942 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
16943 		break;
16944 	default:
16945 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16946 		    "sdintr: default\n");
16947 		sd_pkt_reason_default(un, bp, xp, pktp);
16948 		break;
16949 	}
16950 
16951 exit:
16952 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
16953 
16954 	/* Decrement counter to indicate that the callback routine is done. */
16955 	un->un_in_callback--;
16956 	ASSERT(un->un_in_callback >= 0);
16957 
16958 	/*
16959 	 * At this point, the pkt has been dispatched, ie, it is either
16960 	 * being re-tried or has been returned to its caller and should
16961 	 * not be referenced.
16962 	 */
16963 
16964 	mutex_exit(SD_MUTEX(un));
16965 }
16966 
16967 
16968 /*
16969  *    Function: sd_print_incomplete_msg
16970  *
16971  * Description: Prints the error message for a CMD_INCOMPLETE error.
16972  *
16973  *   Arguments: un - ptr to associated softstate for the device.
16974  *		bp - ptr to the buf(9S) for the command.
16975  *		arg - message string ptr
16976  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
16977  *			or SD_NO_RETRY_ISSUED.
16978  *
16979  *     Context: May be called under interrupt context
16980  */
16981 
16982 static void
16983 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
16984 {
16985 	struct scsi_pkt	*pktp;
16986 	char	*msgp;
16987 	char	*cmdp = arg;
16988 
16989 	ASSERT(un != NULL);
16990 	ASSERT(mutex_owned(SD_MUTEX(un)));
16991 	ASSERT(bp != NULL);
16992 	ASSERT(arg != NULL);
16993 	pktp = SD_GET_PKTP(bp);
16994 	ASSERT(pktp != NULL);
16995 
16996 	switch (code) {
16997 	case SD_DELAYED_RETRY_ISSUED:
16998 	case SD_IMMEDIATE_RETRY_ISSUED:
16999 		msgp = "retrying";
17000 		break;
17001 	case SD_NO_RETRY_ISSUED:
17002 	default:
17003 		msgp = "giving up";
17004 		break;
17005 	}
17006 
17007 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17008 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17009 		    "incomplete %s- %s\n", cmdp, msgp);
17010 	}
17011 }
17012 
17013 
17014 
17015 /*
17016  *    Function: sd_pkt_status_good
17017  *
17018  * Description: Processing for a STATUS_GOOD code in pkt_status.
17019  *
17020  *     Context: May be called under interrupt context
17021  */
17022 
17023 static void
17024 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17025 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17026 {
17027 	char	*cmdp;
17028 
17029 	ASSERT(un != NULL);
17030 	ASSERT(mutex_owned(SD_MUTEX(un)));
17031 	ASSERT(bp != NULL);
17032 	ASSERT(xp != NULL);
17033 	ASSERT(pktp != NULL);
17034 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
17035 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17036 	ASSERT(pktp->pkt_resid != 0);
17037 
17038 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17039 
17040 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17041 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17042 	case SCMD_READ:
17043 		cmdp = "read";
17044 		break;
17045 	case SCMD_WRITE:
17046 		cmdp = "write";
17047 		break;
17048 	default:
17049 		SD_UPDATE_B_RESID(bp, pktp);
17050 		sd_return_command(un, bp);
17051 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17052 		return;
17053 	}
17054 
17055 	/*
17056 	 * See if we can retry the read/write, preferrably immediately.
17057 	 * If retries are exhaused, then sd_retry_command() will update
17058 	 * the b_resid count.
17059 	 */
17060 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17061 	    cmdp, EIO, (clock_t)0, NULL);
17062 
17063 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17064 }
17065 
17066 
17067 
17068 
17069 
17070 /*
17071  *    Function: sd_handle_request_sense
17072  *
17073  * Description: Processing for non-auto Request Sense command.
17074  *
17075  *   Arguments: un - ptr to associated softstate
17076  *		sense_bp - ptr to buf(9S) for the RQS command
17077  *		sense_xp - ptr to the sd_xbuf for the RQS command
17078  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17079  *
17080  *     Context: May be called under interrupt context
17081  */
17082 
17083 static void
17084 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17085 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17086 {
17087 	struct buf	*cmd_bp;	/* buf for the original command */
17088 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
17089 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
17090 
17091 	ASSERT(un != NULL);
17092 	ASSERT(mutex_owned(SD_MUTEX(un)));
17093 	ASSERT(sense_bp != NULL);
17094 	ASSERT(sense_xp != NULL);
17095 	ASSERT(sense_pktp != NULL);
17096 
17097 	/*
17098 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
17099 	 * RQS command and not the original command.
17100 	 */
17101 	ASSERT(sense_pktp == un->un_rqs_pktp);
17102 	ASSERT(sense_bp   == un->un_rqs_bp);
17103 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17104 	    (FLAG_SENSING | FLAG_HEAD));
17105 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17106 	    FLAG_SENSING) == FLAG_SENSING);
17107 
17108 	/* These are the bp, xp, and pktp for the original command */
17109 	cmd_bp = sense_xp->xb_sense_bp;
17110 	cmd_xp = SD_GET_XBUF(cmd_bp);
17111 	cmd_pktp = SD_GET_PKTP(cmd_bp);
17112 
17113 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
17114 		/*
17115 		 * The REQUEST SENSE command failed.  Release the REQUEST
17116 		 * SENSE command for re-use, get back the bp for the original
17117 		 * command, and attempt to re-try the original command if
17118 		 * FLAG_DIAGNOSE is not set in the original packet.
17119 		 */
17120 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17121 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17122 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17123 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17124 			    NULL, NULL, EIO, (clock_t)0, NULL);
17125 			return;
17126 		}
17127 	}
17128 
17129 	/*
17130 	 * Save the relevant sense info into the xp for the original cmd.
17131 	 *
17132 	 * Note: if the request sense failed the state info will be zero
17133 	 * as set in sd_mark_rqs_busy()
17134 	 */
17135 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17136 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17137 	cmd_xp->xb_sense_resid  = sense_pktp->pkt_resid;
17138 	bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH);
17139 
17140 	/*
17141 	 *  Free up the RQS command....
17142 	 *  NOTE:
17143 	 *	Must do this BEFORE calling sd_validate_sense_data!
17144 	 *	sd_validate_sense_data may return the original command in
17145 	 *	which case the pkt will be freed and the flags can no
17146 	 *	longer be touched.
17147 	 *	SD_MUTEX is held through this process until the command
17148 	 *	is dispatched based upon the sense data, so there are
17149 	 *	no race conditions.
17150 	 */
17151 	(void) sd_mark_rqs_idle(un, sense_xp);
17152 
17153 	/*
17154 	 * For a retryable command see if we have valid sense data, if so then
17155 	 * turn it over to sd_decode_sense() to figure out the right course of
17156 	 * action. Just fail a non-retryable command.
17157 	 */
17158 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17159 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp) ==
17160 		    SD_SENSE_DATA_IS_VALID) {
17161 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17162 		}
17163 	} else {
17164 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17165 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17166 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17167 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17168 		sd_return_failed_command(un, cmd_bp, EIO);
17169 	}
17170 }
17171 
17172 
17173 
17174 
17175 /*
17176  *    Function: sd_handle_auto_request_sense
17177  *
17178  * Description: Processing for auto-request sense information.
17179  *
17180  *   Arguments: un - ptr to associated softstate
17181  *		bp - ptr to buf(9S) for the command
17182  *		xp - ptr to the sd_xbuf for the command
17183  *		pktp - ptr to the scsi_pkt(9S) for the command
17184  *
17185  *     Context: May be called under interrupt context
17186  */
17187 
17188 static void
17189 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17190 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17191 {
17192 	struct scsi_arq_status *asp;
17193 
17194 	ASSERT(un != NULL);
17195 	ASSERT(mutex_owned(SD_MUTEX(un)));
17196 	ASSERT(bp != NULL);
17197 	ASSERT(xp != NULL);
17198 	ASSERT(pktp != NULL);
17199 	ASSERT(pktp != un->un_rqs_pktp);
17200 	ASSERT(bp   != un->un_rqs_bp);
17201 
17202 	/*
17203 	 * For auto-request sense, we get a scsi_arq_status back from
17204 	 * the HBA, with the sense data in the sts_sensedata member.
17205 	 * The pkt_scbp of the packet points to this scsi_arq_status.
17206 	 */
17207 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17208 
17209 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17210 		/*
17211 		 * The auto REQUEST SENSE failed; see if we can re-try
17212 		 * the original command.
17213 		 */
17214 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17215 		    "auto request sense failed (reason=%s)\n",
17216 		    scsi_rname(asp->sts_rqpkt_reason));
17217 
17218 		sd_reset_target(un, pktp);
17219 
17220 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17221 		    NULL, NULL, EIO, (clock_t)0, NULL);
17222 		return;
17223 	}
17224 
17225 	/* Save the relevant sense info into the xp for the original cmd. */
17226 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17227 	xp->xb_sense_state  = asp->sts_rqpkt_state;
17228 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17229 	bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17230 	    min(sizeof (struct scsi_extended_sense), SENSE_LENGTH));
17231 
17232 	/*
17233 	 * See if we have valid sense data, if so then turn it over to
17234 	 * sd_decode_sense() to figure out the right course of action.
17235 	 */
17236 	if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) {
17237 		sd_decode_sense(un, bp, xp, pktp);
17238 	}
17239 }
17240 
17241 
17242 /*
17243  *    Function: sd_print_sense_failed_msg
17244  *
17245  * Description: Print log message when RQS has failed.
17246  *
17247  *   Arguments: un - ptr to associated softstate
17248  *		bp - ptr to buf(9S) for the command
17249  *		arg - generic message string ptr
17250  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17251  *			or SD_NO_RETRY_ISSUED
17252  *
17253  *     Context: May be called from interrupt context
17254  */
17255 
17256 static void
17257 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17258 	int code)
17259 {
17260 	char	*msgp = arg;
17261 
17262 	ASSERT(un != NULL);
17263 	ASSERT(mutex_owned(SD_MUTEX(un)));
17264 	ASSERT(bp != NULL);
17265 
17266 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17267 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17268 	}
17269 }
17270 
17271 
17272 /*
17273  *    Function: sd_validate_sense_data
17274  *
17275  * Description: Check the given sense data for validity.
17276  *		If the sense data is not valid, the command will
17277  *		be either failed or retried!
17278  *
17279  * Return Code: SD_SENSE_DATA_IS_INVALID
17280  *		SD_SENSE_DATA_IS_VALID
17281  *
17282  *     Context: May be called from interrupt context
17283  */
17284 
17285 static int
17286 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp)
17287 {
17288 	struct scsi_extended_sense *esp;
17289 	struct	scsi_pkt *pktp;
17290 	size_t	actual_len;
17291 	char	*msgp = NULL;
17292 
17293 	ASSERT(un != NULL);
17294 	ASSERT(mutex_owned(SD_MUTEX(un)));
17295 	ASSERT(bp != NULL);
17296 	ASSERT(bp != un->un_rqs_bp);
17297 	ASSERT(xp != NULL);
17298 
17299 	pktp = SD_GET_PKTP(bp);
17300 	ASSERT(pktp != NULL);
17301 
17302 	/*
17303 	 * Check the status of the RQS command (auto or manual).
17304 	 */
17305 	switch (xp->xb_sense_status & STATUS_MASK) {
17306 	case STATUS_GOOD:
17307 		break;
17308 
17309 	case STATUS_RESERVATION_CONFLICT:
17310 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17311 		return (SD_SENSE_DATA_IS_INVALID);
17312 
17313 	case STATUS_BUSY:
17314 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17315 		    "Busy Status on REQUEST SENSE\n");
17316 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17317 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
17318 		return (SD_SENSE_DATA_IS_INVALID);
17319 
17320 	case STATUS_QFULL:
17321 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17322 		    "QFULL Status on REQUEST SENSE\n");
17323 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17324 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
17325 		return (SD_SENSE_DATA_IS_INVALID);
17326 
17327 	case STATUS_CHECK:
17328 	case STATUS_TERMINATED:
17329 		msgp = "Check Condition on REQUEST SENSE\n";
17330 		goto sense_failed;
17331 
17332 	default:
17333 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17334 		goto sense_failed;
17335 	}
17336 
17337 	/*
17338 	 * See if we got the minimum required amount of sense data.
17339 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17340 	 * or less.
17341 	 */
17342 	actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid);
17343 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17344 	    (actual_len == 0)) {
17345 		msgp = "Request Sense couldn't get sense data\n";
17346 		goto sense_failed;
17347 	}
17348 
17349 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
17350 		msgp = "Not enough sense information\n";
17351 		goto sense_failed;
17352 	}
17353 
17354 	/*
17355 	 * We require the extended sense data
17356 	 */
17357 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17358 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
17359 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17360 			static char tmp[8];
17361 			static char buf[148];
17362 			char *p = (char *)(xp->xb_sense_data);
17363 			int i;
17364 
17365 			mutex_enter(&sd_sense_mutex);
17366 			(void) strcpy(buf, "undecodable sense information:");
17367 			for (i = 0; i < actual_len; i++) {
17368 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17369 				(void) strcpy(&buf[strlen(buf)], tmp);
17370 			}
17371 			i = strlen(buf);
17372 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
17373 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf);
17374 			mutex_exit(&sd_sense_mutex);
17375 		}
17376 		/* Note: Legacy behavior, fail the command with no retry */
17377 		sd_return_failed_command(un, bp, EIO);
17378 		return (SD_SENSE_DATA_IS_INVALID);
17379 	}
17380 
17381 	/*
17382 	 * Check that es_code is valid (es_class concatenated with es_code
17383 	 * make up the "response code" field.  es_class will always be 7, so
17384 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17385 	 * format.
17386 	 */
17387 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17388 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17389 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17390 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17391 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17392 		goto sense_failed;
17393 	}
17394 
17395 	return (SD_SENSE_DATA_IS_VALID);
17396 
17397 sense_failed:
17398 	/*
17399 	 * If the request sense failed (for whatever reason), attempt
17400 	 * to retry the original command.
17401 	 */
17402 #if defined(__i386) || defined(__amd64)
17403 	/*
17404 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17405 	 * sddef.h for Sparc platform, and x86 uses 1 binary
17406 	 * for both SCSI/FC.
17407 	 * The SD_RETRY_DELAY value need to be adjusted here
17408 	 * when SD_RETRY_DELAY change in sddef.h
17409 	 */
17410 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17411 	    sd_print_sense_failed_msg, msgp, EIO,
17412 		un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17413 #else
17414 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17415 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17416 #endif
17417 
17418 	return (SD_SENSE_DATA_IS_INVALID);
17419 }
17420 
17421 
17422 
17423 /*
17424  *    Function: sd_decode_sense
17425  *
17426  * Description: Take recovery action(s) when SCSI Sense Data is received.
17427  *
17428  *     Context: Interrupt context.
17429  */
17430 
17431 static void
17432 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17433 	struct scsi_pkt *pktp)
17434 {
17435 	uint8_t sense_key;
17436 
17437 	ASSERT(un != NULL);
17438 	ASSERT(mutex_owned(SD_MUTEX(un)));
17439 	ASSERT(bp != NULL);
17440 	ASSERT(bp != un->un_rqs_bp);
17441 	ASSERT(xp != NULL);
17442 	ASSERT(pktp != NULL);
17443 
17444 	sense_key = scsi_sense_key(xp->xb_sense_data);
17445 
17446 	switch (sense_key) {
17447 	case KEY_NO_SENSE:
17448 		sd_sense_key_no_sense(un, bp, xp, pktp);
17449 		break;
17450 	case KEY_RECOVERABLE_ERROR:
17451 		sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17452 		    bp, xp, pktp);
17453 		break;
17454 	case KEY_NOT_READY:
17455 		sd_sense_key_not_ready(un, xp->xb_sense_data,
17456 		    bp, xp, pktp);
17457 		break;
17458 	case KEY_MEDIUM_ERROR:
17459 	case KEY_HARDWARE_ERROR:
17460 		sd_sense_key_medium_or_hardware_error(un,
17461 		    xp->xb_sense_data, bp, xp, pktp);
17462 		break;
17463 	case KEY_ILLEGAL_REQUEST:
17464 		sd_sense_key_illegal_request(un, bp, xp, pktp);
17465 		break;
17466 	case KEY_UNIT_ATTENTION:
17467 		sd_sense_key_unit_attention(un, xp->xb_sense_data,
17468 		    bp, xp, pktp);
17469 		break;
17470 	case KEY_WRITE_PROTECT:
17471 	case KEY_VOLUME_OVERFLOW:
17472 	case KEY_MISCOMPARE:
17473 		sd_sense_key_fail_command(un, bp, xp, pktp);
17474 		break;
17475 	case KEY_BLANK_CHECK:
17476 		sd_sense_key_blank_check(un, bp, xp, pktp);
17477 		break;
17478 	case KEY_ABORTED_COMMAND:
17479 		sd_sense_key_aborted_command(un, bp, xp, pktp);
17480 		break;
17481 	case KEY_VENDOR_UNIQUE:
17482 	case KEY_COPY_ABORTED:
17483 	case KEY_EQUAL:
17484 	case KEY_RESERVED:
17485 	default:
17486 		sd_sense_key_default(un, xp->xb_sense_data,
17487 		    bp, xp, pktp);
17488 		break;
17489 	}
17490 }
17491 
17492 
17493 /*
17494  *    Function: sd_dump_memory
17495  *
17496  * Description: Debug logging routine to print the contents of a user provided
17497  *		buffer. The output of the buffer is broken up into 256 byte
17498  *		segments due to a size constraint of the scsi_log.
17499  *		implementation.
17500  *
17501  *   Arguments: un - ptr to softstate
17502  *		comp - component mask
17503  *		title - "title" string to preceed data when printed
17504  *		data - ptr to data block to be printed
17505  *		len - size of data block to be printed
17506  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17507  *
17508  *     Context: May be called from interrupt context
17509  */
17510 
17511 #define	SD_DUMP_MEMORY_BUF_SIZE	256
17512 
17513 static char *sd_dump_format_string[] = {
17514 		" 0x%02x",
17515 		" %c"
17516 };
17517 
17518 static void
17519 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17520     int len, int fmt)
17521 {
17522 	int	i, j;
17523 	int	avail_count;
17524 	int	start_offset;
17525 	int	end_offset;
17526 	size_t	entry_len;
17527 	char	*bufp;
17528 	char	*local_buf;
17529 	char	*format_string;
17530 
17531 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17532 
17533 	/*
17534 	 * In the debug version of the driver, this function is called from a
17535 	 * number of places which are NOPs in the release driver.
17536 	 * The debug driver therefore has additional methods of filtering
17537 	 * debug output.
17538 	 */
17539 #ifdef SDDEBUG
17540 	/*
17541 	 * In the debug version of the driver we can reduce the amount of debug
17542 	 * messages by setting sd_error_level to something other than
17543 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17544 	 * sd_component_mask.
17545 	 */
17546 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17547 	    (sd_error_level != SCSI_ERR_ALL)) {
17548 		return;
17549 	}
17550 	if (((sd_component_mask & comp) == 0) ||
17551 	    (sd_error_level != SCSI_ERR_ALL)) {
17552 		return;
17553 	}
17554 #else
17555 	if (sd_error_level != SCSI_ERR_ALL) {
17556 		return;
17557 	}
17558 #endif
17559 
17560 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17561 	bufp = local_buf;
17562 	/*
17563 	 * Available length is the length of local_buf[], minus the
17564 	 * length of the title string, minus one for the ":", minus
17565 	 * one for the newline, minus one for the NULL terminator.
17566 	 * This gives the #bytes available for holding the printed
17567 	 * values from the given data buffer.
17568 	 */
17569 	if (fmt == SD_LOG_HEX) {
17570 		format_string = sd_dump_format_string[0];
17571 	} else /* SD_LOG_CHAR */ {
17572 		format_string = sd_dump_format_string[1];
17573 	}
17574 	/*
17575 	 * Available count is the number of elements from the given
17576 	 * data buffer that we can fit into the available length.
17577 	 * This is based upon the size of the format string used.
17578 	 * Make one entry and find it's size.
17579 	 */
17580 	(void) sprintf(bufp, format_string, data[0]);
17581 	entry_len = strlen(bufp);
17582 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17583 
17584 	j = 0;
17585 	while (j < len) {
17586 		bufp = local_buf;
17587 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17588 		start_offset = j;
17589 
17590 		end_offset = start_offset + avail_count;
17591 
17592 		(void) sprintf(bufp, "%s:", title);
17593 		bufp += strlen(bufp);
17594 		for (i = start_offset; ((i < end_offset) && (j < len));
17595 		    i++, j++) {
17596 			(void) sprintf(bufp, format_string, data[i]);
17597 			bufp += entry_len;
17598 		}
17599 		(void) sprintf(bufp, "\n");
17600 
17601 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17602 	}
17603 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17604 }
17605 
17606 /*
17607  *    Function: sd_print_sense_msg
17608  *
17609  * Description: Log a message based upon the given sense data.
17610  *
17611  *   Arguments: un - ptr to associated softstate
17612  *		bp - ptr to buf(9S) for the command
17613  *		arg - ptr to associate sd_sense_info struct
17614  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17615  *			or SD_NO_RETRY_ISSUED
17616  *
17617  *     Context: May be called from interrupt context
17618  */
17619 
17620 static void
17621 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17622 {
17623 	struct sd_xbuf	*xp;
17624 	struct scsi_pkt	*pktp;
17625 	uint8_t *sensep;
17626 	daddr_t request_blkno;
17627 	diskaddr_t err_blkno;
17628 	int severity;
17629 	int pfa_flag;
17630 	extern struct scsi_key_strings scsi_cmds[];
17631 
17632 	ASSERT(un != NULL);
17633 	ASSERT(mutex_owned(SD_MUTEX(un)));
17634 	ASSERT(bp != NULL);
17635 	xp = SD_GET_XBUF(bp);
17636 	ASSERT(xp != NULL);
17637 	pktp = SD_GET_PKTP(bp);
17638 	ASSERT(pktp != NULL);
17639 	ASSERT(arg != NULL);
17640 
17641 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17642 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17643 
17644 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
17645 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17646 		severity = SCSI_ERR_RETRYABLE;
17647 	}
17648 
17649 	/* Use absolute block number for the request block number */
17650 	request_blkno = xp->xb_blkno;
17651 
17652 	/*
17653 	 * Now try to get the error block number from the sense data
17654 	 */
17655 	sensep = xp->xb_sense_data;
17656 
17657 	if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
17658 		(uint64_t *)&err_blkno)) {
17659 		/*
17660 		 * We retrieved the error block number from the information
17661 		 * portion of the sense data.
17662 		 *
17663 		 * For USCSI commands we are better off using the error
17664 		 * block no. as the requested block no. (This is the best
17665 		 * we can estimate.)
17666 		 */
17667 		if ((SD_IS_BUFIO(xp) == FALSE) &&
17668 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17669 			request_blkno = err_blkno;
17670 		}
17671 	} else {
17672 		/*
17673 		 * Without the es_valid bit set (for fixed format) or an
17674 		 * information descriptor (for descriptor format) we cannot
17675 		 * be certain of the error blkno, so just use the
17676 		 * request_blkno.
17677 		 */
17678 		err_blkno = (diskaddr_t)request_blkno;
17679 	}
17680 
17681 	/*
17682 	 * The following will log the buffer contents for the release driver
17683 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17684 	 * level is set to verbose.
17685 	 */
17686 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17687 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17688 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
17689 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
17690 
17691 	if (pfa_flag == FALSE) {
17692 		/* This is normally only set for USCSI */
17693 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
17694 			return;
17695 		}
17696 
17697 		if ((SD_IS_BUFIO(xp) == TRUE) &&
17698 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
17699 		    (severity < sd_error_level))) {
17700 			return;
17701 		}
17702 	}
17703 
17704 	/*
17705 	 * Check for Sonoma Failover and keep a count of how many failed I/O's
17706 	 */
17707 	if ((SD_IS_LSI(un)) &&
17708 	    (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
17709 	    (scsi_sense_asc(sensep) == 0x94) &&
17710 	    (scsi_sense_ascq(sensep) == 0x01)) {
17711 		un->un_sonoma_failure_count++;
17712 		if (un->un_sonoma_failure_count > 1) {
17713 			return;
17714 		}
17715 	}
17716 
17717 	scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
17718 	    request_blkno, err_blkno, scsi_cmds,
17719 	    (struct scsi_extended_sense *)sensep,
17720 	    un->un_additional_codes, NULL);
17721 }
17722 
17723 /*
17724  *    Function: sd_sense_key_no_sense
17725  *
17726  * Description: Recovery action when sense data was not received.
17727  *
17728  *     Context: May be called from interrupt context
17729  */
17730 
17731 static void
17732 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
17733 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17734 {
17735 	struct sd_sense_info	si;
17736 
17737 	ASSERT(un != NULL);
17738 	ASSERT(mutex_owned(SD_MUTEX(un)));
17739 	ASSERT(bp != NULL);
17740 	ASSERT(xp != NULL);
17741 	ASSERT(pktp != NULL);
17742 
17743 	si.ssi_severity = SCSI_ERR_FATAL;
17744 	si.ssi_pfa_flag = FALSE;
17745 
17746 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17747 
17748 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17749 		&si, EIO, (clock_t)0, NULL);
17750 }
17751 
17752 
17753 /*
17754  *    Function: sd_sense_key_recoverable_error
17755  *
17756  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
17757  *
17758  *     Context: May be called from interrupt context
17759  */
17760 
17761 static void
17762 sd_sense_key_recoverable_error(struct sd_lun *un,
17763 	uint8_t *sense_datap,
17764 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17765 {
17766 	struct sd_sense_info	si;
17767 	uint8_t asc = scsi_sense_asc(sense_datap);
17768 
17769 	ASSERT(un != NULL);
17770 	ASSERT(mutex_owned(SD_MUTEX(un)));
17771 	ASSERT(bp != NULL);
17772 	ASSERT(xp != NULL);
17773 	ASSERT(pktp != NULL);
17774 
17775 	/*
17776 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
17777 	 */
17778 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
17779 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17780 		si.ssi_severity = SCSI_ERR_INFO;
17781 		si.ssi_pfa_flag = TRUE;
17782 	} else {
17783 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
17784 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
17785 		si.ssi_severity = SCSI_ERR_RECOVERED;
17786 		si.ssi_pfa_flag = FALSE;
17787 	}
17788 
17789 	if (pktp->pkt_resid == 0) {
17790 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17791 		sd_return_command(un, bp);
17792 		return;
17793 	}
17794 
17795 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17796 	    &si, EIO, (clock_t)0, NULL);
17797 }
17798 
17799 
17800 
17801 
17802 /*
17803  *    Function: sd_sense_key_not_ready
17804  *
17805  * Description: Recovery actions for a SCSI "Not Ready" sense key.
17806  *
17807  *     Context: May be called from interrupt context
17808  */
17809 
17810 static void
17811 sd_sense_key_not_ready(struct sd_lun *un,
17812 	uint8_t *sense_datap,
17813 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17814 {
17815 	struct sd_sense_info	si;
17816 	uint8_t asc = scsi_sense_asc(sense_datap);
17817 	uint8_t ascq = scsi_sense_ascq(sense_datap);
17818 
17819 	ASSERT(un != NULL);
17820 	ASSERT(mutex_owned(SD_MUTEX(un)));
17821 	ASSERT(bp != NULL);
17822 	ASSERT(xp != NULL);
17823 	ASSERT(pktp != NULL);
17824 
17825 	si.ssi_severity = SCSI_ERR_FATAL;
17826 	si.ssi_pfa_flag = FALSE;
17827 
17828 	/*
17829 	 * Update error stats after first NOT READY error. Disks may have
17830 	 * been powered down and may need to be restarted.  For CDROMs,
17831 	 * report NOT READY errors only if media is present.
17832 	 */
17833 	if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) ||
17834 	    (xp->xb_retry_count > 0)) {
17835 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17836 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
17837 	}
17838 
17839 	/*
17840 	 * Just fail if the "not ready" retry limit has been reached.
17841 	 */
17842 	if (xp->xb_retry_count >= un->un_notready_retry_count) {
17843 		/* Special check for error message printing for removables. */
17844 		if (un->un_f_has_removable_media && (asc == 0x04) &&
17845 		    (ascq >= 0x04)) {
17846 			si.ssi_severity = SCSI_ERR_ALL;
17847 		}
17848 		goto fail_command;
17849 	}
17850 
17851 	/*
17852 	 * Check the ASC and ASCQ in the sense data as needed, to determine
17853 	 * what to do.
17854 	 */
17855 	switch (asc) {
17856 	case 0x04:	/* LOGICAL UNIT NOT READY */
17857 		/*
17858 		 * disk drives that don't spin up result in a very long delay
17859 		 * in format without warning messages. We will log a message
17860 		 * if the error level is set to verbose.
17861 		 */
17862 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17863 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17864 			    "logical unit not ready, resetting disk\n");
17865 		}
17866 
17867 		/*
17868 		 * There are different requirements for CDROMs and disks for
17869 		 * the number of retries.  If a CD-ROM is giving this, it is
17870 		 * probably reading TOC and is in the process of getting
17871 		 * ready, so we should keep on trying for a long time to make
17872 		 * sure that all types of media are taken in account (for
17873 		 * some media the drive takes a long time to read TOC).  For
17874 		 * disks we do not want to retry this too many times as this
17875 		 * can cause a long hang in format when the drive refuses to
17876 		 * spin up (a very common failure).
17877 		 */
17878 		switch (ascq) {
17879 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
17880 			/*
17881 			 * Disk drives frequently refuse to spin up which
17882 			 * results in a very long hang in format without
17883 			 * warning messages.
17884 			 *
17885 			 * Note: This code preserves the legacy behavior of
17886 			 * comparing xb_retry_count against zero for fibre
17887 			 * channel targets instead of comparing against the
17888 			 * un_reset_retry_count value.  The reason for this
17889 			 * discrepancy has been so utterly lost beneath the
17890 			 * Sands of Time that even Indiana Jones could not
17891 			 * find it.
17892 			 */
17893 			if (un->un_f_is_fibre == TRUE) {
17894 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17895 					(xp->xb_retry_count > 0)) &&
17896 					(un->un_startstop_timeid == NULL)) {
17897 					scsi_log(SD_DEVINFO(un), sd_label,
17898 					CE_WARN, "logical unit not ready, "
17899 					"resetting disk\n");
17900 					sd_reset_target(un, pktp);
17901 				}
17902 			} else {
17903 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17904 					(xp->xb_retry_count >
17905 					un->un_reset_retry_count)) &&
17906 					(un->un_startstop_timeid == NULL)) {
17907 					scsi_log(SD_DEVINFO(un), sd_label,
17908 					CE_WARN, "logical unit not ready, "
17909 					"resetting disk\n");
17910 					sd_reset_target(un, pktp);
17911 				}
17912 			}
17913 			break;
17914 
17915 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
17916 			/*
17917 			 * If the target is in the process of becoming
17918 			 * ready, just proceed with the retry. This can
17919 			 * happen with CD-ROMs that take a long time to
17920 			 * read TOC after a power cycle or reset.
17921 			 */
17922 			goto do_retry;
17923 
17924 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
17925 			break;
17926 
17927 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
17928 			/*
17929 			 * Retries cannot help here so just fail right away.
17930 			 */
17931 			goto fail_command;
17932 
17933 		case 0x88:
17934 			/*
17935 			 * Vendor-unique code for T3/T4: it indicates a
17936 			 * path problem in a mutipathed config, but as far as
17937 			 * the target driver is concerned it equates to a fatal
17938 			 * error, so we should just fail the command right away
17939 			 * (without printing anything to the console). If this
17940 			 * is not a T3/T4, fall thru to the default recovery
17941 			 * action.
17942 			 * T3/T4 is FC only, don't need to check is_fibre
17943 			 */
17944 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
17945 				sd_return_failed_command(un, bp, EIO);
17946 				return;
17947 			}
17948 			/* FALLTHRU */
17949 
17950 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
17951 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
17952 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
17953 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
17954 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
17955 		default:    /* Possible future codes in SCSI spec? */
17956 			/*
17957 			 * For removable-media devices, do not retry if
17958 			 * ASCQ > 2 as these result mostly from USCSI commands
17959 			 * on MMC devices issued to check status of an
17960 			 * operation initiated in immediate mode.  Also for
17961 			 * ASCQ >= 4 do not print console messages as these
17962 			 * mainly represent a user-initiated operation
17963 			 * instead of a system failure.
17964 			 */
17965 			if (un->un_f_has_removable_media) {
17966 				si.ssi_severity = SCSI_ERR_ALL;
17967 				goto fail_command;
17968 			}
17969 			break;
17970 		}
17971 
17972 		/*
17973 		 * As part of our recovery attempt for the NOT READY
17974 		 * condition, we issue a START STOP UNIT command. However
17975 		 * we want to wait for a short delay before attempting this
17976 		 * as there may still be more commands coming back from the
17977 		 * target with the check condition. To do this we use
17978 		 * timeout(9F) to call sd_start_stop_unit_callback() after
17979 		 * the delay interval expires. (sd_start_stop_unit_callback()
17980 		 * dispatches sd_start_stop_unit_task(), which will issue
17981 		 * the actual START STOP UNIT command. The delay interval
17982 		 * is one-half of the delay that we will use to retry the
17983 		 * command that generated the NOT READY condition.
17984 		 *
17985 		 * Note that we could just dispatch sd_start_stop_unit_task()
17986 		 * from here and allow it to sleep for the delay interval,
17987 		 * but then we would be tying up the taskq thread
17988 		 * uncesessarily for the duration of the delay.
17989 		 *
17990 		 * Do not issue the START STOP UNIT if the current command
17991 		 * is already a START STOP UNIT.
17992 		 */
17993 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
17994 			break;
17995 		}
17996 
17997 		/*
17998 		 * Do not schedule the timeout if one is already pending.
17999 		 */
18000 		if (un->un_startstop_timeid != NULL) {
18001 			SD_INFO(SD_LOG_ERROR, un,
18002 			    "sd_sense_key_not_ready: restart already issued to"
18003 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18004 			    ddi_get_instance(SD_DEVINFO(un)));
18005 			break;
18006 		}
18007 
18008 		/*
18009 		 * Schedule the START STOP UNIT command, then queue the command
18010 		 * for a retry.
18011 		 *
18012 		 * Note: A timeout is not scheduled for this retry because we
18013 		 * want the retry to be serial with the START_STOP_UNIT. The
18014 		 * retry will be started when the START_STOP_UNIT is completed
18015 		 * in sd_start_stop_unit_task.
18016 		 */
18017 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18018 		    un, SD_BSY_TIMEOUT / 2);
18019 		xp->xb_retry_count++;
18020 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18021 		return;
18022 
18023 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18024 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18025 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18026 			    "unit does not respond to selection\n");
18027 		}
18028 		break;
18029 
18030 	case 0x3A:	/* MEDIUM NOT PRESENT */
18031 		if (sd_error_level >= SCSI_ERR_FATAL) {
18032 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18033 			    "Caddy not inserted in drive\n");
18034 		}
18035 
18036 		sr_ejected(un);
18037 		un->un_mediastate = DKIO_EJECTED;
18038 		/* The state has changed, inform the media watch routines */
18039 		cv_broadcast(&un->un_state_cv);
18040 		/* Just fail if no media is present in the drive. */
18041 		goto fail_command;
18042 
18043 	default:
18044 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18045 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18046 			    "Unit not Ready. Additional sense code 0x%x\n",
18047 			    asc);
18048 		}
18049 		break;
18050 	}
18051 
18052 do_retry:
18053 
18054 	/*
18055 	 * Retry the command, as some targets may report NOT READY for
18056 	 * several seconds after being reset.
18057 	 */
18058 	xp->xb_retry_count++;
18059 	si.ssi_severity = SCSI_ERR_RETRYABLE;
18060 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18061 	    &si, EIO, SD_BSY_TIMEOUT, NULL);
18062 
18063 	return;
18064 
18065 fail_command:
18066 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18067 	sd_return_failed_command(un, bp, EIO);
18068 }
18069 
18070 
18071 
18072 /*
18073  *    Function: sd_sense_key_medium_or_hardware_error
18074  *
18075  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18076  *		sense key.
18077  *
18078  *     Context: May be called from interrupt context
18079  */
18080 
18081 static void
18082 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
18083 	uint8_t *sense_datap,
18084 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18085 {
18086 	struct sd_sense_info	si;
18087 	uint8_t sense_key = scsi_sense_key(sense_datap);
18088 	uint8_t asc = scsi_sense_asc(sense_datap);
18089 
18090 	ASSERT(un != NULL);
18091 	ASSERT(mutex_owned(SD_MUTEX(un)));
18092 	ASSERT(bp != NULL);
18093 	ASSERT(xp != NULL);
18094 	ASSERT(pktp != NULL);
18095 
18096 	si.ssi_severity = SCSI_ERR_FATAL;
18097 	si.ssi_pfa_flag = FALSE;
18098 
18099 	if (sense_key == KEY_MEDIUM_ERROR) {
18100 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18101 	}
18102 
18103 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18104 
18105 	if ((un->un_reset_retry_count != 0) &&
18106 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
18107 		mutex_exit(SD_MUTEX(un));
18108 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18109 		if (un->un_f_allow_bus_device_reset == TRUE) {
18110 
18111 			boolean_t try_resetting_target = B_TRUE;
18112 
18113 			/*
18114 			 * We need to be able to handle specific ASC when we are
18115 			 * handling a KEY_HARDWARE_ERROR. In particular
18116 			 * taking the default action of resetting the target may
18117 			 * not be the appropriate way to attempt recovery.
18118 			 * Resetting a target because of a single LUN failure
18119 			 * victimizes all LUNs on that target.
18120 			 *
18121 			 * This is true for the LSI arrays, if an LSI
18122 			 * array controller returns an ASC of 0x84 (LUN Dead) we
18123 			 * should trust it.
18124 			 */
18125 
18126 			if (sense_key == KEY_HARDWARE_ERROR) {
18127 				switch (asc) {
18128 				case 0x84:
18129 					if (SD_IS_LSI(un)) {
18130 						try_resetting_target = B_FALSE;
18131 					}
18132 					break;
18133 				default:
18134 					break;
18135 				}
18136 			}
18137 
18138 			if (try_resetting_target == B_TRUE) {
18139 				int reset_retval = 0;
18140 				if (un->un_f_lun_reset_enabled == TRUE) {
18141 					SD_TRACE(SD_LOG_IO_CORE, un,
18142 					    "sd_sense_key_medium_or_hardware_"
18143 					    "error: issuing RESET_LUN\n");
18144 					reset_retval =
18145 					    scsi_reset(SD_ADDRESS(un),
18146 					    RESET_LUN);
18147 				}
18148 				if (reset_retval == 0) {
18149 					SD_TRACE(SD_LOG_IO_CORE, un,
18150 					    "sd_sense_key_medium_or_hardware_"
18151 					    "error: issuing RESET_TARGET\n");
18152 					(void) scsi_reset(SD_ADDRESS(un),
18153 					    RESET_TARGET);
18154 				}
18155 			}
18156 		}
18157 		mutex_enter(SD_MUTEX(un));
18158 	}
18159 
18160 	/*
18161 	 * This really ought to be a fatal error, but we will retry anyway
18162 	 * as some drives report this as a spurious error.
18163 	 */
18164 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18165 	    &si, EIO, (clock_t)0, NULL);
18166 }
18167 
18168 
18169 
18170 /*
18171  *    Function: sd_sense_key_illegal_request
18172  *
18173  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18174  *
18175  *     Context: May be called from interrupt context
18176  */
18177 
18178 static void
18179 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18180 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18181 {
18182 	struct sd_sense_info	si;
18183 
18184 	ASSERT(un != NULL);
18185 	ASSERT(mutex_owned(SD_MUTEX(un)));
18186 	ASSERT(bp != NULL);
18187 	ASSERT(xp != NULL);
18188 	ASSERT(pktp != NULL);
18189 
18190 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
18191 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18192 
18193 	si.ssi_severity = SCSI_ERR_INFO;
18194 	si.ssi_pfa_flag = FALSE;
18195 
18196 	/* Pointless to retry if the target thinks it's an illegal request */
18197 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18198 	sd_return_failed_command(un, bp, EIO);
18199 }
18200 
18201 
18202 
18203 
18204 /*
18205  *    Function: sd_sense_key_unit_attention
18206  *
18207  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18208  *
18209  *     Context: May be called from interrupt context
18210  */
18211 
18212 static void
18213 sd_sense_key_unit_attention(struct sd_lun *un,
18214 	uint8_t *sense_datap,
18215 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18216 {
18217 	/*
18218 	 * For UNIT ATTENTION we allow retries for one minute. Devices
18219 	 * like Sonoma can return UNIT ATTENTION close to a minute
18220 	 * under certain conditions.
18221 	 */
18222 	int	retry_check_flag = SD_RETRIES_UA;
18223 	boolean_t	kstat_updated = B_FALSE;
18224 	struct	sd_sense_info		si;
18225 	uint8_t asc = scsi_sense_asc(sense_datap);
18226 
18227 	ASSERT(un != NULL);
18228 	ASSERT(mutex_owned(SD_MUTEX(un)));
18229 	ASSERT(bp != NULL);
18230 	ASSERT(xp != NULL);
18231 	ASSERT(pktp != NULL);
18232 
18233 	si.ssi_severity = SCSI_ERR_INFO;
18234 	si.ssi_pfa_flag = FALSE;
18235 
18236 
18237 	switch (asc) {
18238 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18239 		if (sd_report_pfa != 0) {
18240 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18241 			si.ssi_pfa_flag = TRUE;
18242 			retry_check_flag = SD_RETRIES_STANDARD;
18243 			goto do_retry;
18244 		}
18245 
18246 		break;
18247 
18248 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18249 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18250 			un->un_resvd_status |=
18251 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
18252 		}
18253 #ifdef _LP64
18254 		if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18255 			if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18256 			    un, KM_NOSLEEP) == 0) {
18257 				/*
18258 				 * If we can't dispatch the task we'll just
18259 				 * live without descriptor sense.  We can
18260 				 * try again on the next "unit attention"
18261 				 */
18262 				SD_ERROR(SD_LOG_ERROR, un,
18263 				    "sd_sense_key_unit_attention: "
18264 				    "Could not dispatch "
18265 				    "sd_reenable_dsense_task\n");
18266 			}
18267 		}
18268 #endif /* _LP64 */
18269 		/* FALLTHRU */
18270 
18271 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18272 		if (!un->un_f_has_removable_media) {
18273 			break;
18274 		}
18275 
18276 		/*
18277 		 * When we get a unit attention from a removable-media device,
18278 		 * it may be in a state that will take a long time to recover
18279 		 * (e.g., from a reset).  Since we are executing in interrupt
18280 		 * context here, we cannot wait around for the device to come
18281 		 * back. So hand this command off to sd_media_change_task()
18282 		 * for deferred processing under taskq thread context. (Note
18283 		 * that the command still may be failed if a problem is
18284 		 * encountered at a later time.)
18285 		 */
18286 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18287 		    KM_NOSLEEP) == 0) {
18288 			/*
18289 			 * Cannot dispatch the request so fail the command.
18290 			 */
18291 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
18292 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18293 			si.ssi_severity = SCSI_ERR_FATAL;
18294 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18295 			sd_return_failed_command(un, bp, EIO);
18296 		}
18297 
18298 		/*
18299 		 * If failed to dispatch sd_media_change_task(), we already
18300 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
18301 		 * we should update kstat later if it encounters an error. So,
18302 		 * we update kstat_updated flag here.
18303 		 */
18304 		kstat_updated = B_TRUE;
18305 
18306 		/*
18307 		 * Either the command has been successfully dispatched to a
18308 		 * task Q for retrying, or the dispatch failed. In either case
18309 		 * do NOT retry again by calling sd_retry_command. This sets up
18310 		 * two retries of the same command and when one completes and
18311 		 * frees the resources the other will access freed memory,
18312 		 * a bad thing.
18313 		 */
18314 		return;
18315 
18316 	default:
18317 		break;
18318 	}
18319 
18320 	/*
18321 	 * Update kstat if we haven't done that.
18322 	 */
18323 	if (!kstat_updated) {
18324 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18325 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18326 	}
18327 
18328 do_retry:
18329 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18330 	    EIO, SD_UA_RETRY_DELAY, NULL);
18331 }
18332 
18333 
18334 
18335 /*
18336  *    Function: sd_sense_key_fail_command
18337  *
18338  * Description: Use to fail a command when we don't like the sense key that
18339  *		was returned.
18340  *
18341  *     Context: May be called from interrupt context
18342  */
18343 
18344 static void
18345 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
18346 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18347 {
18348 	struct sd_sense_info	si;
18349 
18350 	ASSERT(un != NULL);
18351 	ASSERT(mutex_owned(SD_MUTEX(un)));
18352 	ASSERT(bp != NULL);
18353 	ASSERT(xp != NULL);
18354 	ASSERT(pktp != NULL);
18355 
18356 	si.ssi_severity = SCSI_ERR_FATAL;
18357 	si.ssi_pfa_flag = FALSE;
18358 
18359 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18360 	sd_return_failed_command(un, bp, EIO);
18361 }
18362 
18363 
18364 
18365 /*
18366  *    Function: sd_sense_key_blank_check
18367  *
18368  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18369  *		Has no monetary connotation.
18370  *
18371  *     Context: May be called from interrupt context
18372  */
18373 
18374 static void
18375 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
18376 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18377 {
18378 	struct sd_sense_info	si;
18379 
18380 	ASSERT(un != NULL);
18381 	ASSERT(mutex_owned(SD_MUTEX(un)));
18382 	ASSERT(bp != NULL);
18383 	ASSERT(xp != NULL);
18384 	ASSERT(pktp != NULL);
18385 
18386 	/*
18387 	 * Blank check is not fatal for removable devices, therefore
18388 	 * it does not require a console message.
18389 	 */
18390 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18391 	    SCSI_ERR_FATAL;
18392 	si.ssi_pfa_flag = FALSE;
18393 
18394 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18395 	sd_return_failed_command(un, bp, EIO);
18396 }
18397 
18398 
18399 
18400 
18401 /*
18402  *    Function: sd_sense_key_aborted_command
18403  *
18404  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18405  *
18406  *     Context: May be called from interrupt context
18407  */
18408 
18409 static void
18410 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18411 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18412 {
18413 	struct sd_sense_info	si;
18414 
18415 	ASSERT(un != NULL);
18416 	ASSERT(mutex_owned(SD_MUTEX(un)));
18417 	ASSERT(bp != NULL);
18418 	ASSERT(xp != NULL);
18419 	ASSERT(pktp != NULL);
18420 
18421 	si.ssi_severity = SCSI_ERR_FATAL;
18422 	si.ssi_pfa_flag = FALSE;
18423 
18424 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18425 
18426 	/*
18427 	 * This really ought to be a fatal error, but we will retry anyway
18428 	 * as some drives report this as a spurious error.
18429 	 */
18430 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18431 	    &si, EIO, (clock_t)0, NULL);
18432 }
18433 
18434 
18435 
18436 /*
18437  *    Function: sd_sense_key_default
18438  *
18439  * Description: Default recovery action for several SCSI sense keys (basically
18440  *		attempts a retry).
18441  *
18442  *     Context: May be called from interrupt context
18443  */
18444 
18445 static void
18446 sd_sense_key_default(struct sd_lun *un,
18447 	uint8_t *sense_datap,
18448 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18449 {
18450 	struct sd_sense_info	si;
18451 	uint8_t sense_key = scsi_sense_key(sense_datap);
18452 
18453 	ASSERT(un != NULL);
18454 	ASSERT(mutex_owned(SD_MUTEX(un)));
18455 	ASSERT(bp != NULL);
18456 	ASSERT(xp != NULL);
18457 	ASSERT(pktp != NULL);
18458 
18459 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18460 
18461 	/*
18462 	 * Undecoded sense key.	Attempt retries and hope that will fix
18463 	 * the problem.  Otherwise, we're dead.
18464 	 */
18465 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18466 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18467 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18468 	}
18469 
18470 	si.ssi_severity = SCSI_ERR_FATAL;
18471 	si.ssi_pfa_flag = FALSE;
18472 
18473 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18474 	    &si, EIO, (clock_t)0, NULL);
18475 }
18476 
18477 
18478 
18479 /*
18480  *    Function: sd_print_retry_msg
18481  *
18482  * Description: Print a message indicating the retry action being taken.
18483  *
18484  *   Arguments: un - ptr to associated softstate
18485  *		bp - ptr to buf(9S) for the command
18486  *		arg - not used.
18487  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18488  *			or SD_NO_RETRY_ISSUED
18489  *
18490  *     Context: May be called from interrupt context
18491  */
18492 /* ARGSUSED */
18493 static void
18494 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18495 {
18496 	struct sd_xbuf	*xp;
18497 	struct scsi_pkt *pktp;
18498 	char *reasonp;
18499 	char *msgp;
18500 
18501 	ASSERT(un != NULL);
18502 	ASSERT(mutex_owned(SD_MUTEX(un)));
18503 	ASSERT(bp != NULL);
18504 	pktp = SD_GET_PKTP(bp);
18505 	ASSERT(pktp != NULL);
18506 	xp = SD_GET_XBUF(bp);
18507 	ASSERT(xp != NULL);
18508 
18509 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18510 	mutex_enter(&un->un_pm_mutex);
18511 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18512 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18513 	    (pktp->pkt_flags & FLAG_SILENT)) {
18514 		mutex_exit(&un->un_pm_mutex);
18515 		goto update_pkt_reason;
18516 	}
18517 	mutex_exit(&un->un_pm_mutex);
18518 
18519 	/*
18520 	 * Suppress messages if they are all the same pkt_reason; with
18521 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18522 	 * If we are in panic, then suppress the retry messages.
18523 	 */
18524 	switch (flag) {
18525 	case SD_NO_RETRY_ISSUED:
18526 		msgp = "giving up";
18527 		break;
18528 	case SD_IMMEDIATE_RETRY_ISSUED:
18529 	case SD_DELAYED_RETRY_ISSUED:
18530 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18531 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18532 		    (sd_error_level != SCSI_ERR_ALL))) {
18533 			return;
18534 		}
18535 		msgp = "retrying command";
18536 		break;
18537 	default:
18538 		goto update_pkt_reason;
18539 	}
18540 
18541 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18542 	    scsi_rname(pktp->pkt_reason));
18543 
18544 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18545 	    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18546 
18547 update_pkt_reason:
18548 	/*
18549 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18550 	 * This is to prevent multiple console messages for the same failure
18551 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18552 	 * when the command is retried successfully because there still may be
18553 	 * more commands coming back with the same value of pktp->pkt_reason.
18554 	 */
18555 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18556 		un->un_last_pkt_reason = pktp->pkt_reason;
18557 	}
18558 }
18559 
18560 
18561 /*
18562  *    Function: sd_print_cmd_incomplete_msg
18563  *
18564  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18565  *
18566  *   Arguments: un - ptr to associated softstate
18567  *		bp - ptr to buf(9S) for the command
18568  *		arg - passed to sd_print_retry_msg()
18569  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18570  *			or SD_NO_RETRY_ISSUED
18571  *
18572  *     Context: May be called from interrupt context
18573  */
18574 
18575 static void
18576 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18577 	int code)
18578 {
18579 	dev_info_t	*dip;
18580 
18581 	ASSERT(un != NULL);
18582 	ASSERT(mutex_owned(SD_MUTEX(un)));
18583 	ASSERT(bp != NULL);
18584 
18585 	switch (code) {
18586 	case SD_NO_RETRY_ISSUED:
18587 		/* Command was failed. Someone turned off this target? */
18588 		if (un->un_state != SD_STATE_OFFLINE) {
18589 			/*
18590 			 * Suppress message if we are detaching and
18591 			 * device has been disconnected
18592 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18593 			 * private interface and not part of the DDI
18594 			 */
18595 			dip = un->un_sd->sd_dev;
18596 			if (!(DEVI_IS_DETACHING(dip) &&
18597 			    DEVI_IS_DEVICE_REMOVED(dip))) {
18598 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18599 				"disk not responding to selection\n");
18600 			}
18601 			New_state(un, SD_STATE_OFFLINE);
18602 		}
18603 		break;
18604 
18605 	case SD_DELAYED_RETRY_ISSUED:
18606 	case SD_IMMEDIATE_RETRY_ISSUED:
18607 	default:
18608 		/* Command was successfully queued for retry */
18609 		sd_print_retry_msg(un, bp, arg, code);
18610 		break;
18611 	}
18612 }
18613 
18614 
18615 /*
18616  *    Function: sd_pkt_reason_cmd_incomplete
18617  *
18618  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18619  *
18620  *     Context: May be called from interrupt context
18621  */
18622 
18623 static void
18624 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18625 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18626 {
18627 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18628 
18629 	ASSERT(un != NULL);
18630 	ASSERT(mutex_owned(SD_MUTEX(un)));
18631 	ASSERT(bp != NULL);
18632 	ASSERT(xp != NULL);
18633 	ASSERT(pktp != NULL);
18634 
18635 	/* Do not do a reset if selection did not complete */
18636 	/* Note: Should this not just check the bit? */
18637 	if (pktp->pkt_state != STATE_GOT_BUS) {
18638 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18639 		sd_reset_target(un, pktp);
18640 	}
18641 
18642 	/*
18643 	 * If the target was not successfully selected, then set
18644 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
18645 	 * with the target, and further retries and/or commands are
18646 	 * likely to take a long time.
18647 	 */
18648 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18649 		flag |= SD_RETRIES_FAILFAST;
18650 	}
18651 
18652 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18653 
18654 	sd_retry_command(un, bp, flag,
18655 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18656 }
18657 
18658 
18659 
18660 /*
18661  *    Function: sd_pkt_reason_cmd_tran_err
18662  *
18663  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
18664  *
18665  *     Context: May be called from interrupt context
18666  */
18667 
18668 static void
18669 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
18670 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18671 {
18672 	ASSERT(un != NULL);
18673 	ASSERT(mutex_owned(SD_MUTEX(un)));
18674 	ASSERT(bp != NULL);
18675 	ASSERT(xp != NULL);
18676 	ASSERT(pktp != NULL);
18677 
18678 	/*
18679 	 * Do not reset if we got a parity error, or if
18680 	 * selection did not complete.
18681 	 */
18682 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18683 	/* Note: Should this not just check the bit for pkt_state? */
18684 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
18685 	    (pktp->pkt_state != STATE_GOT_BUS)) {
18686 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18687 		sd_reset_target(un, pktp);
18688 	}
18689 
18690 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18691 
18692 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18693 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18694 }
18695 
18696 
18697 
18698 /*
18699  *    Function: sd_pkt_reason_cmd_reset
18700  *
18701  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
18702  *
18703  *     Context: May be called from interrupt context
18704  */
18705 
18706 static void
18707 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
18708 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18709 {
18710 	ASSERT(un != NULL);
18711 	ASSERT(mutex_owned(SD_MUTEX(un)));
18712 	ASSERT(bp != NULL);
18713 	ASSERT(xp != NULL);
18714 	ASSERT(pktp != NULL);
18715 
18716 	/* The target may still be running the command, so try to reset. */
18717 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18718 	sd_reset_target(un, pktp);
18719 
18720 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18721 
18722 	/*
18723 	 * If pkt_reason is CMD_RESET chances are that this pkt got
18724 	 * reset because another target on this bus caused it. The target
18725 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18726 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18727 	 */
18728 
18729 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18730 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18731 }
18732 
18733 
18734 
18735 
18736 /*
18737  *    Function: sd_pkt_reason_cmd_aborted
18738  *
18739  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
18740  *
18741  *     Context: May be called from interrupt context
18742  */
18743 
18744 static void
18745 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
18746 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18747 {
18748 	ASSERT(un != NULL);
18749 	ASSERT(mutex_owned(SD_MUTEX(un)));
18750 	ASSERT(bp != NULL);
18751 	ASSERT(xp != NULL);
18752 	ASSERT(pktp != NULL);
18753 
18754 	/* The target may still be running the command, so try to reset. */
18755 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18756 	sd_reset_target(un, pktp);
18757 
18758 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18759 
18760 	/*
18761 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
18762 	 * aborted because another target on this bus caused it. The target
18763 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18764 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18765 	 */
18766 
18767 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18768 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18769 }
18770 
18771 
18772 
18773 /*
18774  *    Function: sd_pkt_reason_cmd_timeout
18775  *
18776  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
18777  *
18778  *     Context: May be called from interrupt context
18779  */
18780 
18781 static void
18782 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
18783 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18784 {
18785 	ASSERT(un != NULL);
18786 	ASSERT(mutex_owned(SD_MUTEX(un)));
18787 	ASSERT(bp != NULL);
18788 	ASSERT(xp != NULL);
18789 	ASSERT(pktp != NULL);
18790 
18791 
18792 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18793 	sd_reset_target(un, pktp);
18794 
18795 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18796 
18797 	/*
18798 	 * A command timeout indicates that we could not establish
18799 	 * communication with the target, so set SD_RETRIES_FAILFAST
18800 	 * as further retries/commands are likely to take a long time.
18801 	 */
18802 	sd_retry_command(un, bp,
18803 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
18804 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18805 }
18806 
18807 
18808 
18809 /*
18810  *    Function: sd_pkt_reason_cmd_unx_bus_free
18811  *
18812  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
18813  *
18814  *     Context: May be called from interrupt context
18815  */
18816 
18817 static void
18818 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
18819 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18820 {
18821 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
18822 
18823 	ASSERT(un != NULL);
18824 	ASSERT(mutex_owned(SD_MUTEX(un)));
18825 	ASSERT(bp != NULL);
18826 	ASSERT(xp != NULL);
18827 	ASSERT(pktp != NULL);
18828 
18829 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18830 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18831 
18832 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
18833 	    sd_print_retry_msg : NULL;
18834 
18835 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18836 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18837 }
18838 
18839 
18840 /*
18841  *    Function: sd_pkt_reason_cmd_tag_reject
18842  *
18843  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
18844  *
18845  *     Context: May be called from interrupt context
18846  */
18847 
18848 static void
18849 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
18850 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18851 {
18852 	ASSERT(un != NULL);
18853 	ASSERT(mutex_owned(SD_MUTEX(un)));
18854 	ASSERT(bp != NULL);
18855 	ASSERT(xp != NULL);
18856 	ASSERT(pktp != NULL);
18857 
18858 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18859 	pktp->pkt_flags = 0;
18860 	un->un_tagflags = 0;
18861 	if (un->un_f_opt_queueing == TRUE) {
18862 		un->un_throttle = min(un->un_throttle, 3);
18863 	} else {
18864 		un->un_throttle = 1;
18865 	}
18866 	mutex_exit(SD_MUTEX(un));
18867 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
18868 	mutex_enter(SD_MUTEX(un));
18869 
18870 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18871 
18872 	/* Legacy behavior not to check retry counts here. */
18873 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
18874 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18875 }
18876 
18877 
18878 /*
18879  *    Function: sd_pkt_reason_default
18880  *
18881  * Description: Default recovery actions for SCSA pkt_reason values that
18882  *		do not have more explicit recovery actions.
18883  *
18884  *     Context: May be called from interrupt context
18885  */
18886 
18887 static void
18888 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
18889 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
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 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18898 	sd_reset_target(un, pktp);
18899 
18900 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18901 
18902 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18903 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18904 }
18905 
18906 
18907 
18908 /*
18909  *    Function: sd_pkt_status_check_condition
18910  *
18911  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
18912  *
18913  *     Context: May be called from interrupt context
18914  */
18915 
18916 static void
18917 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
18918 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18919 {
18920 	ASSERT(un != NULL);
18921 	ASSERT(mutex_owned(SD_MUTEX(un)));
18922 	ASSERT(bp != NULL);
18923 	ASSERT(xp != NULL);
18924 	ASSERT(pktp != NULL);
18925 
18926 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
18927 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
18928 
18929 	/*
18930 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
18931 	 * command will be retried after the request sense). Otherwise, retry
18932 	 * the command. Note: we are issuing the request sense even though the
18933 	 * retry limit may have been reached for the failed command.
18934 	 */
18935 	if (un->un_f_arq_enabled == FALSE) {
18936 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18937 		    "no ARQ, sending request sense command\n");
18938 		sd_send_request_sense_command(un, bp, pktp);
18939 	} else {
18940 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18941 		    "ARQ,retrying request sense command\n");
18942 #if defined(__i386) || defined(__amd64)
18943 		/*
18944 		 * The SD_RETRY_DELAY value need to be adjusted here
18945 		 * when SD_RETRY_DELAY change in sddef.h
18946 		 */
18947 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
18948 			un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
18949 			NULL);
18950 #else
18951 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
18952 		    EIO, SD_RETRY_DELAY, NULL);
18953 #endif
18954 	}
18955 
18956 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
18957 }
18958 
18959 
18960 /*
18961  *    Function: sd_pkt_status_busy
18962  *
18963  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
18964  *
18965  *     Context: May be called from interrupt context
18966  */
18967 
18968 static void
18969 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18970 	struct scsi_pkt *pktp)
18971 {
18972 	ASSERT(un != NULL);
18973 	ASSERT(mutex_owned(SD_MUTEX(un)));
18974 	ASSERT(bp != NULL);
18975 	ASSERT(xp != NULL);
18976 	ASSERT(pktp != NULL);
18977 
18978 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18979 	    "sd_pkt_status_busy: entry\n");
18980 
18981 	/* If retries are exhausted, just fail the command. */
18982 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
18983 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18984 		    "device busy too long\n");
18985 		sd_return_failed_command(un, bp, EIO);
18986 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18987 		    "sd_pkt_status_busy: exit\n");
18988 		return;
18989 	}
18990 	xp->xb_retry_count++;
18991 
18992 	/*
18993 	 * Try to reset the target. However, we do not want to perform
18994 	 * more than one reset if the device continues to fail. The reset
18995 	 * will be performed when the retry count reaches the reset
18996 	 * threshold.  This threshold should be set such that at least
18997 	 * one retry is issued before the reset is performed.
18998 	 */
18999 	if (xp->xb_retry_count ==
19000 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19001 		int rval = 0;
19002 		mutex_exit(SD_MUTEX(un));
19003 		if (un->un_f_allow_bus_device_reset == TRUE) {
19004 			/*
19005 			 * First try to reset the LUN; if we cannot then
19006 			 * try to reset the target.
19007 			 */
19008 			if (un->un_f_lun_reset_enabled == TRUE) {
19009 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19010 				    "sd_pkt_status_busy: RESET_LUN\n");
19011 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19012 			}
19013 			if (rval == 0) {
19014 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19015 				    "sd_pkt_status_busy: RESET_TARGET\n");
19016 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19017 			}
19018 		}
19019 		if (rval == 0) {
19020 			/*
19021 			 * If the RESET_LUN and/or RESET_TARGET failed,
19022 			 * try RESET_ALL
19023 			 */
19024 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19025 			    "sd_pkt_status_busy: RESET_ALL\n");
19026 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19027 		}
19028 		mutex_enter(SD_MUTEX(un));
19029 		if (rval == 0) {
19030 			/*
19031 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19032 			 * At this point we give up & fail the command.
19033 			 */
19034 			sd_return_failed_command(un, bp, EIO);
19035 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19036 			    "sd_pkt_status_busy: exit (failed cmd)\n");
19037 			return;
19038 		}
19039 	}
19040 
19041 	/*
19042 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19043 	 * we have already checked the retry counts above.
19044 	 */
19045 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19046 	    EIO, SD_BSY_TIMEOUT, NULL);
19047 
19048 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19049 	    "sd_pkt_status_busy: exit\n");
19050 }
19051 
19052 
19053 /*
19054  *    Function: sd_pkt_status_reservation_conflict
19055  *
19056  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19057  *		command status.
19058  *
19059  *     Context: May be called from interrupt context
19060  */
19061 
19062 static void
19063 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19064 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19065 {
19066 	ASSERT(un != NULL);
19067 	ASSERT(mutex_owned(SD_MUTEX(un)));
19068 	ASSERT(bp != NULL);
19069 	ASSERT(xp != NULL);
19070 	ASSERT(pktp != NULL);
19071 
19072 	/*
19073 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19074 	 * conflict could be due to various reasons like incorrect keys, not
19075 	 * registered or not reserved etc. So, we return EACCES to the caller.
19076 	 */
19077 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19078 		int cmd = SD_GET_PKT_OPCODE(pktp);
19079 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19080 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19081 			sd_return_failed_command(un, bp, EACCES);
19082 			return;
19083 		}
19084 	}
19085 
19086 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19087 
19088 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19089 		if (sd_failfast_enable != 0) {
19090 			/* By definition, we must panic here.... */
19091 			sd_panic_for_res_conflict(un);
19092 			/*NOTREACHED*/
19093 		}
19094 		SD_ERROR(SD_LOG_IO, un,
19095 		    "sd_handle_resv_conflict: Disk Reserved\n");
19096 		sd_return_failed_command(un, bp, EACCES);
19097 		return;
19098 	}
19099 
19100 	/*
19101 	 * 1147670: retry only if sd_retry_on_reservation_conflict
19102 	 * property is set (default is 1). Retries will not succeed
19103 	 * on a disk reserved by another initiator. HA systems
19104 	 * may reset this via sd.conf to avoid these retries.
19105 	 *
19106 	 * Note: The legacy return code for this failure is EIO, however EACCES
19107 	 * seems more appropriate for a reservation conflict.
19108 	 */
19109 	if (sd_retry_on_reservation_conflict == 0) {
19110 		SD_ERROR(SD_LOG_IO, un,
19111 		    "sd_handle_resv_conflict: Device Reserved\n");
19112 		sd_return_failed_command(un, bp, EIO);
19113 		return;
19114 	}
19115 
19116 	/*
19117 	 * Retry the command if we can.
19118 	 *
19119 	 * Note: The legacy return code for this failure is EIO, however EACCES
19120 	 * seems more appropriate for a reservation conflict.
19121 	 */
19122 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19123 	    (clock_t)2, NULL);
19124 }
19125 
19126 
19127 
19128 /*
19129  *    Function: sd_pkt_status_qfull
19130  *
19131  * Description: Handle a QUEUE FULL condition from the target.  This can
19132  *		occur if the HBA does not handle the queue full condition.
19133  *		(Basically this means third-party HBAs as Sun HBAs will
19134  *		handle the queue full condition.)  Note that if there are
19135  *		some commands already in the transport, then the queue full
19136  *		has occurred because the queue for this nexus is actually
19137  *		full. If there are no commands in the transport, then the
19138  *		queue full is resulting from some other initiator or lun
19139  *		consuming all the resources at the target.
19140  *
19141  *     Context: May be called from interrupt context
19142  */
19143 
19144 static void
19145 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
19146 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19147 {
19148 	ASSERT(un != NULL);
19149 	ASSERT(mutex_owned(SD_MUTEX(un)));
19150 	ASSERT(bp != NULL);
19151 	ASSERT(xp != NULL);
19152 	ASSERT(pktp != NULL);
19153 
19154 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19155 	    "sd_pkt_status_qfull: entry\n");
19156 
19157 	/*
19158 	 * Just lower the QFULL throttle and retry the command.  Note that
19159 	 * we do not limit the number of retries here.
19160 	 */
19161 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19162 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19163 	    SD_RESTART_TIMEOUT, NULL);
19164 
19165 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19166 	    "sd_pkt_status_qfull: exit\n");
19167 }
19168 
19169 
19170 /*
19171  *    Function: sd_reset_target
19172  *
19173  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19174  *		RESET_TARGET, or RESET_ALL.
19175  *
19176  *     Context: May be called under interrupt context.
19177  */
19178 
19179 static void
19180 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19181 {
19182 	int rval = 0;
19183 
19184 	ASSERT(un != NULL);
19185 	ASSERT(mutex_owned(SD_MUTEX(un)));
19186 	ASSERT(pktp != NULL);
19187 
19188 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19189 
19190 	/*
19191 	 * No need to reset if the transport layer has already done so.
19192 	 */
19193 	if ((pktp->pkt_statistics &
19194 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19195 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19196 		    "sd_reset_target: no reset\n");
19197 		return;
19198 	}
19199 
19200 	mutex_exit(SD_MUTEX(un));
19201 
19202 	if (un->un_f_allow_bus_device_reset == TRUE) {
19203 		if (un->un_f_lun_reset_enabled == TRUE) {
19204 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19205 			    "sd_reset_target: RESET_LUN\n");
19206 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19207 		}
19208 		if (rval == 0) {
19209 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19210 			    "sd_reset_target: RESET_TARGET\n");
19211 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19212 		}
19213 	}
19214 
19215 	if (rval == 0) {
19216 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19217 		    "sd_reset_target: RESET_ALL\n");
19218 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19219 	}
19220 
19221 	mutex_enter(SD_MUTEX(un));
19222 
19223 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19224 }
19225 
19226 
19227 /*
19228  *    Function: sd_media_change_task
19229  *
19230  * Description: Recovery action for CDROM to become available.
19231  *
19232  *     Context: Executes in a taskq() thread context
19233  */
19234 
19235 static void
19236 sd_media_change_task(void *arg)
19237 {
19238 	struct	scsi_pkt	*pktp = arg;
19239 	struct	sd_lun		*un;
19240 	struct	buf		*bp;
19241 	struct	sd_xbuf		*xp;
19242 	int	err		= 0;
19243 	int	retry_count	= 0;
19244 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
19245 	struct	sd_sense_info	si;
19246 
19247 	ASSERT(pktp != NULL);
19248 	bp = (struct buf *)pktp->pkt_private;
19249 	ASSERT(bp != NULL);
19250 	xp = SD_GET_XBUF(bp);
19251 	ASSERT(xp != NULL);
19252 	un = SD_GET_UN(bp);
19253 	ASSERT(un != NULL);
19254 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19255 	ASSERT(un->un_f_monitor_media_state);
19256 
19257 	si.ssi_severity = SCSI_ERR_INFO;
19258 	si.ssi_pfa_flag = FALSE;
19259 
19260 	/*
19261 	 * When a reset is issued on a CDROM, it takes a long time to
19262 	 * recover. First few attempts to read capacity and other things
19263 	 * related to handling unit attention fail (with a ASC 0x4 and
19264 	 * ASCQ 0x1). In that case we want to do enough retries and we want
19265 	 * to limit the retries in other cases of genuine failures like
19266 	 * no media in drive.
19267 	 */
19268 	while (retry_count++ < retry_limit) {
19269 		if ((err = sd_handle_mchange(un)) == 0) {
19270 			break;
19271 		}
19272 		if (err == EAGAIN) {
19273 			retry_limit = SD_UNIT_ATTENTION_RETRY;
19274 		}
19275 		/* Sleep for 0.5 sec. & try again */
19276 		delay(drv_usectohz(500000));
19277 	}
19278 
19279 	/*
19280 	 * Dispatch (retry or fail) the original command here,
19281 	 * along with appropriate console messages....
19282 	 *
19283 	 * Must grab the mutex before calling sd_retry_command,
19284 	 * sd_print_sense_msg and sd_return_failed_command.
19285 	 */
19286 	mutex_enter(SD_MUTEX(un));
19287 	if (err != SD_CMD_SUCCESS) {
19288 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
19289 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19290 		si.ssi_severity = SCSI_ERR_FATAL;
19291 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19292 		sd_return_failed_command(un, bp, EIO);
19293 	} else {
19294 		sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
19295 		    &si, EIO, (clock_t)0, NULL);
19296 	}
19297 	mutex_exit(SD_MUTEX(un));
19298 }
19299 
19300 
19301 
19302 /*
19303  *    Function: sd_handle_mchange
19304  *
19305  * Description: Perform geometry validation & other recovery when CDROM
19306  *		has been removed from drive.
19307  *
19308  * Return Code: 0 for success
19309  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
19310  *		sd_send_scsi_READ_CAPACITY()
19311  *
19312  *     Context: Executes in a taskq() thread context
19313  */
19314 
19315 static int
19316 sd_handle_mchange(struct sd_lun *un)
19317 {
19318 	uint64_t	capacity;
19319 	uint32_t	lbasize;
19320 	int		rval;
19321 
19322 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19323 	ASSERT(un->un_f_monitor_media_state);
19324 
19325 	if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
19326 	    SD_PATH_DIRECT_PRIORITY)) != 0) {
19327 		return (rval);
19328 	}
19329 
19330 	mutex_enter(SD_MUTEX(un));
19331 	sd_update_block_info(un, lbasize, capacity);
19332 
19333 	if (un->un_errstats != NULL) {
19334 		struct	sd_errstats *stp =
19335 		    (struct sd_errstats *)un->un_errstats->ks_data;
19336 		stp->sd_capacity.value.ui64 = (uint64_t)
19337 		    ((uint64_t)un->un_blockcount *
19338 		    (uint64_t)un->un_tgt_blocksize);
19339 	}
19340 
19341 	/*
19342 	 * Note: Maybe let the strategy/partitioning chain worry about getting
19343 	 * valid geometry.
19344 	 */
19345 	un->un_f_geometry_is_valid = FALSE;
19346 	(void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY);
19347 	if (un->un_f_geometry_is_valid == FALSE) {
19348 		mutex_exit(SD_MUTEX(un));
19349 		return (EIO);
19350 	}
19351 
19352 	mutex_exit(SD_MUTEX(un));
19353 
19354 	/*
19355 	 * Try to lock the door
19356 	 */
19357 	return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
19358 	    SD_PATH_DIRECT_PRIORITY));
19359 }
19360 
19361 
19362 /*
19363  *    Function: sd_send_scsi_DOORLOCK
19364  *
19365  * Description: Issue the scsi DOOR LOCK command
19366  *
19367  *   Arguments: un    - pointer to driver soft state (unit) structure for
19368  *			this target.
19369  *		flag  - SD_REMOVAL_ALLOW
19370  *			SD_REMOVAL_PREVENT
19371  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19372  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19373  *			to use the USCSI "direct" chain and bypass the normal
19374  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19375  *			command is issued as part of an error recovery action.
19376  *
19377  * Return Code: 0   - Success
19378  *		errno return code from sd_send_scsi_cmd()
19379  *
19380  *     Context: Can sleep.
19381  */
19382 
19383 static int
19384 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag)
19385 {
19386 	union scsi_cdb		cdb;
19387 	struct uscsi_cmd	ucmd_buf;
19388 	struct scsi_extended_sense	sense_buf;
19389 	int			status;
19390 
19391 	ASSERT(un != NULL);
19392 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19393 
19394 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19395 
19396 	/* already determined doorlock is not supported, fake success */
19397 	if (un->un_f_doorlock_supported == FALSE) {
19398 		return (0);
19399 	}
19400 
19401 	bzero(&cdb, sizeof (cdb));
19402 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19403 
19404 	cdb.scc_cmd = SCMD_DOORLOCK;
19405 	cdb.cdb_opaque[4] = (uchar_t)flag;
19406 
19407 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19408 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19409 	ucmd_buf.uscsi_bufaddr	= NULL;
19410 	ucmd_buf.uscsi_buflen	= 0;
19411 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19412 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19413 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19414 	ucmd_buf.uscsi_timeout	= 15;
19415 
19416 	SD_TRACE(SD_LOG_IO, un,
19417 	    "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n");
19418 
19419 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19420 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19421 
19422 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19423 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19424 	    (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19425 		/* fake success and skip subsequent doorlock commands */
19426 		un->un_f_doorlock_supported = FALSE;
19427 		return (0);
19428 	}
19429 
19430 	return (status);
19431 }
19432 
19433 /*
19434  *    Function: sd_send_scsi_READ_CAPACITY
19435  *
19436  * Description: This routine uses the scsi READ CAPACITY command to determine
19437  *		the device capacity in number of blocks and the device native
19438  *		block size. If this function returns a failure, then the
19439  *		values in *capp and *lbap are undefined.  If the capacity
19440  *		returned is 0xffffffff then the lun is too large for a
19441  *		normal READ CAPACITY command and the results of a
19442  *		READ CAPACITY 16 will be used instead.
19443  *
19444  *   Arguments: un   - ptr to soft state struct for the target
19445  *		capp - ptr to unsigned 64-bit variable to receive the
19446  *			capacity value from the command.
19447  *		lbap - ptr to unsigned 32-bit varaible to receive the
19448  *			block size value from the command
19449  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19450  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19451  *			to use the USCSI "direct" chain and bypass the normal
19452  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19453  *			command is issued as part of an error recovery action.
19454  *
19455  * Return Code: 0   - Success
19456  *		EIO - IO error
19457  *		EACCES - Reservation conflict detected
19458  *		EAGAIN - Device is becoming ready
19459  *		errno return code from sd_send_scsi_cmd()
19460  *
19461  *     Context: Can sleep.  Blocks until command completes.
19462  */
19463 
19464 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
19465 
19466 static int
19467 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap,
19468 	int path_flag)
19469 {
19470 	struct	scsi_extended_sense	sense_buf;
19471 	struct	uscsi_cmd	ucmd_buf;
19472 	union	scsi_cdb	cdb;
19473 	uint32_t		*capacity_buf;
19474 	uint64_t		capacity;
19475 	uint32_t		lbasize;
19476 	int			status;
19477 
19478 	ASSERT(un != NULL);
19479 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19480 	ASSERT(capp != NULL);
19481 	ASSERT(lbap != NULL);
19482 
19483 	SD_TRACE(SD_LOG_IO, un,
19484 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19485 
19486 	/*
19487 	 * First send a READ_CAPACITY command to the target.
19488 	 * (This command is mandatory under SCSI-2.)
19489 	 *
19490 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
19491 	 * Medium Indicator bit is cleared.  The address field must be
19492 	 * zero if the PMI bit is zero.
19493 	 */
19494 	bzero(&cdb, sizeof (cdb));
19495 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19496 
19497 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
19498 
19499 	cdb.scc_cmd = SCMD_READ_CAPACITY;
19500 
19501 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19502 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19503 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
19504 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
19505 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19506 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19507 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19508 	ucmd_buf.uscsi_timeout	= 60;
19509 
19510 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19511 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19512 
19513 	switch (status) {
19514 	case 0:
19515 		/* Return failure if we did not get valid capacity data. */
19516 		if (ucmd_buf.uscsi_resid != 0) {
19517 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19518 			return (EIO);
19519 		}
19520 
19521 		/*
19522 		 * Read capacity and block size from the READ CAPACITY 10 data.
19523 		 * This data may be adjusted later due to device specific
19524 		 * issues.
19525 		 *
19526 		 * According to the SCSI spec, the READ CAPACITY 10
19527 		 * command returns the following:
19528 		 *
19529 		 *  bytes 0-3: Maximum logical block address available.
19530 		 *		(MSB in byte:0 & LSB in byte:3)
19531 		 *
19532 		 *  bytes 4-7: Block length in bytes
19533 		 *		(MSB in byte:4 & LSB in byte:7)
19534 		 *
19535 		 */
19536 		capacity = BE_32(capacity_buf[0]);
19537 		lbasize = BE_32(capacity_buf[1]);
19538 
19539 		/*
19540 		 * Done with capacity_buf
19541 		 */
19542 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19543 
19544 		/*
19545 		 * if the reported capacity is set to all 0xf's, then
19546 		 * this disk is too large and requires SBC-2 commands.
19547 		 * Reissue the request using READ CAPACITY 16.
19548 		 */
19549 		if (capacity == 0xffffffff) {
19550 			status = sd_send_scsi_READ_CAPACITY_16(un, &capacity,
19551 			    &lbasize, path_flag);
19552 			if (status != 0) {
19553 				return (status);
19554 			}
19555 		}
19556 		break;	/* Success! */
19557 	case EIO:
19558 		switch (ucmd_buf.uscsi_status) {
19559 		case STATUS_RESERVATION_CONFLICT:
19560 			status = EACCES;
19561 			break;
19562 		case STATUS_CHECK:
19563 			/*
19564 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
19565 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19566 			 */
19567 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19568 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
19569 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
19570 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19571 				return (EAGAIN);
19572 			}
19573 			break;
19574 		default:
19575 			break;
19576 		}
19577 		/* FALLTHRU */
19578 	default:
19579 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19580 		return (status);
19581 	}
19582 
19583 	/*
19584 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
19585 	 * (2352 and 0 are common) so for these devices always force the value
19586 	 * to 2048 as required by the ATAPI specs.
19587 	 */
19588 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
19589 		lbasize = 2048;
19590 	}
19591 
19592 	/*
19593 	 * Get the maximum LBA value from the READ CAPACITY data.
19594 	 * Here we assume that the Partial Medium Indicator (PMI) bit
19595 	 * was cleared when issuing the command. This means that the LBA
19596 	 * returned from the device is the LBA of the last logical block
19597 	 * on the logical unit.  The actual logical block count will be
19598 	 * this value plus one.
19599 	 *
19600 	 * Currently the capacity is saved in terms of un->un_sys_blocksize,
19601 	 * so scale the capacity value to reflect this.
19602 	 */
19603 	capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize);
19604 
19605 #if defined(__i386) || defined(__amd64)
19606 	/*
19607 	 * Refer to comments related to off-by-1 at the
19608 	 * header of this file.
19609 	 * Treat 1TB disk as (1T - 512)B.
19610 	 */
19611 	if (un->un_f_capacity_adjusted == 1)
19612 	    capacity = DK_MAX_BLOCKS;
19613 #endif
19614 
19615 	/*
19616 	 * Copy the values from the READ CAPACITY command into the space
19617 	 * provided by the caller.
19618 	 */
19619 	*capp = capacity;
19620 	*lbap = lbasize;
19621 
19622 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
19623 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19624 
19625 	/*
19626 	 * Both the lbasize and capacity from the device must be nonzero,
19627 	 * otherwise we assume that the values are not valid and return
19628 	 * failure to the caller. (4203735)
19629 	 */
19630 	if ((capacity == 0) || (lbasize == 0)) {
19631 		return (EIO);
19632 	}
19633 
19634 	return (0);
19635 }
19636 
19637 /*
19638  *    Function: sd_send_scsi_READ_CAPACITY_16
19639  *
19640  * Description: This routine uses the scsi READ CAPACITY 16 command to
19641  *		determine the device capacity in number of blocks and the
19642  *		device native block size.  If this function returns a failure,
19643  *		then the values in *capp and *lbap are undefined.
19644  *		This routine should always be called by
19645  *		sd_send_scsi_READ_CAPACITY which will appy any device
19646  *		specific adjustments to capacity and lbasize.
19647  *
19648  *   Arguments: un   - ptr to soft state struct for the target
19649  *		capp - ptr to unsigned 64-bit variable to receive the
19650  *			capacity value from the command.
19651  *		lbap - ptr to unsigned 32-bit varaible to receive the
19652  *			block size value from the command
19653  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19654  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19655  *			to use the USCSI "direct" chain and bypass the normal
19656  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
19657  *			this command is issued as part of an error recovery
19658  *			action.
19659  *
19660  * Return Code: 0   - Success
19661  *		EIO - IO error
19662  *		EACCES - Reservation conflict detected
19663  *		EAGAIN - Device is becoming ready
19664  *		errno return code from sd_send_scsi_cmd()
19665  *
19666  *     Context: Can sleep.  Blocks until command completes.
19667  */
19668 
19669 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
19670 
19671 static int
19672 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
19673 	uint32_t *lbap, int path_flag)
19674 {
19675 	struct	scsi_extended_sense	sense_buf;
19676 	struct	uscsi_cmd	ucmd_buf;
19677 	union	scsi_cdb	cdb;
19678 	uint64_t		*capacity16_buf;
19679 	uint64_t		capacity;
19680 	uint32_t		lbasize;
19681 	int			status;
19682 
19683 	ASSERT(un != NULL);
19684 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19685 	ASSERT(capp != NULL);
19686 	ASSERT(lbap != NULL);
19687 
19688 	SD_TRACE(SD_LOG_IO, un,
19689 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19690 
19691 	/*
19692 	 * First send a READ_CAPACITY_16 command to the target.
19693 	 *
19694 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
19695 	 * Medium Indicator bit is cleared.  The address field must be
19696 	 * zero if the PMI bit is zero.
19697 	 */
19698 	bzero(&cdb, sizeof (cdb));
19699 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19700 
19701 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
19702 
19703 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19704 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
19705 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
19706 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
19707 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19708 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19709 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19710 	ucmd_buf.uscsi_timeout	= 60;
19711 
19712 	/*
19713 	 * Read Capacity (16) is a Service Action In command.  One
19714 	 * command byte (0x9E) is overloaded for multiple operations,
19715 	 * with the second CDB byte specifying the desired operation
19716 	 */
19717 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
19718 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
19719 
19720 	/*
19721 	 * Fill in allocation length field
19722 	 */
19723 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
19724 
19725 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19726 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19727 
19728 	switch (status) {
19729 	case 0:
19730 		/* Return failure if we did not get valid capacity data. */
19731 		if (ucmd_buf.uscsi_resid > 20) {
19732 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19733 			return (EIO);
19734 		}
19735 
19736 		/*
19737 		 * Read capacity and block size from the READ CAPACITY 10 data.
19738 		 * This data may be adjusted later due to device specific
19739 		 * issues.
19740 		 *
19741 		 * According to the SCSI spec, the READ CAPACITY 10
19742 		 * command returns the following:
19743 		 *
19744 		 *  bytes 0-7: Maximum logical block address available.
19745 		 *		(MSB in byte:0 & LSB in byte:7)
19746 		 *
19747 		 *  bytes 8-11: Block length in bytes
19748 		 *		(MSB in byte:8 & LSB in byte:11)
19749 		 *
19750 		 */
19751 		capacity = BE_64(capacity16_buf[0]);
19752 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
19753 
19754 		/*
19755 		 * Done with capacity16_buf
19756 		 */
19757 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19758 
19759 		/*
19760 		 * if the reported capacity is set to all 0xf's, then
19761 		 * this disk is too large.  This could only happen with
19762 		 * a device that supports LBAs larger than 64 bits which
19763 		 * are not defined by any current T10 standards.
19764 		 */
19765 		if (capacity == 0xffffffffffffffff) {
19766 			return (EIO);
19767 		}
19768 		break;	/* Success! */
19769 	case EIO:
19770 		switch (ucmd_buf.uscsi_status) {
19771 		case STATUS_RESERVATION_CONFLICT:
19772 			status = EACCES;
19773 			break;
19774 		case STATUS_CHECK:
19775 			/*
19776 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
19777 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19778 			 */
19779 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19780 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
19781 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
19782 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19783 				return (EAGAIN);
19784 			}
19785 			break;
19786 		default:
19787 			break;
19788 		}
19789 		/* FALLTHRU */
19790 	default:
19791 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19792 		return (status);
19793 	}
19794 
19795 	*capp = capacity;
19796 	*lbap = lbasize;
19797 
19798 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
19799 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19800 
19801 	return (0);
19802 }
19803 
19804 
19805 /*
19806  *    Function: sd_send_scsi_START_STOP_UNIT
19807  *
19808  * Description: Issue a scsi START STOP UNIT command to the target.
19809  *
19810  *   Arguments: un    - pointer to driver soft state (unit) structure for
19811  *			this target.
19812  *		flag  - SD_TARGET_START
19813  *			SD_TARGET_STOP
19814  *			SD_TARGET_EJECT
19815  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19816  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19817  *			to use the USCSI "direct" chain and bypass the normal
19818  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19819  *			command is issued as part of an error recovery action.
19820  *
19821  * Return Code: 0   - Success
19822  *		EIO - IO error
19823  *		EACCES - Reservation conflict detected
19824  *		ENXIO  - Not Ready, medium not present
19825  *		errno return code from sd_send_scsi_cmd()
19826  *
19827  *     Context: Can sleep.
19828  */
19829 
19830 static int
19831 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag)
19832 {
19833 	struct	scsi_extended_sense	sense_buf;
19834 	union scsi_cdb		cdb;
19835 	struct uscsi_cmd	ucmd_buf;
19836 	int			status;
19837 
19838 	ASSERT(un != NULL);
19839 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19840 
19841 	SD_TRACE(SD_LOG_IO, un,
19842 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
19843 
19844 	if (un->un_f_check_start_stop &&
19845 	    ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
19846 	    (un->un_f_start_stop_supported != TRUE)) {
19847 		return (0);
19848 	}
19849 
19850 	bzero(&cdb, sizeof (cdb));
19851 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19852 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19853 
19854 	cdb.scc_cmd = SCMD_START_STOP;
19855 	cdb.cdb_opaque[4] = (uchar_t)flag;
19856 
19857 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19858 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19859 	ucmd_buf.uscsi_bufaddr	= NULL;
19860 	ucmd_buf.uscsi_buflen	= 0;
19861 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19862 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19863 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19864 	ucmd_buf.uscsi_timeout	= 200;
19865 
19866 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19867 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19868 
19869 	switch (status) {
19870 	case 0:
19871 		break;	/* Success! */
19872 	case EIO:
19873 		switch (ucmd_buf.uscsi_status) {
19874 		case STATUS_RESERVATION_CONFLICT:
19875 			status = EACCES;
19876 			break;
19877 		case STATUS_CHECK:
19878 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
19879 				switch (scsi_sense_key(
19880 						(uint8_t *)&sense_buf)) {
19881 				case KEY_ILLEGAL_REQUEST:
19882 					status = ENOTSUP;
19883 					break;
19884 				case KEY_NOT_READY:
19885 					if (scsi_sense_asc(
19886 						    (uint8_t *)&sense_buf)
19887 					    == 0x3A) {
19888 						status = ENXIO;
19889 					}
19890 					break;
19891 				default:
19892 					break;
19893 				}
19894 			}
19895 			break;
19896 		default:
19897 			break;
19898 		}
19899 		break;
19900 	default:
19901 		break;
19902 	}
19903 
19904 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
19905 
19906 	return (status);
19907 }
19908 
19909 
19910 /*
19911  *    Function: sd_start_stop_unit_callback
19912  *
19913  * Description: timeout(9F) callback to begin recovery process for a
19914  *		device that has spun down.
19915  *
19916  *   Arguments: arg - pointer to associated softstate struct.
19917  *
19918  *     Context: Executes in a timeout(9F) thread context
19919  */
19920 
19921 static void
19922 sd_start_stop_unit_callback(void *arg)
19923 {
19924 	struct sd_lun	*un = arg;
19925 	ASSERT(un != NULL);
19926 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19927 
19928 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
19929 
19930 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
19931 }
19932 
19933 
19934 /*
19935  *    Function: sd_start_stop_unit_task
19936  *
19937  * Description: Recovery procedure when a drive is spun down.
19938  *
19939  *   Arguments: arg - pointer to associated softstate struct.
19940  *
19941  *     Context: Executes in a taskq() thread context
19942  */
19943 
19944 static void
19945 sd_start_stop_unit_task(void *arg)
19946 {
19947 	struct sd_lun	*un = arg;
19948 
19949 	ASSERT(un != NULL);
19950 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19951 
19952 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
19953 
19954 	/*
19955 	 * Some unformatted drives report not ready error, no need to
19956 	 * restart if format has been initiated.
19957 	 */
19958 	mutex_enter(SD_MUTEX(un));
19959 	if (un->un_f_format_in_progress == TRUE) {
19960 		mutex_exit(SD_MUTEX(un));
19961 		return;
19962 	}
19963 	mutex_exit(SD_MUTEX(un));
19964 
19965 	/*
19966 	 * When a START STOP command is issued from here, it is part of a
19967 	 * failure recovery operation and must be issued before any other
19968 	 * commands, including any pending retries. Thus it must be sent
19969 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
19970 	 * succeeds or not, we will start I/O after the attempt.
19971 	 */
19972 	(void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
19973 	    SD_PATH_DIRECT_PRIORITY);
19974 
19975 	/*
19976 	 * The above call blocks until the START_STOP_UNIT command completes.
19977 	 * Now that it has completed, we must re-try the original IO that
19978 	 * received the NOT READY condition in the first place. There are
19979 	 * three possible conditions here:
19980 	 *
19981 	 *  (1) The original IO is on un_retry_bp.
19982 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
19983 	 *	is NULL.
19984 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
19985 	 *	points to some other, unrelated bp.
19986 	 *
19987 	 * For each case, we must call sd_start_cmds() with un_retry_bp
19988 	 * as the argument. If un_retry_bp is NULL, this will initiate
19989 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
19990 	 * then this will process the bp on un_retry_bp. That may or may not
19991 	 * be the original IO, but that does not matter: the important thing
19992 	 * is to keep the IO processing going at this point.
19993 	 *
19994 	 * Note: This is a very specific error recovery sequence associated
19995 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
19996 	 * serialize the I/O with completion of the spin-up.
19997 	 */
19998 	mutex_enter(SD_MUTEX(un));
19999 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20000 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20001 	    un, un->un_retry_bp);
20002 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
20003 	sd_start_cmds(un, un->un_retry_bp);
20004 	mutex_exit(SD_MUTEX(un));
20005 
20006 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20007 }
20008 
20009 
20010 /*
20011  *    Function: sd_send_scsi_INQUIRY
20012  *
20013  * Description: Issue the scsi INQUIRY command.
20014  *
20015  *   Arguments: un
20016  *		bufaddr
20017  *		buflen
20018  *		evpd
20019  *		page_code
20020  *		page_length
20021  *
20022  * Return Code: 0   - Success
20023  *		errno return code from sd_send_scsi_cmd()
20024  *
20025  *     Context: Can sleep. Does not return until command is completed.
20026  */
20027 
20028 static int
20029 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen,
20030 	uchar_t evpd, uchar_t page_code, size_t *residp)
20031 {
20032 	union scsi_cdb		cdb;
20033 	struct uscsi_cmd	ucmd_buf;
20034 	int			status;
20035 
20036 	ASSERT(un != NULL);
20037 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20038 	ASSERT(bufaddr != NULL);
20039 
20040 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20041 
20042 	bzero(&cdb, sizeof (cdb));
20043 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20044 	bzero(bufaddr, buflen);
20045 
20046 	cdb.scc_cmd = SCMD_INQUIRY;
20047 	cdb.cdb_opaque[1] = evpd;
20048 	cdb.cdb_opaque[2] = page_code;
20049 	FORMG0COUNT(&cdb, buflen);
20050 
20051 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20052 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20053 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20054 	ucmd_buf.uscsi_buflen	= buflen;
20055 	ucmd_buf.uscsi_rqbuf	= NULL;
20056 	ucmd_buf.uscsi_rqlen	= 0;
20057 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
20058 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
20059 
20060 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20061 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT);
20062 
20063 	if ((status == 0) && (residp != NULL)) {
20064 		*residp = ucmd_buf.uscsi_resid;
20065 	}
20066 
20067 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20068 
20069 	return (status);
20070 }
20071 
20072 
20073 /*
20074  *    Function: sd_send_scsi_TEST_UNIT_READY
20075  *
20076  * Description: Issue the scsi TEST UNIT READY command.
20077  *		This routine can be told to set the flag USCSI_DIAGNOSE to
20078  *		prevent retrying failed commands. Use this when the intent
20079  *		is either to check for device readiness, to clear a Unit
20080  *		Attention, or to clear any outstanding sense data.
20081  *		However under specific conditions the expected behavior
20082  *		is for retries to bring a device ready, so use the flag
20083  *		with caution.
20084  *
20085  *   Arguments: un
20086  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20087  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20088  *			0: dont check for media present, do retries on cmd.
20089  *
20090  * Return Code: 0   - Success
20091  *		EIO - IO error
20092  *		EACCES - Reservation conflict detected
20093  *		ENXIO  - Not Ready, medium not present
20094  *		errno return code from sd_send_scsi_cmd()
20095  *
20096  *     Context: Can sleep. Does not return until command is completed.
20097  */
20098 
20099 static int
20100 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag)
20101 {
20102 	struct	scsi_extended_sense	sense_buf;
20103 	union scsi_cdb		cdb;
20104 	struct uscsi_cmd	ucmd_buf;
20105 	int			status;
20106 
20107 	ASSERT(un != NULL);
20108 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20109 
20110 	SD_TRACE(SD_LOG_IO, un,
20111 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20112 
20113 	/*
20114 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20115 	 * timeouts when they receive a TUR and the queue is not empty. Check
20116 	 * the configuration flag set during attach (indicating the drive has
20117 	 * this firmware bug) and un_ncmds_in_transport before issuing the
20118 	 * TUR. If there are
20119 	 * pending commands return success, this is a bit arbitrary but is ok
20120 	 * for non-removables (i.e. the eliteI disks) and non-clustering
20121 	 * configurations.
20122 	 */
20123 	if (un->un_f_cfg_tur_check == TRUE) {
20124 		mutex_enter(SD_MUTEX(un));
20125 		if (un->un_ncmds_in_transport != 0) {
20126 			mutex_exit(SD_MUTEX(un));
20127 			return (0);
20128 		}
20129 		mutex_exit(SD_MUTEX(un));
20130 	}
20131 
20132 	bzero(&cdb, sizeof (cdb));
20133 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20134 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20135 
20136 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20137 
20138 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20139 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20140 	ucmd_buf.uscsi_bufaddr	= NULL;
20141 	ucmd_buf.uscsi_buflen	= 0;
20142 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20143 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20144 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20145 
20146 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20147 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
20148 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20149 	}
20150 	ucmd_buf.uscsi_timeout	= 60;
20151 
20152 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20153 	    UIO_SYSSPACE, UIO_SYSSPACE,
20154 	    ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD));
20155 
20156 	switch (status) {
20157 	case 0:
20158 		break;	/* Success! */
20159 	case EIO:
20160 		switch (ucmd_buf.uscsi_status) {
20161 		case STATUS_RESERVATION_CONFLICT:
20162 			status = EACCES;
20163 			break;
20164 		case STATUS_CHECK:
20165 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20166 				break;
20167 			}
20168 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20169 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20170 				KEY_NOT_READY) &&
20171 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20172 				status = ENXIO;
20173 			}
20174 			break;
20175 		default:
20176 			break;
20177 		}
20178 		break;
20179 	default:
20180 		break;
20181 	}
20182 
20183 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20184 
20185 	return (status);
20186 }
20187 
20188 
20189 /*
20190  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20191  *
20192  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20193  *
20194  *   Arguments: un
20195  *
20196  * Return Code: 0   - Success
20197  *		EACCES
20198  *		ENOTSUP
20199  *		errno return code from sd_send_scsi_cmd()
20200  *
20201  *     Context: Can sleep. Does not return until command is completed.
20202  */
20203 
20204 static int
20205 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t  usr_cmd,
20206 	uint16_t data_len, uchar_t *data_bufp)
20207 {
20208 	struct scsi_extended_sense	sense_buf;
20209 	union scsi_cdb		cdb;
20210 	struct uscsi_cmd	ucmd_buf;
20211 	int			status;
20212 	int			no_caller_buf = FALSE;
20213 
20214 	ASSERT(un != NULL);
20215 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20216 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20217 
20218 	SD_TRACE(SD_LOG_IO, un,
20219 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20220 
20221 	bzero(&cdb, sizeof (cdb));
20222 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20223 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20224 	if (data_bufp == NULL) {
20225 		/* Allocate a default buf if the caller did not give one */
20226 		ASSERT(data_len == 0);
20227 		data_len  = MHIOC_RESV_KEY_SIZE;
20228 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20229 		no_caller_buf = TRUE;
20230 	}
20231 
20232 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20233 	cdb.cdb_opaque[1] = usr_cmd;
20234 	FORMG1COUNT(&cdb, data_len);
20235 
20236 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20237 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20238 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
20239 	ucmd_buf.uscsi_buflen	= data_len;
20240 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20241 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20242 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20243 	ucmd_buf.uscsi_timeout	= 60;
20244 
20245 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20246 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
20247 
20248 	switch (status) {
20249 	case 0:
20250 		break;	/* Success! */
20251 	case EIO:
20252 		switch (ucmd_buf.uscsi_status) {
20253 		case STATUS_RESERVATION_CONFLICT:
20254 			status = EACCES;
20255 			break;
20256 		case STATUS_CHECK:
20257 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20258 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20259 				KEY_ILLEGAL_REQUEST)) {
20260 				status = ENOTSUP;
20261 			}
20262 			break;
20263 		default:
20264 			break;
20265 		}
20266 		break;
20267 	default:
20268 		break;
20269 	}
20270 
20271 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20272 
20273 	if (no_caller_buf == TRUE) {
20274 		kmem_free(data_bufp, data_len);
20275 	}
20276 
20277 	return (status);
20278 }
20279 
20280 
20281 /*
20282  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20283  *
20284  * Description: This routine is the driver entry point for handling CD-ROM
20285  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20286  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20287  *		device.
20288  *
20289  *   Arguments: un  -   Pointer to soft state struct for the target.
20290  *		usr_cmd SCSI-3 reservation facility command (one of
20291  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20292  *			SD_SCSI3_PREEMPTANDABORT)
20293  *		usr_bufp - user provided pointer register, reserve descriptor or
20294  *			preempt and abort structure (mhioc_register_t,
20295  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
20296  *
20297  * Return Code: 0   - Success
20298  *		EACCES
20299  *		ENOTSUP
20300  *		errno return code from sd_send_scsi_cmd()
20301  *
20302  *     Context: Can sleep. Does not return until command is completed.
20303  */
20304 
20305 static int
20306 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd,
20307 	uchar_t	*usr_bufp)
20308 {
20309 	struct scsi_extended_sense	sense_buf;
20310 	union scsi_cdb		cdb;
20311 	struct uscsi_cmd	ucmd_buf;
20312 	int			status;
20313 	uchar_t			data_len = sizeof (sd_prout_t);
20314 	sd_prout_t		*prp;
20315 
20316 	ASSERT(un != NULL);
20317 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20318 	ASSERT(data_len == 24);	/* required by scsi spec */
20319 
20320 	SD_TRACE(SD_LOG_IO, un,
20321 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
20322 
20323 	if (usr_bufp == NULL) {
20324 		return (EINVAL);
20325 	}
20326 
20327 	bzero(&cdb, sizeof (cdb));
20328 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20329 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20330 	prp = kmem_zalloc(data_len, KM_SLEEP);
20331 
20332 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
20333 	cdb.cdb_opaque[1] = usr_cmd;
20334 	FORMG1COUNT(&cdb, data_len);
20335 
20336 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20337 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20338 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
20339 	ucmd_buf.uscsi_buflen	= data_len;
20340 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20341 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20342 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20343 	ucmd_buf.uscsi_timeout	= 60;
20344 
20345 	switch (usr_cmd) {
20346 	case SD_SCSI3_REGISTER: {
20347 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
20348 
20349 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20350 		bcopy(ptr->newkey.key, prp->service_key,
20351 		    MHIOC_RESV_KEY_SIZE);
20352 		prp->aptpl = ptr->aptpl;
20353 		break;
20354 	}
20355 	case SD_SCSI3_RESERVE:
20356 	case SD_SCSI3_RELEASE: {
20357 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20358 
20359 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20360 		prp->scope_address = BE_32(ptr->scope_specific_addr);
20361 		cdb.cdb_opaque[2] = ptr->type;
20362 		break;
20363 	}
20364 	case SD_SCSI3_PREEMPTANDABORT: {
20365 		mhioc_preemptandabort_t *ptr =
20366 		    (mhioc_preemptandabort_t *)usr_bufp;
20367 
20368 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20369 		bcopy(ptr->victim_key.key, prp->service_key,
20370 		    MHIOC_RESV_KEY_SIZE);
20371 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
20372 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
20373 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
20374 		break;
20375 	}
20376 	case SD_SCSI3_REGISTERANDIGNOREKEY:
20377 	{
20378 		mhioc_registerandignorekey_t *ptr;
20379 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
20380 		bcopy(ptr->newkey.key,
20381 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
20382 		prp->aptpl = ptr->aptpl;
20383 		break;
20384 	}
20385 	default:
20386 		ASSERT(FALSE);
20387 		break;
20388 	}
20389 
20390 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20391 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
20392 
20393 	switch (status) {
20394 	case 0:
20395 		break;	/* Success! */
20396 	case EIO:
20397 		switch (ucmd_buf.uscsi_status) {
20398 		case STATUS_RESERVATION_CONFLICT:
20399 			status = EACCES;
20400 			break;
20401 		case STATUS_CHECK:
20402 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20403 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20404 				KEY_ILLEGAL_REQUEST)) {
20405 				status = ENOTSUP;
20406 			}
20407 			break;
20408 		default:
20409 			break;
20410 		}
20411 		break;
20412 	default:
20413 		break;
20414 	}
20415 
20416 	kmem_free(prp, data_len);
20417 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
20418 	return (status);
20419 }
20420 
20421 
20422 /*
20423  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
20424  *
20425  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
20426  *
20427  *   Arguments: un - pointer to the target's soft state struct
20428  *
20429  * Return Code: 0 - success
20430  *		errno-type error code
20431  *
20432  *     Context: kernel thread context only.
20433  */
20434 
20435 static int
20436 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
20437 {
20438 	struct sd_uscsi_info	*uip;
20439 	struct uscsi_cmd	*uscmd;
20440 	union scsi_cdb		*cdb;
20441 	struct buf		*bp;
20442 	int			rval = 0;
20443 
20444 	SD_TRACE(SD_LOG_IO, un,
20445 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
20446 
20447 	ASSERT(un != NULL);
20448 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20449 
20450 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
20451 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
20452 
20453 	/*
20454 	 * First get some memory for the uscsi_cmd struct and cdb
20455 	 * and initialize for SYNCHRONIZE_CACHE cmd.
20456 	 */
20457 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
20458 	uscmd->uscsi_cdblen = CDB_GROUP1;
20459 	uscmd->uscsi_cdb = (caddr_t)cdb;
20460 	uscmd->uscsi_bufaddr = NULL;
20461 	uscmd->uscsi_buflen = 0;
20462 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
20463 	uscmd->uscsi_rqlen = SENSE_LENGTH;
20464 	uscmd->uscsi_rqresid = SENSE_LENGTH;
20465 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20466 	uscmd->uscsi_timeout = sd_io_time;
20467 
20468 	/*
20469 	 * Allocate an sd_uscsi_info struct and fill it with the info
20470 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
20471 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
20472 	 * since we allocate the buf here in this function, we do not
20473 	 * need to preserve the prior contents of b_private.
20474 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
20475 	 */
20476 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
20477 	uip->ui_flags = SD_PATH_DIRECT;
20478 	uip->ui_cmdp  = uscmd;
20479 
20480 	bp = getrbuf(KM_SLEEP);
20481 	bp->b_private = uip;
20482 
20483 	/*
20484 	 * Setup buffer to carry uscsi request.
20485 	 */
20486 	bp->b_flags  = B_BUSY;
20487 	bp->b_bcount = 0;
20488 	bp->b_blkno  = 0;
20489 
20490 	if (dkc != NULL) {
20491 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
20492 		uip->ui_dkc = *dkc;
20493 	}
20494 
20495 	bp->b_edev = SD_GET_DEV(un);
20496 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
20497 
20498 	(void) sd_uscsi_strategy(bp);
20499 
20500 	/*
20501 	 * If synchronous request, wait for completion
20502 	 * If async just return and let b_iodone callback
20503 	 * cleanup.
20504 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
20505 	 * but it was also incremented in sd_uscsi_strategy(), so
20506 	 * we should be ok.
20507 	 */
20508 	if (dkc == NULL) {
20509 		(void) biowait(bp);
20510 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
20511 	}
20512 
20513 	return (rval);
20514 }
20515 
20516 
20517 static int
20518 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
20519 {
20520 	struct sd_uscsi_info *uip;
20521 	struct uscsi_cmd *uscmd;
20522 	uint8_t *sense_buf;
20523 	struct sd_lun *un;
20524 	int status;
20525 
20526 	uip = (struct sd_uscsi_info *)(bp->b_private);
20527 	ASSERT(uip != NULL);
20528 
20529 	uscmd = uip->ui_cmdp;
20530 	ASSERT(uscmd != NULL);
20531 
20532 	sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
20533 	ASSERT(sense_buf != NULL);
20534 
20535 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
20536 	ASSERT(un != NULL);
20537 
20538 	status = geterror(bp);
20539 	switch (status) {
20540 	case 0:
20541 		break;	/* Success! */
20542 	case EIO:
20543 		switch (uscmd->uscsi_status) {
20544 		case STATUS_RESERVATION_CONFLICT:
20545 			/* Ignore reservation conflict */
20546 			status = 0;
20547 			goto done;
20548 
20549 		case STATUS_CHECK:
20550 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
20551 			    (scsi_sense_key(sense_buf) ==
20552 				KEY_ILLEGAL_REQUEST)) {
20553 				/* Ignore Illegal Request error */
20554 				mutex_enter(SD_MUTEX(un));
20555 				un->un_f_sync_cache_supported = FALSE;
20556 				mutex_exit(SD_MUTEX(un));
20557 				status = ENOTSUP;
20558 				goto done;
20559 			}
20560 			break;
20561 		default:
20562 			break;
20563 		}
20564 		/* FALLTHRU */
20565 	default:
20566 		/*
20567 		 * Don't log an error message if this device
20568 		 * has removable media.
20569 		 */
20570 		if (!un->un_f_has_removable_media) {
20571 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
20572 			    "SYNCHRONIZE CACHE command failed (%d)\n", status);
20573 		}
20574 		break;
20575 	}
20576 
20577 done:
20578 	if (uip->ui_dkc.dkc_callback != NULL) {
20579 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
20580 	}
20581 
20582 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
20583 	freerbuf(bp);
20584 	kmem_free(uip, sizeof (struct sd_uscsi_info));
20585 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
20586 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
20587 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
20588 
20589 	return (status);
20590 }
20591 
20592 
20593 /*
20594  *    Function: sd_send_scsi_GET_CONFIGURATION
20595  *
20596  * Description: Issues the get configuration command to the device.
20597  *		Called from sd_check_for_writable_cd & sd_get_media_info
20598  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
20599  *   Arguments: un
20600  *		ucmdbuf
20601  *		rqbuf
20602  *		rqbuflen
20603  *		bufaddr
20604  *		buflen
20605  *
20606  * Return Code: 0   - Success
20607  *		errno return code from sd_send_scsi_cmd()
20608  *
20609  *     Context: Can sleep. Does not return until command is completed.
20610  *
20611  */
20612 
20613 static int
20614 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf,
20615 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen)
20616 {
20617 	char	cdb[CDB_GROUP1];
20618 	int	status;
20619 
20620 	ASSERT(un != NULL);
20621 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20622 	ASSERT(bufaddr != NULL);
20623 	ASSERT(ucmdbuf != NULL);
20624 	ASSERT(rqbuf != NULL);
20625 
20626 	SD_TRACE(SD_LOG_IO, un,
20627 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
20628 
20629 	bzero(cdb, sizeof (cdb));
20630 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
20631 	bzero(rqbuf, rqbuflen);
20632 	bzero(bufaddr, buflen);
20633 
20634 	/*
20635 	 * Set up cdb field for the get configuration command.
20636 	 */
20637 	cdb[0] = SCMD_GET_CONFIGURATION;
20638 	cdb[1] = 0x02;  /* Requested Type */
20639 	cdb[8] = SD_PROFILE_HEADER_LEN;
20640 	ucmdbuf->uscsi_cdb = cdb;
20641 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
20642 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
20643 	ucmdbuf->uscsi_buflen = buflen;
20644 	ucmdbuf->uscsi_timeout = sd_io_time;
20645 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
20646 	ucmdbuf->uscsi_rqlen = rqbuflen;
20647 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
20648 
20649 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE,
20650 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
20651 
20652 	switch (status) {
20653 	case 0:
20654 		break;  /* Success! */
20655 	case EIO:
20656 		switch (ucmdbuf->uscsi_status) {
20657 		case STATUS_RESERVATION_CONFLICT:
20658 			status = EACCES;
20659 			break;
20660 		default:
20661 			break;
20662 		}
20663 		break;
20664 	default:
20665 		break;
20666 	}
20667 
20668 	if (status == 0) {
20669 		SD_DUMP_MEMORY(un, SD_LOG_IO,
20670 		    "sd_send_scsi_GET_CONFIGURATION: data",
20671 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
20672 	}
20673 
20674 	SD_TRACE(SD_LOG_IO, un,
20675 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
20676 
20677 	return (status);
20678 }
20679 
20680 /*
20681  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
20682  *
20683  * Description: Issues the get configuration command to the device to
20684  *              retrieve a specfic feature. Called from
20685  *		sd_check_for_writable_cd & sd_set_mmc_caps.
20686  *   Arguments: un
20687  *              ucmdbuf
20688  *              rqbuf
20689  *              rqbuflen
20690  *              bufaddr
20691  *              buflen
20692  *		feature
20693  *
20694  * Return Code: 0   - Success
20695  *              errno return code from sd_send_scsi_cmd()
20696  *
20697  *     Context: Can sleep. Does not return until command is completed.
20698  *
20699  */
20700 static int
20701 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
20702 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
20703 	uchar_t *bufaddr, uint_t buflen, char feature)
20704 {
20705 	char    cdb[CDB_GROUP1];
20706 	int	status;
20707 
20708 	ASSERT(un != NULL);
20709 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20710 	ASSERT(bufaddr != NULL);
20711 	ASSERT(ucmdbuf != NULL);
20712 	ASSERT(rqbuf != NULL);
20713 
20714 	SD_TRACE(SD_LOG_IO, un,
20715 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
20716 
20717 	bzero(cdb, sizeof (cdb));
20718 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
20719 	bzero(rqbuf, rqbuflen);
20720 	bzero(bufaddr, buflen);
20721 
20722 	/*
20723 	 * Set up cdb field for the get configuration command.
20724 	 */
20725 	cdb[0] = SCMD_GET_CONFIGURATION;
20726 	cdb[1] = 0x02;  /* Requested Type */
20727 	cdb[3] = feature;
20728 	cdb[8] = buflen;
20729 	ucmdbuf->uscsi_cdb = cdb;
20730 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
20731 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
20732 	ucmdbuf->uscsi_buflen = buflen;
20733 	ucmdbuf->uscsi_timeout = sd_io_time;
20734 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
20735 	ucmdbuf->uscsi_rqlen = rqbuflen;
20736 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
20737 
20738 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE,
20739 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
20740 
20741 	switch (status) {
20742 	case 0:
20743 		break;  /* Success! */
20744 	case EIO:
20745 		switch (ucmdbuf->uscsi_status) {
20746 		case STATUS_RESERVATION_CONFLICT:
20747 			status = EACCES;
20748 			break;
20749 		default:
20750 			break;
20751 		}
20752 		break;
20753 	default:
20754 		break;
20755 	}
20756 
20757 	if (status == 0) {
20758 		SD_DUMP_MEMORY(un, SD_LOG_IO,
20759 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
20760 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
20761 	}
20762 
20763 	SD_TRACE(SD_LOG_IO, un,
20764 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
20765 
20766 	return (status);
20767 }
20768 
20769 
20770 /*
20771  *    Function: sd_send_scsi_MODE_SENSE
20772  *
20773  * Description: Utility function for issuing a scsi MODE SENSE command.
20774  *		Note: This routine uses a consistent implementation for Group0,
20775  *		Group1, and Group2 commands across all platforms. ATAPI devices
20776  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
20777  *
20778  *   Arguments: un - pointer to the softstate struct for the target.
20779  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
20780  *			  CDB_GROUP[1|2] (10 byte).
20781  *		bufaddr - buffer for page data retrieved from the target.
20782  *		buflen - size of page to be retrieved.
20783  *		page_code - page code of data to be retrieved from the target.
20784  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20785  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20786  *			to use the USCSI "direct" chain and bypass the normal
20787  *			command waitq.
20788  *
20789  * Return Code: 0   - Success
20790  *		errno return code from sd_send_scsi_cmd()
20791  *
20792  *     Context: Can sleep. Does not return until command is completed.
20793  */
20794 
20795 static int
20796 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
20797 	size_t buflen,  uchar_t page_code, int path_flag)
20798 {
20799 	struct	scsi_extended_sense	sense_buf;
20800 	union scsi_cdb		cdb;
20801 	struct uscsi_cmd	ucmd_buf;
20802 	int			status;
20803 	int			headlen;
20804 
20805 	ASSERT(un != NULL);
20806 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20807 	ASSERT(bufaddr != NULL);
20808 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
20809 	    (cdbsize == CDB_GROUP2));
20810 
20811 	SD_TRACE(SD_LOG_IO, un,
20812 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
20813 
20814 	bzero(&cdb, sizeof (cdb));
20815 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20816 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20817 	bzero(bufaddr, buflen);
20818 
20819 	if (cdbsize == CDB_GROUP0) {
20820 		cdb.scc_cmd = SCMD_MODE_SENSE;
20821 		cdb.cdb_opaque[2] = page_code;
20822 		FORMG0COUNT(&cdb, buflen);
20823 		headlen = MODE_HEADER_LENGTH;
20824 	} else {
20825 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
20826 		cdb.cdb_opaque[2] = page_code;
20827 		FORMG1COUNT(&cdb, buflen);
20828 		headlen = MODE_HEADER_LENGTH_GRP2;
20829 	}
20830 
20831 	ASSERT(headlen <= buflen);
20832 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20833 
20834 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20835 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20836 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20837 	ucmd_buf.uscsi_buflen	= buflen;
20838 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20839 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20840 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20841 	ucmd_buf.uscsi_timeout	= 60;
20842 
20843 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20844 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20845 
20846 	switch (status) {
20847 	case 0:
20848 		/*
20849 		 * sr_check_wp() uses 0x3f page code and check the header of
20850 		 * mode page to determine if target device is write-protected.
20851 		 * But some USB devices return 0 bytes for 0x3f page code. For
20852 		 * this case, make sure that mode page header is returned at
20853 		 * least.
20854 		 */
20855 		if (buflen - ucmd_buf.uscsi_resid <  headlen)
20856 			status = EIO;
20857 		break;	/* Success! */
20858 	case EIO:
20859 		switch (ucmd_buf.uscsi_status) {
20860 		case STATUS_RESERVATION_CONFLICT:
20861 			status = EACCES;
20862 			break;
20863 		default:
20864 			break;
20865 		}
20866 		break;
20867 	default:
20868 		break;
20869 	}
20870 
20871 	if (status == 0) {
20872 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
20873 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20874 	}
20875 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
20876 
20877 	return (status);
20878 }
20879 
20880 
20881 /*
20882  *    Function: sd_send_scsi_MODE_SELECT
20883  *
20884  * Description: Utility function for issuing a scsi MODE SELECT command.
20885  *		Note: This routine uses a consistent implementation for Group0,
20886  *		Group1, and Group2 commands across all platforms. ATAPI devices
20887  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
20888  *
20889  *   Arguments: un - pointer to the softstate struct for the target.
20890  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
20891  *			  CDB_GROUP[1|2] (10 byte).
20892  *		bufaddr - buffer for page data retrieved from the target.
20893  *		buflen - size of page to be retrieved.
20894  *		save_page - boolean to determin if SP bit should be set.
20895  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20896  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20897  *			to use the USCSI "direct" chain and bypass the normal
20898  *			command waitq.
20899  *
20900  * Return Code: 0   - Success
20901  *		errno return code from sd_send_scsi_cmd()
20902  *
20903  *     Context: Can sleep. Does not return until command is completed.
20904  */
20905 
20906 static int
20907 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
20908 	size_t buflen,  uchar_t save_page, int path_flag)
20909 {
20910 	struct	scsi_extended_sense	sense_buf;
20911 	union scsi_cdb		cdb;
20912 	struct uscsi_cmd	ucmd_buf;
20913 	int			status;
20914 
20915 	ASSERT(un != NULL);
20916 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20917 	ASSERT(bufaddr != NULL);
20918 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
20919 	    (cdbsize == CDB_GROUP2));
20920 
20921 	SD_TRACE(SD_LOG_IO, un,
20922 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
20923 
20924 	bzero(&cdb, sizeof (cdb));
20925 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20926 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20927 
20928 	/* Set the PF bit for many third party drives */
20929 	cdb.cdb_opaque[1] = 0x10;
20930 
20931 	/* Set the savepage(SP) bit if given */
20932 	if (save_page == SD_SAVE_PAGE) {
20933 		cdb.cdb_opaque[1] |= 0x01;
20934 	}
20935 
20936 	if (cdbsize == CDB_GROUP0) {
20937 		cdb.scc_cmd = SCMD_MODE_SELECT;
20938 		FORMG0COUNT(&cdb, buflen);
20939 	} else {
20940 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
20941 		FORMG1COUNT(&cdb, buflen);
20942 	}
20943 
20944 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20945 
20946 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20947 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20948 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20949 	ucmd_buf.uscsi_buflen	= buflen;
20950 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20951 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20952 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20953 	ucmd_buf.uscsi_timeout	= 60;
20954 
20955 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20956 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20957 
20958 	switch (status) {
20959 	case 0:
20960 		break;	/* Success! */
20961 	case EIO:
20962 		switch (ucmd_buf.uscsi_status) {
20963 		case STATUS_RESERVATION_CONFLICT:
20964 			status = EACCES;
20965 			break;
20966 		default:
20967 			break;
20968 		}
20969 		break;
20970 	default:
20971 		break;
20972 	}
20973 
20974 	if (status == 0) {
20975 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
20976 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20977 	}
20978 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
20979 
20980 	return (status);
20981 }
20982 
20983 
20984 /*
20985  *    Function: sd_send_scsi_RDWR
20986  *
20987  * Description: Issue a scsi READ or WRITE command with the given parameters.
20988  *
20989  *   Arguments: un:      Pointer to the sd_lun struct for the target.
20990  *		cmd:	 SCMD_READ or SCMD_WRITE
20991  *		bufaddr: Address of caller's buffer to receive the RDWR data
20992  *		buflen:  Length of caller's buffer receive the RDWR data.
20993  *		start_block: Block number for the start of the RDWR operation.
20994  *			 (Assumes target-native block size.)
20995  *		residp:  Pointer to variable to receive the redisual of the
20996  *			 RDWR operation (may be NULL of no residual requested).
20997  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20998  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20999  *			to use the USCSI "direct" chain and bypass the normal
21000  *			command waitq.
21001  *
21002  * Return Code: 0   - Success
21003  *		errno return code from sd_send_scsi_cmd()
21004  *
21005  *     Context: Can sleep. Does not return until command is completed.
21006  */
21007 
21008 static int
21009 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
21010 	size_t buflen, daddr_t start_block, int path_flag)
21011 {
21012 	struct	scsi_extended_sense	sense_buf;
21013 	union scsi_cdb		cdb;
21014 	struct uscsi_cmd	ucmd_buf;
21015 	uint32_t		block_count;
21016 	int			status;
21017 	int			cdbsize;
21018 	uchar_t			flag;
21019 
21020 	ASSERT(un != NULL);
21021 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21022 	ASSERT(bufaddr != NULL);
21023 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21024 
21025 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21026 
21027 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21028 		return (EINVAL);
21029 	}
21030 
21031 	mutex_enter(SD_MUTEX(un));
21032 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21033 	mutex_exit(SD_MUTEX(un));
21034 
21035 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21036 
21037 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21038 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21039 	    bufaddr, buflen, start_block, block_count);
21040 
21041 	bzero(&cdb, sizeof (cdb));
21042 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21043 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21044 
21045 	/* Compute CDB size to use */
21046 	if (start_block > 0xffffffff)
21047 		cdbsize = CDB_GROUP4;
21048 	else if ((start_block & 0xFFE00000) ||
21049 	    (un->un_f_cfg_is_atapi == TRUE))
21050 		cdbsize = CDB_GROUP1;
21051 	else
21052 		cdbsize = CDB_GROUP0;
21053 
21054 	switch (cdbsize) {
21055 	case CDB_GROUP0:	/* 6-byte CDBs */
21056 		cdb.scc_cmd = cmd;
21057 		FORMG0ADDR(&cdb, start_block);
21058 		FORMG0COUNT(&cdb, block_count);
21059 		break;
21060 	case CDB_GROUP1:	/* 10-byte CDBs */
21061 		cdb.scc_cmd = cmd | SCMD_GROUP1;
21062 		FORMG1ADDR(&cdb, start_block);
21063 		FORMG1COUNT(&cdb, block_count);
21064 		break;
21065 	case CDB_GROUP4:	/* 16-byte CDBs */
21066 		cdb.scc_cmd = cmd | SCMD_GROUP4;
21067 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21068 		FORMG4COUNT(&cdb, block_count);
21069 		break;
21070 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
21071 	default:
21072 		/* All others reserved */
21073 		return (EINVAL);
21074 	}
21075 
21076 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21077 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21078 
21079 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21080 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21081 	ucmd_buf.uscsi_bufaddr	= bufaddr;
21082 	ucmd_buf.uscsi_buflen	= buflen;
21083 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21084 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21085 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
21086 	ucmd_buf.uscsi_timeout	= 60;
21087 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
21088 				UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
21089 	switch (status) {
21090 	case 0:
21091 		break;	/* Success! */
21092 	case EIO:
21093 		switch (ucmd_buf.uscsi_status) {
21094 		case STATUS_RESERVATION_CONFLICT:
21095 			status = EACCES;
21096 			break;
21097 		default:
21098 			break;
21099 		}
21100 		break;
21101 	default:
21102 		break;
21103 	}
21104 
21105 	if (status == 0) {
21106 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21107 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21108 	}
21109 
21110 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21111 
21112 	return (status);
21113 }
21114 
21115 
21116 /*
21117  *    Function: sd_send_scsi_LOG_SENSE
21118  *
21119  * Description: Issue a scsi LOG_SENSE command with the given parameters.
21120  *
21121  *   Arguments: un:      Pointer to the sd_lun struct for the target.
21122  *
21123  * Return Code: 0   - Success
21124  *		errno return code from sd_send_scsi_cmd()
21125  *
21126  *     Context: Can sleep. Does not return until command is completed.
21127  */
21128 
21129 static int
21130 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen,
21131 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
21132 	int path_flag)
21133 
21134 {
21135 	struct	scsi_extended_sense	sense_buf;
21136 	union scsi_cdb		cdb;
21137 	struct uscsi_cmd	ucmd_buf;
21138 	int			status;
21139 
21140 	ASSERT(un != NULL);
21141 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21142 
21143 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21144 
21145 	bzero(&cdb, sizeof (cdb));
21146 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21147 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21148 
21149 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21150 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21151 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21152 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
21153 	FORMG1COUNT(&cdb, buflen);
21154 
21155 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21156 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21157 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21158 	ucmd_buf.uscsi_buflen	= buflen;
21159 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21160 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21161 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21162 	ucmd_buf.uscsi_timeout	= 60;
21163 
21164 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
21165 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
21166 
21167 	switch (status) {
21168 	case 0:
21169 		break;
21170 	case EIO:
21171 		switch (ucmd_buf.uscsi_status) {
21172 		case STATUS_RESERVATION_CONFLICT:
21173 			status = EACCES;
21174 			break;
21175 		case STATUS_CHECK:
21176 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21177 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
21178 				KEY_ILLEGAL_REQUEST) &&
21179 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21180 				/*
21181 				 * ASC 0x24: INVALID FIELD IN CDB
21182 				 */
21183 				switch (page_code) {
21184 				case START_STOP_CYCLE_PAGE:
21185 					/*
21186 					 * The start stop cycle counter is
21187 					 * implemented as page 0x31 in earlier
21188 					 * generation disks. In new generation
21189 					 * disks the start stop cycle counter is
21190 					 * implemented as page 0xE. To properly
21191 					 * handle this case if an attempt for
21192 					 * log page 0xE is made and fails we
21193 					 * will try again using page 0x31.
21194 					 *
21195 					 * Network storage BU committed to
21196 					 * maintain the page 0x31 for this
21197 					 * purpose and will not have any other
21198 					 * page implemented with page code 0x31
21199 					 * until all disks transition to the
21200 					 * standard page.
21201 					 */
21202 					mutex_enter(SD_MUTEX(un));
21203 					un->un_start_stop_cycle_page =
21204 					    START_STOP_CYCLE_VU_PAGE;
21205 					cdb.cdb_opaque[2] =
21206 					    (char)(page_control << 6) |
21207 					    un->un_start_stop_cycle_page;
21208 					mutex_exit(SD_MUTEX(un));
21209 					status = sd_send_scsi_cmd(
21210 					    SD_GET_DEV(un), &ucmd_buf,
21211 					    UIO_SYSSPACE, UIO_SYSSPACE,
21212 					    UIO_SYSSPACE, path_flag);
21213 
21214 					break;
21215 				case TEMPERATURE_PAGE:
21216 					status = ENOTTY;
21217 					break;
21218 				default:
21219 					break;
21220 				}
21221 			}
21222 			break;
21223 		default:
21224 			break;
21225 		}
21226 		break;
21227 	default:
21228 		break;
21229 	}
21230 
21231 	if (status == 0) {
21232 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
21233 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21234 	}
21235 
21236 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
21237 
21238 	return (status);
21239 }
21240 
21241 
21242 /*
21243  *    Function: sdioctl
21244  *
21245  * Description: Driver's ioctl(9e) entry point function.
21246  *
21247  *   Arguments: dev     - device number
21248  *		cmd     - ioctl operation to be performed
21249  *		arg     - user argument, contains data to be set or reference
21250  *			  parameter for get
21251  *		flag    - bit flag, indicating open settings, 32/64 bit type
21252  *		cred_p  - user credential pointer
21253  *		rval_p  - calling process return value (OPT)
21254  *
21255  * Return Code: EINVAL
21256  *		ENOTTY
21257  *		ENXIO
21258  *		EIO
21259  *		EFAULT
21260  *		ENOTSUP
21261  *		EPERM
21262  *
21263  *     Context: Called from the device switch at normal priority.
21264  */
21265 
21266 static int
21267 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
21268 {
21269 	struct sd_lun	*un = NULL;
21270 	int		geom_validated = FALSE;
21271 	int		err = 0;
21272 	int		i = 0;
21273 	cred_t		*cr;
21274 
21275 	/*
21276 	 * All device accesses go thru sdstrategy where we check on suspend
21277 	 * status
21278 	 */
21279 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
21280 		return (ENXIO);
21281 	}
21282 
21283 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21284 
21285 	/*
21286 	 * Moved this wait from sd_uscsi_strategy to here for
21287 	 * reasons of deadlock prevention. Internal driver commands,
21288 	 * specifically those to change a devices power level, result
21289 	 * in a call to sd_uscsi_strategy.
21290 	 */
21291 	mutex_enter(SD_MUTEX(un));
21292 	while ((un->un_state == SD_STATE_SUSPENDED) ||
21293 	    (un->un_state == SD_STATE_PM_CHANGING)) {
21294 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
21295 	}
21296 	/*
21297 	 * Twiddling the counter here protects commands from now
21298 	 * through to the top of sd_uscsi_strategy. Without the
21299 	 * counter inc. a power down, for example, could get in
21300 	 * after the above check for state is made and before
21301 	 * execution gets to the top of sd_uscsi_strategy.
21302 	 * That would cause problems.
21303 	 */
21304 	un->un_ncmds_in_driver++;
21305 
21306 	if ((un->un_f_geometry_is_valid == FALSE) &&
21307 	    (flag & (FNDELAY | FNONBLOCK))) {
21308 		switch (cmd) {
21309 		case CDROMPAUSE:
21310 		case CDROMRESUME:
21311 		case CDROMPLAYMSF:
21312 		case CDROMPLAYTRKIND:
21313 		case CDROMREADTOCHDR:
21314 		case CDROMREADTOCENTRY:
21315 		case CDROMSTOP:
21316 		case CDROMSTART:
21317 		case CDROMVOLCTRL:
21318 		case CDROMSUBCHNL:
21319 		case CDROMREADMODE2:
21320 		case CDROMREADMODE1:
21321 		case CDROMREADOFFSET:
21322 		case CDROMSBLKMODE:
21323 		case CDROMGBLKMODE:
21324 		case CDROMGDRVSPEED:
21325 		case CDROMSDRVSPEED:
21326 		case CDROMCDDA:
21327 		case CDROMCDXA:
21328 		case CDROMSUBCODE:
21329 			if (!ISCD(un)) {
21330 				un->un_ncmds_in_driver--;
21331 				ASSERT(un->un_ncmds_in_driver >= 0);
21332 				mutex_exit(SD_MUTEX(un));
21333 				return (ENOTTY);
21334 			}
21335 			break;
21336 		case FDEJECT:
21337 		case DKIOCEJECT:
21338 		case CDROMEJECT:
21339 			if (!un->un_f_eject_media_supported) {
21340 				un->un_ncmds_in_driver--;
21341 				ASSERT(un->un_ncmds_in_driver >= 0);
21342 				mutex_exit(SD_MUTEX(un));
21343 				return (ENOTTY);
21344 			}
21345 			break;
21346 		case DKIOCSVTOC:
21347 		case DKIOCSETEFI:
21348 		case DKIOCSMBOOT:
21349 		case DKIOCFLUSHWRITECACHE:
21350 			mutex_exit(SD_MUTEX(un));
21351 			err = sd_send_scsi_TEST_UNIT_READY(un, 0);
21352 			if (err != 0) {
21353 				mutex_enter(SD_MUTEX(un));
21354 				un->un_ncmds_in_driver--;
21355 				ASSERT(un->un_ncmds_in_driver >= 0);
21356 				mutex_exit(SD_MUTEX(un));
21357 				return (EIO);
21358 			}
21359 			mutex_enter(SD_MUTEX(un));
21360 			/* FALLTHROUGH */
21361 		case DKIOCREMOVABLE:
21362 		case DKIOCHOTPLUGGABLE:
21363 		case DKIOCINFO:
21364 		case DKIOCGMEDIAINFO:
21365 		case MHIOCENFAILFAST:
21366 		case MHIOCSTATUS:
21367 		case MHIOCTKOWN:
21368 		case MHIOCRELEASE:
21369 		case MHIOCGRP_INKEYS:
21370 		case MHIOCGRP_INRESV:
21371 		case MHIOCGRP_REGISTER:
21372 		case MHIOCGRP_RESERVE:
21373 		case MHIOCGRP_PREEMPTANDABORT:
21374 		case MHIOCGRP_REGISTERANDIGNOREKEY:
21375 		case CDROMCLOSETRAY:
21376 		case USCSICMD:
21377 			goto skip_ready_valid;
21378 		default:
21379 			break;
21380 		}
21381 
21382 		mutex_exit(SD_MUTEX(un));
21383 		err = sd_ready_and_valid(un);
21384 		mutex_enter(SD_MUTEX(un));
21385 		if (err == SD_READY_NOT_VALID) {
21386 			switch (cmd) {
21387 			case DKIOCGAPART:
21388 			case DKIOCGGEOM:
21389 			case DKIOCSGEOM:
21390 			case DKIOCGVTOC:
21391 			case DKIOCSVTOC:
21392 			case DKIOCSAPART:
21393 			case DKIOCG_PHYGEOM:
21394 			case DKIOCG_VIRTGEOM:
21395 				err = ENOTSUP;
21396 				un->un_ncmds_in_driver--;
21397 				ASSERT(un->un_ncmds_in_driver >= 0);
21398 				mutex_exit(SD_MUTEX(un));
21399 				return (err);
21400 			}
21401 		}
21402 		if (err != SD_READY_VALID) {
21403 			switch (cmd) {
21404 			case DKIOCSTATE:
21405 			case CDROMGDRVSPEED:
21406 			case CDROMSDRVSPEED:
21407 			case FDEJECT:	/* for eject command */
21408 			case DKIOCEJECT:
21409 			case CDROMEJECT:
21410 			case DKIOCGETEFI:
21411 			case DKIOCSGEOM:
21412 			case DKIOCREMOVABLE:
21413 			case DKIOCHOTPLUGGABLE:
21414 			case DKIOCSAPART:
21415 			case DKIOCSETEFI:
21416 				break;
21417 			default:
21418 				if (un->un_f_has_removable_media) {
21419 					err = ENXIO;
21420 				} else {
21421 				/* Do not map SD_RESERVED_BY_OTHERS to EIO */
21422 					if (err == SD_RESERVED_BY_OTHERS) {
21423 						err = EACCES;
21424 					} else {
21425 						err = EIO;
21426 					}
21427 				}
21428 				un->un_ncmds_in_driver--;
21429 				ASSERT(un->un_ncmds_in_driver >= 0);
21430 				mutex_exit(SD_MUTEX(un));
21431 				return (err);
21432 			}
21433 		}
21434 		geom_validated = TRUE;
21435 	}
21436 	if ((un->un_f_geometry_is_valid == TRUE) &&
21437 	    (un->un_solaris_size > 0)) {
21438 		/*
21439 		 * the "geometry_is_valid" flag could be true if we
21440 		 * have an fdisk table but no Solaris partition
21441 		 */
21442 		if (un->un_vtoc.v_sanity != VTOC_SANE) {
21443 			/* it is EFI, so return ENOTSUP for these */
21444 			switch (cmd) {
21445 			case DKIOCGAPART:
21446 			case DKIOCGGEOM:
21447 			case DKIOCGVTOC:
21448 			case DKIOCSVTOC:
21449 			case DKIOCSAPART:
21450 				err = ENOTSUP;
21451 				un->un_ncmds_in_driver--;
21452 				ASSERT(un->un_ncmds_in_driver >= 0);
21453 				mutex_exit(SD_MUTEX(un));
21454 				return (err);
21455 			}
21456 		}
21457 	}
21458 
21459 skip_ready_valid:
21460 	mutex_exit(SD_MUTEX(un));
21461 
21462 	switch (cmd) {
21463 	case DKIOCINFO:
21464 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
21465 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
21466 		break;
21467 
21468 	case DKIOCGMEDIAINFO:
21469 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
21470 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
21471 		break;
21472 
21473 	case DKIOCGGEOM:
21474 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n");
21475 		err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag,
21476 		    geom_validated);
21477 		break;
21478 
21479 	case DKIOCSGEOM:
21480 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n");
21481 		err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag);
21482 		break;
21483 
21484 	case DKIOCGAPART:
21485 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n");
21486 		err = sd_dkio_get_partition(dev, (caddr_t)arg, flag,
21487 		    geom_validated);
21488 		break;
21489 
21490 	case DKIOCSAPART:
21491 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n");
21492 		err = sd_dkio_set_partition(dev, (caddr_t)arg, flag);
21493 		break;
21494 
21495 	case DKIOCGVTOC:
21496 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n");
21497 		err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag,
21498 		    geom_validated);
21499 		break;
21500 
21501 	case DKIOCGETEFI:
21502 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n");
21503 		err = sd_dkio_get_efi(dev, (caddr_t)arg, flag);
21504 		break;
21505 
21506 	case DKIOCPARTITION:
21507 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n");
21508 		err = sd_dkio_partition(dev, (caddr_t)arg, flag);
21509 		break;
21510 
21511 	case DKIOCSVTOC:
21512 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n");
21513 		err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag);
21514 		break;
21515 
21516 	case DKIOCSETEFI:
21517 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n");
21518 		err = sd_dkio_set_efi(dev, (caddr_t)arg, flag);
21519 		break;
21520 
21521 	case DKIOCGMBOOT:
21522 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n");
21523 		err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag);
21524 		break;
21525 
21526 	case DKIOCSMBOOT:
21527 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n");
21528 		err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag);
21529 		break;
21530 
21531 	case DKIOCLOCK:
21532 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
21533 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
21534 		    SD_PATH_STANDARD);
21535 		break;
21536 
21537 	case DKIOCUNLOCK:
21538 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
21539 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
21540 		    SD_PATH_STANDARD);
21541 		break;
21542 
21543 	case DKIOCSTATE: {
21544 		enum dkio_state		state;
21545 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
21546 
21547 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
21548 			err = EFAULT;
21549 		} else {
21550 			err = sd_check_media(dev, state);
21551 			if (err == 0) {
21552 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
21553 				    sizeof (int), flag) != 0)
21554 					err = EFAULT;
21555 			}
21556 		}
21557 		break;
21558 	}
21559 
21560 	case DKIOCREMOVABLE:
21561 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
21562 		/*
21563 		 * At present, vold only does automount for removable-media
21564 		 * devices, in order not to break current applications, we
21565 		 * still let hopluggable devices pretend to be removable media
21566 		 * devices for vold. In the near future, once vold is EOL'ed,
21567 		 * we should remove this workaround.
21568 		 */
21569 		if (un->un_f_has_removable_media || un->un_f_is_hotpluggable) {
21570 			i = 1;
21571 		} else {
21572 			i = 0;
21573 		}
21574 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
21575 			err = EFAULT;
21576 		} else {
21577 			err = 0;
21578 		}
21579 		break;
21580 
21581 	case DKIOCHOTPLUGGABLE:
21582 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
21583 		if (un->un_f_is_hotpluggable) {
21584 			i = 1;
21585 		} else {
21586 			i = 0;
21587 		}
21588 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
21589 			err = EFAULT;
21590 		} else {
21591 			err = 0;
21592 		}
21593 		break;
21594 
21595 	case DKIOCGTEMPERATURE:
21596 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
21597 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
21598 		break;
21599 
21600 	case MHIOCENFAILFAST:
21601 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
21602 		if ((err = drv_priv(cred_p)) == 0) {
21603 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
21604 		}
21605 		break;
21606 
21607 	case MHIOCTKOWN:
21608 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
21609 		if ((err = drv_priv(cred_p)) == 0) {
21610 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
21611 		}
21612 		break;
21613 
21614 	case MHIOCRELEASE:
21615 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
21616 		if ((err = drv_priv(cred_p)) == 0) {
21617 			err = sd_mhdioc_release(dev);
21618 		}
21619 		break;
21620 
21621 	case MHIOCSTATUS:
21622 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
21623 		if ((err = drv_priv(cred_p)) == 0) {
21624 			switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) {
21625 			case 0:
21626 				err = 0;
21627 				break;
21628 			case EACCES:
21629 				*rval_p = 1;
21630 				err = 0;
21631 				break;
21632 			default:
21633 				err = EIO;
21634 				break;
21635 			}
21636 		}
21637 		break;
21638 
21639 	case MHIOCQRESERVE:
21640 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
21641 		if ((err = drv_priv(cred_p)) == 0) {
21642 			err = sd_reserve_release(dev, SD_RESERVE);
21643 		}
21644 		break;
21645 
21646 	case MHIOCREREGISTERDEVID:
21647 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
21648 		if (drv_priv(cred_p) == EPERM) {
21649 			err = EPERM;
21650 		} else if (!un->un_f_devid_supported) {
21651 			err = ENOTTY;
21652 		} else {
21653 			err = sd_mhdioc_register_devid(dev);
21654 		}
21655 		break;
21656 
21657 	case MHIOCGRP_INKEYS:
21658 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
21659 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
21660 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21661 				err = ENOTSUP;
21662 			} else {
21663 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
21664 				    flag);
21665 			}
21666 		}
21667 		break;
21668 
21669 	case MHIOCGRP_INRESV:
21670 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
21671 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
21672 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21673 				err = ENOTSUP;
21674 			} else {
21675 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
21676 			}
21677 		}
21678 		break;
21679 
21680 	case MHIOCGRP_REGISTER:
21681 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
21682 		if ((err = drv_priv(cred_p)) != EPERM) {
21683 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21684 				err = ENOTSUP;
21685 			} else if (arg != NULL) {
21686 				mhioc_register_t reg;
21687 				if (ddi_copyin((void *)arg, &reg,
21688 				    sizeof (mhioc_register_t), flag) != 0) {
21689 					err = EFAULT;
21690 				} else {
21691 					err =
21692 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21693 					    un, SD_SCSI3_REGISTER,
21694 					    (uchar_t *)&reg);
21695 				}
21696 			}
21697 		}
21698 		break;
21699 
21700 	case MHIOCGRP_RESERVE:
21701 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
21702 		if ((err = drv_priv(cred_p)) != EPERM) {
21703 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21704 				err = ENOTSUP;
21705 			} else if (arg != NULL) {
21706 				mhioc_resv_desc_t resv_desc;
21707 				if (ddi_copyin((void *)arg, &resv_desc,
21708 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
21709 					err = EFAULT;
21710 				} else {
21711 					err =
21712 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21713 					    un, SD_SCSI3_RESERVE,
21714 					    (uchar_t *)&resv_desc);
21715 				}
21716 			}
21717 		}
21718 		break;
21719 
21720 	case MHIOCGRP_PREEMPTANDABORT:
21721 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
21722 		if ((err = drv_priv(cred_p)) != EPERM) {
21723 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21724 				err = ENOTSUP;
21725 			} else if (arg != NULL) {
21726 				mhioc_preemptandabort_t preempt_abort;
21727 				if (ddi_copyin((void *)arg, &preempt_abort,
21728 				    sizeof (mhioc_preemptandabort_t),
21729 				    flag) != 0) {
21730 					err = EFAULT;
21731 				} else {
21732 					err =
21733 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21734 					    un, SD_SCSI3_PREEMPTANDABORT,
21735 					    (uchar_t *)&preempt_abort);
21736 				}
21737 			}
21738 		}
21739 		break;
21740 
21741 	case MHIOCGRP_REGISTERANDIGNOREKEY:
21742 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
21743 		if ((err = drv_priv(cred_p)) != EPERM) {
21744 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21745 				err = ENOTSUP;
21746 			} else if (arg != NULL) {
21747 				mhioc_registerandignorekey_t r_and_i;
21748 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
21749 				    sizeof (mhioc_registerandignorekey_t),
21750 				    flag) != 0) {
21751 					err = EFAULT;
21752 				} else {
21753 					err =
21754 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21755 					    un, SD_SCSI3_REGISTERANDIGNOREKEY,
21756 					    (uchar_t *)&r_and_i);
21757 				}
21758 			}
21759 		}
21760 		break;
21761 
21762 	case USCSICMD:
21763 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
21764 		cr = ddi_get_cred();
21765 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
21766 			err = EPERM;
21767 		} else {
21768 			err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag);
21769 		}
21770 		break;
21771 
21772 	case CDROMPAUSE:
21773 	case CDROMRESUME:
21774 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
21775 		if (!ISCD(un)) {
21776 			err = ENOTTY;
21777 		} else {
21778 			err = sr_pause_resume(dev, cmd);
21779 		}
21780 		break;
21781 
21782 	case CDROMPLAYMSF:
21783 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
21784 		if (!ISCD(un)) {
21785 			err = ENOTTY;
21786 		} else {
21787 			err = sr_play_msf(dev, (caddr_t)arg, flag);
21788 		}
21789 		break;
21790 
21791 	case CDROMPLAYTRKIND:
21792 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
21793 #if defined(__i386) || defined(__amd64)
21794 		/*
21795 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
21796 		 */
21797 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
21798 #else
21799 		if (!ISCD(un)) {
21800 #endif
21801 			err = ENOTTY;
21802 		} else {
21803 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
21804 		}
21805 		break;
21806 
21807 	case CDROMREADTOCHDR:
21808 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
21809 		if (!ISCD(un)) {
21810 			err = ENOTTY;
21811 		} else {
21812 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
21813 		}
21814 		break;
21815 
21816 	case CDROMREADTOCENTRY:
21817 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
21818 		if (!ISCD(un)) {
21819 			err = ENOTTY;
21820 		} else {
21821 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
21822 		}
21823 		break;
21824 
21825 	case CDROMSTOP:
21826 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
21827 		if (!ISCD(un)) {
21828 			err = ENOTTY;
21829 		} else {
21830 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP,
21831 			    SD_PATH_STANDARD);
21832 		}
21833 		break;
21834 
21835 	case CDROMSTART:
21836 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
21837 		if (!ISCD(un)) {
21838 			err = ENOTTY;
21839 		} else {
21840 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
21841 			    SD_PATH_STANDARD);
21842 		}
21843 		break;
21844 
21845 	case CDROMCLOSETRAY:
21846 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
21847 		if (!ISCD(un)) {
21848 			err = ENOTTY;
21849 		} else {
21850 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE,
21851 			    SD_PATH_STANDARD);
21852 		}
21853 		break;
21854 
21855 	case FDEJECT:	/* for eject command */
21856 	case DKIOCEJECT:
21857 	case CDROMEJECT:
21858 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
21859 		if (!un->un_f_eject_media_supported) {
21860 			err = ENOTTY;
21861 		} else {
21862 			err = sr_eject(dev);
21863 		}
21864 		break;
21865 
21866 	case CDROMVOLCTRL:
21867 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
21868 		if (!ISCD(un)) {
21869 			err = ENOTTY;
21870 		} else {
21871 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
21872 		}
21873 		break;
21874 
21875 	case CDROMSUBCHNL:
21876 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
21877 		if (!ISCD(un)) {
21878 			err = ENOTTY;
21879 		} else {
21880 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
21881 		}
21882 		break;
21883 
21884 	case CDROMREADMODE2:
21885 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
21886 		if (!ISCD(un)) {
21887 			err = ENOTTY;
21888 		} else if (un->un_f_cfg_is_atapi == TRUE) {
21889 			/*
21890 			 * If the drive supports READ CD, use that instead of
21891 			 * switching the LBA size via a MODE SELECT
21892 			 * Block Descriptor
21893 			 */
21894 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
21895 		} else {
21896 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
21897 		}
21898 		break;
21899 
21900 	case CDROMREADMODE1:
21901 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
21902 		if (!ISCD(un)) {
21903 			err = ENOTTY;
21904 		} else {
21905 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
21906 		}
21907 		break;
21908 
21909 	case CDROMREADOFFSET:
21910 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
21911 		if (!ISCD(un)) {
21912 			err = ENOTTY;
21913 		} else {
21914 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
21915 			    flag);
21916 		}
21917 		break;
21918 
21919 	case CDROMSBLKMODE:
21920 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
21921 		/*
21922 		 * There is no means of changing block size in case of atapi
21923 		 * drives, thus return ENOTTY if drive type is atapi
21924 		 */
21925 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
21926 			err = ENOTTY;
21927 		} else if (un->un_f_mmc_cap == TRUE) {
21928 
21929 			/*
21930 			 * MMC Devices do not support changing the
21931 			 * logical block size
21932 			 *
21933 			 * Note: EINVAL is being returned instead of ENOTTY to
21934 			 * maintain consistancy with the original mmc
21935 			 * driver update.
21936 			 */
21937 			err = EINVAL;
21938 		} else {
21939 			mutex_enter(SD_MUTEX(un));
21940 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
21941 			    (un->un_ncmds_in_transport > 0)) {
21942 				mutex_exit(SD_MUTEX(un));
21943 				err = EINVAL;
21944 			} else {
21945 				mutex_exit(SD_MUTEX(un));
21946 				err = sr_change_blkmode(dev, cmd, arg, flag);
21947 			}
21948 		}
21949 		break;
21950 
21951 	case CDROMGBLKMODE:
21952 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
21953 		if (!ISCD(un)) {
21954 			err = ENOTTY;
21955 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
21956 		    (un->un_f_blockcount_is_valid != FALSE)) {
21957 			/*
21958 			 * Drive is an ATAPI drive so return target block
21959 			 * size for ATAPI drives since we cannot change the
21960 			 * blocksize on ATAPI drives. Used primarily to detect
21961 			 * if an ATAPI cdrom is present.
21962 			 */
21963 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
21964 			    sizeof (int), flag) != 0) {
21965 				err = EFAULT;
21966 			} else {
21967 				err = 0;
21968 			}
21969 
21970 		} else {
21971 			/*
21972 			 * Drive supports changing block sizes via a Mode
21973 			 * Select.
21974 			 */
21975 			err = sr_change_blkmode(dev, cmd, arg, flag);
21976 		}
21977 		break;
21978 
21979 	case CDROMGDRVSPEED:
21980 	case CDROMSDRVSPEED:
21981 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
21982 		if (!ISCD(un)) {
21983 			err = ENOTTY;
21984 		} else if (un->un_f_mmc_cap == TRUE) {
21985 			/*
21986 			 * Note: In the future the driver implementation
21987 			 * for getting and
21988 			 * setting cd speed should entail:
21989 			 * 1) If non-mmc try the Toshiba mode page
21990 			 *    (sr_change_speed)
21991 			 * 2) If mmc but no support for Real Time Streaming try
21992 			 *    the SET CD SPEED (0xBB) command
21993 			 *   (sr_atapi_change_speed)
21994 			 * 3) If mmc and support for Real Time Streaming
21995 			 *    try the GET PERFORMANCE and SET STREAMING
21996 			 *    commands (not yet implemented, 4380808)
21997 			 */
21998 			/*
21999 			 * As per recent MMC spec, CD-ROM speed is variable
22000 			 * and changes with LBA. Since there is no such
22001 			 * things as drive speed now, fail this ioctl.
22002 			 *
22003 			 * Note: EINVAL is returned for consistancy of original
22004 			 * implementation which included support for getting
22005 			 * the drive speed of mmc devices but not setting
22006 			 * the drive speed. Thus EINVAL would be returned
22007 			 * if a set request was made for an mmc device.
22008 			 * We no longer support get or set speed for
22009 			 * mmc but need to remain consistant with regard
22010 			 * to the error code returned.
22011 			 */
22012 			err = EINVAL;
22013 		} else if (un->un_f_cfg_is_atapi == TRUE) {
22014 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
22015 		} else {
22016 			err = sr_change_speed(dev, cmd, arg, flag);
22017 		}
22018 		break;
22019 
22020 	case CDROMCDDA:
22021 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
22022 		if (!ISCD(un)) {
22023 			err = ENOTTY;
22024 		} else {
22025 			err = sr_read_cdda(dev, (void *)arg, flag);
22026 		}
22027 		break;
22028 
22029 	case CDROMCDXA:
22030 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
22031 		if (!ISCD(un)) {
22032 			err = ENOTTY;
22033 		} else {
22034 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
22035 		}
22036 		break;
22037 
22038 	case CDROMSUBCODE:
22039 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
22040 		if (!ISCD(un)) {
22041 			err = ENOTTY;
22042 		} else {
22043 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
22044 		}
22045 		break;
22046 
22047 	case DKIOCPARTINFO: {
22048 		/*
22049 		 * Return parameters describing the selected disk slice.
22050 		 * Note: this ioctl is for the intel platform only
22051 		 */
22052 #if defined(__i386) || defined(__amd64)
22053 		int part;
22054 
22055 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
22056 		part = SDPART(dev);
22057 
22058 		/* don't check un_solaris_size for pN */
22059 		if (part < P0_RAW_DISK && un->un_solaris_size == 0) {
22060 			err = EIO;
22061 		} else {
22062 			struct part_info p;
22063 
22064 			p.p_start = (daddr_t)un->un_offset[part];
22065 			p.p_length = (int)un->un_map[part].dkl_nblk;
22066 #ifdef _MULTI_DATAMODEL
22067 			switch (ddi_model_convert_from(flag & FMODELS)) {
22068 			case DDI_MODEL_ILP32:
22069 			{
22070 				struct part_info32 p32;
22071 
22072 				p32.p_start = (daddr32_t)p.p_start;
22073 				p32.p_length = p.p_length;
22074 				if (ddi_copyout(&p32, (void *)arg,
22075 				    sizeof (p32), flag))
22076 					err = EFAULT;
22077 				break;
22078 			}
22079 
22080 			case DDI_MODEL_NONE:
22081 			{
22082 				if (ddi_copyout(&p, (void *)arg, sizeof (p),
22083 				    flag))
22084 					err = EFAULT;
22085 				break;
22086 			}
22087 			}
22088 #else /* ! _MULTI_DATAMODEL */
22089 			if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
22090 				err = EFAULT;
22091 #endif /* _MULTI_DATAMODEL */
22092 		}
22093 #else
22094 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
22095 		err = ENOTTY;
22096 #endif
22097 		break;
22098 	}
22099 
22100 	case DKIOCG_PHYGEOM: {
22101 		/* Return the driver's notion of the media physical geometry */
22102 #if defined(__i386) || defined(__amd64)
22103 		uint64_t	capacity;
22104 		struct dk_geom	disk_geom;
22105 		struct dk_geom	*dkgp = &disk_geom;
22106 
22107 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
22108 		mutex_enter(SD_MUTEX(un));
22109 
22110 		if (un->un_g.dkg_nhead != 0 &&
22111 		    un->un_g.dkg_nsect != 0) {
22112 			/*
22113 			 * We succeeded in getting a geometry, but
22114 			 * right now it is being reported as just the
22115 			 * Solaris fdisk partition, just like for
22116 			 * DKIOCGGEOM. We need to change that to be
22117 			 * correct for the entire disk now.
22118 			 */
22119 			bcopy(&un->un_g, dkgp, sizeof (*dkgp));
22120 			dkgp->dkg_acyl = 0;
22121 			dkgp->dkg_ncyl = un->un_blockcount /
22122 			    (dkgp->dkg_nhead * dkgp->dkg_nsect);
22123 		} else {
22124 			bzero(dkgp, sizeof (struct dk_geom));
22125 			/*
22126 			 * This disk does not have a Solaris VTOC
22127 			 * so we must present a physical geometry
22128 			 * that will remain consistent regardless
22129 			 * of how the disk is used. This will ensure
22130 			 * that the geometry does not change regardless
22131 			 * of the fdisk partition type (ie. EFI, FAT32,
22132 			 * Solaris, etc).
22133 			 */
22134 			if (ISCD(un)) {
22135 				dkgp->dkg_nhead = un->un_pgeom.g_nhead;
22136 				dkgp->dkg_nsect = un->un_pgeom.g_nsect;
22137 				dkgp->dkg_ncyl = un->un_pgeom.g_ncyl;
22138 				dkgp->dkg_acyl = un->un_pgeom.g_acyl;
22139 			} else {
22140 				/*
22141 				 * Invalid un_blockcount can generate invalid
22142 				 * dk_geom and may result in division by zero
22143 				 * system failure. Should make sure blockcount
22144 				 * is valid before using it here.
22145 				 */
22146 				if (un->un_f_blockcount_is_valid == FALSE) {
22147 					mutex_exit(SD_MUTEX(un));
22148 					err = EIO;
22149 
22150 					break;
22151 				}
22152 
22153 				/*
22154 				 * Refer to comments related to off-by-1 at the
22155 				 * header of this file
22156 				 */
22157 				if (!un->un_f_capacity_adjusted &&
22158 					!un->un_f_has_removable_media &&
22159 				    !un->un_f_is_hotpluggable &&
22160 					(un->un_tgt_blocksize ==
22161 					un->un_sys_blocksize))
22162 					capacity = un->un_blockcount - 1;
22163 				else
22164 					capacity = un->un_blockcount;
22165 
22166 				sd_convert_geometry(capacity, dkgp);
22167 				dkgp->dkg_acyl = 0;
22168 				dkgp->dkg_ncyl = capacity /
22169 				    (dkgp->dkg_nhead * dkgp->dkg_nsect);
22170 			}
22171 		}
22172 		dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
22173 
22174 		if (ddi_copyout(dkgp, (void *)arg,
22175 		    sizeof (struct dk_geom), flag)) {
22176 			mutex_exit(SD_MUTEX(un));
22177 			err = EFAULT;
22178 		} else {
22179 			mutex_exit(SD_MUTEX(un));
22180 			err = 0;
22181 		}
22182 #else
22183 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
22184 		err = ENOTTY;
22185 #endif
22186 		break;
22187 	}
22188 
22189 	case DKIOCG_VIRTGEOM: {
22190 		/* Return the driver's notion of the media's logical geometry */
22191 #if defined(__i386) || defined(__amd64)
22192 		struct dk_geom	disk_geom;
22193 		struct dk_geom	*dkgp = &disk_geom;
22194 
22195 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
22196 		mutex_enter(SD_MUTEX(un));
22197 		/*
22198 		 * If there is no HBA geometry available, or
22199 		 * if the HBA returned us something that doesn't
22200 		 * really fit into an Int 13/function 8 geometry
22201 		 * result, just fail the ioctl.  See PSARC 1998/313.
22202 		 */
22203 		if (un->un_lgeom.g_nhead == 0 ||
22204 		    un->un_lgeom.g_nsect == 0 ||
22205 		    un->un_lgeom.g_ncyl > 1024) {
22206 			mutex_exit(SD_MUTEX(un));
22207 			err = EINVAL;
22208 		} else {
22209 			dkgp->dkg_ncyl	= un->un_lgeom.g_ncyl;
22210 			dkgp->dkg_acyl	= un->un_lgeom.g_acyl;
22211 			dkgp->dkg_pcyl	= dkgp->dkg_ncyl + dkgp->dkg_acyl;
22212 			dkgp->dkg_nhead	= un->un_lgeom.g_nhead;
22213 			dkgp->dkg_nsect	= un->un_lgeom.g_nsect;
22214 
22215 			if (ddi_copyout(dkgp, (void *)arg,
22216 			    sizeof (struct dk_geom), flag)) {
22217 				mutex_exit(SD_MUTEX(un));
22218 				err = EFAULT;
22219 			} else {
22220 				mutex_exit(SD_MUTEX(un));
22221 				err = 0;
22222 			}
22223 		}
22224 #else
22225 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
22226 		err = ENOTTY;
22227 #endif
22228 		break;
22229 	}
22230 #ifdef SDDEBUG
22231 /* RESET/ABORTS testing ioctls */
22232 	case DKIOCRESET: {
22233 		int	reset_level;
22234 
22235 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
22236 			err = EFAULT;
22237 		} else {
22238 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
22239 			    "reset_level = 0x%lx\n", reset_level);
22240 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
22241 				err = 0;
22242 			} else {
22243 				err = EIO;
22244 			}
22245 		}
22246 		break;
22247 	}
22248 
22249 	case DKIOCABORT:
22250 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
22251 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
22252 			err = 0;
22253 		} else {
22254 			err = EIO;
22255 		}
22256 		break;
22257 #endif
22258 
22259 #ifdef SD_FAULT_INJECTION
22260 /* SDIOC FaultInjection testing ioctls */
22261 	case SDIOCSTART:
22262 	case SDIOCSTOP:
22263 	case SDIOCINSERTPKT:
22264 	case SDIOCINSERTXB:
22265 	case SDIOCINSERTUN:
22266 	case SDIOCINSERTARQ:
22267 	case SDIOCPUSH:
22268 	case SDIOCRETRIEVE:
22269 	case SDIOCRUN:
22270 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
22271 		    "SDIOC detected cmd:0x%X:\n", cmd);
22272 		/* call error generator */
22273 		sd_faultinjection_ioctl(cmd, arg, un);
22274 		err = 0;
22275 		break;
22276 
22277 #endif /* SD_FAULT_INJECTION */
22278 
22279 	case DKIOCFLUSHWRITECACHE:
22280 		{
22281 			struct dk_callback *dkc = (struct dk_callback *)arg;
22282 
22283 			mutex_enter(SD_MUTEX(un));
22284 			if (!un->un_f_sync_cache_supported ||
22285 			    !un->un_f_write_cache_enabled) {
22286 				err = un->un_f_sync_cache_supported ?
22287 					0 : ENOTSUP;
22288 				mutex_exit(SD_MUTEX(un));
22289 				if ((flag & FKIOCTL) && dkc != NULL &&
22290 				    dkc->dkc_callback != NULL) {
22291 					(*dkc->dkc_callback)(dkc->dkc_cookie,
22292 					    err);
22293 					/*
22294 					 * Did callback and reported error.
22295 					 * Since we did a callback, ioctl
22296 					 * should return 0.
22297 					 */
22298 					err = 0;
22299 				}
22300 				break;
22301 			}
22302 			mutex_exit(SD_MUTEX(un));
22303 
22304 			if ((flag & FKIOCTL) && dkc != NULL &&
22305 			    dkc->dkc_callback != NULL) {
22306 				/* async SYNC CACHE request */
22307 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
22308 			} else {
22309 				/* synchronous SYNC CACHE request */
22310 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
22311 			}
22312 		}
22313 		break;
22314 
22315 	case DKIOCGETWCE: {
22316 
22317 		int wce;
22318 
22319 		if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) {
22320 			break;
22321 		}
22322 
22323 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
22324 			err = EFAULT;
22325 		}
22326 		break;
22327 	}
22328 
22329 	case DKIOCSETWCE: {
22330 
22331 		int wce, sync_supported;
22332 
22333 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
22334 			err = EFAULT;
22335 			break;
22336 		}
22337 
22338 		/*
22339 		 * Synchronize multiple threads trying to enable
22340 		 * or disable the cache via the un_f_wcc_cv
22341 		 * condition variable.
22342 		 */
22343 		mutex_enter(SD_MUTEX(un));
22344 
22345 		/*
22346 		 * Don't allow the cache to be enabled if the
22347 		 * config file has it disabled.
22348 		 */
22349 		if (un->un_f_opt_disable_cache && wce) {
22350 			mutex_exit(SD_MUTEX(un));
22351 			err = EINVAL;
22352 			break;
22353 		}
22354 
22355 		/*
22356 		 * Wait for write cache change in progress
22357 		 * bit to be clear before proceeding.
22358 		 */
22359 		while (un->un_f_wcc_inprog)
22360 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
22361 
22362 		un->un_f_wcc_inprog = 1;
22363 
22364 		if (un->un_f_write_cache_enabled && wce == 0) {
22365 			/*
22366 			 * Disable the write cache.  Don't clear
22367 			 * un_f_write_cache_enabled until after
22368 			 * the mode select and flush are complete.
22369 			 */
22370 			sync_supported = un->un_f_sync_cache_supported;
22371 			mutex_exit(SD_MUTEX(un));
22372 			if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE,
22373 			    SD_CACHE_DISABLE)) == 0 && sync_supported) {
22374 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
22375 			}
22376 
22377 			mutex_enter(SD_MUTEX(un));
22378 			if (err == 0) {
22379 				un->un_f_write_cache_enabled = 0;
22380 			}
22381 
22382 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
22383 			/*
22384 			 * Set un_f_write_cache_enabled first, so there is
22385 			 * no window where the cache is enabled, but the
22386 			 * bit says it isn't.
22387 			 */
22388 			un->un_f_write_cache_enabled = 1;
22389 			mutex_exit(SD_MUTEX(un));
22390 
22391 			err = sd_cache_control(un, SD_CACHE_NOCHANGE,
22392 				SD_CACHE_ENABLE);
22393 
22394 			mutex_enter(SD_MUTEX(un));
22395 
22396 			if (err) {
22397 				un->un_f_write_cache_enabled = 0;
22398 			}
22399 		}
22400 
22401 		un->un_f_wcc_inprog = 0;
22402 		cv_broadcast(&un->un_wcc_cv);
22403 		mutex_exit(SD_MUTEX(un));
22404 		break;
22405 	}
22406 
22407 	default:
22408 		err = ENOTTY;
22409 		break;
22410 	}
22411 	mutex_enter(SD_MUTEX(un));
22412 	un->un_ncmds_in_driver--;
22413 	ASSERT(un->un_ncmds_in_driver >= 0);
22414 	mutex_exit(SD_MUTEX(un));
22415 
22416 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
22417 	return (err);
22418 }
22419 
22420 
22421 /*
22422  *    Function: sd_uscsi_ioctl
22423  *
22424  * Description: This routine is the driver entry point for handling USCSI ioctl
22425  *		requests (USCSICMD).
22426  *
22427  *   Arguments: dev	- the device number
22428  *		arg	- user provided scsi command
22429  *		flag	- this argument is a pass through to ddi_copyxxx()
22430  *			  directly from the mode argument of ioctl().
22431  *
22432  * Return Code: code returned by sd_send_scsi_cmd
22433  *		ENXIO
22434  *		EFAULT
22435  *		EAGAIN
22436  */
22437 
22438 static int
22439 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag)
22440 {
22441 #ifdef _MULTI_DATAMODEL
22442 	/*
22443 	 * For use when a 32 bit app makes a call into a
22444 	 * 64 bit ioctl
22445 	 */
22446 	struct uscsi_cmd32	uscsi_cmd_32_for_64;
22447 	struct uscsi_cmd32	*ucmd32 = &uscsi_cmd_32_for_64;
22448 	model_t			model;
22449 #endif /* _MULTI_DATAMODEL */
22450 	struct uscsi_cmd	*scmd = NULL;
22451 	struct sd_lun		*un = NULL;
22452 	enum uio_seg		uioseg;
22453 	char			cdb[CDB_GROUP0];
22454 	int			rval = 0;
22455 
22456 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22457 		return (ENXIO);
22458 	}
22459 
22460 	SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un);
22461 
22462 	scmd = (struct uscsi_cmd *)
22463 	    kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
22464 
22465 #ifdef _MULTI_DATAMODEL
22466 	switch (model = ddi_model_convert_from(flag & FMODELS)) {
22467 	case DDI_MODEL_ILP32:
22468 	{
22469 		if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) {
22470 			rval = EFAULT;
22471 			goto done;
22472 		}
22473 		/*
22474 		 * Convert the ILP32 uscsi data from the
22475 		 * application to LP64 for internal use.
22476 		 */
22477 		uscsi_cmd32touscsi_cmd(ucmd32, scmd);
22478 		break;
22479 	}
22480 	case DDI_MODEL_NONE:
22481 		if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) {
22482 			rval = EFAULT;
22483 			goto done;
22484 		}
22485 		break;
22486 	}
22487 #else /* ! _MULTI_DATAMODEL */
22488 	if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) {
22489 		rval = EFAULT;
22490 		goto done;
22491 	}
22492 #endif /* _MULTI_DATAMODEL */
22493 
22494 	scmd->uscsi_flags &= ~USCSI_NOINTR;
22495 	uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE;
22496 	if (un->un_f_format_in_progress == TRUE) {
22497 		rval = EAGAIN;
22498 		goto done;
22499 	}
22500 
22501 	/*
22502 	 * Gotta do the ddi_copyin() here on the uscsi_cdb so that
22503 	 * we will have a valid cdb[0] to test.
22504 	 */
22505 	if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) &&
22506 	    (cdb[0] == SCMD_FORMAT)) {
22507 		SD_TRACE(SD_LOG_IOCTL, un,
22508 		    "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]);
22509 		mutex_enter(SD_MUTEX(un));
22510 		un->un_f_format_in_progress = TRUE;
22511 		mutex_exit(SD_MUTEX(un));
22512 		rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg,
22513 		    SD_PATH_STANDARD);
22514 		mutex_enter(SD_MUTEX(un));
22515 		un->un_f_format_in_progress = FALSE;
22516 		mutex_exit(SD_MUTEX(un));
22517 	} else {
22518 		SD_TRACE(SD_LOG_IOCTL, un,
22519 		    "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]);
22520 		/*
22521 		 * It's OK to fall into here even if the ddi_copyin()
22522 		 * on the uscsi_cdb above fails, because sd_send_scsi_cmd()
22523 		 * does this same copyin and will return the EFAULT
22524 		 * if it fails.
22525 		 */
22526 		rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg,
22527 		    SD_PATH_STANDARD);
22528 	}
22529 #ifdef _MULTI_DATAMODEL
22530 	switch (model) {
22531 	case DDI_MODEL_ILP32:
22532 		/*
22533 		 * Convert back to ILP32 before copyout to the
22534 		 * application
22535 		 */
22536 		uscsi_cmdtouscsi_cmd32(scmd, ucmd32);
22537 		if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) {
22538 			if (rval != 0) {
22539 				rval = EFAULT;
22540 			}
22541 		}
22542 		break;
22543 	case DDI_MODEL_NONE:
22544 		if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) {
22545 			if (rval != 0) {
22546 				rval = EFAULT;
22547 			}
22548 		}
22549 		break;
22550 	}
22551 #else /* ! _MULTI_DATAMODE */
22552 	if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) {
22553 		if (rval != 0) {
22554 			rval = EFAULT;
22555 		}
22556 	}
22557 #endif /* _MULTI_DATAMODE */
22558 done:
22559 	kmem_free(scmd, sizeof (struct uscsi_cmd));
22560 
22561 	SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un);
22562 
22563 	return (rval);
22564 }
22565 
22566 
22567 /*
22568  *    Function: sd_dkio_ctrl_info
22569  *
22570  * Description: This routine is the driver entry point for handling controller
22571  *		information ioctl requests (DKIOCINFO).
22572  *
22573  *   Arguments: dev  - the device number
22574  *		arg  - pointer to user provided dk_cinfo structure
22575  *		       specifying the controller type and attributes.
22576  *		flag - this argument is a pass through to ddi_copyxxx()
22577  *		       directly from the mode argument of ioctl().
22578  *
22579  * Return Code: 0
22580  *		EFAULT
22581  *		ENXIO
22582  */
22583 
22584 static int
22585 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
22586 {
22587 	struct sd_lun	*un = NULL;
22588 	struct dk_cinfo	*info;
22589 	dev_info_t	*pdip;
22590 	int		lun, tgt;
22591 
22592 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22593 		return (ENXIO);
22594 	}
22595 
22596 	info = (struct dk_cinfo *)
22597 		kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
22598 
22599 	switch (un->un_ctype) {
22600 	case CTYPE_CDROM:
22601 		info->dki_ctype = DKC_CDROM;
22602 		break;
22603 	default:
22604 		info->dki_ctype = DKC_SCSI_CCS;
22605 		break;
22606 	}
22607 	pdip = ddi_get_parent(SD_DEVINFO(un));
22608 	info->dki_cnum = ddi_get_instance(pdip);
22609 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
22610 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
22611 	} else {
22612 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
22613 		    DK_DEVLEN - 1);
22614 	}
22615 
22616 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
22617 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
22618 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
22619 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
22620 
22621 	/* Unit Information */
22622 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
22623 	info->dki_slave = ((tgt << 3) | lun);
22624 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
22625 	    DK_DEVLEN - 1);
22626 	info->dki_flags = DKI_FMTVOL;
22627 	info->dki_partition = SDPART(dev);
22628 
22629 	/* Max Transfer size of this device in blocks */
22630 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
22631 	info->dki_addr = 0;
22632 	info->dki_space = 0;
22633 	info->dki_prio = 0;
22634 	info->dki_vec = 0;
22635 
22636 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
22637 		kmem_free(info, sizeof (struct dk_cinfo));
22638 		return (EFAULT);
22639 	} else {
22640 		kmem_free(info, sizeof (struct dk_cinfo));
22641 		return (0);
22642 	}
22643 }
22644 
22645 
22646 /*
22647  *    Function: sd_get_media_info
22648  *
22649  * Description: This routine is the driver entry point for handling ioctl
22650  *		requests for the media type or command set profile used by the
22651  *		drive to operate on the media (DKIOCGMEDIAINFO).
22652  *
22653  *   Arguments: dev	- the device number
22654  *		arg	- pointer to user provided dk_minfo structure
22655  *			  specifying the media type, logical block size and
22656  *			  drive capacity.
22657  *		flag	- this argument is a pass through to ddi_copyxxx()
22658  *			  directly from the mode argument of ioctl().
22659  *
22660  * Return Code: 0
22661  *		EACCESS
22662  *		EFAULT
22663  *		ENXIO
22664  *		EIO
22665  */
22666 
22667 static int
22668 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
22669 {
22670 	struct sd_lun		*un = NULL;
22671 	struct uscsi_cmd	com;
22672 	struct scsi_inquiry	*sinq;
22673 	struct dk_minfo		media_info;
22674 	u_longlong_t		media_capacity;
22675 	uint64_t		capacity;
22676 	uint_t			lbasize;
22677 	uchar_t			*out_data;
22678 	uchar_t			*rqbuf;
22679 	int			rval = 0;
22680 	int			rtn;
22681 
22682 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
22683 	    (un->un_state == SD_STATE_OFFLINE)) {
22684 		return (ENXIO);
22685 	}
22686 
22687 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n");
22688 
22689 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
22690 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
22691 
22692 	/* Issue a TUR to determine if the drive is ready with media present */
22693 	rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA);
22694 	if (rval == ENXIO) {
22695 		goto done;
22696 	}
22697 
22698 	/* Now get configuration data */
22699 	if (ISCD(un)) {
22700 		media_info.dki_media_type = DK_CDROM;
22701 
22702 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
22703 		if (un->un_f_mmc_cap == TRUE) {
22704 			rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf,
22705 				SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN);
22706 
22707 			if (rtn) {
22708 				/*
22709 				 * Failed for other than an illegal request
22710 				 * or command not supported
22711 				 */
22712 				if ((com.uscsi_status == STATUS_CHECK) &&
22713 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
22714 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
22715 					    (rqbuf[12] != 0x20)) {
22716 						rval = EIO;
22717 						goto done;
22718 					}
22719 				}
22720 			} else {
22721 				/*
22722 				 * The GET CONFIGURATION command succeeded
22723 				 * so set the media type according to the
22724 				 * returned data
22725 				 */
22726 				media_info.dki_media_type = out_data[6];
22727 				media_info.dki_media_type <<= 8;
22728 				media_info.dki_media_type |= out_data[7];
22729 			}
22730 		}
22731 	} else {
22732 		/*
22733 		 * The profile list is not available, so we attempt to identify
22734 		 * the media type based on the inquiry data
22735 		 */
22736 		sinq = un->un_sd->sd_inq;
22737 		if (sinq->inq_qual == 0) {
22738 			/* This is a direct access device */
22739 			media_info.dki_media_type = DK_FIXED_DISK;
22740 
22741 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
22742 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
22743 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
22744 					media_info.dki_media_type = DK_ZIP;
22745 				} else if (
22746 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
22747 					media_info.dki_media_type = DK_JAZ;
22748 				}
22749 			}
22750 		} else {
22751 			/* Not a CD or direct access so return unknown media */
22752 			media_info.dki_media_type = DK_UNKNOWN;
22753 		}
22754 	}
22755 
22756 	/* Now read the capacity so we can provide the lbasize and capacity */
22757 	switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
22758 	    SD_PATH_DIRECT)) {
22759 	case 0:
22760 		break;
22761 	case EACCES:
22762 		rval = EACCES;
22763 		goto done;
22764 	default:
22765 		rval = EIO;
22766 		goto done;
22767 	}
22768 
22769 	media_info.dki_lbsize = lbasize;
22770 	media_capacity = capacity;
22771 
22772 	/*
22773 	 * sd_send_scsi_READ_CAPACITY() reports capacity in
22774 	 * un->un_sys_blocksize chunks. So we need to convert it into
22775 	 * cap.lbasize chunks.
22776 	 */
22777 	media_capacity *= un->un_sys_blocksize;
22778 	media_capacity /= lbasize;
22779 	media_info.dki_capacity = media_capacity;
22780 
22781 	if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) {
22782 		rval = EFAULT;
22783 		/* Put goto. Anybody might add some code below in future */
22784 		goto done;
22785 	}
22786 done:
22787 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
22788 	kmem_free(rqbuf, SENSE_LENGTH);
22789 	return (rval);
22790 }
22791 
22792 
22793 /*
22794  *    Function: sd_dkio_get_geometry
22795  *
22796  * Description: This routine is the driver entry point for handling user
22797  *		requests to get the device geometry (DKIOCGGEOM).
22798  *
22799  *   Arguments: dev  - the device number
22800  *		arg  - pointer to user provided dk_geom structure specifying
22801  *			the controller's notion of the current geometry.
22802  *		flag - this argument is a pass through to ddi_copyxxx()
22803  *		       directly from the mode argument of ioctl().
22804  *		geom_validated - flag indicating if the device geometry has been
22805  *				 previously validated in the sdioctl routine.
22806  *
22807  * Return Code: 0
22808  *		EFAULT
22809  *		ENXIO
22810  *		EIO
22811  */
22812 
22813 static int
22814 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated)
22815 {
22816 	struct sd_lun	*un = NULL;
22817 	struct dk_geom	*tmp_geom = NULL;
22818 	int		rval = 0;
22819 
22820 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22821 		return (ENXIO);
22822 	}
22823 
22824 	if (geom_validated == FALSE) {
22825 		/*
22826 		 * sd_validate_geometry does not spin a disk up
22827 		 * if it was spun down. We need to make sure it
22828 		 * is ready.
22829 		 */
22830 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22831 			return (rval);
22832 		}
22833 		mutex_enter(SD_MUTEX(un));
22834 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
22835 		mutex_exit(SD_MUTEX(un));
22836 	}
22837 	if (rval)
22838 		return (rval);
22839 
22840 	/*
22841 	 * It is possible that un_solaris_size is 0(uninitialized)
22842 	 * after sd_unit_attach. Reservation conflict may cause the
22843 	 * above situation. Thus, the zero check of un_solaris_size
22844 	 * should occur after the sd_validate_geometry() call.
22845 	 */
22846 #if defined(__i386) || defined(__amd64)
22847 	if (un->un_solaris_size == 0) {
22848 		return (EIO);
22849 	}
22850 #endif
22851 
22852 	/*
22853 	 * Make a local copy of the soft state geometry to avoid some potential
22854 	 * race conditions associated with holding the mutex and updating the
22855 	 * write_reinstruct value
22856 	 */
22857 	tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
22858 	mutex_enter(SD_MUTEX(un));
22859 	bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom));
22860 	mutex_exit(SD_MUTEX(un));
22861 
22862 	if (tmp_geom->dkg_write_reinstruct == 0) {
22863 		tmp_geom->dkg_write_reinstruct =
22864 		    (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
22865 		    sd_rot_delay) / (int)60000);
22866 	}
22867 
22868 	rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
22869 	    flag);
22870 	if (rval != 0) {
22871 		rval = EFAULT;
22872 	}
22873 
22874 	kmem_free(tmp_geom, sizeof (struct dk_geom));
22875 	return (rval);
22876 
22877 }
22878 
22879 
22880 /*
22881  *    Function: sd_dkio_set_geometry
22882  *
22883  * Description: This routine is the driver entry point for handling user
22884  *		requests to set the device geometry (DKIOCSGEOM). The actual
22885  *		device geometry is not updated, just the driver "notion" of it.
22886  *
22887  *   Arguments: dev  - the device number
22888  *		arg  - pointer to user provided dk_geom structure used to set
22889  *			the controller's notion of the current geometry.
22890  *		flag - this argument is a pass through to ddi_copyxxx()
22891  *		       directly from the mode argument of ioctl().
22892  *
22893  * Return Code: 0
22894  *		EFAULT
22895  *		ENXIO
22896  *		EIO
22897  */
22898 
22899 static int
22900 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag)
22901 {
22902 	struct sd_lun	*un = NULL;
22903 	struct dk_geom	*tmp_geom;
22904 	struct dk_map	*lp;
22905 	int		rval = 0;
22906 	int		i;
22907 
22908 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22909 		return (ENXIO);
22910 	}
22911 
22912 	/*
22913 	 * Make sure there is no reservation conflict on the lun.
22914 	 */
22915 	if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) {
22916 		return (EACCES);
22917 	}
22918 
22919 #if defined(__i386) || defined(__amd64)
22920 	if (un->un_solaris_size == 0) {
22921 		return (EIO);
22922 	}
22923 #endif
22924 
22925 	/*
22926 	 * We need to copy the user specified geometry into local
22927 	 * storage and then update the softstate. We don't want to hold
22928 	 * the mutex and copyin directly from the user to the soft state
22929 	 */
22930 	tmp_geom = (struct dk_geom *)
22931 	    kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
22932 	rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
22933 	if (rval != 0) {
22934 		kmem_free(tmp_geom, sizeof (struct dk_geom));
22935 		return (EFAULT);
22936 	}
22937 
22938 	mutex_enter(SD_MUTEX(un));
22939 	bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom));
22940 	for (i = 0; i < NDKMAP; i++) {
22941 		lp  = &un->un_map[i];
22942 		un->un_offset[i] =
22943 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
22944 #if defined(__i386) || defined(__amd64)
22945 		un->un_offset[i] += un->un_solaris_offset;
22946 #endif
22947 	}
22948 	un->un_f_geometry_is_valid = FALSE;
22949 	mutex_exit(SD_MUTEX(un));
22950 	kmem_free(tmp_geom, sizeof (struct dk_geom));
22951 
22952 	return (rval);
22953 }
22954 
22955 
22956 /*
22957  *    Function: sd_dkio_get_partition
22958  *
22959  * Description: This routine is the driver entry point for handling user
22960  *		requests to get the partition table (DKIOCGAPART).
22961  *
22962  *   Arguments: dev  - the device number
22963  *		arg  - pointer to user provided dk_allmap structure specifying
22964  *			the controller's notion of the current partition table.
22965  *		flag - this argument is a pass through to ddi_copyxxx()
22966  *		       directly from the mode argument of ioctl().
22967  *		geom_validated - flag indicating if the device geometry has been
22968  *				 previously validated in the sdioctl routine.
22969  *
22970  * Return Code: 0
22971  *		EFAULT
22972  *		ENXIO
22973  *		EIO
22974  */
22975 
22976 static int
22977 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated)
22978 {
22979 	struct sd_lun	*un = NULL;
22980 	int		rval = 0;
22981 	int		size;
22982 
22983 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22984 		return (ENXIO);
22985 	}
22986 
22987 	/*
22988 	 * Make sure the geometry is valid before getting the partition
22989 	 * information.
22990 	 */
22991 	mutex_enter(SD_MUTEX(un));
22992 	if (geom_validated == FALSE) {
22993 		/*
22994 		 * sd_validate_geometry does not spin a disk up
22995 		 * if it was spun down. We need to make sure it
22996 		 * is ready before validating the geometry.
22997 		 */
22998 		mutex_exit(SD_MUTEX(un));
22999 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
23000 			return (rval);
23001 		}
23002 		mutex_enter(SD_MUTEX(un));
23003 
23004 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
23005 			mutex_exit(SD_MUTEX(un));
23006 			return (rval);
23007 		}
23008 	}
23009 	mutex_exit(SD_MUTEX(un));
23010 
23011 	/*
23012 	 * It is possible that un_solaris_size is 0(uninitialized)
23013 	 * after sd_unit_attach. Reservation conflict may cause the
23014 	 * above situation. Thus, the zero check of un_solaris_size
23015 	 * should occur after the sd_validate_geometry() call.
23016 	 */
23017 #if defined(__i386) || defined(__amd64)
23018 	if (un->un_solaris_size == 0) {
23019 		return (EIO);
23020 	}
23021 #endif
23022 
23023 #ifdef _MULTI_DATAMODEL
23024 	switch (ddi_model_convert_from(flag & FMODELS)) {
23025 	case DDI_MODEL_ILP32: {
23026 		struct dk_map32 dk_map32[NDKMAP];
23027 		int		i;
23028 
23029 		for (i = 0; i < NDKMAP; i++) {
23030 			dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno;
23031 			dk_map32[i].dkl_nblk  = un->un_map[i].dkl_nblk;
23032 		}
23033 		size = NDKMAP * sizeof (struct dk_map32);
23034 		rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
23035 		if (rval != 0) {
23036 			rval = EFAULT;
23037 		}
23038 		break;
23039 	}
23040 	case DDI_MODEL_NONE:
23041 		size = NDKMAP * sizeof (struct dk_map);
23042 		rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
23043 		if (rval != 0) {
23044 			rval = EFAULT;
23045 		}
23046 		break;
23047 	}
23048 #else /* ! _MULTI_DATAMODEL */
23049 	size = NDKMAP * sizeof (struct dk_map);
23050 	rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
23051 	if (rval != 0) {
23052 		rval = EFAULT;
23053 	}
23054 #endif /* _MULTI_DATAMODEL */
23055 	return (rval);
23056 }
23057 
23058 
23059 /*
23060  *    Function: sd_dkio_set_partition
23061  *
23062  * Description: This routine is the driver entry point for handling user
23063  *		requests to set the partition table (DKIOCSAPART). The actual
23064  *		device partition is not updated.
23065  *
23066  *   Arguments: dev  - the device number
23067  *		arg  - pointer to user provided dk_allmap structure used to set
23068  *			the controller's notion of the partition table.
23069  *		flag - this argument is a pass through to ddi_copyxxx()
23070  *		       directly from the mode argument of ioctl().
23071  *
23072  * Return Code: 0
23073  *		EINVAL
23074  *		EFAULT
23075  *		ENXIO
23076  *		EIO
23077  */
23078 
23079 static int
23080 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag)
23081 {
23082 	struct sd_lun	*un = NULL;
23083 	struct dk_map	dk_map[NDKMAP];
23084 	struct dk_map	*lp;
23085 	int		rval = 0;
23086 	int		size;
23087 	int		i;
23088 #if defined(_SUNOS_VTOC_16)
23089 	struct dkl_partition	*vp;
23090 #endif
23091 
23092 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23093 		return (ENXIO);
23094 	}
23095 
23096 	/*
23097 	 * Set the map for all logical partitions.  We lock
23098 	 * the priority just to make sure an interrupt doesn't
23099 	 * come in while the map is half updated.
23100 	 */
23101 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size))
23102 	mutex_enter(SD_MUTEX(un));
23103 	if (un->un_blockcount > DK_MAX_BLOCKS) {
23104 		mutex_exit(SD_MUTEX(un));
23105 		return (ENOTSUP);
23106 	}
23107 	mutex_exit(SD_MUTEX(un));
23108 
23109 	/*
23110 	 * Make sure there is no reservation conflict on the lun.
23111 	 */
23112 	if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) {
23113 		return (EACCES);
23114 	}
23115 
23116 #if defined(__i386) || defined(__amd64)
23117 	if (un->un_solaris_size == 0) {
23118 		return (EIO);
23119 	}
23120 #endif
23121 
23122 #ifdef _MULTI_DATAMODEL
23123 	switch (ddi_model_convert_from(flag & FMODELS)) {
23124 	case DDI_MODEL_ILP32: {
23125 		struct dk_map32 dk_map32[NDKMAP];
23126 
23127 		size = NDKMAP * sizeof (struct dk_map32);
23128 		rval = ddi_copyin((void *)arg, dk_map32, size, flag);
23129 		if (rval != 0) {
23130 			return (EFAULT);
23131 		}
23132 		for (i = 0; i < NDKMAP; i++) {
23133 			dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
23134 			dk_map[i].dkl_nblk  = dk_map32[i].dkl_nblk;
23135 		}
23136 		break;
23137 	}
23138 	case DDI_MODEL_NONE:
23139 		size = NDKMAP * sizeof (struct dk_map);
23140 		rval = ddi_copyin((void *)arg, dk_map, size, flag);
23141 		if (rval != 0) {
23142 			return (EFAULT);
23143 		}
23144 		break;
23145 	}
23146 #else /* ! _MULTI_DATAMODEL */
23147 	size = NDKMAP * sizeof (struct dk_map);
23148 	rval = ddi_copyin((void *)arg, dk_map, size, flag);
23149 	if (rval != 0) {
23150 		return (EFAULT);
23151 	}
23152 #endif /* _MULTI_DATAMODEL */
23153 
23154 	mutex_enter(SD_MUTEX(un));
23155 	/* Note: The size used in this bcopy is set based upon the data model */
23156 	bcopy(dk_map, un->un_map, size);
23157 #if defined(_SUNOS_VTOC_16)
23158 	vp = (struct dkl_partition *)&(un->un_vtoc);
23159 #endif	/* defined(_SUNOS_VTOC_16) */
23160 	for (i = 0; i < NDKMAP; i++) {
23161 		lp  = &un->un_map[i];
23162 		un->un_offset[i] =
23163 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
23164 #if defined(_SUNOS_VTOC_16)
23165 		vp->p_start = un->un_offset[i];
23166 		vp->p_size = lp->dkl_nblk;
23167 		vp++;
23168 #endif	/* defined(_SUNOS_VTOC_16) */
23169 #if defined(__i386) || defined(__amd64)
23170 		un->un_offset[i] += un->un_solaris_offset;
23171 #endif
23172 	}
23173 	mutex_exit(SD_MUTEX(un));
23174 	return (rval);
23175 }
23176 
23177 
23178 /*
23179  *    Function: sd_dkio_get_vtoc
23180  *
23181  * Description: This routine is the driver entry point for handling user
23182  *		requests to get the current volume table of contents
23183  *		(DKIOCGVTOC).
23184  *
23185  *   Arguments: dev  - the device number
23186  *		arg  - pointer to user provided vtoc structure specifying
23187  *			the current vtoc.
23188  *		flag - this argument is a pass through to ddi_copyxxx()
23189  *		       directly from the mode argument of ioctl().
23190  *		geom_validated - flag indicating if the device geometry has been
23191  *				 previously validated in the sdioctl routine.
23192  *
23193  * Return Code: 0
23194  *		EFAULT
23195  *		ENXIO
23196  *		EIO
23197  */
23198 
23199 static int
23200 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated)
23201 {
23202 	struct sd_lun	*un = NULL;
23203 #if defined(_SUNOS_VTOC_8)
23204 	struct vtoc	user_vtoc;
23205 #endif	/* defined(_SUNOS_VTOC_8) */
23206 	int		rval = 0;
23207 
23208 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23209 		return (ENXIO);
23210 	}
23211 
23212 	mutex_enter(SD_MUTEX(un));
23213 	if (geom_validated == FALSE) {
23214 		/*
23215 		 * sd_validate_geometry does not spin a disk up
23216 		 * if it was spun down. We need to make sure it
23217 		 * is ready.
23218 		 */
23219 		mutex_exit(SD_MUTEX(un));
23220 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
23221 			return (rval);
23222 		}
23223 		mutex_enter(SD_MUTEX(un));
23224 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
23225 			mutex_exit(SD_MUTEX(un));
23226 			return (rval);
23227 		}
23228 	}
23229 
23230 #if defined(_SUNOS_VTOC_8)
23231 	sd_build_user_vtoc(un, &user_vtoc);
23232 	mutex_exit(SD_MUTEX(un));
23233 
23234 #ifdef _MULTI_DATAMODEL
23235 	switch (ddi_model_convert_from(flag & FMODELS)) {
23236 	case DDI_MODEL_ILP32: {
23237 		struct vtoc32 user_vtoc32;
23238 
23239 		vtoctovtoc32(user_vtoc, user_vtoc32);
23240 		if (ddi_copyout(&user_vtoc32, (void *)arg,
23241 		    sizeof (struct vtoc32), flag)) {
23242 			return (EFAULT);
23243 		}
23244 		break;
23245 	}
23246 
23247 	case DDI_MODEL_NONE:
23248 		if (ddi_copyout(&user_vtoc, (void *)arg,
23249 		    sizeof (struct vtoc), flag)) {
23250 			return (EFAULT);
23251 		}
23252 		break;
23253 	}
23254 #else /* ! _MULTI_DATAMODEL */
23255 	if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
23256 		return (EFAULT);
23257 	}
23258 #endif /* _MULTI_DATAMODEL */
23259 
23260 #elif defined(_SUNOS_VTOC_16)
23261 	mutex_exit(SD_MUTEX(un));
23262 
23263 #ifdef _MULTI_DATAMODEL
23264 	/*
23265 	 * The un_vtoc structure is a "struct dk_vtoc"  which is always
23266 	 * 32-bit to maintain compatibility with existing on-disk
23267 	 * structures.  Thus, we need to convert the structure when copying
23268 	 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
23269 	 * program.  If the target is a 32-bit program, then no conversion
23270 	 * is necessary.
23271 	 */
23272 	/* LINTED: logical expression always true: op "||" */
23273 	ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32));
23274 	switch (ddi_model_convert_from(flag & FMODELS)) {
23275 	case DDI_MODEL_ILP32:
23276 		if (ddi_copyout(&(un->un_vtoc), (void *)arg,
23277 		    sizeof (un->un_vtoc), flag)) {
23278 			return (EFAULT);
23279 		}
23280 		break;
23281 
23282 	case DDI_MODEL_NONE: {
23283 		struct vtoc user_vtoc;
23284 
23285 		vtoc32tovtoc(un->un_vtoc, user_vtoc);
23286 		if (ddi_copyout(&user_vtoc, (void *)arg,
23287 		    sizeof (struct vtoc), flag)) {
23288 			return (EFAULT);
23289 		}
23290 		break;
23291 	}
23292 	}
23293 #else /* ! _MULTI_DATAMODEL */
23294 	if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc),
23295 	    flag)) {
23296 		return (EFAULT);
23297 	}
23298 #endif /* _MULTI_DATAMODEL */
23299 #else
23300 #error "No VTOC format defined."
23301 #endif
23302 
23303 	return (rval);
23304 }
23305 
23306 static int
23307 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag)
23308 {
23309 	struct sd_lun	*un = NULL;
23310 	dk_efi_t	user_efi;
23311 	int		rval = 0;
23312 	void		*buffer;
23313 
23314 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
23315 		return (ENXIO);
23316 
23317 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
23318 		return (EFAULT);
23319 
23320 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
23321 
23322 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
23323 	    (user_efi.dki_length > un->un_max_xfer_size))
23324 		return (EINVAL);
23325 
23326 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
23327 	rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length,
23328 	    user_efi.dki_lba, SD_PATH_DIRECT);
23329 	if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
23330 	    user_efi.dki_length, flag) != 0)
23331 		rval = EFAULT;
23332 
23333 	kmem_free(buffer, user_efi.dki_length);
23334 	return (rval);
23335 }
23336 
23337 /*
23338  *    Function: sd_build_user_vtoc
23339  *
23340  * Description: This routine populates a pass by reference variable with the
23341  *		current volume table of contents.
23342  *
23343  *   Arguments: un - driver soft state (unit) structure
23344  *		user_vtoc - pointer to vtoc structure to be populated
23345  */
23346 
23347 static void
23348 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
23349 {
23350 	struct dk_map2		*lpart;
23351 	struct dk_map		*lmap;
23352 	struct partition	*vpart;
23353 	int			nblks;
23354 	int			i;
23355 
23356 	ASSERT(mutex_owned(SD_MUTEX(un)));
23357 
23358 	/*
23359 	 * Return vtoc structure fields in the provided VTOC area, addressed
23360 	 * by *vtoc.
23361 	 */
23362 	bzero(user_vtoc, sizeof (struct vtoc));
23363 	user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0];
23364 	user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1];
23365 	user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2];
23366 	user_vtoc->v_sanity	= VTOC_SANE;
23367 	user_vtoc->v_version	= un->un_vtoc.v_version;
23368 	bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
23369 	user_vtoc->v_sectorsz = un->un_sys_blocksize;
23370 	user_vtoc->v_nparts = un->un_vtoc.v_nparts;
23371 	bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved,
23372 	    sizeof (un->un_vtoc.v_reserved));
23373 	/*
23374 	 * Convert partitioning information.
23375 	 *
23376 	 * Note the conversion from starting cylinder number
23377 	 * to starting sector number.
23378 	 */
23379 	lmap = un->un_map;
23380 	lpart = (struct dk_map2 *)un->un_vtoc.v_part;
23381 	vpart = user_vtoc->v_part;
23382 
23383 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
23384 
23385 	for (i = 0; i < V_NUMPAR; i++) {
23386 		vpart->p_tag	= lpart->p_tag;
23387 		vpart->p_flag	= lpart->p_flag;
23388 		vpart->p_start	= lmap->dkl_cylno * nblks;
23389 		vpart->p_size	= lmap->dkl_nblk;
23390 		lmap++;
23391 		lpart++;
23392 		vpart++;
23393 
23394 		/* (4364927) */
23395 		user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i];
23396 	}
23397 
23398 	bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
23399 }
23400 
23401 static int
23402 sd_dkio_partition(dev_t dev, caddr_t arg, int flag)
23403 {
23404 	struct sd_lun		*un = NULL;
23405 	struct partition64	p64;
23406 	int			rval = 0;
23407 	uint_t			nparts;
23408 	efi_gpe_t		*partitions;
23409 	efi_gpt_t		*buffer;
23410 	diskaddr_t		gpe_lba;
23411 
23412 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23413 		return (ENXIO);
23414 	}
23415 
23416 	if (ddi_copyin((const void *)arg, &p64,
23417 	    sizeof (struct partition64), flag)) {
23418 		return (EFAULT);
23419 	}
23420 
23421 	buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
23422 	rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE,
23423 		1, SD_PATH_DIRECT);
23424 	if (rval != 0)
23425 		goto done_error;
23426 
23427 	sd_swap_efi_gpt(buffer);
23428 
23429 	if ((rval = sd_validate_efi(buffer)) != 0)
23430 		goto done_error;
23431 
23432 	nparts = buffer->efi_gpt_NumberOfPartitionEntries;
23433 	gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
23434 	if (p64.p_partno > nparts) {
23435 		/* couldn't find it */
23436 		rval = ESRCH;
23437 		goto done_error;
23438 	}
23439 	/*
23440 	 * if we're dealing with a partition that's out of the normal
23441 	 * 16K block, adjust accordingly
23442 	 */
23443 	gpe_lba += p64.p_partno / sizeof (efi_gpe_t);
23444 	rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE,
23445 			gpe_lba, SD_PATH_DIRECT);
23446 	if (rval) {
23447 		goto done_error;
23448 	}
23449 	partitions = (efi_gpe_t *)buffer;
23450 
23451 	sd_swap_efi_gpe(nparts, partitions);
23452 
23453 	partitions += p64.p_partno;
23454 	bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
23455 	    sizeof (struct uuid));
23456 	p64.p_start = partitions->efi_gpe_StartingLBA;
23457 	p64.p_size = partitions->efi_gpe_EndingLBA -
23458 			p64.p_start + 1;
23459 
23460 	if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
23461 		rval = EFAULT;
23462 
23463 done_error:
23464 	kmem_free(buffer, EFI_MIN_ARRAY_SIZE);
23465 	return (rval);
23466 }
23467 
23468 
23469 /*
23470  *    Function: sd_dkio_set_vtoc
23471  *
23472  * Description: This routine is the driver entry point for handling user
23473  *		requests to set the current volume table of contents
23474  *		(DKIOCSVTOC).
23475  *
23476  *   Arguments: dev  - the device number
23477  *		arg  - pointer to user provided vtoc structure used to set the
23478  *			current vtoc.
23479  *		flag - this argument is a pass through to ddi_copyxxx()
23480  *		       directly from the mode argument of ioctl().
23481  *
23482  * Return Code: 0
23483  *		EFAULT
23484  *		ENXIO
23485  *		EINVAL
23486  *		ENOTSUP
23487  */
23488 
23489 static int
23490 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag)
23491 {
23492 	struct sd_lun	*un = NULL;
23493 	struct vtoc	user_vtoc;
23494 	int		rval = 0;
23495 
23496 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23497 		return (ENXIO);
23498 	}
23499 
23500 #if defined(__i386) || defined(__amd64)
23501 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
23502 		return (EINVAL);
23503 	}
23504 #endif
23505 
23506 #ifdef _MULTI_DATAMODEL
23507 	switch (ddi_model_convert_from(flag & FMODELS)) {
23508 	case DDI_MODEL_ILP32: {
23509 		struct vtoc32 user_vtoc32;
23510 
23511 		if (ddi_copyin((const void *)arg, &user_vtoc32,
23512 		    sizeof (struct vtoc32), flag)) {
23513 			return (EFAULT);
23514 		}
23515 		vtoc32tovtoc(user_vtoc32, user_vtoc);
23516 		break;
23517 	}
23518 
23519 	case DDI_MODEL_NONE:
23520 		if (ddi_copyin((const void *)arg, &user_vtoc,
23521 		    sizeof (struct vtoc), flag)) {
23522 			return (EFAULT);
23523 		}
23524 		break;
23525 	}
23526 #else /* ! _MULTI_DATAMODEL */
23527 	if (ddi_copyin((const void *)arg, &user_vtoc,
23528 	    sizeof (struct vtoc), flag)) {
23529 		return (EFAULT);
23530 	}
23531 #endif /* _MULTI_DATAMODEL */
23532 
23533 	mutex_enter(SD_MUTEX(un));
23534 	if (un->un_blockcount > DK_MAX_BLOCKS) {
23535 		mutex_exit(SD_MUTEX(un));
23536 		return (ENOTSUP);
23537 	}
23538 	if (un->un_g.dkg_ncyl == 0) {
23539 		mutex_exit(SD_MUTEX(un));
23540 		return (EINVAL);
23541 	}
23542 
23543 	mutex_exit(SD_MUTEX(un));
23544 	sd_clear_efi(un);
23545 	ddi_remove_minor_node(SD_DEVINFO(un), "wd");
23546 	ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw");
23547 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h",
23548 	    S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23549 	    un->un_node_type, NULL);
23550 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw",
23551 	    S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23552 	    un->un_node_type, NULL);
23553 	mutex_enter(SD_MUTEX(un));
23554 
23555 	if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) {
23556 		if ((rval = sd_write_label(dev)) == 0) {
23557 			if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT))
23558 			    != 0) {
23559 				SD_ERROR(SD_LOG_IOCTL_DKIO, un,
23560 				    "sd_dkio_set_vtoc: "
23561 				    "Failed validate geometry\n");
23562 			}
23563 		}
23564 	}
23565 
23566 	/*
23567 	 * If sd_build_label_vtoc, or sd_write_label failed above write the
23568 	 * devid anyway, what can it hurt? Also preserve the device id by
23569 	 * writing to the disk acyl for the case where a devid has been
23570 	 * fabricated.
23571 	 */
23572 	if (un->un_f_devid_supported &&
23573 	    (un->un_f_opt_fab_devid == TRUE)) {
23574 		if (un->un_devid == NULL) {
23575 			sd_register_devid(un, SD_DEVINFO(un),
23576 			    SD_TARGET_IS_UNRESERVED);
23577 		} else {
23578 			/*
23579 			 * The device id for this disk has been
23580 			 * fabricated. Fabricated device id's are
23581 			 * managed by storing them in the last 2
23582 			 * available sectors on the drive. The device
23583 			 * id must be preserved by writing it back out
23584 			 * to this location.
23585 			 */
23586 			if (sd_write_deviceid(un) != 0) {
23587 				ddi_devid_free(un->un_devid);
23588 				un->un_devid = NULL;
23589 			}
23590 		}
23591 	}
23592 	mutex_exit(SD_MUTEX(un));
23593 	return (rval);
23594 }
23595 
23596 
23597 /*
23598  *    Function: sd_build_label_vtoc
23599  *
23600  * Description: This routine updates the driver soft state current volume table
23601  *		of contents based on a user specified vtoc.
23602  *
23603  *   Arguments: un - driver soft state (unit) structure
23604  *		user_vtoc - pointer to vtoc structure specifying vtoc to be used
23605  *			    to update the driver soft state.
23606  *
23607  * Return Code: 0
23608  *		EINVAL
23609  */
23610 
23611 static int
23612 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
23613 {
23614 	struct dk_map		*lmap;
23615 	struct partition	*vpart;
23616 	int			nblks;
23617 #if defined(_SUNOS_VTOC_8)
23618 	int			ncyl;
23619 	struct dk_map2		*lpart;
23620 #endif	/* defined(_SUNOS_VTOC_8) */
23621 	int			i;
23622 
23623 	ASSERT(mutex_owned(SD_MUTEX(un)));
23624 
23625 	/* Sanity-check the vtoc */
23626 	if (user_vtoc->v_sanity != VTOC_SANE ||
23627 	    user_vtoc->v_sectorsz != un->un_sys_blocksize ||
23628 	    user_vtoc->v_nparts != V_NUMPAR) {
23629 		return (EINVAL);
23630 	}
23631 
23632 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
23633 	if (nblks == 0) {
23634 		return (EINVAL);
23635 	}
23636 
23637 #if defined(_SUNOS_VTOC_8)
23638 	vpart = user_vtoc->v_part;
23639 	for (i = 0; i < V_NUMPAR; i++) {
23640 		if ((vpart->p_start % nblks) != 0) {
23641 			return (EINVAL);
23642 		}
23643 		ncyl = vpart->p_start / nblks;
23644 		ncyl += vpart->p_size / nblks;
23645 		if ((vpart->p_size % nblks) != 0) {
23646 			ncyl++;
23647 		}
23648 		if (ncyl > (int)un->un_g.dkg_ncyl) {
23649 			return (EINVAL);
23650 		}
23651 		vpart++;
23652 	}
23653 #endif	/* defined(_SUNOS_VTOC_8) */
23654 
23655 	/* Put appropriate vtoc structure fields into the disk label */
23656 #if defined(_SUNOS_VTOC_16)
23657 	/*
23658 	 * The vtoc is always a 32bit data structure to maintain the
23659 	 * on-disk format. Convert "in place" instead of bcopying it.
23660 	 */
23661 	vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc))));
23662 
23663 	/*
23664 	 * in the 16-slice vtoc, starting sectors are expressed in
23665 	 * numbers *relative* to the start of the Solaris fdisk partition.
23666 	 */
23667 	lmap = un->un_map;
23668 	vpart = user_vtoc->v_part;
23669 
23670 	for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
23671 		lmap->dkl_cylno = vpart->p_start / nblks;
23672 		lmap->dkl_nblk = vpart->p_size;
23673 	}
23674 
23675 #elif defined(_SUNOS_VTOC_8)
23676 
23677 	un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
23678 	un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
23679 	un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
23680 
23681 	un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
23682 	un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version;
23683 
23684 	bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL);
23685 
23686 	un->un_vtoc.v_nparts = user_vtoc->v_nparts;
23687 
23688 	bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved,
23689 	    sizeof (un->un_vtoc.v_reserved));
23690 
23691 	/*
23692 	 * Note the conversion from starting sector number
23693 	 * to starting cylinder number.
23694 	 * Return error if division results in a remainder.
23695 	 */
23696 	lmap = un->un_map;
23697 	lpart = un->un_vtoc.v_part;
23698 	vpart = user_vtoc->v_part;
23699 
23700 	for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
23701 		lpart->p_tag  = vpart->p_tag;
23702 		lpart->p_flag = vpart->p_flag;
23703 		lmap->dkl_cylno = vpart->p_start / nblks;
23704 		lmap->dkl_nblk = vpart->p_size;
23705 
23706 		lmap++;
23707 		lpart++;
23708 		vpart++;
23709 
23710 		/* (4387723) */
23711 #ifdef _LP64
23712 		if (user_vtoc->timestamp[i] > TIME32_MAX) {
23713 			un->un_vtoc.v_timestamp[i] = TIME32_MAX;
23714 		} else {
23715 			un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
23716 		}
23717 #else
23718 		un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
23719 #endif
23720 	}
23721 
23722 	bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII);
23723 #else
23724 #error "No VTOC format defined."
23725 #endif
23726 	return (0);
23727 }
23728 
23729 /*
23730  *    Function: sd_clear_efi
23731  *
23732  * Description: This routine clears all EFI labels.
23733  *
23734  *   Arguments: un - driver soft state (unit) structure
23735  *
23736  * Return Code: void
23737  */
23738 
23739 static void
23740 sd_clear_efi(struct sd_lun *un)
23741 {
23742 	efi_gpt_t	*gpt;
23743 	uint_t		lbasize;
23744 	uint64_t	cap;
23745 	int rval;
23746 
23747 	ASSERT(!mutex_owned(SD_MUTEX(un)));
23748 
23749 	mutex_enter(SD_MUTEX(un));
23750 	un->un_reserved = -1;
23751 	mutex_exit(SD_MUTEX(un));
23752 	gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP);
23753 
23754 	if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) {
23755 		goto done;
23756 	}
23757 
23758 	sd_swap_efi_gpt(gpt);
23759 	rval = sd_validate_efi(gpt);
23760 	if (rval == 0) {
23761 		/* clear primary */
23762 		bzero(gpt, sizeof (efi_gpt_t));
23763 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1,
23764 			SD_PATH_DIRECT))) {
23765 			SD_INFO(SD_LOG_IO_PARTITION, un,
23766 				"sd_clear_efi: clear primary label failed\n");
23767 		}
23768 	}
23769 	/* the backup */
23770 	rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
23771 	    SD_PATH_DIRECT);
23772 	if (rval) {
23773 		goto done;
23774 	}
23775 	/*
23776 	 * The MMC standard allows READ CAPACITY to be
23777 	 * inaccurate by a bounded amount (in the interest of
23778 	 * response latency).  As a result, failed READs are
23779 	 * commonplace (due to the reading of metadata and not
23780 	 * data). Depending on the per-Vendor/drive Sense data,
23781 	 * the failed READ can cause many (unnecessary) retries.
23782 	 */
23783 	if ((rval = sd_send_scsi_READ(un, gpt, lbasize,
23784 	    cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY :
23785 		SD_PATH_DIRECT)) != 0) {
23786 		goto done;
23787 	}
23788 	sd_swap_efi_gpt(gpt);
23789 	rval = sd_validate_efi(gpt);
23790 	if (rval == 0) {
23791 		/* clear backup */
23792 		SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n",
23793 			cap-1);
23794 		bzero(gpt, sizeof (efi_gpt_t));
23795 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE,
23796 		    cap-1, SD_PATH_DIRECT))) {
23797 			SD_INFO(SD_LOG_IO_PARTITION, un,
23798 				"sd_clear_efi: clear backup label failed\n");
23799 		}
23800 	} else {
23801 		/*
23802 		 * Refer to comments related to off-by-1 at the
23803 		 * header of this file
23804 		 */
23805 		if ((rval = sd_send_scsi_READ(un, gpt, lbasize,
23806 		    cap - 2, ISCD(un) ? SD_PATH_DIRECT_PRIORITY :
23807 			SD_PATH_DIRECT)) != 0) {
23808 			goto done;
23809 		}
23810 		sd_swap_efi_gpt(gpt);
23811 		rval = sd_validate_efi(gpt);
23812 		if (rval == 0) {
23813 			/* clear legacy backup EFI label */
23814 			SD_TRACE(SD_LOG_IOCTL, un,
23815 			    "sd_clear_efi clear backup@%lu\n", cap-2);
23816 			bzero(gpt, sizeof (efi_gpt_t));
23817 			if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE,
23818 			    cap-2, SD_PATH_DIRECT))) {
23819 				SD_INFO(SD_LOG_IO_PARTITION,
23820 				    un, "sd_clear_efi: "
23821 				    " clear legacy backup label failed\n");
23822 			}
23823 		}
23824 	}
23825 
23826 done:
23827 	kmem_free(gpt, sizeof (efi_gpt_t));
23828 }
23829 
23830 /*
23831  *    Function: sd_set_vtoc
23832  *
23833  * Description: This routine writes data to the appropriate positions
23834  *
23835  *   Arguments: un - driver soft state (unit) structure
23836  *              dkl  - the data to be written
23837  *
23838  * Return: void
23839  */
23840 
23841 static int
23842 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl)
23843 {
23844 	void			*shadow_buf;
23845 	uint_t			label_addr;
23846 	int			sec;
23847 	int			blk;
23848 	int			head;
23849 	int			cyl;
23850 	int			rval;
23851 
23852 #if defined(__i386) || defined(__amd64)
23853 	label_addr = un->un_solaris_offset + DK_LABEL_LOC;
23854 #else
23855 	/* Write the primary label at block 0 of the solaris partition. */
23856 	label_addr = 0;
23857 #endif
23858 
23859 	if (NOT_DEVBSIZE(un)) {
23860 		shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP);
23861 		/*
23862 		 * Read the target's first block.
23863 		 */
23864 		if ((rval = sd_send_scsi_READ(un, shadow_buf,
23865 		    un->un_tgt_blocksize, label_addr,
23866 		    SD_PATH_STANDARD)) != 0) {
23867 			goto exit;
23868 		}
23869 		/*
23870 		 * Copy the contents of the label into the shadow buffer
23871 		 * which is of the size of target block size.
23872 		 */
23873 		bcopy(dkl, shadow_buf, sizeof (struct dk_label));
23874 	}
23875 
23876 	/* Write the primary label */
23877 	if (NOT_DEVBSIZE(un)) {
23878 		rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize,
23879 		    label_addr, SD_PATH_STANDARD);
23880 	} else {
23881 		rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
23882 		    label_addr, SD_PATH_STANDARD);
23883 	}
23884 	if (rval != 0) {
23885 		return (rval);
23886 	}
23887 
23888 	/*
23889 	 * Calculate where the backup labels go.  They are always on
23890 	 * the last alternate cylinder, but some older drives put them
23891 	 * on head 2 instead of the last head.	They are always on the
23892 	 * first 5 odd sectors of the appropriate track.
23893 	 *
23894 	 * We have no choice at this point, but to believe that the
23895 	 * disk label is valid.	 Use the geometry of the disk
23896 	 * as described in the label.
23897 	 */
23898 	cyl  = dkl->dkl_ncyl  + dkl->dkl_acyl - 1;
23899 	head = dkl->dkl_nhead - 1;
23900 
23901 	/*
23902 	 * Write and verify the backup labels. Make sure we don't try to
23903 	 * write past the last cylinder.
23904 	 */
23905 	for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
23906 		blk = (daddr_t)(
23907 		    (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
23908 		    (head * dkl->dkl_nsect) + sec);
23909 #if defined(__i386) || defined(__amd64)
23910 		blk += un->un_solaris_offset;
23911 #endif
23912 		if (NOT_DEVBSIZE(un)) {
23913 			uint64_t	tblk;
23914 			/*
23915 			 * Need to read the block first for read modify write.
23916 			 */
23917 			tblk = (uint64_t)blk;
23918 			blk = (int)((tblk * un->un_sys_blocksize) /
23919 			    un->un_tgt_blocksize);
23920 			if ((rval = sd_send_scsi_READ(un, shadow_buf,
23921 			    un->un_tgt_blocksize, blk,
23922 			    SD_PATH_STANDARD)) != 0) {
23923 				goto exit;
23924 			}
23925 			/*
23926 			 * Modify the shadow buffer with the label.
23927 			 */
23928 			bcopy(dkl, shadow_buf, sizeof (struct dk_label));
23929 			rval = sd_send_scsi_WRITE(un, shadow_buf,
23930 			    un->un_tgt_blocksize, blk, SD_PATH_STANDARD);
23931 		} else {
23932 			rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
23933 			    blk, SD_PATH_STANDARD);
23934 			SD_INFO(SD_LOG_IO_PARTITION, un,
23935 			"sd_set_vtoc: wrote backup label %d\n", blk);
23936 		}
23937 		if (rval != 0) {
23938 			goto exit;
23939 		}
23940 	}
23941 exit:
23942 	if (NOT_DEVBSIZE(un)) {
23943 		kmem_free(shadow_buf, un->un_tgt_blocksize);
23944 	}
23945 	return (rval);
23946 }
23947 
23948 /*
23949  *    Function: sd_clear_vtoc
23950  *
23951  * Description: This routine clears out the VTOC labels.
23952  *
23953  *   Arguments: un - driver soft state (unit) structure
23954  *
23955  * Return: void
23956  */
23957 
23958 static void
23959 sd_clear_vtoc(struct sd_lun *un)
23960 {
23961 	struct dk_label		*dkl;
23962 
23963 	mutex_exit(SD_MUTEX(un));
23964 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
23965 	mutex_enter(SD_MUTEX(un));
23966 	/*
23967 	 * sd_set_vtoc uses these fields in order to figure out
23968 	 * where to overwrite the backup labels
23969 	 */
23970 	dkl->dkl_apc    = un->un_g.dkg_apc;
23971 	dkl->dkl_ncyl   = un->un_g.dkg_ncyl;
23972 	dkl->dkl_acyl   = un->un_g.dkg_acyl;
23973 	dkl->dkl_nhead  = un->un_g.dkg_nhead;
23974 	dkl->dkl_nsect  = un->un_g.dkg_nsect;
23975 	mutex_exit(SD_MUTEX(un));
23976 	(void) sd_set_vtoc(un, dkl);
23977 	kmem_free(dkl, sizeof (struct dk_label));
23978 
23979 	mutex_enter(SD_MUTEX(un));
23980 }
23981 
23982 /*
23983  *    Function: sd_write_label
23984  *
23985  * Description: This routine will validate and write the driver soft state vtoc
23986  *		contents to the device.
23987  *
23988  *   Arguments: dev - the device number
23989  *
23990  * Return Code: the code returned by sd_send_scsi_cmd()
23991  *		0
23992  *		EINVAL
23993  *		ENXIO
23994  *		ENOMEM
23995  */
23996 
23997 static int
23998 sd_write_label(dev_t dev)
23999 {
24000 	struct sd_lun		*un;
24001 	struct dk_label		*dkl;
24002 	short			sum;
24003 	short			*sp;
24004 	int			i;
24005 	int			rval;
24006 
24007 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
24008 	    (un->un_state == SD_STATE_OFFLINE)) {
24009 		return (ENXIO);
24010 	}
24011 	ASSERT(mutex_owned(SD_MUTEX(un)));
24012 	mutex_exit(SD_MUTEX(un));
24013 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
24014 	mutex_enter(SD_MUTEX(un));
24015 
24016 	bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
24017 	dkl->dkl_rpm	= un->un_g.dkg_rpm;
24018 	dkl->dkl_pcyl	= un->un_g.dkg_pcyl;
24019 	dkl->dkl_apc	= un->un_g.dkg_apc;
24020 	dkl->dkl_intrlv = un->un_g.dkg_intrlv;
24021 	dkl->dkl_ncyl	= un->un_g.dkg_ncyl;
24022 	dkl->dkl_acyl	= un->un_g.dkg_acyl;
24023 	dkl->dkl_nhead	= un->un_g.dkg_nhead;
24024 	dkl->dkl_nsect	= un->un_g.dkg_nsect;
24025 
24026 #if defined(_SUNOS_VTOC_8)
24027 	dkl->dkl_obs1	= un->un_g.dkg_obs1;
24028 	dkl->dkl_obs2	= un->un_g.dkg_obs2;
24029 	dkl->dkl_obs3	= un->un_g.dkg_obs3;
24030 	for (i = 0; i < NDKMAP; i++) {
24031 		dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno;
24032 		dkl->dkl_map[i].dkl_nblk  = un->un_map[i].dkl_nblk;
24033 	}
24034 	bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
24035 #elif defined(_SUNOS_VTOC_16)
24036 	dkl->dkl_skew	= un->un_dkg_skew;
24037 #else
24038 #error "No VTOC format defined."
24039 #endif
24040 
24041 	dkl->dkl_magic			= DKL_MAGIC;
24042 	dkl->dkl_write_reinstruct	= un->un_g.dkg_write_reinstruct;
24043 	dkl->dkl_read_reinstruct	= un->un_g.dkg_read_reinstruct;
24044 
24045 	/* Construct checksum for the new disk label */
24046 	sum = 0;
24047 	sp = (short *)dkl;
24048 	i = sizeof (struct dk_label) / sizeof (short);
24049 	while (i--) {
24050 		sum ^= *sp++;
24051 	}
24052 	dkl->dkl_cksum = sum;
24053 
24054 	mutex_exit(SD_MUTEX(un));
24055 
24056 	rval = sd_set_vtoc(un, dkl);
24057 exit:
24058 	kmem_free(dkl, sizeof (struct dk_label));
24059 	mutex_enter(SD_MUTEX(un));
24060 	return (rval);
24061 }
24062 
24063 static int
24064 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag)
24065 {
24066 	struct sd_lun	*un = NULL;
24067 	dk_efi_t	user_efi;
24068 	int		rval = 0;
24069 	void		*buffer;
24070 	int		valid_efi;
24071 
24072 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
24073 		return (ENXIO);
24074 
24075 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
24076 		return (EFAULT);
24077 
24078 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
24079 
24080 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
24081 	    (user_efi.dki_length > un->un_max_xfer_size))
24082 		return (EINVAL);
24083 
24084 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
24085 	if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
24086 		rval = EFAULT;
24087 	} else {
24088 		/*
24089 		 * let's clear the vtoc labels and clear the softstate
24090 		 * vtoc.
24091 		 */
24092 		mutex_enter(SD_MUTEX(un));
24093 		if (un->un_vtoc.v_sanity == VTOC_SANE) {
24094 			SD_TRACE(SD_LOG_IO_PARTITION, un,
24095 				"sd_dkio_set_efi: CLEAR VTOC\n");
24096 			sd_clear_vtoc(un);
24097 			bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
24098 			mutex_exit(SD_MUTEX(un));
24099 			ddi_remove_minor_node(SD_DEVINFO(un), "h");
24100 			ddi_remove_minor_node(SD_DEVINFO(un), "h,raw");
24101 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd",
24102 			    S_IFBLK,
24103 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
24104 			    un->un_node_type, NULL);
24105 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw",
24106 			    S_IFCHR,
24107 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
24108 			    un->un_node_type, NULL);
24109 		} else
24110 			mutex_exit(SD_MUTEX(un));
24111 		rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length,
24112 		    user_efi.dki_lba, SD_PATH_DIRECT);
24113 		if (rval == 0) {
24114 			mutex_enter(SD_MUTEX(un));
24115 
24116 			/*
24117 			 * Set the un_reserved for valid efi label.
24118 			 * Function clear_efi in fdisk and efi_write in
24119 			 * libefi both change efi label on disk in 3 steps
24120 			 * 1. Change primary gpt and gpe
24121 			 * 2. Change backup gpe
24122 			 * 3. Change backup gpt, which is one block
24123 			 * We only reread the efi label after the 3rd step,
24124 			 * or there will be warning "primary label corrupt".
24125 			 */
24126 			if (user_efi.dki_length == un->un_tgt_blocksize) {
24127 				un->un_f_geometry_is_valid = FALSE;
24128 				valid_efi = sd_use_efi(un, SD_PATH_DIRECT);
24129 				if ((valid_efi == 0) &&
24130 				    un->un_f_devid_supported &&
24131 				    (un->un_f_opt_fab_devid == TRUE)) {
24132 					if (un->un_devid == NULL) {
24133 						sd_register_devid(un,
24134 						    SD_DEVINFO(un),
24135 						    SD_TARGET_IS_UNRESERVED);
24136 					} else {
24137 						/*
24138 						 * The device id for this disk
24139 						 * has been fabricated. The
24140 						 * device id must be preserved
24141 						 * by writing it back out to
24142 						 * disk.
24143 						 */
24144 						if (sd_write_deviceid(un)
24145 						    != 0) {
24146 							ddi_devid_free(
24147 							    un->un_devid);
24148 							un->un_devid = NULL;
24149 						}
24150 					}
24151 				}
24152 			}
24153 
24154 			mutex_exit(SD_MUTEX(un));
24155 		}
24156 	}
24157 	kmem_free(buffer, user_efi.dki_length);
24158 	return (rval);
24159 }
24160 
24161 /*
24162  *    Function: sd_dkio_get_mboot
24163  *
24164  * Description: This routine is the driver entry point for handling user
24165  *		requests to get the current device mboot (DKIOCGMBOOT)
24166  *
24167  *   Arguments: dev  - the device number
24168  *		arg  - pointer to user provided mboot structure specifying
24169  *			the current mboot.
24170  *		flag - this argument is a pass through to ddi_copyxxx()
24171  *		       directly from the mode argument of ioctl().
24172  *
24173  * Return Code: 0
24174  *		EINVAL
24175  *		EFAULT
24176  *		ENXIO
24177  */
24178 
24179 static int
24180 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag)
24181 {
24182 	struct sd_lun	*un;
24183 	struct mboot	*mboot;
24184 	int		rval;
24185 	size_t		buffer_size;
24186 
24187 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
24188 	    (un->un_state == SD_STATE_OFFLINE)) {
24189 		return (ENXIO);
24190 	}
24191 
24192 	if (!un->un_f_mboot_supported || arg == NULL) {
24193 		return (EINVAL);
24194 	}
24195 
24196 	/*
24197 	 * Read the mboot block, located at absolute block 0 on the target.
24198 	 */
24199 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot));
24200 
24201 	SD_TRACE(SD_LOG_IO_PARTITION, un,
24202 	    "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
24203 
24204 	mboot = kmem_zalloc(buffer_size, KM_SLEEP);
24205 	if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0,
24206 	    SD_PATH_STANDARD)) == 0) {
24207 		if (ddi_copyout(mboot, (void *)arg,
24208 		    sizeof (struct mboot), flag) != 0) {
24209 			rval = EFAULT;
24210 		}
24211 	}
24212 	kmem_free(mboot, buffer_size);
24213 	return (rval);
24214 }
24215 
24216 
24217 /*
24218  *    Function: sd_dkio_set_mboot
24219  *
24220  * Description: This routine is the driver entry point for handling user
24221  *		requests to validate and set the device master boot
24222  *		(DKIOCSMBOOT).
24223  *
24224  *   Arguments: dev  - the device number
24225  *		arg  - pointer to user provided mboot structure used to set the
24226  *			master boot.
24227  *		flag - this argument is a pass through to ddi_copyxxx()
24228  *		       directly from the mode argument of ioctl().
24229  *
24230  * Return Code: 0
24231  *		EINVAL
24232  *		EFAULT
24233  *		ENXIO
24234  */
24235 
24236 static int
24237 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag)
24238 {
24239 	struct sd_lun	*un = NULL;
24240 	struct mboot	*mboot = NULL;
24241 	int		rval;
24242 	ushort_t	magic;
24243 
24244 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24245 		return (ENXIO);
24246 	}
24247 
24248 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24249 
24250 	if (!un->un_f_mboot_supported) {
24251 		return (EINVAL);
24252 	}
24253 
24254 	if (arg == NULL) {
24255 		return (EINVAL);
24256 	}
24257 
24258 	mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP);
24259 
24260 	if (ddi_copyin((const void *)arg, mboot,
24261 	    sizeof (struct mboot), flag) != 0) {
24262 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
24263 		return (EFAULT);
24264 	}
24265 
24266 	/* Is this really a master boot record? */
24267 	magic = LE_16(mboot->signature);
24268 	if (magic != MBB_MAGIC) {
24269 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
24270 		return (EINVAL);
24271 	}
24272 
24273 	rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0,
24274 	    SD_PATH_STANDARD);
24275 
24276 	mutex_enter(SD_MUTEX(un));
24277 #if defined(__i386) || defined(__amd64)
24278 	if (rval == 0) {
24279 		/*
24280 		 * mboot has been written successfully.
24281 		 * update the fdisk and vtoc tables in memory
24282 		 */
24283 		rval = sd_update_fdisk_and_vtoc(un);
24284 		if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) {
24285 			mutex_exit(SD_MUTEX(un));
24286 			kmem_free(mboot, (size_t)(sizeof (struct mboot)));
24287 			return (rval);
24288 		}
24289 	}
24290 
24291 #ifdef __lock_lint
24292 	sd_setup_default_geometry(un);
24293 #endif
24294 
24295 #else
24296 	if (rval == 0) {
24297 		/*
24298 		 * mboot has been written successfully.
24299 		 * set up the default geometry and VTOC
24300 		 */
24301 		if (un->un_blockcount <= DK_MAX_BLOCKS)
24302 			sd_setup_default_geometry(un);
24303 	}
24304 #endif
24305 	mutex_exit(SD_MUTEX(un));
24306 	kmem_free(mboot, (size_t)(sizeof (struct mboot)));
24307 	return (rval);
24308 }
24309 
24310 
24311 /*
24312  *    Function: sd_setup_default_geometry
24313  *
24314  * Description: This local utility routine sets the default geometry as part of
24315  *		setting the device mboot.
24316  *
24317  *   Arguments: un - driver soft state (unit) structure
24318  *
24319  * Note: This may be redundant with sd_build_default_label.
24320  */
24321 
24322 static void
24323 sd_setup_default_geometry(struct sd_lun *un)
24324 {
24325 	/* zero out the soft state geometry and partition table. */
24326 	bzero(&un->un_g, sizeof (struct dk_geom));
24327 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
24328 	bzero(un->un_map, NDKMAP * (sizeof (struct dk_map)));
24329 	un->un_asciilabel[0] = '\0';
24330 
24331 	/*
24332 	 * For the rpm, we use the minimum for the disk.
24333 	 * For the head, cyl and number of sector per track,
24334 	 * if the capacity <= 1GB, head = 64, sect = 32.
24335 	 * else head = 255, sect 63
24336 	 * Note: the capacity should be equal to C*H*S values.
24337 	 * This will cause some truncation of size due to
24338 	 * round off errors. For CD-ROMs, this truncation can
24339 	 * have adverse side effects, so returning ncyl and
24340 	 * nhead as 1. The nsect will overflow for most of
24341 	 * CD-ROMs as nsect is of type ushort.
24342 	 */
24343 	if (ISCD(un)) {
24344 		un->un_g.dkg_ncyl = 1;
24345 		un->un_g.dkg_nhead = 1;
24346 		un->un_g.dkg_nsect = un->un_blockcount;
24347 	} else {
24348 		if (un->un_blockcount <= 0x1000) {
24349 			/* Needed for unlabeled SCSI floppies. */
24350 			un->un_g.dkg_nhead = 2;
24351 			un->un_g.dkg_ncyl = 80;
24352 			un->un_g.dkg_pcyl = 80;
24353 			un->un_g.dkg_nsect = un->un_blockcount / (2 * 80);
24354 		} else if (un->un_blockcount <= 0x200000) {
24355 			un->un_g.dkg_nhead = 64;
24356 			un->un_g.dkg_nsect = 32;
24357 			un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32);
24358 		} else {
24359 			un->un_g.dkg_nhead = 255;
24360 			un->un_g.dkg_nsect = 63;
24361 			un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63);
24362 		}
24363 		un->un_blockcount = un->un_g.dkg_ncyl *
24364 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect;
24365 	}
24366 	un->un_g.dkg_acyl = 0;
24367 	un->un_g.dkg_bcyl = 0;
24368 	un->un_g.dkg_intrlv = 1;
24369 	un->un_g.dkg_rpm = 200;
24370 	un->un_g.dkg_read_reinstruct = 0;
24371 	un->un_g.dkg_write_reinstruct = 0;
24372 	if (un->un_g.dkg_pcyl == 0) {
24373 		un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl;
24374 	}
24375 
24376 	un->un_map['a'-'a'].dkl_cylno = 0;
24377 	un->un_map['a'-'a'].dkl_nblk = un->un_blockcount;
24378 	un->un_map['c'-'a'].dkl_cylno = 0;
24379 	un->un_map['c'-'a'].dkl_nblk = un->un_blockcount;
24380 	un->un_f_geometry_is_valid = FALSE;
24381 }
24382 
24383 
24384 #if defined(__i386) || defined(__amd64)
24385 /*
24386  *    Function: sd_update_fdisk_and_vtoc
24387  *
24388  * Description: This local utility routine updates the device fdisk and vtoc
24389  *		as part of setting the device mboot.
24390  *
24391  *   Arguments: un - driver soft state (unit) structure
24392  *
24393  * Return Code: 0 for success or errno-type return code.
24394  *
24395  *    Note:x86: This looks like a duplicate of sd_validate_geometry(), but
24396  *		these did exist seperately in x86 sd.c!!!
24397  */
24398 
24399 static int
24400 sd_update_fdisk_and_vtoc(struct sd_lun *un)
24401 {
24402 	static char	labelstring[128];
24403 	static char	buf[256];
24404 	char		*label = 0;
24405 	int		count;
24406 	int		label_rc = 0;
24407 	int		gvalid = un->un_f_geometry_is_valid;
24408 	int		fdisk_rval;
24409 	int		lbasize;
24410 	int		capacity;
24411 
24412 	ASSERT(mutex_owned(SD_MUTEX(un)));
24413 
24414 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
24415 		return (EINVAL);
24416 	}
24417 
24418 	if (un->un_f_blockcount_is_valid == FALSE) {
24419 		return (EINVAL);
24420 	}
24421 
24422 #if defined(_SUNOS_VTOC_16)
24423 	/*
24424 	 * Set up the "whole disk" fdisk partition; this should always
24425 	 * exist, regardless of whether the disk contains an fdisk table
24426 	 * or vtoc.
24427 	 */
24428 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
24429 	un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount;
24430 #endif	/* defined(_SUNOS_VTOC_16) */
24431 
24432 	/*
24433 	 * copy the lbasize and capacity so that if they're
24434 	 * reset while we're not holding the SD_MUTEX(un), we will
24435 	 * continue to use valid values after the SD_MUTEX(un) is
24436 	 * reacquired.
24437 	 */
24438 	lbasize  = un->un_tgt_blocksize;
24439 	capacity = un->un_blockcount;
24440 
24441 	/*
24442 	 * refresh the logical and physical geometry caches.
24443 	 * (data from mode sense format/rigid disk geometry pages,
24444 	 * and scsi_ifgetcap("geometry").
24445 	 */
24446 	sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT);
24447 
24448 	/*
24449 	 * Only DIRECT ACCESS devices will have Sun labels.
24450 	 * CD's supposedly have a Sun label, too
24451 	 */
24452 	if (un->un_f_vtoc_label_supported) {
24453 		fdisk_rval = sd_read_fdisk(un, capacity, lbasize,
24454 		    SD_PATH_DIRECT);
24455 		if (fdisk_rval == SD_CMD_FAILURE) {
24456 			ASSERT(mutex_owned(SD_MUTEX(un)));
24457 			return (EIO);
24458 		}
24459 
24460 		if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) {
24461 			ASSERT(mutex_owned(SD_MUTEX(un)));
24462 			return (EACCES);
24463 		}
24464 
24465 		if (un->un_solaris_size <= DK_LABEL_LOC) {
24466 			/*
24467 			 * Found fdisk table but no Solaris partition entry,
24468 			 * so don't call sd_uselabel() and don't create
24469 			 * a default label.
24470 			 */
24471 			label_rc = 0;
24472 			un->un_f_geometry_is_valid = TRUE;
24473 			goto no_solaris_partition;
24474 		}
24475 
24476 #if defined(_SUNOS_VTOC_8)
24477 		label = (char *)un->un_asciilabel;
24478 #elif defined(_SUNOS_VTOC_16)
24479 		label = (char *)un->un_vtoc.v_asciilabel;
24480 #else
24481 #error "No VTOC format defined."
24482 #endif
24483 	} else if (capacity < 0) {
24484 		ASSERT(mutex_owned(SD_MUTEX(un)));
24485 		return (EINVAL);
24486 	}
24487 
24488 	/*
24489 	 * For Removable media We reach here if we have found a
24490 	 * SOLARIS PARTITION.
24491 	 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS
24492 	 * PARTITION has changed from the previous one, hence we will setup a
24493 	 * default VTOC in this case.
24494 	 */
24495 	if (un->un_f_geometry_is_valid == FALSE) {
24496 		sd_build_default_label(un);
24497 		label_rc = 0;
24498 	}
24499 
24500 no_solaris_partition:
24501 	if ((!un->un_f_has_removable_media ||
24502 	    (un->un_f_has_removable_media &&
24503 	    un->un_mediastate == DKIO_EJECTED)) &&
24504 		(un->un_state == SD_STATE_NORMAL && !gvalid)) {
24505 		/*
24506 		 * Print out a message indicating who and what we are.
24507 		 * We do this only when we happen to really validate the
24508 		 * geometry. We may call sd_validate_geometry() at other
24509 		 * times, ioctl()'s like Get VTOC in which case we
24510 		 * don't want to print the label.
24511 		 * If the geometry is valid, print the label string,
24512 		 * else print vendor and product info, if available
24513 		 */
24514 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
24515 			SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label);
24516 		} else {
24517 			mutex_enter(&sd_label_mutex);
24518 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
24519 			    labelstring);
24520 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
24521 			    &labelstring[64]);
24522 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
24523 			    labelstring, &labelstring[64]);
24524 			if (un->un_f_blockcount_is_valid == TRUE) {
24525 				(void) sprintf(&buf[strlen(buf)],
24526 				    ", %" PRIu64 " %u byte blocks\n",
24527 				    un->un_blockcount,
24528 				    un->un_tgt_blocksize);
24529 			} else {
24530 				(void) sprintf(&buf[strlen(buf)],
24531 				    ", (unknown capacity)\n");
24532 			}
24533 			SD_INFO(SD_LOG_IOCTL_DKIO, un, buf);
24534 			mutex_exit(&sd_label_mutex);
24535 		}
24536 	}
24537 
24538 #if defined(_SUNOS_VTOC_16)
24539 	/*
24540 	 * If we have valid geometry, set up the remaining fdisk partitions.
24541 	 * Note that dkl_cylno is not used for the fdisk map entries, so
24542 	 * we set it to an entirely bogus value.
24543 	 */
24544 	for (count = 0; count < FD_NUMPART; count++) {
24545 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
24546 		un->un_map[FDISK_P1 + count].dkl_nblk =
24547 		    un->un_fmap[count].fmap_nblk;
24548 		un->un_offset[FDISK_P1 + count] =
24549 		    un->un_fmap[count].fmap_start;
24550 	}
24551 #endif
24552 
24553 	for (count = 0; count < NDKMAP; count++) {
24554 #if defined(_SUNOS_VTOC_8)
24555 		struct dk_map *lp  = &un->un_map[count];
24556 		un->un_offset[count] =
24557 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
24558 #elif defined(_SUNOS_VTOC_16)
24559 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
24560 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
24561 #else
24562 #error "No VTOC format defined."
24563 #endif
24564 	}
24565 
24566 	ASSERT(mutex_owned(SD_MUTEX(un)));
24567 	return (label_rc);
24568 }
24569 #endif
24570 
24571 
24572 /*
24573  *    Function: sd_check_media
24574  *
24575  * Description: This utility routine implements the functionality for the
24576  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
24577  *		driver state changes from that specified by the user
24578  *		(inserted or ejected). For example, if the user specifies
24579  *		DKIO_EJECTED and the current media state is inserted this
24580  *		routine will immediately return DKIO_INSERTED. However, if the
24581  *		current media state is not inserted the user thread will be
24582  *		blocked until the drive state changes. If DKIO_NONE is specified
24583  *		the user thread will block until a drive state change occurs.
24584  *
24585  *   Arguments: dev  - the device number
24586  *		state  - user pointer to a dkio_state, updated with the current
24587  *			drive state at return.
24588  *
24589  * Return Code: ENXIO
24590  *		EIO
24591  *		EAGAIN
24592  *		EINTR
24593  */
24594 
24595 static int
24596 sd_check_media(dev_t dev, enum dkio_state state)
24597 {
24598 	struct sd_lun		*un = NULL;
24599 	enum dkio_state		prev_state;
24600 	opaque_t		token = NULL;
24601 	int			rval = 0;
24602 
24603 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24604 		return (ENXIO);
24605 	}
24606 
24607 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
24608 
24609 	mutex_enter(SD_MUTEX(un));
24610 
24611 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
24612 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
24613 
24614 	prev_state = un->un_mediastate;
24615 
24616 	/* is there anything to do? */
24617 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
24618 		/*
24619 		 * submit the request to the scsi_watch service;
24620 		 * scsi_media_watch_cb() does the real work
24621 		 */
24622 		mutex_exit(SD_MUTEX(un));
24623 
24624 		/*
24625 		 * This change handles the case where a scsi watch request is
24626 		 * added to a device that is powered down. To accomplish this
24627 		 * we power up the device before adding the scsi watch request,
24628 		 * since the scsi watch sends a TUR directly to the device
24629 		 * which the device cannot handle if it is powered down.
24630 		 */
24631 		if (sd_pm_entry(un) != DDI_SUCCESS) {
24632 			mutex_enter(SD_MUTEX(un));
24633 			goto done;
24634 		}
24635 
24636 		token = scsi_watch_request_submit(SD_SCSI_DEVP(un),
24637 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
24638 		    (caddr_t)dev);
24639 
24640 		sd_pm_exit(un);
24641 
24642 		mutex_enter(SD_MUTEX(un));
24643 		if (token == NULL) {
24644 			rval = EAGAIN;
24645 			goto done;
24646 		}
24647 
24648 		/*
24649 		 * This is a special case IOCTL that doesn't return
24650 		 * until the media state changes. Routine sdpower
24651 		 * knows about and handles this so don't count it
24652 		 * as an active cmd in the driver, which would
24653 		 * keep the device busy to the pm framework.
24654 		 * If the count isn't decremented the device can't
24655 		 * be powered down.
24656 		 */
24657 		un->un_ncmds_in_driver--;
24658 		ASSERT(un->un_ncmds_in_driver >= 0);
24659 
24660 		/*
24661 		 * if a prior request had been made, this will be the same
24662 		 * token, as scsi_watch was designed that way.
24663 		 */
24664 		un->un_swr_token = token;
24665 		un->un_specified_mediastate = state;
24666 
24667 		/*
24668 		 * now wait for media change
24669 		 * we will not be signalled unless mediastate == state but it is
24670 		 * still better to test for this condition, since there is a
24671 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
24672 		 */
24673 		SD_TRACE(SD_LOG_COMMON, un,
24674 		    "sd_check_media: waiting for media state change\n");
24675 		while (un->un_mediastate == state) {
24676 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
24677 				SD_TRACE(SD_LOG_COMMON, un,
24678 				    "sd_check_media: waiting for media state "
24679 				    "was interrupted\n");
24680 				un->un_ncmds_in_driver++;
24681 				rval = EINTR;
24682 				goto done;
24683 			}
24684 			SD_TRACE(SD_LOG_COMMON, un,
24685 			    "sd_check_media: received signal, state=%x\n",
24686 			    un->un_mediastate);
24687 		}
24688 		/*
24689 		 * Inc the counter to indicate the device once again
24690 		 * has an active outstanding cmd.
24691 		 */
24692 		un->un_ncmds_in_driver++;
24693 	}
24694 
24695 	/* invalidate geometry */
24696 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
24697 		sr_ejected(un);
24698 	}
24699 
24700 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
24701 		uint64_t	capacity;
24702 		uint_t		lbasize;
24703 
24704 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
24705 		mutex_exit(SD_MUTEX(un));
24706 		/*
24707 		 * Since the following routines use SD_PATH_DIRECT, we must
24708 		 * call PM directly before the upcoming disk accesses. This
24709 		 * may cause the disk to be power/spin up.
24710 		 */
24711 
24712 		if (sd_pm_entry(un) == DDI_SUCCESS) {
24713 			rval = sd_send_scsi_READ_CAPACITY(un,
24714 			    &capacity,
24715 			    &lbasize, SD_PATH_DIRECT);
24716 			if (rval != 0) {
24717 				sd_pm_exit(un);
24718 				mutex_enter(SD_MUTEX(un));
24719 				goto done;
24720 			}
24721 		} else {
24722 			rval = EIO;
24723 			mutex_enter(SD_MUTEX(un));
24724 			goto done;
24725 		}
24726 		mutex_enter(SD_MUTEX(un));
24727 
24728 		sd_update_block_info(un, lbasize, capacity);
24729 
24730 		un->un_f_geometry_is_valid	= FALSE;
24731 		(void) sd_validate_geometry(un, SD_PATH_DIRECT);
24732 
24733 		mutex_exit(SD_MUTEX(un));
24734 		rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
24735 		    SD_PATH_DIRECT);
24736 		sd_pm_exit(un);
24737 
24738 		mutex_enter(SD_MUTEX(un));
24739 	}
24740 done:
24741 	un->un_f_watcht_stopped = FALSE;
24742 	if (un->un_swr_token) {
24743 		/*
24744 		 * Use of this local token and the mutex ensures that we avoid
24745 		 * some race conditions associated with terminating the
24746 		 * scsi watch.
24747 		 */
24748 		token = un->un_swr_token;
24749 		un->un_swr_token = (opaque_t)NULL;
24750 		mutex_exit(SD_MUTEX(un));
24751 		(void) scsi_watch_request_terminate(token,
24752 		    SCSI_WATCH_TERMINATE_WAIT);
24753 		mutex_enter(SD_MUTEX(un));
24754 	}
24755 
24756 	/*
24757 	 * Update the capacity kstat value, if no media previously
24758 	 * (capacity kstat is 0) and a media has been inserted
24759 	 * (un_f_blockcount_is_valid == TRUE)
24760 	 */
24761 	if (un->un_errstats) {
24762 		struct sd_errstats	*stp = NULL;
24763 
24764 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
24765 		if ((stp->sd_capacity.value.ui64 == 0) &&
24766 		    (un->un_f_blockcount_is_valid == TRUE)) {
24767 			stp->sd_capacity.value.ui64 =
24768 			    (uint64_t)((uint64_t)un->un_blockcount *
24769 			    un->un_sys_blocksize);
24770 		}
24771 	}
24772 	mutex_exit(SD_MUTEX(un));
24773 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
24774 	return (rval);
24775 }
24776 
24777 
24778 /*
24779  *    Function: sd_delayed_cv_broadcast
24780  *
24781  * Description: Delayed cv_broadcast to allow for target to recover from media
24782  *		insertion.
24783  *
24784  *   Arguments: arg - driver soft state (unit) structure
24785  */
24786 
24787 static void
24788 sd_delayed_cv_broadcast(void *arg)
24789 {
24790 	struct sd_lun *un = arg;
24791 
24792 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
24793 
24794 	mutex_enter(SD_MUTEX(un));
24795 	un->un_dcvb_timeid = NULL;
24796 	cv_broadcast(&un->un_state_cv);
24797 	mutex_exit(SD_MUTEX(un));
24798 }
24799 
24800 
24801 /*
24802  *    Function: sd_media_watch_cb
24803  *
24804  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
24805  *		routine processes the TUR sense data and updates the driver
24806  *		state if a transition has occurred. The user thread
24807  *		(sd_check_media) is then signalled.
24808  *
24809  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24810  *			among multiple watches that share this callback function
24811  *		resultp - scsi watch facility result packet containing scsi
24812  *			  packet, status byte and sense data
24813  *
24814  * Return Code: 0 for success, -1 for failure
24815  */
24816 
24817 static int
24818 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24819 {
24820 	struct sd_lun			*un;
24821 	struct scsi_status		*statusp = resultp->statusp;
24822 	uint8_t				*sensep = (uint8_t *)resultp->sensep;
24823 	enum dkio_state			state = DKIO_NONE;
24824 	dev_t				dev = (dev_t)arg;
24825 	uchar_t				actual_sense_length;
24826 	uint8_t				skey, asc, ascq;
24827 
24828 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24829 		return (-1);
24830 	}
24831 	actual_sense_length = resultp->actual_sense_length;
24832 
24833 	mutex_enter(SD_MUTEX(un));
24834 	SD_TRACE(SD_LOG_COMMON, un,
24835 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
24836 	    *((char *)statusp), (void *)sensep, actual_sense_length);
24837 
24838 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
24839 		un->un_mediastate = DKIO_DEV_GONE;
24840 		cv_broadcast(&un->un_state_cv);
24841 		mutex_exit(SD_MUTEX(un));
24842 
24843 		return (0);
24844 	}
24845 
24846 	/*
24847 	 * If there was a check condition then sensep points to valid sense data
24848 	 * If status was not a check condition but a reservation or busy status
24849 	 * then the new state is DKIO_NONE
24850 	 */
24851 	if (sensep != NULL) {
24852 		skey = scsi_sense_key(sensep);
24853 		asc = scsi_sense_asc(sensep);
24854 		ascq = scsi_sense_ascq(sensep);
24855 
24856 		SD_INFO(SD_LOG_COMMON, un,
24857 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
24858 		    skey, asc, ascq);
24859 		/* This routine only uses up to 13 bytes of sense data. */
24860 		if (actual_sense_length >= 13) {
24861 			if (skey == KEY_UNIT_ATTENTION) {
24862 				if (asc == 0x28) {
24863 					state = DKIO_INSERTED;
24864 				}
24865 			} else {
24866 				/*
24867 				 * if 02/04/02  means that the host
24868 				 * should send start command. Explicitly
24869 				 * leave the media state as is
24870 				 * (inserted) as the media is inserted
24871 				 * and host has stopped device for PM
24872 				 * reasons. Upon next true read/write
24873 				 * to this media will bring the
24874 				 * device to the right state good for
24875 				 * media access.
24876 				 */
24877 				if ((skey == KEY_NOT_READY) &&
24878 				    (asc == 0x3a)) {
24879 					state = DKIO_EJECTED;
24880 				}
24881 
24882 				/*
24883 				 * If the drivge is busy with an operation
24884 				 * or long write, keep the media in an
24885 				 * inserted state.
24886 				 */
24887 
24888 				if ((skey == KEY_NOT_READY) &&
24889 				    (asc == 0x04) &&
24890 				    ((ascq == 0x02) ||
24891 				    (ascq == 0x07) ||
24892 				    (ascq == 0x08))) {
24893 					state = DKIO_INSERTED;
24894 				}
24895 			}
24896 		}
24897 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
24898 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24899 		state = DKIO_INSERTED;
24900 	}
24901 
24902 	SD_TRACE(SD_LOG_COMMON, un,
24903 	    "sd_media_watch_cb: state=%x, specified=%x\n",
24904 	    state, un->un_specified_mediastate);
24905 
24906 	/*
24907 	 * now signal the waiting thread if this is *not* the specified state;
24908 	 * delay the signal if the state is DKIO_INSERTED to allow the target
24909 	 * to recover
24910 	 */
24911 	if (state != un->un_specified_mediastate) {
24912 		un->un_mediastate = state;
24913 		if (state == DKIO_INSERTED) {
24914 			/*
24915 			 * delay the signal to give the drive a chance
24916 			 * to do what it apparently needs to do
24917 			 */
24918 			SD_TRACE(SD_LOG_COMMON, un,
24919 			    "sd_media_watch_cb: delayed cv_broadcast\n");
24920 			if (un->un_dcvb_timeid == NULL) {
24921 				un->un_dcvb_timeid =
24922 				    timeout(sd_delayed_cv_broadcast, un,
24923 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24924 			}
24925 		} else {
24926 			SD_TRACE(SD_LOG_COMMON, un,
24927 			    "sd_media_watch_cb: immediate cv_broadcast\n");
24928 			cv_broadcast(&un->un_state_cv);
24929 		}
24930 	}
24931 	mutex_exit(SD_MUTEX(un));
24932 	return (0);
24933 }
24934 
24935 
24936 /*
24937  *    Function: sd_dkio_get_temp
24938  *
24939  * Description: This routine is the driver entry point for handling ioctl
24940  *		requests to get the disk temperature.
24941  *
24942  *   Arguments: dev  - the device number
24943  *		arg  - pointer to user provided dk_temperature structure.
24944  *		flag - this argument is a pass through to ddi_copyxxx()
24945  *		       directly from the mode argument of ioctl().
24946  *
24947  * Return Code: 0
24948  *		EFAULT
24949  *		ENXIO
24950  *		EAGAIN
24951  */
24952 
24953 static int
24954 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24955 {
24956 	struct sd_lun		*un = NULL;
24957 	struct dk_temperature	*dktemp = NULL;
24958 	uchar_t			*temperature_page;
24959 	int			rval = 0;
24960 	int			path_flag = SD_PATH_STANDARD;
24961 
24962 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24963 		return (ENXIO);
24964 	}
24965 
24966 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24967 
24968 	/* copyin the disk temp argument to get the user flags */
24969 	if (ddi_copyin((void *)arg, dktemp,
24970 	    sizeof (struct dk_temperature), flag) != 0) {
24971 		rval = EFAULT;
24972 		goto done;
24973 	}
24974 
24975 	/* Initialize the temperature to invalid. */
24976 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24977 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24978 
24979 	/*
24980 	 * Note: Investigate removing the "bypass pm" semantic.
24981 	 * Can we just bypass PM always?
24982 	 */
24983 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24984 		path_flag = SD_PATH_DIRECT;
24985 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24986 		mutex_enter(&un->un_pm_mutex);
24987 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24988 			/*
24989 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24990 			 * in low power mode, we can not wake it up, Need to
24991 			 * return EAGAIN.
24992 			 */
24993 			mutex_exit(&un->un_pm_mutex);
24994 			rval = EAGAIN;
24995 			goto done;
24996 		} else {
24997 			/*
24998 			 * Indicate to PM the device is busy. This is required
24999 			 * to avoid a race - i.e. the ioctl is issuing a
25000 			 * command and the pm framework brings down the device
25001 			 * to low power mode (possible power cut-off on some
25002 			 * platforms).
25003 			 */
25004 			mutex_exit(&un->un_pm_mutex);
25005 			if (sd_pm_entry(un) != DDI_SUCCESS) {
25006 				rval = EAGAIN;
25007 				goto done;
25008 			}
25009 		}
25010 	}
25011 
25012 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
25013 
25014 	if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page,
25015 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) {
25016 		goto done2;
25017 	}
25018 
25019 	/*
25020 	 * For the current temperature verify that the parameter length is 0x02
25021 	 * and the parameter code is 0x00
25022 	 */
25023 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
25024 	    (temperature_page[5] == 0x00)) {
25025 		if (temperature_page[9] == 0xFF) {
25026 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
25027 		} else {
25028 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
25029 		}
25030 	}
25031 
25032 	/*
25033 	 * For the reference temperature verify that the parameter
25034 	 * length is 0x02 and the parameter code is 0x01
25035 	 */
25036 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
25037 	    (temperature_page[11] == 0x01)) {
25038 		if (temperature_page[15] == 0xFF) {
25039 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
25040 		} else {
25041 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
25042 		}
25043 	}
25044 
25045 	/* Do the copyout regardless of the temperature commands status. */
25046 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
25047 	    flag) != 0) {
25048 		rval = EFAULT;
25049 	}
25050 
25051 done2:
25052 	if (path_flag == SD_PATH_DIRECT) {
25053 		sd_pm_exit(un);
25054 	}
25055 
25056 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
25057 done:
25058 	if (dktemp != NULL) {
25059 		kmem_free(dktemp, sizeof (struct dk_temperature));
25060 	}
25061 
25062 	return (rval);
25063 }
25064 
25065 
25066 /*
25067  *    Function: sd_log_page_supported
25068  *
25069  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
25070  *		supported log pages.
25071  *
25072  *   Arguments: un -
25073  *		log_page -
25074  *
25075  * Return Code: -1 - on error (log sense is optional and may not be supported).
25076  *		0  - log page not found.
25077  *  		1  - log page found.
25078  */
25079 
25080 static int
25081 sd_log_page_supported(struct sd_lun *un, int log_page)
25082 {
25083 	uchar_t *log_page_data;
25084 	int	i;
25085 	int	match = 0;
25086 	int	log_size;
25087 
25088 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
25089 
25090 	if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0,
25091 	    SD_PATH_DIRECT) != 0) {
25092 		SD_ERROR(SD_LOG_COMMON, un,
25093 		    "sd_log_page_supported: failed log page retrieval\n");
25094 		kmem_free(log_page_data, 0xFF);
25095 		return (-1);
25096 	}
25097 	log_size = log_page_data[3];
25098 
25099 	/*
25100 	 * The list of supported log pages start from the fourth byte. Check
25101 	 * until we run out of log pages or a match is found.
25102 	 */
25103 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
25104 		if (log_page_data[i] == log_page) {
25105 			match++;
25106 		}
25107 	}
25108 	kmem_free(log_page_data, 0xFF);
25109 	return (match);
25110 }
25111 
25112 
25113 /*
25114  *    Function: sd_mhdioc_failfast
25115  *
25116  * Description: This routine is the driver entry point for handling ioctl
25117  *		requests to enable/disable the multihost failfast option.
25118  *		(MHIOCENFAILFAST)
25119  *
25120  *   Arguments: dev	- the device number
25121  *		arg	- user specified probing interval.
25122  *		flag	- this argument is a pass through to ddi_copyxxx()
25123  *			  directly from the mode argument of ioctl().
25124  *
25125  * Return Code: 0
25126  *		EFAULT
25127  *		ENXIO
25128  */
25129 
25130 static int
25131 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
25132 {
25133 	struct sd_lun	*un = NULL;
25134 	int		mh_time;
25135 	int		rval = 0;
25136 
25137 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25138 		return (ENXIO);
25139 	}
25140 
25141 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
25142 		return (EFAULT);
25143 
25144 	if (mh_time) {
25145 		mutex_enter(SD_MUTEX(un));
25146 		un->un_resvd_status |= SD_FAILFAST;
25147 		mutex_exit(SD_MUTEX(un));
25148 		/*
25149 		 * If mh_time is INT_MAX, then this ioctl is being used for
25150 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
25151 		 */
25152 		if (mh_time != INT_MAX) {
25153 			rval = sd_check_mhd(dev, mh_time);
25154 		}
25155 	} else {
25156 		(void) sd_check_mhd(dev, 0);
25157 		mutex_enter(SD_MUTEX(un));
25158 		un->un_resvd_status &= ~SD_FAILFAST;
25159 		mutex_exit(SD_MUTEX(un));
25160 	}
25161 	return (rval);
25162 }
25163 
25164 
25165 /*
25166  *    Function: sd_mhdioc_takeown
25167  *
25168  * Description: This routine is the driver entry point for handling ioctl
25169  *		requests to forcefully acquire exclusive access rights to the
25170  *		multihost disk (MHIOCTKOWN).
25171  *
25172  *   Arguments: dev	- the device number
25173  *		arg	- user provided structure specifying the delay
25174  *			  parameters in milliseconds
25175  *		flag	- this argument is a pass through to ddi_copyxxx()
25176  *			  directly from the mode argument of ioctl().
25177  *
25178  * Return Code: 0
25179  *		EFAULT
25180  *		ENXIO
25181  */
25182 
25183 static int
25184 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
25185 {
25186 	struct sd_lun		*un = NULL;
25187 	struct mhioctkown	*tkown = NULL;
25188 	int			rval = 0;
25189 
25190 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25191 		return (ENXIO);
25192 	}
25193 
25194 	if (arg != NULL) {
25195 		tkown = (struct mhioctkown *)
25196 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
25197 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
25198 		if (rval != 0) {
25199 			rval = EFAULT;
25200 			goto error;
25201 		}
25202 	}
25203 
25204 	rval = sd_take_ownership(dev, tkown);
25205 	mutex_enter(SD_MUTEX(un));
25206 	if (rval == 0) {
25207 		un->un_resvd_status |= SD_RESERVE;
25208 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
25209 			sd_reinstate_resv_delay =
25210 			    tkown->reinstate_resv_delay * 1000;
25211 		} else {
25212 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
25213 		}
25214 		/*
25215 		 * Give the scsi_watch routine interval set by
25216 		 * the MHIOCENFAILFAST ioctl precedence here.
25217 		 */
25218 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
25219 			mutex_exit(SD_MUTEX(un));
25220 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
25221 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
25222 			    "sd_mhdioc_takeown : %d\n",
25223 			    sd_reinstate_resv_delay);
25224 		} else {
25225 			mutex_exit(SD_MUTEX(un));
25226 		}
25227 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
25228 		    sd_mhd_reset_notify_cb, (caddr_t)un);
25229 	} else {
25230 		un->un_resvd_status &= ~SD_RESERVE;
25231 		mutex_exit(SD_MUTEX(un));
25232 	}
25233 
25234 error:
25235 	if (tkown != NULL) {
25236 		kmem_free(tkown, sizeof (struct mhioctkown));
25237 	}
25238 	return (rval);
25239 }
25240 
25241 
25242 /*
25243  *    Function: sd_mhdioc_release
25244  *
25245  * Description: This routine is the driver entry point for handling ioctl
25246  *		requests to release exclusive access rights to the multihost
25247  *		disk (MHIOCRELEASE).
25248  *
25249  *   Arguments: dev	- the device number
25250  *
25251  * Return Code: 0
25252  *		ENXIO
25253  */
25254 
25255 static int
25256 sd_mhdioc_release(dev_t dev)
25257 {
25258 	struct sd_lun		*un = NULL;
25259 	timeout_id_t		resvd_timeid_save;
25260 	int			resvd_status_save;
25261 	int			rval = 0;
25262 
25263 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25264 		return (ENXIO);
25265 	}
25266 
25267 	mutex_enter(SD_MUTEX(un));
25268 	resvd_status_save = un->un_resvd_status;
25269 	un->un_resvd_status &=
25270 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
25271 	if (un->un_resvd_timeid) {
25272 		resvd_timeid_save = un->un_resvd_timeid;
25273 		un->un_resvd_timeid = NULL;
25274 		mutex_exit(SD_MUTEX(un));
25275 		(void) untimeout(resvd_timeid_save);
25276 	} else {
25277 		mutex_exit(SD_MUTEX(un));
25278 	}
25279 
25280 	/*
25281 	 * destroy any pending timeout thread that may be attempting to
25282 	 * reinstate reservation on this device.
25283 	 */
25284 	sd_rmv_resv_reclaim_req(dev);
25285 
25286 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
25287 		mutex_enter(SD_MUTEX(un));
25288 		if ((un->un_mhd_token) &&
25289 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
25290 			mutex_exit(SD_MUTEX(un));
25291 			(void) sd_check_mhd(dev, 0);
25292 		} else {
25293 			mutex_exit(SD_MUTEX(un));
25294 		}
25295 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
25296 		    sd_mhd_reset_notify_cb, (caddr_t)un);
25297 	} else {
25298 		/*
25299 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
25300 		 */
25301 		mutex_enter(SD_MUTEX(un));
25302 		un->un_resvd_status = resvd_status_save;
25303 		mutex_exit(SD_MUTEX(un));
25304 	}
25305 	return (rval);
25306 }
25307 
25308 
25309 /*
25310  *    Function: sd_mhdioc_register_devid
25311  *
25312  * Description: This routine is the driver entry point for handling ioctl
25313  *		requests to register the device id (MHIOCREREGISTERDEVID).
25314  *
25315  *		Note: The implementation for this ioctl has been updated to
25316  *		be consistent with the original PSARC case (1999/357)
25317  *		(4375899, 4241671, 4220005)
25318  *
25319  *   Arguments: dev	- the device number
25320  *
25321  * Return Code: 0
25322  *		ENXIO
25323  */
25324 
25325 static int
25326 sd_mhdioc_register_devid(dev_t dev)
25327 {
25328 	struct sd_lun	*un = NULL;
25329 	int		rval = 0;
25330 
25331 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25332 		return (ENXIO);
25333 	}
25334 
25335 	ASSERT(!mutex_owned(SD_MUTEX(un)));
25336 
25337 	mutex_enter(SD_MUTEX(un));
25338 
25339 	/* If a devid already exists, de-register it */
25340 	if (un->un_devid != NULL) {
25341 		ddi_devid_unregister(SD_DEVINFO(un));
25342 		/*
25343 		 * After unregister devid, needs to free devid memory
25344 		 */
25345 		ddi_devid_free(un->un_devid);
25346 		un->un_devid = NULL;
25347 	}
25348 
25349 	/* Check for reservation conflict */
25350 	mutex_exit(SD_MUTEX(un));
25351 	rval = sd_send_scsi_TEST_UNIT_READY(un, 0);
25352 	mutex_enter(SD_MUTEX(un));
25353 
25354 	switch (rval) {
25355 	case 0:
25356 		sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
25357 		break;
25358 	case EACCES:
25359 		break;
25360 	default:
25361 		rval = EIO;
25362 	}
25363 
25364 	mutex_exit(SD_MUTEX(un));
25365 	return (rval);
25366 }
25367 
25368 
25369 /*
25370  *    Function: sd_mhdioc_inkeys
25371  *
25372  * Description: This routine is the driver entry point for handling ioctl
25373  *		requests to issue the SCSI-3 Persistent In Read Keys command
25374  *		to the device (MHIOCGRP_INKEYS).
25375  *
25376  *   Arguments: dev	- the device number
25377  *		arg	- user provided in_keys structure
25378  *		flag	- this argument is a pass through to ddi_copyxxx()
25379  *			  directly from the mode argument of ioctl().
25380  *
25381  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
25382  *		ENXIO
25383  *		EFAULT
25384  */
25385 
25386 static int
25387 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
25388 {
25389 	struct sd_lun		*un;
25390 	mhioc_inkeys_t		inkeys;
25391 	int			rval = 0;
25392 
25393 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25394 		return (ENXIO);
25395 	}
25396 
25397 #ifdef _MULTI_DATAMODEL
25398 	switch (ddi_model_convert_from(flag & FMODELS)) {
25399 	case DDI_MODEL_ILP32: {
25400 		struct mhioc_inkeys32	inkeys32;
25401 
25402 		if (ddi_copyin(arg, &inkeys32,
25403 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
25404 			return (EFAULT);
25405 		}
25406 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
25407 		if ((rval = sd_persistent_reservation_in_read_keys(un,
25408 		    &inkeys, flag)) != 0) {
25409 			return (rval);
25410 		}
25411 		inkeys32.generation = inkeys.generation;
25412 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
25413 		    flag) != 0) {
25414 			return (EFAULT);
25415 		}
25416 		break;
25417 	}
25418 	case DDI_MODEL_NONE:
25419 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
25420 		    flag) != 0) {
25421 			return (EFAULT);
25422 		}
25423 		if ((rval = sd_persistent_reservation_in_read_keys(un,
25424 		    &inkeys, flag)) != 0) {
25425 			return (rval);
25426 		}
25427 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
25428 		    flag) != 0) {
25429 			return (EFAULT);
25430 		}
25431 		break;
25432 	}
25433 
25434 #else /* ! _MULTI_DATAMODEL */
25435 
25436 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
25437 		return (EFAULT);
25438 	}
25439 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
25440 	if (rval != 0) {
25441 		return (rval);
25442 	}
25443 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
25444 		return (EFAULT);
25445 	}
25446 
25447 #endif /* _MULTI_DATAMODEL */
25448 
25449 	return (rval);
25450 }
25451 
25452 
25453 /*
25454  *    Function: sd_mhdioc_inresv
25455  *
25456  * Description: This routine is the driver entry point for handling ioctl
25457  *		requests to issue the SCSI-3 Persistent In Read Reservations
25458  *		command to the device (MHIOCGRP_INKEYS).
25459  *
25460  *   Arguments: dev	- the device number
25461  *		arg	- user provided in_resv structure
25462  *		flag	- this argument is a pass through to ddi_copyxxx()
25463  *			  directly from the mode argument of ioctl().
25464  *
25465  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
25466  *		ENXIO
25467  *		EFAULT
25468  */
25469 
25470 static int
25471 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
25472 {
25473 	struct sd_lun		*un;
25474 	mhioc_inresvs_t		inresvs;
25475 	int			rval = 0;
25476 
25477 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25478 		return (ENXIO);
25479 	}
25480 
25481 #ifdef _MULTI_DATAMODEL
25482 
25483 	switch (ddi_model_convert_from(flag & FMODELS)) {
25484 	case DDI_MODEL_ILP32: {
25485 		struct mhioc_inresvs32	inresvs32;
25486 
25487 		if (ddi_copyin(arg, &inresvs32,
25488 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
25489 			return (EFAULT);
25490 		}
25491 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
25492 		if ((rval = sd_persistent_reservation_in_read_resv(un,
25493 		    &inresvs, flag)) != 0) {
25494 			return (rval);
25495 		}
25496 		inresvs32.generation = inresvs.generation;
25497 		if (ddi_copyout(&inresvs32, arg,
25498 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
25499 			return (EFAULT);
25500 		}
25501 		break;
25502 	}
25503 	case DDI_MODEL_NONE:
25504 		if (ddi_copyin(arg, &inresvs,
25505 		    sizeof (mhioc_inresvs_t), flag) != 0) {
25506 			return (EFAULT);
25507 		}
25508 		if ((rval = sd_persistent_reservation_in_read_resv(un,
25509 		    &inresvs, flag)) != 0) {
25510 			return (rval);
25511 		}
25512 		if (ddi_copyout(&inresvs, arg,
25513 		    sizeof (mhioc_inresvs_t), flag) != 0) {
25514 			return (EFAULT);
25515 		}
25516 		break;
25517 	}
25518 
25519 #else /* ! _MULTI_DATAMODEL */
25520 
25521 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
25522 		return (EFAULT);
25523 	}
25524 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
25525 	if (rval != 0) {
25526 		return (rval);
25527 	}
25528 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
25529 		return (EFAULT);
25530 	}
25531 
25532 #endif /* ! _MULTI_DATAMODEL */
25533 
25534 	return (rval);
25535 }
25536 
25537 
25538 /*
25539  * The following routines support the clustering functionality described below
25540  * and implement lost reservation reclaim functionality.
25541  *
25542  * Clustering
25543  * ----------
25544  * The clustering code uses two different, independent forms of SCSI
25545  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
25546  * Persistent Group Reservations. For any particular disk, it will use either
25547  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
25548  *
25549  * SCSI-2
25550  * The cluster software takes ownership of a multi-hosted disk by issuing the
25551  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
25552  * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster,
25553  * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues
25554  * the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the driver. The
25555  * meaning of failfast is that if the driver (on this host) ever encounters the
25556  * scsi error return code RESERVATION_CONFLICT from the device, it should
25557  * immediately panic the host. The motivation for this ioctl is that if this
25558  * host does encounter reservation conflict, the underlying cause is that some
25559  * other host of the cluster has decided that this host is no longer in the
25560  * cluster and has seized control of the disks for itself. Since this host is no
25561  * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl
25562  * does two things:
25563  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
25564  *      error to panic the host
25565  *      (b) it sets up a periodic timer to test whether this host still has
25566  *      "access" (in that no other host has reserved the device):  if the
25567  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
25568  *      purpose of that periodic timer is to handle scenarios where the host is
25569  *      otherwise temporarily quiescent, temporarily doing no real i/o.
25570  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
25571  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
25572  * the device itself.
25573  *
25574  * SCSI-3 PGR
25575  * A direct semantic implementation of the SCSI-3 Persistent Reservation
25576  * facility is supported through the shared multihost disk ioctls
25577  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
25578  * MHIOCGRP_PREEMPTANDABORT)
25579  *
25580  * Reservation Reclaim:
25581  * --------------------
25582  * To support the lost reservation reclaim operations this driver creates a
25583  * single thread to handle reinstating reservations on all devices that have
25584  * lost reservations sd_resv_reclaim_requests are logged for all devices that
25585  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
25586  * and the reservation reclaim thread loops through the requests to regain the
25587  * lost reservations.
25588  */
25589 
25590 /*
25591  *    Function: sd_check_mhd()
25592  *
25593  * Description: This function sets up and submits a scsi watch request or
25594  *		terminates an existing watch request. This routine is used in
25595  *		support of reservation reclaim.
25596  *
25597  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
25598  *			 among multiple watches that share the callback function
25599  *		interval - the number of microseconds specifying the watch
25600  *			   interval for issuing TEST UNIT READY commands. If
25601  *			   set to 0 the watch should be terminated. If the
25602  *			   interval is set to 0 and if the device is required
25603  *			   to hold reservation while disabling failfast, the
25604  *			   watch is restarted with an interval of
25605  *			   reinstate_resv_delay.
25606  *
25607  * Return Code: 0	   - Successful submit/terminate of scsi watch request
25608  *		ENXIO      - Indicates an invalid device was specified
25609  *		EAGAIN     - Unable to submit the scsi watch request
25610  */
25611 
25612 static int
25613 sd_check_mhd(dev_t dev, int interval)
25614 {
25615 	struct sd_lun	*un;
25616 	opaque_t	token;
25617 
25618 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25619 		return (ENXIO);
25620 	}
25621 
25622 	/* is this a watch termination request? */
25623 	if (interval == 0) {
25624 		mutex_enter(SD_MUTEX(un));
25625 		/* if there is an existing watch task then terminate it */
25626 		if (un->un_mhd_token) {
25627 			token = un->un_mhd_token;
25628 			un->un_mhd_token = NULL;
25629 			mutex_exit(SD_MUTEX(un));
25630 			(void) scsi_watch_request_terminate(token,
25631 			    SCSI_WATCH_TERMINATE_WAIT);
25632 			mutex_enter(SD_MUTEX(un));
25633 		} else {
25634 			mutex_exit(SD_MUTEX(un));
25635 			/*
25636 			 * Note: If we return here we don't check for the
25637 			 * failfast case. This is the original legacy
25638 			 * implementation but perhaps we should be checking
25639 			 * the failfast case.
25640 			 */
25641 			return (0);
25642 		}
25643 		/*
25644 		 * If the device is required to hold reservation while
25645 		 * disabling failfast, we need to restart the scsi_watch
25646 		 * routine with an interval of reinstate_resv_delay.
25647 		 */
25648 		if (un->un_resvd_status & SD_RESERVE) {
25649 			interval = sd_reinstate_resv_delay/1000;
25650 		} else {
25651 			/* no failfast so bail */
25652 			mutex_exit(SD_MUTEX(un));
25653 			return (0);
25654 		}
25655 		mutex_exit(SD_MUTEX(un));
25656 	}
25657 
25658 	/*
25659 	 * adjust minimum time interval to 1 second,
25660 	 * and convert from msecs to usecs
25661 	 */
25662 	if (interval > 0 && interval < 1000) {
25663 		interval = 1000;
25664 	}
25665 	interval *= 1000;
25666 
25667 	/*
25668 	 * submit the request to the scsi_watch service
25669 	 */
25670 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
25671 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
25672 	if (token == NULL) {
25673 		return (EAGAIN);
25674 	}
25675 
25676 	/*
25677 	 * save token for termination later on
25678 	 */
25679 	mutex_enter(SD_MUTEX(un));
25680 	un->un_mhd_token = token;
25681 	mutex_exit(SD_MUTEX(un));
25682 	return (0);
25683 }
25684 
25685 
25686 /*
25687  *    Function: sd_mhd_watch_cb()
25688  *
25689  * Description: This function is the call back function used by the scsi watch
25690  *		facility. The scsi watch facility sends the "Test Unit Ready"
25691  *		and processes the status. If applicable (i.e. a "Unit Attention"
25692  *		status and automatic "Request Sense" not used) the scsi watch
25693  *		facility will send a "Request Sense" and retrieve the sense data
25694  *		to be passed to this callback function. In either case the
25695  *		automatic "Request Sense" or the facility submitting one, this
25696  *		callback is passed the status and sense data.
25697  *
25698  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25699  *			among multiple watches that share this callback function
25700  *		resultp - scsi watch facility result packet containing scsi
25701  *			  packet, status byte and sense data
25702  *
25703  * Return Code: 0 - continue the watch task
25704  *		non-zero - terminate the watch task
25705  */
25706 
25707 static int
25708 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
25709 {
25710 	struct sd_lun			*un;
25711 	struct scsi_status		*statusp;
25712 	uint8_t				*sensep;
25713 	struct scsi_pkt			*pkt;
25714 	uchar_t				actual_sense_length;
25715 	dev_t  				dev = (dev_t)arg;
25716 
25717 	ASSERT(resultp != NULL);
25718 	statusp			= resultp->statusp;
25719 	sensep			= (uint8_t *)resultp->sensep;
25720 	pkt			= resultp->pkt;
25721 	actual_sense_length	= resultp->actual_sense_length;
25722 
25723 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25724 		return (ENXIO);
25725 	}
25726 
25727 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25728 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
25729 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
25730 
25731 	/* Begin processing of the status and/or sense data */
25732 	if (pkt->pkt_reason != CMD_CMPLT) {
25733 		/* Handle the incomplete packet */
25734 		sd_mhd_watch_incomplete(un, pkt);
25735 		return (0);
25736 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
25737 		if (*((unsigned char *)statusp)
25738 		    == STATUS_RESERVATION_CONFLICT) {
25739 			/*
25740 			 * Handle a reservation conflict by panicking if
25741 			 * configured for failfast or by logging the conflict
25742 			 * and updating the reservation status
25743 			 */
25744 			mutex_enter(SD_MUTEX(un));
25745 			if ((un->un_resvd_status & SD_FAILFAST) &&
25746 			    (sd_failfast_enable)) {
25747 				sd_panic_for_res_conflict(un);
25748 				/*NOTREACHED*/
25749 			}
25750 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25751 			    "sd_mhd_watch_cb: Reservation Conflict\n");
25752 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
25753 			mutex_exit(SD_MUTEX(un));
25754 		}
25755 	}
25756 
25757 	if (sensep != NULL) {
25758 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
25759 			mutex_enter(SD_MUTEX(un));
25760 			if ((scsi_sense_asc(sensep) ==
25761 			    SD_SCSI_RESET_SENSE_CODE) &&
25762 			    (un->un_resvd_status & SD_RESERVE)) {
25763 				/*
25764 				 * The additional sense code indicates a power
25765 				 * on or bus device reset has occurred; update
25766 				 * the reservation status.
25767 				 */
25768 				un->un_resvd_status |=
25769 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25770 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25771 				    "sd_mhd_watch_cb: Lost Reservation\n");
25772 			}
25773 		} else {
25774 			return (0);
25775 		}
25776 	} else {
25777 		mutex_enter(SD_MUTEX(un));
25778 	}
25779 
25780 	if ((un->un_resvd_status & SD_RESERVE) &&
25781 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
25782 		if (un->un_resvd_status & SD_WANT_RESERVE) {
25783 			/*
25784 			 * A reset occurred in between the last probe and this
25785 			 * one so if a timeout is pending cancel it.
25786 			 */
25787 			if (un->un_resvd_timeid) {
25788 				timeout_id_t temp_id = un->un_resvd_timeid;
25789 				un->un_resvd_timeid = NULL;
25790 				mutex_exit(SD_MUTEX(un));
25791 				(void) untimeout(temp_id);
25792 				mutex_enter(SD_MUTEX(un));
25793 			}
25794 			un->un_resvd_status &= ~SD_WANT_RESERVE;
25795 		}
25796 		if (un->un_resvd_timeid == 0) {
25797 			/* Schedule a timeout to handle the lost reservation */
25798 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25799 			    (void *)dev,
25800 			    drv_usectohz(sd_reinstate_resv_delay));
25801 		}
25802 	}
25803 	mutex_exit(SD_MUTEX(un));
25804 	return (0);
25805 }
25806 
25807 
25808 /*
25809  *    Function: sd_mhd_watch_incomplete()
25810  *
25811  * Description: This function is used to find out why a scsi pkt sent by the
25812  *		scsi watch facility was not completed. Under some scenarios this
25813  *		routine will return. Otherwise it will send a bus reset to see
25814  *		if the drive is still online.
25815  *
25816  *   Arguments: un  - driver soft state (unit) structure
25817  *		pkt - incomplete scsi pkt
25818  */
25819 
25820 static void
25821 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25822 {
25823 	int	be_chatty;
25824 	int	perr;
25825 
25826 	ASSERT(pkt != NULL);
25827 	ASSERT(un != NULL);
25828 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
25829 	perr		= (pkt->pkt_statistics & STAT_PERR);
25830 
25831 	mutex_enter(SD_MUTEX(un));
25832 	if (un->un_state == SD_STATE_DUMPING) {
25833 		mutex_exit(SD_MUTEX(un));
25834 		return;
25835 	}
25836 
25837 	switch (pkt->pkt_reason) {
25838 	case CMD_UNX_BUS_FREE:
25839 		/*
25840 		 * If we had a parity error that caused the target to drop BSY*,
25841 		 * don't be chatty about it.
25842 		 */
25843 		if (perr && be_chatty) {
25844 			be_chatty = 0;
25845 		}
25846 		break;
25847 	case CMD_TAG_REJECT:
25848 		/*
25849 		 * The SCSI-2 spec states that a tag reject will be sent by the
25850 		 * target if tagged queuing is not supported. A tag reject may
25851 		 * also be sent during certain initialization periods or to
25852 		 * control internal resources. For the latter case the target
25853 		 * may also return Queue Full.
25854 		 *
25855 		 * If this driver receives a tag reject from a target that is
25856 		 * going through an init period or controlling internal
25857 		 * resources tagged queuing will be disabled. This is a less
25858 		 * than optimal behavior but the driver is unable to determine
25859 		 * the target state and assumes tagged queueing is not supported
25860 		 */
25861 		pkt->pkt_flags = 0;
25862 		un->un_tagflags = 0;
25863 
25864 		if (un->un_f_opt_queueing == TRUE) {
25865 			un->un_throttle = min(un->un_throttle, 3);
25866 		} else {
25867 			un->un_throttle = 1;
25868 		}
25869 		mutex_exit(SD_MUTEX(un));
25870 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25871 		mutex_enter(SD_MUTEX(un));
25872 		break;
25873 	case CMD_INCOMPLETE:
25874 		/*
25875 		 * The transport stopped with an abnormal state, fallthrough and
25876 		 * reset the target and/or bus unless selection did not complete
25877 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
25878 		 * go through a target/bus reset
25879 		 */
25880 		if (pkt->pkt_state == STATE_GOT_BUS) {
25881 			break;
25882 		}
25883 		/*FALLTHROUGH*/
25884 
25885 	case CMD_TIMEOUT:
25886 	default:
25887 		/*
25888 		 * The lun may still be running the command, so a lun reset
25889 		 * should be attempted. If the lun reset fails or cannot be
25890 		 * issued, than try a target reset. Lastly try a bus reset.
25891 		 */
25892 		if ((pkt->pkt_statistics &
25893 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25894 			int reset_retval = 0;
25895 			mutex_exit(SD_MUTEX(un));
25896 			if (un->un_f_allow_bus_device_reset == TRUE) {
25897 				if (un->un_f_lun_reset_enabled == TRUE) {
25898 					reset_retval =
25899 					    scsi_reset(SD_ADDRESS(un),
25900 					    RESET_LUN);
25901 				}
25902 				if (reset_retval == 0) {
25903 					reset_retval =
25904 					    scsi_reset(SD_ADDRESS(un),
25905 					    RESET_TARGET);
25906 				}
25907 			}
25908 			if (reset_retval == 0) {
25909 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25910 			}
25911 			mutex_enter(SD_MUTEX(un));
25912 		}
25913 		break;
25914 	}
25915 
25916 	/* A device/bus reset has occurred; update the reservation status. */
25917 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25918 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25919 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25920 			un->un_resvd_status |=
25921 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25922 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25923 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25924 		}
25925 	}
25926 
25927 	/*
25928 	 * The disk has been turned off; Update the device state.
25929 	 *
25930 	 * Note: Should we be offlining the disk here?
25931 	 */
25932 	if (pkt->pkt_state == STATE_GOT_BUS) {
25933 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25934 		    "Disk not responding to selection\n");
25935 		if (un->un_state != SD_STATE_OFFLINE) {
25936 			New_state(un, SD_STATE_OFFLINE);
25937 		}
25938 	} else if (be_chatty) {
25939 		/*
25940 		 * suppress messages if they are all the same pkt reason;
25941 		 * with TQ, many (up to 256) are returned with the same
25942 		 * pkt_reason
25943 		 */
25944 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25945 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25946 			    "sd_mhd_watch_incomplete: "
25947 			    "SCSI transport failed: reason '%s'\n",
25948 			    scsi_rname(pkt->pkt_reason));
25949 		}
25950 	}
25951 	un->un_last_pkt_reason = pkt->pkt_reason;
25952 	mutex_exit(SD_MUTEX(un));
25953 }
25954 
25955 
25956 /*
25957  *    Function: sd_sname()
25958  *
25959  * Description: This is a simple little routine to return a string containing
25960  *		a printable description of command status byte for use in
25961  *		logging.
25962  *
25963  *   Arguments: status - pointer to a status byte
25964  *
25965  * Return Code: char * - string containing status description.
25966  */
25967 
25968 static char *
25969 sd_sname(uchar_t status)
25970 {
25971 	switch (status & STATUS_MASK) {
25972 	case STATUS_GOOD:
25973 		return ("good status");
25974 	case STATUS_CHECK:
25975 		return ("check condition");
25976 	case STATUS_MET:
25977 		return ("condition met");
25978 	case STATUS_BUSY:
25979 		return ("busy");
25980 	case STATUS_INTERMEDIATE:
25981 		return ("intermediate");
25982 	case STATUS_INTERMEDIATE_MET:
25983 		return ("intermediate - condition met");
25984 	case STATUS_RESERVATION_CONFLICT:
25985 		return ("reservation_conflict");
25986 	case STATUS_TERMINATED:
25987 		return ("command terminated");
25988 	case STATUS_QFULL:
25989 		return ("queue full");
25990 	default:
25991 		return ("<unknown status>");
25992 	}
25993 }
25994 
25995 
25996 /*
25997  *    Function: sd_mhd_resvd_recover()
25998  *
25999  * Description: This function adds a reservation entry to the
26000  *		sd_resv_reclaim_request list and signals the reservation
26001  *		reclaim thread that there is work pending. If the reservation
26002  *		reclaim thread has not been previously created this function
26003  *		will kick it off.
26004  *
26005  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
26006  *			among multiple watches that share this callback function
26007  *
26008  *     Context: This routine is called by timeout() and is run in interrupt
26009  *		context. It must not sleep or call other functions which may
26010  *		sleep.
26011  */
26012 
26013 static void
26014 sd_mhd_resvd_recover(void *arg)
26015 {
26016 	dev_t			dev = (dev_t)arg;
26017 	struct sd_lun		*un;
26018 	struct sd_thr_request	*sd_treq = NULL;
26019 	struct sd_thr_request	*sd_cur = NULL;
26020 	struct sd_thr_request	*sd_prev = NULL;
26021 	int			already_there = 0;
26022 
26023 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26024 		return;
26025 	}
26026 
26027 	mutex_enter(SD_MUTEX(un));
26028 	un->un_resvd_timeid = NULL;
26029 	if (un->un_resvd_status & SD_WANT_RESERVE) {
26030 		/*
26031 		 * There was a reset so don't issue the reserve, allow the
26032 		 * sd_mhd_watch_cb callback function to notice this and
26033 		 * reschedule the timeout for reservation.
26034 		 */
26035 		mutex_exit(SD_MUTEX(un));
26036 		return;
26037 	}
26038 	mutex_exit(SD_MUTEX(un));
26039 
26040 	/*
26041 	 * Add this device to the sd_resv_reclaim_request list and the
26042 	 * sd_resv_reclaim_thread should take care of the rest.
26043 	 *
26044 	 * Note: We can't sleep in this context so if the memory allocation
26045 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
26046 	 * reschedule the timeout for reservation.  (4378460)
26047 	 */
26048 	sd_treq = (struct sd_thr_request *)
26049 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
26050 	if (sd_treq == NULL) {
26051 		return;
26052 	}
26053 
26054 	sd_treq->sd_thr_req_next = NULL;
26055 	sd_treq->dev = dev;
26056 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
26057 	if (sd_tr.srq_thr_req_head == NULL) {
26058 		sd_tr.srq_thr_req_head = sd_treq;
26059 	} else {
26060 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
26061 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
26062 			if (sd_cur->dev == dev) {
26063 				/*
26064 				 * already in Queue so don't log
26065 				 * another request for the device
26066 				 */
26067 				already_there = 1;
26068 				break;
26069 			}
26070 			sd_prev = sd_cur;
26071 		}
26072 		if (!already_there) {
26073 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
26074 			    "logging request for %lx\n", dev);
26075 			sd_prev->sd_thr_req_next = sd_treq;
26076 		} else {
26077 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
26078 		}
26079 	}
26080 
26081 	/*
26082 	 * Create a kernel thread to do the reservation reclaim and free up this
26083 	 * thread. We cannot block this thread while we go away to do the
26084 	 * reservation reclaim
26085 	 */
26086 	if (sd_tr.srq_resv_reclaim_thread == NULL)
26087 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
26088 		    sd_resv_reclaim_thread, NULL,
26089 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
26090 
26091 	/* Tell the reservation reclaim thread that it has work to do */
26092 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
26093 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
26094 }
26095 
26096 /*
26097  *    Function: sd_resv_reclaim_thread()
26098  *
26099  * Description: This function implements the reservation reclaim operations
26100  *
26101  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
26102  *		      among multiple watches that share this callback function
26103  */
26104 
26105 static void
26106 sd_resv_reclaim_thread()
26107 {
26108 	struct sd_lun		*un;
26109 	struct sd_thr_request	*sd_mhreq;
26110 
26111 	/* Wait for work */
26112 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
26113 	if (sd_tr.srq_thr_req_head == NULL) {
26114 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
26115 		    &sd_tr.srq_resv_reclaim_mutex);
26116 	}
26117 
26118 	/* Loop while we have work */
26119 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
26120 		un = ddi_get_soft_state(sd_state,
26121 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
26122 		if (un == NULL) {
26123 			/*
26124 			 * softstate structure is NULL so just
26125 			 * dequeue the request and continue
26126 			 */
26127 			sd_tr.srq_thr_req_head =
26128 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
26129 			kmem_free(sd_tr.srq_thr_cur_req,
26130 			    sizeof (struct sd_thr_request));
26131 			continue;
26132 		}
26133 
26134 		/* dequeue the request */
26135 		sd_mhreq = sd_tr.srq_thr_cur_req;
26136 		sd_tr.srq_thr_req_head =
26137 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
26138 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
26139 
26140 		/*
26141 		 * Reclaim reservation only if SD_RESERVE is still set. There
26142 		 * may have been a call to MHIOCRELEASE before we got here.
26143 		 */
26144 		mutex_enter(SD_MUTEX(un));
26145 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
26146 			/*
26147 			 * Note: The SD_LOST_RESERVE flag is cleared before
26148 			 * reclaiming the reservation. If this is done after the
26149 			 * call to sd_reserve_release a reservation loss in the
26150 			 * window between pkt completion of reserve cmd and
26151 			 * mutex_enter below may not be recognized
26152 			 */
26153 			un->un_resvd_status &= ~SD_LOST_RESERVE;
26154 			mutex_exit(SD_MUTEX(un));
26155 
26156 			if (sd_reserve_release(sd_mhreq->dev,
26157 			    SD_RESERVE) == 0) {
26158 				mutex_enter(SD_MUTEX(un));
26159 				un->un_resvd_status |= SD_RESERVE;
26160 				mutex_exit(SD_MUTEX(un));
26161 				SD_INFO(SD_LOG_IOCTL_MHD, un,
26162 				    "sd_resv_reclaim_thread: "
26163 				    "Reservation Recovered\n");
26164 			} else {
26165 				mutex_enter(SD_MUTEX(un));
26166 				un->un_resvd_status |= SD_LOST_RESERVE;
26167 				mutex_exit(SD_MUTEX(un));
26168 				SD_INFO(SD_LOG_IOCTL_MHD, un,
26169 				    "sd_resv_reclaim_thread: Failed "
26170 				    "Reservation Recovery\n");
26171 			}
26172 		} else {
26173 			mutex_exit(SD_MUTEX(un));
26174 		}
26175 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
26176 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
26177 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
26178 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
26179 		/*
26180 		 * wakeup the destroy thread if anyone is waiting on
26181 		 * us to complete.
26182 		 */
26183 		cv_signal(&sd_tr.srq_inprocess_cv);
26184 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
26185 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
26186 	}
26187 
26188 	/*
26189 	 * cleanup the sd_tr structure now that this thread will not exist
26190 	 */
26191 	ASSERT(sd_tr.srq_thr_req_head == NULL);
26192 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
26193 	sd_tr.srq_resv_reclaim_thread = NULL;
26194 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
26195 	thread_exit();
26196 }
26197 
26198 
26199 /*
26200  *    Function: sd_rmv_resv_reclaim_req()
26201  *
26202  * Description: This function removes any pending reservation reclaim requests
26203  *		for the specified device.
26204  *
26205  *   Arguments: dev - the device 'dev_t'
26206  */
26207 
26208 static void
26209 sd_rmv_resv_reclaim_req(dev_t dev)
26210 {
26211 	struct sd_thr_request *sd_mhreq;
26212 	struct sd_thr_request *sd_prev;
26213 
26214 	/* Remove a reservation reclaim request from the list */
26215 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
26216 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
26217 		/*
26218 		 * We are attempting to reinstate reservation for
26219 		 * this device. We wait for sd_reserve_release()
26220 		 * to return before we return.
26221 		 */
26222 		cv_wait(&sd_tr.srq_inprocess_cv,
26223 		    &sd_tr.srq_resv_reclaim_mutex);
26224 	} else {
26225 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
26226 		if (sd_mhreq && sd_mhreq->dev == dev) {
26227 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
26228 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
26229 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
26230 			return;
26231 		}
26232 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
26233 			if (sd_mhreq && sd_mhreq->dev == dev) {
26234 				break;
26235 			}
26236 			sd_prev = sd_mhreq;
26237 		}
26238 		if (sd_mhreq != NULL) {
26239 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
26240 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
26241 		}
26242 	}
26243 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
26244 }
26245 
26246 
26247 /*
26248  *    Function: sd_mhd_reset_notify_cb()
26249  *
26250  * Description: This is a call back function for scsi_reset_notify. This
26251  *		function updates the softstate reserved status and logs the
26252  *		reset. The driver scsi watch facility callback function
26253  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
26254  *		will reclaim the reservation.
26255  *
26256  *   Arguments: arg  - driver soft state (unit) structure
26257  */
26258 
26259 static void
26260 sd_mhd_reset_notify_cb(caddr_t arg)
26261 {
26262 	struct sd_lun *un = (struct sd_lun *)arg;
26263 
26264 	mutex_enter(SD_MUTEX(un));
26265 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
26266 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
26267 		SD_INFO(SD_LOG_IOCTL_MHD, un,
26268 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
26269 	}
26270 	mutex_exit(SD_MUTEX(un));
26271 }
26272 
26273 
26274 /*
26275  *    Function: sd_take_ownership()
26276  *
26277  * Description: This routine implements an algorithm to achieve a stable
26278  *		reservation on disks which don't implement priority reserve,
26279  *		and makes sure that other host lose re-reservation attempts.
26280  *		This algorithm contains of a loop that keeps issuing the RESERVE
26281  *		for some period of time (min_ownership_delay, default 6 seconds)
26282  *		During that loop, it looks to see if there has been a bus device
26283  *		reset or bus reset (both of which cause an existing reservation
26284  *		to be lost). If the reservation is lost issue RESERVE until a
26285  *		period of min_ownership_delay with no resets has gone by, or
26286  *		until max_ownership_delay has expired. This loop ensures that
26287  *		the host really did manage to reserve the device, in spite of
26288  *		resets. The looping for min_ownership_delay (default six
26289  *		seconds) is important to early generation clustering products,
26290  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
26291  *		MHIOCENFAILFAST periodic timer of two seconds. By having
26292  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
26293  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
26294  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
26295  *		have already noticed, via the MHIOCENFAILFAST polling, that it
26296  *		no longer "owns" the disk and will have panicked itself.  Thus,
26297  *		the host issuing the MHIOCTKOWN is assured (with timing
26298  *		dependencies) that by the time it actually starts to use the
26299  *		disk for real work, the old owner is no longer accessing it.
26300  *
26301  *		min_ownership_delay is the minimum amount of time for which the
26302  *		disk must be reserved continuously devoid of resets before the
26303  *		MHIOCTKOWN ioctl will return success.
26304  *
26305  *		max_ownership_delay indicates the amount of time by which the
26306  *		take ownership should succeed or timeout with an error.
26307  *
26308  *   Arguments: dev - the device 'dev_t'
26309  *		*p  - struct containing timing info.
26310  *
26311  * Return Code: 0 for success or error code
26312  */
26313 
26314 static int
26315 sd_take_ownership(dev_t dev, struct mhioctkown *p)
26316 {
26317 	struct sd_lun	*un;
26318 	int		rval;
26319 	int		err;
26320 	int		reservation_count   = 0;
26321 	int		min_ownership_delay =  6000000; /* in usec */
26322 	int		max_ownership_delay = 30000000; /* in usec */
26323 	clock_t		start_time;	/* starting time of this algorithm */
26324 	clock_t		end_time;	/* time limit for giving up */
26325 	clock_t		ownership_time;	/* time limit for stable ownership */
26326 	clock_t		current_time;
26327 	clock_t		previous_current_time;
26328 
26329 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26330 		return (ENXIO);
26331 	}
26332 
26333 	/*
26334 	 * Attempt a device reservation. A priority reservation is requested.
26335 	 */
26336 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
26337 	    != SD_SUCCESS) {
26338 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26339 		    "sd_take_ownership: return(1)=%d\n", rval);
26340 		return (rval);
26341 	}
26342 
26343 	/* Update the softstate reserved status to indicate the reservation */
26344 	mutex_enter(SD_MUTEX(un));
26345 	un->un_resvd_status |= SD_RESERVE;
26346 	un->un_resvd_status &=
26347 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
26348 	mutex_exit(SD_MUTEX(un));
26349 
26350 	if (p != NULL) {
26351 		if (p->min_ownership_delay != 0) {
26352 			min_ownership_delay = p->min_ownership_delay * 1000;
26353 		}
26354 		if (p->max_ownership_delay != 0) {
26355 			max_ownership_delay = p->max_ownership_delay * 1000;
26356 		}
26357 	}
26358 	SD_INFO(SD_LOG_IOCTL_MHD, un,
26359 	    "sd_take_ownership: min, max delays: %d, %d\n",
26360 	    min_ownership_delay, max_ownership_delay);
26361 
26362 	start_time = ddi_get_lbolt();
26363 	current_time	= start_time;
26364 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
26365 	end_time	= start_time + drv_usectohz(max_ownership_delay);
26366 
26367 	while (current_time - end_time < 0) {
26368 		delay(drv_usectohz(500000));
26369 
26370 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
26371 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
26372 				mutex_enter(SD_MUTEX(un));
26373 				rval = (un->un_resvd_status &
26374 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
26375 				mutex_exit(SD_MUTEX(un));
26376 				break;
26377 			}
26378 		}
26379 		previous_current_time = current_time;
26380 		current_time = ddi_get_lbolt();
26381 		mutex_enter(SD_MUTEX(un));
26382 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
26383 			ownership_time = ddi_get_lbolt() +
26384 			    drv_usectohz(min_ownership_delay);
26385 			reservation_count = 0;
26386 		} else {
26387 			reservation_count++;
26388 		}
26389 		un->un_resvd_status |= SD_RESERVE;
26390 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
26391 		mutex_exit(SD_MUTEX(un));
26392 
26393 		SD_INFO(SD_LOG_IOCTL_MHD, un,
26394 		    "sd_take_ownership: ticks for loop iteration=%ld, "
26395 		    "reservation=%s\n", (current_time - previous_current_time),
26396 		    reservation_count ? "ok" : "reclaimed");
26397 
26398 		if (current_time - ownership_time >= 0 &&
26399 		    reservation_count >= 4) {
26400 			rval = 0; /* Achieved a stable ownership */
26401 			break;
26402 		}
26403 		if (current_time - end_time >= 0) {
26404 			rval = EACCES; /* No ownership in max possible time */
26405 			break;
26406 		}
26407 	}
26408 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
26409 	    "sd_take_ownership: return(2)=%d\n", rval);
26410 	return (rval);
26411 }
26412 
26413 
26414 /*
26415  *    Function: sd_reserve_release()
26416  *
26417  * Description: This function builds and sends scsi RESERVE, RELEASE, and
26418  *		PRIORITY RESERVE commands based on a user specified command type
26419  *
26420  *   Arguments: dev - the device 'dev_t'
26421  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
26422  *		      SD_RESERVE, SD_RELEASE
26423  *
26424  * Return Code: 0 or Error Code
26425  */
26426 
26427 static int
26428 sd_reserve_release(dev_t dev, int cmd)
26429 {
26430 	struct uscsi_cmd	*com = NULL;
26431 	struct sd_lun		*un = NULL;
26432 	char			cdb[CDB_GROUP0];
26433 	int			rval;
26434 
26435 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
26436 	    (cmd == SD_PRIORITY_RESERVE));
26437 
26438 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26439 		return (ENXIO);
26440 	}
26441 
26442 	/* instantiate and initialize the command and cdb */
26443 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
26444 	bzero(cdb, CDB_GROUP0);
26445 	com->uscsi_flags   = USCSI_SILENT;
26446 	com->uscsi_timeout = un->un_reserve_release_time;
26447 	com->uscsi_cdblen  = CDB_GROUP0;
26448 	com->uscsi_cdb	   = cdb;
26449 	if (cmd == SD_RELEASE) {
26450 		cdb[0] = SCMD_RELEASE;
26451 	} else {
26452 		cdb[0] = SCMD_RESERVE;
26453 	}
26454 
26455 	/* Send the command. */
26456 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
26457 	    UIO_SYSSPACE, SD_PATH_STANDARD);
26458 
26459 	/*
26460 	 * "break" a reservation that is held by another host, by issuing a
26461 	 * reset if priority reserve is desired, and we could not get the
26462 	 * device.
26463 	 */
26464 	if ((cmd == SD_PRIORITY_RESERVE) &&
26465 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
26466 		/*
26467 		 * First try to reset the LUN. If we cannot, then try a target
26468 		 * reset, followed by a bus reset if the target reset fails.
26469 		 */
26470 		int reset_retval = 0;
26471 		if (un->un_f_lun_reset_enabled == TRUE) {
26472 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
26473 		}
26474 		if (reset_retval == 0) {
26475 			/* The LUN reset either failed or was not issued */
26476 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26477 		}
26478 		if ((reset_retval == 0) &&
26479 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
26480 			rval = EIO;
26481 			kmem_free(com, sizeof (*com));
26482 			return (rval);
26483 		}
26484 
26485 		bzero(com, sizeof (struct uscsi_cmd));
26486 		com->uscsi_flags   = USCSI_SILENT;
26487 		com->uscsi_cdb	   = cdb;
26488 		com->uscsi_cdblen  = CDB_GROUP0;
26489 		com->uscsi_timeout = 5;
26490 
26491 		/*
26492 		 * Reissue the last reserve command, this time without request
26493 		 * sense.  Assume that it is just a regular reserve command.
26494 		 */
26495 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
26496 		    UIO_SYSSPACE, SD_PATH_STANDARD);
26497 	}
26498 
26499 	/* Return an error if still getting a reservation conflict. */
26500 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
26501 		rval = EACCES;
26502 	}
26503 
26504 	kmem_free(com, sizeof (*com));
26505 	return (rval);
26506 }
26507 
26508 
26509 #define	SD_NDUMP_RETRIES	12
26510 /*
26511  *	System Crash Dump routine
26512  */
26513 
26514 static int
26515 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
26516 {
26517 	int		instance;
26518 	int		partition;
26519 	int		i;
26520 	int		err;
26521 	struct sd_lun	*un;
26522 	struct dk_map	*lp;
26523 	struct scsi_pkt *wr_pktp;
26524 	struct buf	*wr_bp;
26525 	struct buf	wr_buf;
26526 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
26527 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
26528 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
26529 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
26530 	size_t		io_start_offset;
26531 	int		doing_rmw = FALSE;
26532 	int		rval;
26533 #if defined(__i386) || defined(__amd64)
26534 	ssize_t dma_resid;
26535 	daddr_t oblkno;
26536 #endif
26537 
26538 	instance = SDUNIT(dev);
26539 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
26540 	    (!un->un_f_geometry_is_valid) || ISCD(un)) {
26541 		return (ENXIO);
26542 	}
26543 
26544 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
26545 
26546 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
26547 
26548 	partition = SDPART(dev);
26549 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
26550 
26551 	/* Validate blocks to dump at against partition size. */
26552 	lp = &un->un_map[partition];
26553 	if ((blkno + nblk) > lp->dkl_nblk) {
26554 		SD_TRACE(SD_LOG_DUMP, un,
26555 		    "sddump: dump range larger than partition: "
26556 		    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
26557 		    blkno, nblk, lp->dkl_nblk);
26558 		return (EINVAL);
26559 	}
26560 
26561 	mutex_enter(&un->un_pm_mutex);
26562 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
26563 		struct scsi_pkt *start_pktp;
26564 
26565 		mutex_exit(&un->un_pm_mutex);
26566 
26567 		/*
26568 		 * use pm framework to power on HBA 1st
26569 		 */
26570 		(void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON);
26571 
26572 		/*
26573 		 * Dump no long uses sdpower to power on a device, it's
26574 		 * in-line here so it can be done in polled mode.
26575 		 */
26576 
26577 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
26578 
26579 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
26580 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
26581 
26582 		if (start_pktp == NULL) {
26583 			/* We were not given a SCSI packet, fail. */
26584 			return (EIO);
26585 		}
26586 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
26587 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
26588 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
26589 		start_pktp->pkt_flags = FLAG_NOINTR;
26590 
26591 		mutex_enter(SD_MUTEX(un));
26592 		SD_FILL_SCSI1_LUN(un, start_pktp);
26593 		mutex_exit(SD_MUTEX(un));
26594 		/*
26595 		 * Scsi_poll returns 0 (success) if the command completes and
26596 		 * the status block is STATUS_GOOD.
26597 		 */
26598 		if (sd_scsi_poll(un, start_pktp) != 0) {
26599 			scsi_destroy_pkt(start_pktp);
26600 			return (EIO);
26601 		}
26602 		scsi_destroy_pkt(start_pktp);
26603 		(void) sd_ddi_pm_resume(un);
26604 	} else {
26605 		mutex_exit(&un->un_pm_mutex);
26606 	}
26607 
26608 	mutex_enter(SD_MUTEX(un));
26609 	un->un_throttle = 0;
26610 
26611 	/*
26612 	 * The first time through, reset the specific target device.
26613 	 * However, when cpr calls sddump we know that sd is in a
26614 	 * a good state so no bus reset is required.
26615 	 * Clear sense data via Request Sense cmd.
26616 	 * In sddump we don't care about allow_bus_device_reset anymore
26617 	 */
26618 
26619 	if ((un->un_state != SD_STATE_SUSPENDED) &&
26620 	    (un->un_state != SD_STATE_DUMPING)) {
26621 
26622 		New_state(un, SD_STATE_DUMPING);
26623 
26624 		if (un->un_f_is_fibre == FALSE) {
26625 			mutex_exit(SD_MUTEX(un));
26626 			/*
26627 			 * Attempt a bus reset for parallel scsi.
26628 			 *
26629 			 * Note: A bus reset is required because on some host
26630 			 * systems (i.e. E420R) a bus device reset is
26631 			 * insufficient to reset the state of the target.
26632 			 *
26633 			 * Note: Don't issue the reset for fibre-channel,
26634 			 * because this tends to hang the bus (loop) for
26635 			 * too long while everyone is logging out and in
26636 			 * and the deadman timer for dumping will fire
26637 			 * before the dump is complete.
26638 			 */
26639 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
26640 				mutex_enter(SD_MUTEX(un));
26641 				Restore_state(un);
26642 				mutex_exit(SD_MUTEX(un));
26643 				return (EIO);
26644 			}
26645 
26646 			/* Delay to give the device some recovery time. */
26647 			drv_usecwait(10000);
26648 
26649 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
26650 				SD_INFO(SD_LOG_DUMP, un,
26651 					"sddump: sd_send_polled_RQS failed\n");
26652 			}
26653 			mutex_enter(SD_MUTEX(un));
26654 		}
26655 	}
26656 
26657 	/*
26658 	 * Convert the partition-relative block number to a
26659 	 * disk physical block number.
26660 	 */
26661 	blkno += un->un_offset[partition];
26662 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
26663 
26664 
26665 	/*
26666 	 * Check if the device has a non-512 block size.
26667 	 */
26668 	wr_bp = NULL;
26669 	if (NOT_DEVBSIZE(un)) {
26670 		tgt_byte_offset = blkno * un->un_sys_blocksize;
26671 		tgt_byte_count = nblk * un->un_sys_blocksize;
26672 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
26673 		    (tgt_byte_count % un->un_tgt_blocksize)) {
26674 			doing_rmw = TRUE;
26675 			/*
26676 			 * Calculate the block number and number of block
26677 			 * in terms of the media block size.
26678 			 */
26679 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26680 			tgt_nblk =
26681 			    ((tgt_byte_offset + tgt_byte_count +
26682 				(un->un_tgt_blocksize - 1)) /
26683 				un->un_tgt_blocksize) - tgt_blkno;
26684 
26685 			/*
26686 			 * Invoke the routine which is going to do read part
26687 			 * of read-modify-write.
26688 			 * Note that this routine returns a pointer to
26689 			 * a valid bp in wr_bp.
26690 			 */
26691 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
26692 			    &wr_bp);
26693 			if (err) {
26694 				mutex_exit(SD_MUTEX(un));
26695 				return (err);
26696 			}
26697 			/*
26698 			 * Offset is being calculated as -
26699 			 * (original block # * system block size) -
26700 			 * (new block # * target block size)
26701 			 */
26702 			io_start_offset =
26703 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
26704 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
26705 
26706 			ASSERT((io_start_offset >= 0) &&
26707 			    (io_start_offset < un->un_tgt_blocksize));
26708 			/*
26709 			 * Do the modify portion of read modify write.
26710 			 */
26711 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
26712 			    (size_t)nblk * un->un_sys_blocksize);
26713 		} else {
26714 			doing_rmw = FALSE;
26715 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26716 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
26717 		}
26718 
26719 		/* Convert blkno and nblk to target blocks */
26720 		blkno = tgt_blkno;
26721 		nblk = tgt_nblk;
26722 	} else {
26723 		wr_bp = &wr_buf;
26724 		bzero(wr_bp, sizeof (struct buf));
26725 		wr_bp->b_flags		= B_BUSY;
26726 		wr_bp->b_un.b_addr	= addr;
26727 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
26728 		wr_bp->b_resid		= 0;
26729 	}
26730 
26731 	mutex_exit(SD_MUTEX(un));
26732 
26733 	/*
26734 	 * Obtain a SCSI packet for the write command.
26735 	 * It should be safe to call the allocator here without
26736 	 * worrying about being locked for DVMA mapping because
26737 	 * the address we're passed is already a DVMA mapping
26738 	 *
26739 	 * We are also not going to worry about semaphore ownership
26740 	 * in the dump buffer. Dumping is single threaded at present.
26741 	 */
26742 
26743 	wr_pktp = NULL;
26744 
26745 #if defined(__i386) || defined(__amd64)
26746 	dma_resid = wr_bp->b_bcount;
26747 	oblkno = blkno;
26748 	while (dma_resid != 0) {
26749 #endif
26750 
26751 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26752 		wr_bp->b_flags &= ~B_ERROR;
26753 
26754 #if defined(__i386) || defined(__amd64)
26755 		blkno = oblkno +
26756 			((wr_bp->b_bcount - dma_resid) /
26757 			    un->un_tgt_blocksize);
26758 		nblk = dma_resid / un->un_tgt_blocksize;
26759 
26760 		if (wr_pktp) {
26761 			/* Partial DMA transfers after initial transfer */
26762 			rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26763 			    blkno, nblk);
26764 		} else {
26765 			/* Initial transfer */
26766 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26767 			    un->un_pkt_flags, NULL_FUNC, NULL,
26768 			    blkno, nblk);
26769 		}
26770 #else
26771 		rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26772 		    0, NULL_FUNC, NULL, blkno, nblk);
26773 #endif
26774 
26775 		if (rval == 0) {
26776 			/* We were given a SCSI packet, continue. */
26777 			break;
26778 		}
26779 
26780 		if (i == 0) {
26781 			if (wr_bp->b_flags & B_ERROR) {
26782 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26783 				    "no resources for dumping; "
26784 				    "error code: 0x%x, retrying",
26785 				    geterror(wr_bp));
26786 			} else {
26787 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26788 				    "no resources for dumping; retrying");
26789 			}
26790 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
26791 			if (wr_bp->b_flags & B_ERROR) {
26792 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26793 				    "no resources for dumping; error code: "
26794 				    "0x%x, retrying\n", geterror(wr_bp));
26795 			}
26796 		} else {
26797 			if (wr_bp->b_flags & B_ERROR) {
26798 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26799 				    "no resources for dumping; "
26800 				    "error code: 0x%x, retries failed, "
26801 				    "giving up.\n", geterror(wr_bp));
26802 			} else {
26803 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26804 				    "no resources for dumping; "
26805 				    "retries failed, giving up.\n");
26806 			}
26807 			mutex_enter(SD_MUTEX(un));
26808 			Restore_state(un);
26809 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26810 				mutex_exit(SD_MUTEX(un));
26811 				scsi_free_consistent_buf(wr_bp);
26812 			} else {
26813 				mutex_exit(SD_MUTEX(un));
26814 			}
26815 			return (EIO);
26816 		}
26817 		drv_usecwait(10000);
26818 	}
26819 
26820 #if defined(__i386) || defined(__amd64)
26821 	/*
26822 	 * save the resid from PARTIAL_DMA
26823 	 */
26824 	dma_resid = wr_pktp->pkt_resid;
26825 	if (dma_resid != 0)
26826 		nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26827 	wr_pktp->pkt_resid = 0;
26828 #endif
26829 
26830 	/* SunBug 1222170 */
26831 	wr_pktp->pkt_flags = FLAG_NOINTR;
26832 
26833 	err = EIO;
26834 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26835 
26836 		/*
26837 		 * Scsi_poll returns 0 (success) if the command completes and
26838 		 * the status block is STATUS_GOOD.  We should only check
26839 		 * errors if this condition is not true.  Even then we should
26840 		 * send our own request sense packet only if we have a check
26841 		 * condition and auto request sense has not been performed by
26842 		 * the hba.
26843 		 */
26844 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26845 
26846 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26847 		    (wr_pktp->pkt_resid == 0)) {
26848 			err = SD_SUCCESS;
26849 			break;
26850 		}
26851 
26852 		/*
26853 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
26854 		 */
26855 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26856 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26857 			    "Device is gone\n");
26858 			break;
26859 		}
26860 
26861 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26862 			SD_INFO(SD_LOG_DUMP, un,
26863 			    "sddump: write failed with CHECK, try # %d\n", i);
26864 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26865 				(void) sd_send_polled_RQS(un);
26866 			}
26867 
26868 			continue;
26869 		}
26870 
26871 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26872 			int reset_retval = 0;
26873 
26874 			SD_INFO(SD_LOG_DUMP, un,
26875 			    "sddump: write failed with BUSY, try # %d\n", i);
26876 
26877 			if (un->un_f_lun_reset_enabled == TRUE) {
26878 				reset_retval = scsi_reset(SD_ADDRESS(un),
26879 				    RESET_LUN);
26880 			}
26881 			if (reset_retval == 0) {
26882 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26883 			}
26884 			(void) sd_send_polled_RQS(un);
26885 
26886 		} else {
26887 			SD_INFO(SD_LOG_DUMP, un,
26888 			    "sddump: write failed with 0x%x, try # %d\n",
26889 			    SD_GET_PKT_STATUS(wr_pktp), i);
26890 			mutex_enter(SD_MUTEX(un));
26891 			sd_reset_target(un, wr_pktp);
26892 			mutex_exit(SD_MUTEX(un));
26893 		}
26894 
26895 		/*
26896 		 * If we are not getting anywhere with lun/target resets,
26897 		 * let's reset the bus.
26898 		 */
26899 		if (i == SD_NDUMP_RETRIES/2) {
26900 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26901 			(void) sd_send_polled_RQS(un);
26902 		}
26903 
26904 	}
26905 #if defined(__i386) || defined(__amd64)
26906 	}	/* dma_resid */
26907 #endif
26908 
26909 	scsi_destroy_pkt(wr_pktp);
26910 	mutex_enter(SD_MUTEX(un));
26911 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26912 		mutex_exit(SD_MUTEX(un));
26913 		scsi_free_consistent_buf(wr_bp);
26914 	} else {
26915 		mutex_exit(SD_MUTEX(un));
26916 	}
26917 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26918 	return (err);
26919 }
26920 
26921 /*
26922  *    Function: sd_scsi_poll()
26923  *
26924  * Description: This is a wrapper for the scsi_poll call.
26925  *
26926  *   Arguments: sd_lun - The unit structure
26927  *              scsi_pkt - The scsi packet being sent to the device.
26928  *
26929  * Return Code: 0 - Command completed successfully with good status
26930  *             -1 - Command failed.  This could indicate a check condition
26931  *                  or other status value requiring recovery action.
26932  *
26933  */
26934 
26935 static int
26936 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26937 {
26938 	int status;
26939 
26940 	ASSERT(un != NULL);
26941 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26942 	ASSERT(pktp != NULL);
26943 
26944 	status = SD_SUCCESS;
26945 
26946 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26947 		pktp->pkt_flags |= un->un_tagflags;
26948 		pktp->pkt_flags &= ~FLAG_NODISCON;
26949 	}
26950 
26951 	status = sd_ddi_scsi_poll(pktp);
26952 	/*
26953 	 * Scsi_poll returns 0 (success) if the command completes and the
26954 	 * status block is STATUS_GOOD.  We should only check errors if this
26955 	 * condition is not true.  Even then we should send our own request
26956 	 * sense packet only if we have a check condition and auto
26957 	 * request sense has not been performed by the hba.
26958 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26959 	 */
26960 	if ((status != SD_SUCCESS) &&
26961 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26962 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26963 	    (pktp->pkt_reason != CMD_DEV_GONE))
26964 		(void) sd_send_polled_RQS(un);
26965 
26966 	return (status);
26967 }
26968 
26969 /*
26970  *    Function: sd_send_polled_RQS()
26971  *
26972  * Description: This sends the request sense command to a device.
26973  *
26974  *   Arguments: sd_lun - The unit structure
26975  *
26976  * Return Code: 0 - Command completed successfully with good status
26977  *             -1 - Command failed.
26978  *
26979  */
26980 
26981 static int
26982 sd_send_polled_RQS(struct sd_lun *un)
26983 {
26984 	int	ret_val;
26985 	struct	scsi_pkt	*rqs_pktp;
26986 	struct	buf		*rqs_bp;
26987 
26988 	ASSERT(un != NULL);
26989 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26990 
26991 	ret_val = SD_SUCCESS;
26992 
26993 	rqs_pktp = un->un_rqs_pktp;
26994 	rqs_bp	 = un->un_rqs_bp;
26995 
26996 	mutex_enter(SD_MUTEX(un));
26997 
26998 	if (un->un_sense_isbusy) {
26999 		ret_val = SD_FAILURE;
27000 		mutex_exit(SD_MUTEX(un));
27001 		return (ret_val);
27002 	}
27003 
27004 	/*
27005 	 * If the request sense buffer (and packet) is not in use,
27006 	 * let's set the un_sense_isbusy and send our packet
27007 	 */
27008 	un->un_sense_isbusy 	= 1;
27009 	rqs_pktp->pkt_resid  	= 0;
27010 	rqs_pktp->pkt_reason 	= 0;
27011 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
27012 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
27013 
27014 	mutex_exit(SD_MUTEX(un));
27015 
27016 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
27017 	    " 0x%p\n", rqs_bp->b_un.b_addr);
27018 
27019 	/*
27020 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
27021 	 * axle - it has a call into us!
27022 	 */
27023 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
27024 		SD_INFO(SD_LOG_COMMON, un,
27025 		    "sd_send_polled_RQS: RQS failed\n");
27026 	}
27027 
27028 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
27029 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
27030 
27031 	mutex_enter(SD_MUTEX(un));
27032 	un->un_sense_isbusy = 0;
27033 	mutex_exit(SD_MUTEX(un));
27034 
27035 	return (ret_val);
27036 }
27037 
27038 /*
27039  * Defines needed for localized version of the scsi_poll routine.
27040  */
27041 #define	SD_CSEC		10000			/* usecs */
27042 #define	SD_SEC_TO_CSEC	(1000000/SD_CSEC)
27043 
27044 
27045 /*
27046  *    Function: sd_ddi_scsi_poll()
27047  *
27048  * Description: Localized version of the scsi_poll routine.  The purpose is to
27049  *		send a scsi_pkt to a device as a polled command.  This version
27050  *		is to ensure more robust handling of transport errors.
27051  *		Specifically this routine cures not ready, coming ready
27052  *		transition for power up and reset of sonoma's.  This can take
27053  *		up to 45 seconds for power-on and 20 seconds for reset of a
27054  * 		sonoma lun.
27055  *
27056  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
27057  *
27058  * Return Code: 0 - Command completed successfully with good status
27059  *             -1 - Command failed.
27060  *
27061  */
27062 
27063 static int
27064 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
27065 {
27066 	int busy_count;
27067 	int timeout;
27068 	int rval = SD_FAILURE;
27069 	int savef;
27070 	uint8_t *sensep;
27071 	long savet;
27072 	void (*savec)();
27073 	/*
27074 	 * The following is defined in machdep.c and is used in determining if
27075 	 * the scsi transport system will do polled I/O instead of interrupt
27076 	 * I/O when called from xx_dump().
27077 	 */
27078 	extern int do_polled_io;
27079 
27080 	/*
27081 	 * save old flags in pkt, to restore at end
27082 	 */
27083 	savef = pkt->pkt_flags;
27084 	savec = pkt->pkt_comp;
27085 	savet = pkt->pkt_time;
27086 
27087 	pkt->pkt_flags |= FLAG_NOINTR;
27088 
27089 	/*
27090 	 * XXX there is nothing in the SCSA spec that states that we should not
27091 	 * do a callback for polled cmds; however, removing this will break sd
27092 	 * and probably other target drivers
27093 	 */
27094 	pkt->pkt_comp = NULL;
27095 
27096 	/*
27097 	 * we don't like a polled command without timeout.
27098 	 * 60 seconds seems long enough.
27099 	 */
27100 	if (pkt->pkt_time == 0) {
27101 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
27102 	}
27103 
27104 	/*
27105 	 * Send polled cmd.
27106 	 *
27107 	 * We do some error recovery for various errors.  Tran_busy,
27108 	 * queue full, and non-dispatched commands are retried every 10 msec.
27109 	 * as they are typically transient failures.  Busy status and Not
27110 	 * Ready are retried every second as this status takes a while to
27111 	 * change.  Unit attention is retried for pkt_time (60) times
27112 	 * with no delay.
27113 	 */
27114 	timeout = pkt->pkt_time * SD_SEC_TO_CSEC;
27115 
27116 	for (busy_count = 0; busy_count < timeout; busy_count++) {
27117 		int rc;
27118 		int poll_delay;
27119 
27120 		/*
27121 		 * Initialize pkt status variables.
27122 		 */
27123 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
27124 
27125 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
27126 			if (rc != TRAN_BUSY) {
27127 				/* Transport failed - give up. */
27128 				break;
27129 			} else {
27130 				/* Transport busy - try again. */
27131 				poll_delay = 1 * SD_CSEC; /* 10 msec */
27132 			}
27133 		} else {
27134 			/*
27135 			 * Transport accepted - check pkt status.
27136 			 */
27137 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
27138 			if (pkt->pkt_reason == CMD_CMPLT &&
27139 			    rc == STATUS_CHECK &&
27140 			    pkt->pkt_state & STATE_ARQ_DONE) {
27141 				struct scsi_arq_status *arqstat =
27142 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
27143 
27144 				sensep = (uint8_t *)&arqstat->sts_sensedata;
27145 			} else {
27146 				sensep = NULL;
27147 			}
27148 
27149 			if ((pkt->pkt_reason == CMD_CMPLT) &&
27150 			    (rc == STATUS_GOOD)) {
27151 				/* No error - we're done */
27152 				rval = SD_SUCCESS;
27153 				break;
27154 
27155 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
27156 				/* Lost connection - give up */
27157 				break;
27158 
27159 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
27160 			    (pkt->pkt_state == 0)) {
27161 				/* Pkt not dispatched - try again. */
27162 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
27163 
27164 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
27165 			    (rc == STATUS_QFULL)) {
27166 				/* Queue full - try again. */
27167 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
27168 
27169 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
27170 			    (rc == STATUS_BUSY)) {
27171 				/* Busy - try again. */
27172 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
27173 				busy_count += (SD_SEC_TO_CSEC - 1);
27174 
27175 			} else if ((sensep != NULL) &&
27176 			    (scsi_sense_key(sensep) ==
27177 				KEY_UNIT_ATTENTION)) {
27178 				/* Unit Attention - try again */
27179 				busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */
27180 				continue;
27181 
27182 			} else if ((sensep != NULL) &&
27183 			    (scsi_sense_key(sensep) == KEY_NOT_READY) &&
27184 			    (scsi_sense_asc(sensep) == 0x04) &&
27185 			    (scsi_sense_ascq(sensep) == 0x01)) {
27186 				/* Not ready -> ready - try again. */
27187 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
27188 				busy_count += (SD_SEC_TO_CSEC - 1);
27189 
27190 			} else {
27191 				/* BAD status - give up. */
27192 				break;
27193 			}
27194 		}
27195 
27196 		if ((curthread->t_flag & T_INTR_THREAD) == 0 &&
27197 		    !do_polled_io) {
27198 			delay(drv_usectohz(poll_delay));
27199 		} else {
27200 			/* we busy wait during cpr_dump or interrupt threads */
27201 			drv_usecwait(poll_delay);
27202 		}
27203 	}
27204 
27205 	pkt->pkt_flags = savef;
27206 	pkt->pkt_comp = savec;
27207 	pkt->pkt_time = savet;
27208 	return (rval);
27209 }
27210 
27211 
27212 /*
27213  *    Function: sd_persistent_reservation_in_read_keys
27214  *
27215  * Description: This routine is the driver entry point for handling CD-ROM
27216  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
27217  *		by sending the SCSI-3 PRIN commands to the device.
27218  *		Processes the read keys command response by copying the
27219  *		reservation key information into the user provided buffer.
27220  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
27221  *
27222  *   Arguments: un   -  Pointer to soft state struct for the target.
27223  *		usrp -	user provided pointer to multihost Persistent In Read
27224  *			Keys structure (mhioc_inkeys_t)
27225  *		flag -	this argument is a pass through to ddi_copyxxx()
27226  *			directly from the mode argument of ioctl().
27227  *
27228  * Return Code: 0   - Success
27229  *		EACCES
27230  *		ENOTSUP
27231  *		errno return code from sd_send_scsi_cmd()
27232  *
27233  *     Context: Can sleep. Does not return until command is completed.
27234  */
27235 
27236 static int
27237 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
27238     mhioc_inkeys_t *usrp, int flag)
27239 {
27240 #ifdef _MULTI_DATAMODEL
27241 	struct mhioc_key_list32	li32;
27242 #endif
27243 	sd_prin_readkeys_t	*in;
27244 	mhioc_inkeys_t		*ptr;
27245 	mhioc_key_list_t	li;
27246 	uchar_t			*data_bufp;
27247 	int 			data_len;
27248 	int			rval;
27249 	size_t			copysz;
27250 
27251 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
27252 		return (EINVAL);
27253 	}
27254 	bzero(&li, sizeof (mhioc_key_list_t));
27255 
27256 	/*
27257 	 * Get the listsize from user
27258 	 */
27259 #ifdef _MULTI_DATAMODEL
27260 
27261 	switch (ddi_model_convert_from(flag & FMODELS)) {
27262 	case DDI_MODEL_ILP32:
27263 		copysz = sizeof (struct mhioc_key_list32);
27264 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
27265 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27266 			    "sd_persistent_reservation_in_read_keys: "
27267 			    "failed ddi_copyin: mhioc_key_list32_t\n");
27268 			rval = EFAULT;
27269 			goto done;
27270 		}
27271 		li.listsize = li32.listsize;
27272 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
27273 		break;
27274 
27275 	case DDI_MODEL_NONE:
27276 		copysz = sizeof (mhioc_key_list_t);
27277 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
27278 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27279 			    "sd_persistent_reservation_in_read_keys: "
27280 			    "failed ddi_copyin: mhioc_key_list_t\n");
27281 			rval = EFAULT;
27282 			goto done;
27283 		}
27284 		break;
27285 	}
27286 
27287 #else /* ! _MULTI_DATAMODEL */
27288 	copysz = sizeof (mhioc_key_list_t);
27289 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
27290 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27291 		    "sd_persistent_reservation_in_read_keys: "
27292 		    "failed ddi_copyin: mhioc_key_list_t\n");
27293 		rval = EFAULT;
27294 		goto done;
27295 	}
27296 #endif
27297 
27298 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
27299 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
27300 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
27301 
27302 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS,
27303 	    data_len, data_bufp)) != 0) {
27304 		goto done;
27305 	}
27306 	in = (sd_prin_readkeys_t *)data_bufp;
27307 	ptr->generation = BE_32(in->generation);
27308 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
27309 
27310 	/*
27311 	 * Return the min(listsize, listlen) keys
27312 	 */
27313 #ifdef _MULTI_DATAMODEL
27314 
27315 	switch (ddi_model_convert_from(flag & FMODELS)) {
27316 	case DDI_MODEL_ILP32:
27317 		li32.listlen = li.listlen;
27318 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
27319 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27320 			    "sd_persistent_reservation_in_read_keys: "
27321 			    "failed ddi_copyout: mhioc_key_list32_t\n");
27322 			rval = EFAULT;
27323 			goto done;
27324 		}
27325 		break;
27326 
27327 	case DDI_MODEL_NONE:
27328 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
27329 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27330 			    "sd_persistent_reservation_in_read_keys: "
27331 			    "failed ddi_copyout: mhioc_key_list_t\n");
27332 			rval = EFAULT;
27333 			goto done;
27334 		}
27335 		break;
27336 	}
27337 
27338 #else /* ! _MULTI_DATAMODEL */
27339 
27340 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
27341 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27342 		    "sd_persistent_reservation_in_read_keys: "
27343 		    "failed ddi_copyout: mhioc_key_list_t\n");
27344 		rval = EFAULT;
27345 		goto done;
27346 	}
27347 
27348 #endif /* _MULTI_DATAMODEL */
27349 
27350 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
27351 	    li.listsize * MHIOC_RESV_KEY_SIZE);
27352 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
27353 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27354 		    "sd_persistent_reservation_in_read_keys: "
27355 		    "failed ddi_copyout: keylist\n");
27356 		rval = EFAULT;
27357 	}
27358 done:
27359 	kmem_free(data_bufp, data_len);
27360 	return (rval);
27361 }
27362 
27363 
27364 /*
27365  *    Function: sd_persistent_reservation_in_read_resv
27366  *
27367  * Description: This routine is the driver entry point for handling CD-ROM
27368  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
27369  *		by sending the SCSI-3 PRIN commands to the device.
27370  *		Process the read persistent reservations command response by
27371  *		copying the reservation information into the user provided
27372  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
27373  *
27374  *   Arguments: un   -  Pointer to soft state struct for the target.
27375  *		usrp -	user provided pointer to multihost Persistent In Read
27376  *			Keys structure (mhioc_inkeys_t)
27377  *		flag -	this argument is a pass through to ddi_copyxxx()
27378  *			directly from the mode argument of ioctl().
27379  *
27380  * Return Code: 0   - Success
27381  *		EACCES
27382  *		ENOTSUP
27383  *		errno return code from sd_send_scsi_cmd()
27384  *
27385  *     Context: Can sleep. Does not return until command is completed.
27386  */
27387 
27388 static int
27389 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
27390     mhioc_inresvs_t *usrp, int flag)
27391 {
27392 #ifdef _MULTI_DATAMODEL
27393 	struct mhioc_resv_desc_list32 resvlist32;
27394 #endif
27395 	sd_prin_readresv_t	*in;
27396 	mhioc_inresvs_t		*ptr;
27397 	sd_readresv_desc_t	*readresv_ptr;
27398 	mhioc_resv_desc_list_t	resvlist;
27399 	mhioc_resv_desc_t 	resvdesc;
27400 	uchar_t			*data_bufp;
27401 	int 			data_len;
27402 	int			rval;
27403 	int			i;
27404 	size_t			copysz;
27405 	mhioc_resv_desc_t	*bufp;
27406 
27407 	if ((ptr = usrp) == NULL) {
27408 		return (EINVAL);
27409 	}
27410 
27411 	/*
27412 	 * Get the listsize from user
27413 	 */
27414 #ifdef _MULTI_DATAMODEL
27415 	switch (ddi_model_convert_from(flag & FMODELS)) {
27416 	case DDI_MODEL_ILP32:
27417 		copysz = sizeof (struct mhioc_resv_desc_list32);
27418 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
27419 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27420 			    "sd_persistent_reservation_in_read_resv: "
27421 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
27422 			rval = EFAULT;
27423 			goto done;
27424 		}
27425 		resvlist.listsize = resvlist32.listsize;
27426 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
27427 		break;
27428 
27429 	case DDI_MODEL_NONE:
27430 		copysz = sizeof (mhioc_resv_desc_list_t);
27431 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
27432 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27433 			    "sd_persistent_reservation_in_read_resv: "
27434 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
27435 			rval = EFAULT;
27436 			goto done;
27437 		}
27438 		break;
27439 	}
27440 #else /* ! _MULTI_DATAMODEL */
27441 	copysz = sizeof (mhioc_resv_desc_list_t);
27442 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
27443 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27444 		    "sd_persistent_reservation_in_read_resv: "
27445 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
27446 		rval = EFAULT;
27447 		goto done;
27448 	}
27449 #endif /* ! _MULTI_DATAMODEL */
27450 
27451 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
27452 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
27453 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
27454 
27455 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV,
27456 	    data_len, data_bufp)) != 0) {
27457 		goto done;
27458 	}
27459 	in = (sd_prin_readresv_t *)data_bufp;
27460 	ptr->generation = BE_32(in->generation);
27461 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
27462 
27463 	/*
27464 	 * Return the min(listsize, listlen( keys
27465 	 */
27466 #ifdef _MULTI_DATAMODEL
27467 
27468 	switch (ddi_model_convert_from(flag & FMODELS)) {
27469 	case DDI_MODEL_ILP32:
27470 		resvlist32.listlen = resvlist.listlen;
27471 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
27472 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27473 			    "sd_persistent_reservation_in_read_resv: "
27474 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27475 			rval = EFAULT;
27476 			goto done;
27477 		}
27478 		break;
27479 
27480 	case DDI_MODEL_NONE:
27481 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
27482 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27483 			    "sd_persistent_reservation_in_read_resv: "
27484 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27485 			rval = EFAULT;
27486 			goto done;
27487 		}
27488 		break;
27489 	}
27490 
27491 #else /* ! _MULTI_DATAMODEL */
27492 
27493 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
27494 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27495 		    "sd_persistent_reservation_in_read_resv: "
27496 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27497 		rval = EFAULT;
27498 		goto done;
27499 	}
27500 
27501 #endif /* ! _MULTI_DATAMODEL */
27502 
27503 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
27504 	bufp = resvlist.list;
27505 	copysz = sizeof (mhioc_resv_desc_t);
27506 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
27507 	    i++, readresv_ptr++, bufp++) {
27508 
27509 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
27510 		    MHIOC_RESV_KEY_SIZE);
27511 		resvdesc.type  = readresv_ptr->type;
27512 		resvdesc.scope = readresv_ptr->scope;
27513 		resvdesc.scope_specific_addr =
27514 		    BE_32(readresv_ptr->scope_specific_addr);
27515 
27516 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
27517 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27518 			    "sd_persistent_reservation_in_read_resv: "
27519 			    "failed ddi_copyout: resvlist\n");
27520 			rval = EFAULT;
27521 			goto done;
27522 		}
27523 	}
27524 done:
27525 	kmem_free(data_bufp, data_len);
27526 	return (rval);
27527 }
27528 
27529 
27530 /*
27531  *    Function: sr_change_blkmode()
27532  *
27533  * Description: This routine is the driver entry point for handling CD-ROM
27534  *		block mode ioctl requests. Support for returning and changing
27535  *		the current block size in use by the device is implemented. The
27536  *		LBA size is changed via a MODE SELECT Block Descriptor.
27537  *
27538  *		This routine issues a mode sense with an allocation length of
27539  *		12 bytes for the mode page header and a single block descriptor.
27540  *
27541  *   Arguments: dev - the device 'dev_t'
27542  *		cmd - the request type; one of CDROMGBLKMODE (get) or
27543  *		      CDROMSBLKMODE (set)
27544  *		data - current block size or requested block size
27545  *		flag - this argument is a pass through to ddi_copyxxx() directly
27546  *		       from the mode argument of ioctl().
27547  *
27548  * Return Code: the code returned by sd_send_scsi_cmd()
27549  *		EINVAL if invalid arguments are provided
27550  *		EFAULT if ddi_copyxxx() fails
27551  *		ENXIO if fail ddi_get_soft_state
27552  *		EIO if invalid mode sense block descriptor length
27553  *
27554  */
27555 
27556 static int
27557 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
27558 {
27559 	struct sd_lun			*un = NULL;
27560 	struct mode_header		*sense_mhp, *select_mhp;
27561 	struct block_descriptor		*sense_desc, *select_desc;
27562 	int				current_bsize;
27563 	int				rval = EINVAL;
27564 	uchar_t				*sense = NULL;
27565 	uchar_t				*select = NULL;
27566 
27567 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
27568 
27569 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27570 		return (ENXIO);
27571 	}
27572 
27573 	/*
27574 	 * The block length is changed via the Mode Select block descriptor, the
27575 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
27576 	 * required as part of this routine. Therefore the mode sense allocation
27577 	 * length is specified to be the length of a mode page header and a
27578 	 * block descriptor.
27579 	 */
27580 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27581 
27582 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
27583 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) {
27584 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27585 		    "sr_change_blkmode: Mode Sense Failed\n");
27586 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27587 		return (rval);
27588 	}
27589 
27590 	/* Check the block descriptor len to handle only 1 block descriptor */
27591 	sense_mhp = (struct mode_header *)sense;
27592 	if ((sense_mhp->bdesc_length == 0) ||
27593 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
27594 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27595 		    "sr_change_blkmode: Mode Sense returned invalid block"
27596 		    " descriptor length\n");
27597 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27598 		return (EIO);
27599 	}
27600 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
27601 	current_bsize = ((sense_desc->blksize_hi << 16) |
27602 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
27603 
27604 	/* Process command */
27605 	switch (cmd) {
27606 	case CDROMGBLKMODE:
27607 		/* Return the block size obtained during the mode sense */
27608 		if (ddi_copyout(&current_bsize, (void *)data,
27609 		    sizeof (int), flag) != 0)
27610 			rval = EFAULT;
27611 		break;
27612 	case CDROMSBLKMODE:
27613 		/* Validate the requested block size */
27614 		switch (data) {
27615 		case CDROM_BLK_512:
27616 		case CDROM_BLK_1024:
27617 		case CDROM_BLK_2048:
27618 		case CDROM_BLK_2056:
27619 		case CDROM_BLK_2336:
27620 		case CDROM_BLK_2340:
27621 		case CDROM_BLK_2352:
27622 		case CDROM_BLK_2368:
27623 		case CDROM_BLK_2448:
27624 		case CDROM_BLK_2646:
27625 		case CDROM_BLK_2647:
27626 			break;
27627 		default:
27628 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27629 			    "sr_change_blkmode: "
27630 			    "Block Size '%ld' Not Supported\n", data);
27631 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27632 			return (EINVAL);
27633 		}
27634 
27635 		/*
27636 		 * The current block size matches the requested block size so
27637 		 * there is no need to send the mode select to change the size
27638 		 */
27639 		if (current_bsize == data) {
27640 			break;
27641 		}
27642 
27643 		/* Build the select data for the requested block size */
27644 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27645 		select_mhp = (struct mode_header *)select;
27646 		select_desc =
27647 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
27648 		/*
27649 		 * The LBA size is changed via the block descriptor, so the
27650 		 * descriptor is built according to the user data
27651 		 */
27652 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
27653 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
27654 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
27655 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
27656 
27657 		/* Send the mode select for the requested block size */
27658 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
27659 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27660 		    SD_PATH_STANDARD)) != 0) {
27661 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27662 			    "sr_change_blkmode: Mode Select Failed\n");
27663 			/*
27664 			 * The mode select failed for the requested block size,
27665 			 * so reset the data for the original block size and
27666 			 * send it to the target. The error is indicated by the
27667 			 * return value for the failed mode select.
27668 			 */
27669 			select_desc->blksize_hi  = sense_desc->blksize_hi;
27670 			select_desc->blksize_mid = sense_desc->blksize_mid;
27671 			select_desc->blksize_lo  = sense_desc->blksize_lo;
27672 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
27673 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27674 			    SD_PATH_STANDARD);
27675 		} else {
27676 			ASSERT(!mutex_owned(SD_MUTEX(un)));
27677 			mutex_enter(SD_MUTEX(un));
27678 			sd_update_block_info(un, (uint32_t)data, 0);
27679 
27680 			mutex_exit(SD_MUTEX(un));
27681 		}
27682 		break;
27683 	default:
27684 		/* should not reach here, but check anyway */
27685 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27686 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
27687 		rval = EINVAL;
27688 		break;
27689 	}
27690 
27691 	if (select) {
27692 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
27693 	}
27694 	if (sense) {
27695 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27696 	}
27697 	return (rval);
27698 }
27699 
27700 
27701 /*
27702  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27703  * implement driver support for getting and setting the CD speed. The command
27704  * set used will be based on the device type. If the device has not been
27705  * identified as MMC the Toshiba vendor specific mode page will be used. If
27706  * the device is MMC but does not support the Real Time Streaming feature
27707  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27708  * be used to read the speed.
27709  */
27710 
27711 /*
27712  *    Function: sr_change_speed()
27713  *
27714  * Description: This routine is the driver entry point for handling CD-ROM
27715  *		drive speed ioctl requests for devices supporting the Toshiba
27716  *		vendor specific drive speed mode page. Support for returning
27717  *		and changing the current drive speed in use by the device is
27718  *		implemented.
27719  *
27720  *   Arguments: dev - the device 'dev_t'
27721  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
27722  *		      CDROMSDRVSPEED (set)
27723  *		data - current drive speed or requested drive speed
27724  *		flag - this argument is a pass through to ddi_copyxxx() directly
27725  *		       from the mode argument of ioctl().
27726  *
27727  * Return Code: the code returned by sd_send_scsi_cmd()
27728  *		EINVAL if invalid arguments are provided
27729  *		EFAULT if ddi_copyxxx() fails
27730  *		ENXIO if fail ddi_get_soft_state
27731  *		EIO if invalid mode sense block descriptor length
27732  */
27733 
27734 static int
27735 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27736 {
27737 	struct sd_lun			*un = NULL;
27738 	struct mode_header		*sense_mhp, *select_mhp;
27739 	struct mode_speed		*sense_page, *select_page;
27740 	int				current_speed;
27741 	int				rval = EINVAL;
27742 	int				bd_len;
27743 	uchar_t				*sense = NULL;
27744 	uchar_t				*select = NULL;
27745 
27746 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27747 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27748 		return (ENXIO);
27749 	}
27750 
27751 	/*
27752 	 * Note: The drive speed is being modified here according to a Toshiba
27753 	 * vendor specific mode page (0x31).
27754 	 */
27755 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27756 
27757 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
27758 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27759 		SD_PATH_STANDARD)) != 0) {
27760 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27761 		    "sr_change_speed: Mode Sense Failed\n");
27762 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27763 		return (rval);
27764 	}
27765 	sense_mhp  = (struct mode_header *)sense;
27766 
27767 	/* Check the block descriptor len to handle only 1 block descriptor */
27768 	bd_len = sense_mhp->bdesc_length;
27769 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27770 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27771 		    "sr_change_speed: Mode Sense returned invalid block "
27772 		    "descriptor length\n");
27773 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27774 		return (EIO);
27775 	}
27776 
27777 	sense_page = (struct mode_speed *)
27778 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27779 	current_speed = sense_page->speed;
27780 
27781 	/* Process command */
27782 	switch (cmd) {
27783 	case CDROMGDRVSPEED:
27784 		/* Return the drive speed obtained during the mode sense */
27785 		if (current_speed == 0x2) {
27786 			current_speed = CDROM_TWELVE_SPEED;
27787 		}
27788 		if (ddi_copyout(&current_speed, (void *)data,
27789 		    sizeof (int), flag) != 0) {
27790 			rval = EFAULT;
27791 		}
27792 		break;
27793 	case CDROMSDRVSPEED:
27794 		/* Validate the requested drive speed */
27795 		switch ((uchar_t)data) {
27796 		case CDROM_TWELVE_SPEED:
27797 			data = 0x2;
27798 			/*FALLTHROUGH*/
27799 		case CDROM_NORMAL_SPEED:
27800 		case CDROM_DOUBLE_SPEED:
27801 		case CDROM_QUAD_SPEED:
27802 		case CDROM_MAXIMUM_SPEED:
27803 			break;
27804 		default:
27805 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27806 			    "sr_change_speed: "
27807 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27808 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27809 			return (EINVAL);
27810 		}
27811 
27812 		/*
27813 		 * The current drive speed matches the requested drive speed so
27814 		 * there is no need to send the mode select to change the speed
27815 		 */
27816 		if (current_speed == data) {
27817 			break;
27818 		}
27819 
27820 		/* Build the select data for the requested drive speed */
27821 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27822 		select_mhp = (struct mode_header *)select;
27823 		select_mhp->bdesc_length = 0;
27824 		select_page =
27825 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27826 		select_page =
27827 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27828 		select_page->mode_page.code = CDROM_MODE_SPEED;
27829 		select_page->mode_page.length = 2;
27830 		select_page->speed = (uchar_t)data;
27831 
27832 		/* Send the mode select for the requested block size */
27833 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
27834 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27835 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
27836 			/*
27837 			 * The mode select failed for the requested drive speed,
27838 			 * so reset the data for the original drive speed and
27839 			 * send it to the target. The error is indicated by the
27840 			 * return value for the failed mode select.
27841 			 */
27842 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27843 			    "sr_drive_speed: Mode Select Failed\n");
27844 			select_page->speed = sense_page->speed;
27845 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
27846 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27847 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27848 		}
27849 		break;
27850 	default:
27851 		/* should not reach here, but check anyway */
27852 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27853 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27854 		rval = EINVAL;
27855 		break;
27856 	}
27857 
27858 	if (select) {
27859 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27860 	}
27861 	if (sense) {
27862 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27863 	}
27864 
27865 	return (rval);
27866 }
27867 
27868 
27869 /*
27870  *    Function: sr_atapi_change_speed()
27871  *
27872  * Description: This routine is the driver entry point for handling CD-ROM
27873  *		drive speed ioctl requests for MMC devices that do not support
27874  *		the Real Time Streaming feature (0x107).
27875  *
27876  *		Note: This routine will use the SET SPEED command which may not
27877  *		be supported by all devices.
27878  *
27879  *   Arguments: dev- the device 'dev_t'
27880  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27881  *		     CDROMSDRVSPEED (set)
27882  *		data- current drive speed or requested drive speed
27883  *		flag- this argument is a pass through to ddi_copyxxx() directly
27884  *		      from the mode argument of ioctl().
27885  *
27886  * Return Code: the code returned by sd_send_scsi_cmd()
27887  *		EINVAL if invalid arguments are provided
27888  *		EFAULT if ddi_copyxxx() fails
27889  *		ENXIO if fail ddi_get_soft_state
27890  *		EIO if invalid mode sense block descriptor length
27891  */
27892 
27893 static int
27894 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27895 {
27896 	struct sd_lun			*un;
27897 	struct uscsi_cmd		*com = NULL;
27898 	struct mode_header_grp2		*sense_mhp;
27899 	uchar_t				*sense_page;
27900 	uchar_t				*sense = NULL;
27901 	char				cdb[CDB_GROUP5];
27902 	int				bd_len;
27903 	int				current_speed = 0;
27904 	int				max_speed = 0;
27905 	int				rval;
27906 
27907 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27908 
27909 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27910 		return (ENXIO);
27911 	}
27912 
27913 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27914 
27915 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
27916 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27917 	    SD_PATH_STANDARD)) != 0) {
27918 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27919 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27920 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27921 		return (rval);
27922 	}
27923 
27924 	/* Check the block descriptor len to handle only 1 block descriptor */
27925 	sense_mhp = (struct mode_header_grp2 *)sense;
27926 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27927 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27928 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27929 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27930 		    "block descriptor length\n");
27931 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27932 		return (EIO);
27933 	}
27934 
27935 	/* Calculate the current and maximum drive speeds */
27936 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27937 	current_speed = (sense_page[14] << 8) | sense_page[15];
27938 	max_speed = (sense_page[8] << 8) | sense_page[9];
27939 
27940 	/* Process the command */
27941 	switch (cmd) {
27942 	case CDROMGDRVSPEED:
27943 		current_speed /= SD_SPEED_1X;
27944 		if (ddi_copyout(&current_speed, (void *)data,
27945 		    sizeof (int), flag) != 0)
27946 			rval = EFAULT;
27947 		break;
27948 	case CDROMSDRVSPEED:
27949 		/* Convert the speed code to KB/sec */
27950 		switch ((uchar_t)data) {
27951 		case CDROM_NORMAL_SPEED:
27952 			current_speed = SD_SPEED_1X;
27953 			break;
27954 		case CDROM_DOUBLE_SPEED:
27955 			current_speed = 2 * SD_SPEED_1X;
27956 			break;
27957 		case CDROM_QUAD_SPEED:
27958 			current_speed = 4 * SD_SPEED_1X;
27959 			break;
27960 		case CDROM_TWELVE_SPEED:
27961 			current_speed = 12 * SD_SPEED_1X;
27962 			break;
27963 		case CDROM_MAXIMUM_SPEED:
27964 			current_speed = 0xffff;
27965 			break;
27966 		default:
27967 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27968 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27969 			    (uchar_t)data);
27970 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27971 			return (EINVAL);
27972 		}
27973 
27974 		/* Check the request against the drive's max speed. */
27975 		if (current_speed != 0xffff) {
27976 			if (current_speed > max_speed) {
27977 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27978 				return (EINVAL);
27979 			}
27980 		}
27981 
27982 		/*
27983 		 * Build and send the SET SPEED command
27984 		 *
27985 		 * Note: The SET SPEED (0xBB) command used in this routine is
27986 		 * obsolete per the SCSI MMC spec but still supported in the
27987 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27988 		 * therefore the command is still implemented in this routine.
27989 		 */
27990 		bzero(cdb, sizeof (cdb));
27991 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27992 		cdb[2] = (uchar_t)(current_speed >> 8);
27993 		cdb[3] = (uchar_t)current_speed;
27994 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27995 		com->uscsi_cdb	   = (caddr_t)cdb;
27996 		com->uscsi_cdblen  = CDB_GROUP5;
27997 		com->uscsi_bufaddr = NULL;
27998 		com->uscsi_buflen  = 0;
27999 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
28000 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0,
28001 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28002 		break;
28003 	default:
28004 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28005 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
28006 		rval = EINVAL;
28007 	}
28008 
28009 	if (sense) {
28010 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
28011 	}
28012 	if (com) {
28013 		kmem_free(com, sizeof (*com));
28014 	}
28015 	return (rval);
28016 }
28017 
28018 
28019 /*
28020  *    Function: sr_pause_resume()
28021  *
28022  * Description: This routine is the driver entry point for handling CD-ROM
28023  *		pause/resume ioctl requests. This only affects the audio play
28024  *		operation.
28025  *
28026  *   Arguments: dev - the device 'dev_t'
28027  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
28028  *		      for setting the resume bit of the cdb.
28029  *
28030  * Return Code: the code returned by sd_send_scsi_cmd()
28031  *		EINVAL if invalid mode specified
28032  *
28033  */
28034 
28035 static int
28036 sr_pause_resume(dev_t dev, int cmd)
28037 {
28038 	struct sd_lun		*un;
28039 	struct uscsi_cmd	*com;
28040 	char			cdb[CDB_GROUP1];
28041 	int			rval;
28042 
28043 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28044 		return (ENXIO);
28045 	}
28046 
28047 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28048 	bzero(cdb, CDB_GROUP1);
28049 	cdb[0] = SCMD_PAUSE_RESUME;
28050 	switch (cmd) {
28051 	case CDROMRESUME:
28052 		cdb[8] = 1;
28053 		break;
28054 	case CDROMPAUSE:
28055 		cdb[8] = 0;
28056 		break;
28057 	default:
28058 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
28059 		    " Command '%x' Not Supported\n", cmd);
28060 		rval = EINVAL;
28061 		goto done;
28062 	}
28063 
28064 	com->uscsi_cdb    = cdb;
28065 	com->uscsi_cdblen = CDB_GROUP1;
28066 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
28067 
28068 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28069 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28070 
28071 done:
28072 	kmem_free(com, sizeof (*com));
28073 	return (rval);
28074 }
28075 
28076 
28077 /*
28078  *    Function: sr_play_msf()
28079  *
28080  * Description: This routine is the driver entry point for handling CD-ROM
28081  *		ioctl requests to output the audio signals at the specified
28082  *		starting address and continue the audio play until the specified
28083  *		ending address (CDROMPLAYMSF) The address is in Minute Second
28084  *		Frame (MSF) format.
28085  *
28086  *   Arguments: dev	- the device 'dev_t'
28087  *		data	- pointer to user provided audio msf structure,
28088  *		          specifying start/end addresses.
28089  *		flag	- this argument is a pass through to ddi_copyxxx()
28090  *		          directly from the mode argument of ioctl().
28091  *
28092  * Return Code: the code returned by sd_send_scsi_cmd()
28093  *		EFAULT if ddi_copyxxx() fails
28094  *		ENXIO if fail ddi_get_soft_state
28095  *		EINVAL if data pointer is NULL
28096  */
28097 
28098 static int
28099 sr_play_msf(dev_t dev, caddr_t data, int flag)
28100 {
28101 	struct sd_lun		*un;
28102 	struct uscsi_cmd	*com;
28103 	struct cdrom_msf	msf_struct;
28104 	struct cdrom_msf	*msf = &msf_struct;
28105 	char			cdb[CDB_GROUP1];
28106 	int			rval;
28107 
28108 	if (data == NULL) {
28109 		return (EINVAL);
28110 	}
28111 
28112 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28113 		return (ENXIO);
28114 	}
28115 
28116 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
28117 		return (EFAULT);
28118 	}
28119 
28120 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28121 	bzero(cdb, CDB_GROUP1);
28122 	cdb[0] = SCMD_PLAYAUDIO_MSF;
28123 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
28124 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
28125 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
28126 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
28127 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
28128 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
28129 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
28130 	} else {
28131 		cdb[3] = msf->cdmsf_min0;
28132 		cdb[4] = msf->cdmsf_sec0;
28133 		cdb[5] = msf->cdmsf_frame0;
28134 		cdb[6] = msf->cdmsf_min1;
28135 		cdb[7] = msf->cdmsf_sec1;
28136 		cdb[8] = msf->cdmsf_frame1;
28137 	}
28138 	com->uscsi_cdb    = cdb;
28139 	com->uscsi_cdblen = CDB_GROUP1;
28140 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
28141 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28142 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28143 	kmem_free(com, sizeof (*com));
28144 	return (rval);
28145 }
28146 
28147 
28148 /*
28149  *    Function: sr_play_trkind()
28150  *
28151  * Description: This routine is the driver entry point for handling CD-ROM
28152  *		ioctl requests to output the audio signals at the specified
28153  *		starting address and continue the audio play until the specified
28154  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
28155  *		format.
28156  *
28157  *   Arguments: dev	- the device 'dev_t'
28158  *		data	- pointer to user provided audio track/index structure,
28159  *		          specifying start/end addresses.
28160  *		flag	- this argument is a pass through to ddi_copyxxx()
28161  *		          directly from the mode argument of ioctl().
28162  *
28163  * Return Code: the code returned by sd_send_scsi_cmd()
28164  *		EFAULT if ddi_copyxxx() fails
28165  *		ENXIO if fail ddi_get_soft_state
28166  *		EINVAL if data pointer is NULL
28167  */
28168 
28169 static int
28170 sr_play_trkind(dev_t dev, caddr_t data, int flag)
28171 {
28172 	struct cdrom_ti		ti_struct;
28173 	struct cdrom_ti		*ti = &ti_struct;
28174 	struct uscsi_cmd	*com = NULL;
28175 	char			cdb[CDB_GROUP1];
28176 	int			rval;
28177 
28178 	if (data == NULL) {
28179 		return (EINVAL);
28180 	}
28181 
28182 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
28183 		return (EFAULT);
28184 	}
28185 
28186 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28187 	bzero(cdb, CDB_GROUP1);
28188 	cdb[0] = SCMD_PLAYAUDIO_TI;
28189 	cdb[4] = ti->cdti_trk0;
28190 	cdb[5] = ti->cdti_ind0;
28191 	cdb[7] = ti->cdti_trk1;
28192 	cdb[8] = ti->cdti_ind1;
28193 	com->uscsi_cdb    = cdb;
28194 	com->uscsi_cdblen = CDB_GROUP1;
28195 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
28196 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28197 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28198 	kmem_free(com, sizeof (*com));
28199 	return (rval);
28200 }
28201 
28202 
28203 /*
28204  *    Function: sr_read_all_subcodes()
28205  *
28206  * Description: This routine is the driver entry point for handling CD-ROM
28207  *		ioctl requests to return raw subcode data while the target is
28208  *		playing audio (CDROMSUBCODE).
28209  *
28210  *   Arguments: dev	- the device 'dev_t'
28211  *		data	- pointer to user provided cdrom subcode structure,
28212  *		          specifying the transfer length and address.
28213  *		flag	- this argument is a pass through to ddi_copyxxx()
28214  *		          directly from the mode argument of ioctl().
28215  *
28216  * Return Code: the code returned by sd_send_scsi_cmd()
28217  *		EFAULT if ddi_copyxxx() fails
28218  *		ENXIO if fail ddi_get_soft_state
28219  *		EINVAL if data pointer is NULL
28220  */
28221 
28222 static int
28223 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
28224 {
28225 	struct sd_lun		*un = NULL;
28226 	struct uscsi_cmd	*com = NULL;
28227 	struct cdrom_subcode	*subcode = NULL;
28228 	int			rval;
28229 	size_t			buflen;
28230 	char			cdb[CDB_GROUP5];
28231 
28232 #ifdef _MULTI_DATAMODEL
28233 	/* To support ILP32 applications in an LP64 world */
28234 	struct cdrom_subcode32		cdrom_subcode32;
28235 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
28236 #endif
28237 	if (data == NULL) {
28238 		return (EINVAL);
28239 	}
28240 
28241 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28242 		return (ENXIO);
28243 	}
28244 
28245 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
28246 
28247 #ifdef _MULTI_DATAMODEL
28248 	switch (ddi_model_convert_from(flag & FMODELS)) {
28249 	case DDI_MODEL_ILP32:
28250 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
28251 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28252 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
28253 			kmem_free(subcode, sizeof (struct cdrom_subcode));
28254 			return (EFAULT);
28255 		}
28256 		/* Convert the ILP32 uscsi data from the application to LP64 */
28257 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
28258 		break;
28259 	case DDI_MODEL_NONE:
28260 		if (ddi_copyin(data, subcode,
28261 		    sizeof (struct cdrom_subcode), flag)) {
28262 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28263 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
28264 			kmem_free(subcode, sizeof (struct cdrom_subcode));
28265 			return (EFAULT);
28266 		}
28267 		break;
28268 	}
28269 #else /* ! _MULTI_DATAMODEL */
28270 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
28271 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28272 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
28273 		kmem_free(subcode, sizeof (struct cdrom_subcode));
28274 		return (EFAULT);
28275 	}
28276 #endif /* _MULTI_DATAMODEL */
28277 
28278 	/*
28279 	 * Since MMC-2 expects max 3 bytes for length, check if the
28280 	 * length input is greater than 3 bytes
28281 	 */
28282 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
28283 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28284 		    "sr_read_all_subcodes: "
28285 		    "cdrom transfer length too large: %d (limit %d)\n",
28286 		    subcode->cdsc_length, 0xFFFFFF);
28287 		kmem_free(subcode, sizeof (struct cdrom_subcode));
28288 		return (EINVAL);
28289 	}
28290 
28291 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
28292 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28293 	bzero(cdb, CDB_GROUP5);
28294 
28295 	if (un->un_f_mmc_cap == TRUE) {
28296 		cdb[0] = (char)SCMD_READ_CD;
28297 		cdb[2] = (char)0xff;
28298 		cdb[3] = (char)0xff;
28299 		cdb[4] = (char)0xff;
28300 		cdb[5] = (char)0xff;
28301 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
28302 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
28303 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
28304 		cdb[10] = 1;
28305 	} else {
28306 		/*
28307 		 * Note: A vendor specific command (0xDF) is being used her to
28308 		 * request a read of all subcodes.
28309 		 */
28310 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
28311 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
28312 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
28313 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
28314 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
28315 	}
28316 	com->uscsi_cdb	   = cdb;
28317 	com->uscsi_cdblen  = CDB_GROUP5;
28318 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
28319 	com->uscsi_buflen  = buflen;
28320 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28321 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28322 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28323 	kmem_free(subcode, sizeof (struct cdrom_subcode));
28324 	kmem_free(com, sizeof (*com));
28325 	return (rval);
28326 }
28327 
28328 
28329 /*
28330  *    Function: sr_read_subchannel()
28331  *
28332  * Description: This routine is the driver entry point for handling CD-ROM
28333  *		ioctl requests to return the Q sub-channel data of the CD
28334  *		current position block. (CDROMSUBCHNL) The data includes the
28335  *		track number, index number, absolute CD-ROM address (LBA or MSF
28336  *		format per the user) , track relative CD-ROM address (LBA or MSF
28337  *		format per the user), control data and audio status.
28338  *
28339  *   Arguments: dev	- the device 'dev_t'
28340  *		data	- pointer to user provided cdrom sub-channel structure
28341  *		flag	- this argument is a pass through to ddi_copyxxx()
28342  *		          directly from the mode argument of ioctl().
28343  *
28344  * Return Code: the code returned by sd_send_scsi_cmd()
28345  *		EFAULT if ddi_copyxxx() fails
28346  *		ENXIO if fail ddi_get_soft_state
28347  *		EINVAL if data pointer is NULL
28348  */
28349 
28350 static int
28351 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
28352 {
28353 	struct sd_lun		*un;
28354 	struct uscsi_cmd	*com;
28355 	struct cdrom_subchnl	subchanel;
28356 	struct cdrom_subchnl	*subchnl = &subchanel;
28357 	char			cdb[CDB_GROUP1];
28358 	caddr_t			buffer;
28359 	int			rval;
28360 
28361 	if (data == NULL) {
28362 		return (EINVAL);
28363 	}
28364 
28365 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28366 	    (un->un_state == SD_STATE_OFFLINE)) {
28367 		return (ENXIO);
28368 	}
28369 
28370 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
28371 		return (EFAULT);
28372 	}
28373 
28374 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
28375 	bzero(cdb, CDB_GROUP1);
28376 	cdb[0] = SCMD_READ_SUBCHANNEL;
28377 	/* Set the MSF bit based on the user requested address format */
28378 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
28379 	/*
28380 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
28381 	 * returned
28382 	 */
28383 	cdb[2] = 0x40;
28384 	/*
28385 	 * Set byte 3 to specify the return data format. A value of 0x01
28386 	 * indicates that the CD-ROM current position should be returned.
28387 	 */
28388 	cdb[3] = 0x01;
28389 	cdb[8] = 0x10;
28390 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28391 	com->uscsi_cdb	   = cdb;
28392 	com->uscsi_cdblen  = CDB_GROUP1;
28393 	com->uscsi_bufaddr = buffer;
28394 	com->uscsi_buflen  = 16;
28395 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28396 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28397 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28398 	if (rval != 0) {
28399 		kmem_free(buffer, 16);
28400 		kmem_free(com, sizeof (*com));
28401 		return (rval);
28402 	}
28403 
28404 	/* Process the returned Q sub-channel data */
28405 	subchnl->cdsc_audiostatus = buffer[1];
28406 	subchnl->cdsc_adr	= (buffer[5] & 0xF0);
28407 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
28408 	subchnl->cdsc_trk	= buffer[6];
28409 	subchnl->cdsc_ind	= buffer[7];
28410 	if (subchnl->cdsc_format & CDROM_LBA) {
28411 		subchnl->cdsc_absaddr.lba =
28412 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
28413 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
28414 		subchnl->cdsc_reladdr.lba =
28415 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
28416 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
28417 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
28418 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
28419 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
28420 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
28421 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
28422 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
28423 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
28424 	} else {
28425 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
28426 		subchnl->cdsc_absaddr.msf.second = buffer[10];
28427 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
28428 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
28429 		subchnl->cdsc_reladdr.msf.second = buffer[14];
28430 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
28431 	}
28432 	kmem_free(buffer, 16);
28433 	kmem_free(com, sizeof (*com));
28434 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
28435 	    != 0) {
28436 		return (EFAULT);
28437 	}
28438 	return (rval);
28439 }
28440 
28441 
28442 /*
28443  *    Function: sr_read_tocentry()
28444  *
28445  * Description: This routine is the driver entry point for handling CD-ROM
28446  *		ioctl requests to read from the Table of Contents (TOC)
28447  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
28448  *		fields, the starting address (LBA or MSF format per the user)
28449  *		and the data mode if the user specified track is a data track.
28450  *
28451  *		Note: The READ HEADER (0x44) command used in this routine is
28452  *		obsolete per the SCSI MMC spec but still supported in the
28453  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
28454  *		therefore the command is still implemented in this routine.
28455  *
28456  *   Arguments: dev	- the device 'dev_t'
28457  *		data	- pointer to user provided toc entry structure,
28458  *			  specifying the track # and the address format
28459  *			  (LBA or MSF).
28460  *		flag	- this argument is a pass through to ddi_copyxxx()
28461  *		          directly from the mode argument of ioctl().
28462  *
28463  * Return Code: the code returned by sd_send_scsi_cmd()
28464  *		EFAULT if ddi_copyxxx() fails
28465  *		ENXIO if fail ddi_get_soft_state
28466  *		EINVAL if data pointer is NULL
28467  */
28468 
28469 static int
28470 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
28471 {
28472 	struct sd_lun		*un = NULL;
28473 	struct uscsi_cmd	*com;
28474 	struct cdrom_tocentry	toc_entry;
28475 	struct cdrom_tocentry	*entry = &toc_entry;
28476 	caddr_t			buffer;
28477 	int			rval;
28478 	char			cdb[CDB_GROUP1];
28479 
28480 	if (data == NULL) {
28481 		return (EINVAL);
28482 	}
28483 
28484 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28485 	    (un->un_state == SD_STATE_OFFLINE)) {
28486 		return (ENXIO);
28487 	}
28488 
28489 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
28490 		return (EFAULT);
28491 	}
28492 
28493 	/* Validate the requested track and address format */
28494 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
28495 		return (EINVAL);
28496 	}
28497 
28498 	if (entry->cdte_track == 0) {
28499 		return (EINVAL);
28500 	}
28501 
28502 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
28503 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28504 	bzero(cdb, CDB_GROUP1);
28505 
28506 	cdb[0] = SCMD_READ_TOC;
28507 	/* Set the MSF bit based on the user requested address format  */
28508 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
28509 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28510 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
28511 	} else {
28512 		cdb[6] = entry->cdte_track;
28513 	}
28514 
28515 	/*
28516 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
28517 	 * (4 byte TOC response header + 8 byte track descriptor)
28518 	 */
28519 	cdb[8] = 12;
28520 	com->uscsi_cdb	   = cdb;
28521 	com->uscsi_cdblen  = CDB_GROUP1;
28522 	com->uscsi_bufaddr = buffer;
28523 	com->uscsi_buflen  = 0x0C;
28524 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
28525 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28526 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28527 	if (rval != 0) {
28528 		kmem_free(buffer, 12);
28529 		kmem_free(com, sizeof (*com));
28530 		return (rval);
28531 	}
28532 
28533 	/* Process the toc entry */
28534 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
28535 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
28536 	if (entry->cdte_format & CDROM_LBA) {
28537 		entry->cdte_addr.lba =
28538 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
28539 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
28540 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
28541 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
28542 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
28543 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
28544 		/*
28545 		 * Send a READ TOC command using the LBA address format to get
28546 		 * the LBA for the track requested so it can be used in the
28547 		 * READ HEADER request
28548 		 *
28549 		 * Note: The MSF bit of the READ HEADER command specifies the
28550 		 * output format. The block address specified in that command
28551 		 * must be in LBA format.
28552 		 */
28553 		cdb[1] = 0;
28554 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28555 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28556 		if (rval != 0) {
28557 			kmem_free(buffer, 12);
28558 			kmem_free(com, sizeof (*com));
28559 			return (rval);
28560 		}
28561 	} else {
28562 		entry->cdte_addr.msf.minute	= buffer[9];
28563 		entry->cdte_addr.msf.second	= buffer[10];
28564 		entry->cdte_addr.msf.frame	= buffer[11];
28565 		/*
28566 		 * Send a READ TOC command using the LBA address format to get
28567 		 * the LBA for the track requested so it can be used in the
28568 		 * READ HEADER request
28569 		 *
28570 		 * Note: The MSF bit of the READ HEADER command specifies the
28571 		 * output format. The block address specified in that command
28572 		 * must be in LBA format.
28573 		 */
28574 		cdb[1] = 0;
28575 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28576 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28577 		if (rval != 0) {
28578 			kmem_free(buffer, 12);
28579 			kmem_free(com, sizeof (*com));
28580 			return (rval);
28581 		}
28582 	}
28583 
28584 	/*
28585 	 * Build and send the READ HEADER command to determine the data mode of
28586 	 * the user specified track.
28587 	 */
28588 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
28589 	    (entry->cdte_track != CDROM_LEADOUT)) {
28590 		bzero(cdb, CDB_GROUP1);
28591 		cdb[0] = SCMD_READ_HEADER;
28592 		cdb[2] = buffer[8];
28593 		cdb[3] = buffer[9];
28594 		cdb[4] = buffer[10];
28595 		cdb[5] = buffer[11];
28596 		cdb[8] = 0x08;
28597 		com->uscsi_buflen = 0x08;
28598 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28599 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28600 		if (rval == 0) {
28601 			entry->cdte_datamode = buffer[0];
28602 		} else {
28603 			/*
28604 			 * READ HEADER command failed, since this is
28605 			 * obsoleted in one spec, its better to return
28606 			 * -1 for an invlid track so that we can still
28607 			 * recieve the rest of the TOC data.
28608 			 */
28609 			entry->cdte_datamode = (uchar_t)-1;
28610 		}
28611 	} else {
28612 		entry->cdte_datamode = (uchar_t)-1;
28613 	}
28614 
28615 	kmem_free(buffer, 12);
28616 	kmem_free(com, sizeof (*com));
28617 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
28618 		return (EFAULT);
28619 
28620 	return (rval);
28621 }
28622 
28623 
28624 /*
28625  *    Function: sr_read_tochdr()
28626  *
28627  * Description: This routine is the driver entry point for handling CD-ROM
28628  * 		ioctl requests to read the Table of Contents (TOC) header
28629  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
28630  *		and ending track numbers
28631  *
28632  *   Arguments: dev	- the device 'dev_t'
28633  *		data	- pointer to user provided toc header structure,
28634  *			  specifying the starting and ending track numbers.
28635  *		flag	- this argument is a pass through to ddi_copyxxx()
28636  *			  directly from the mode argument of ioctl().
28637  *
28638  * Return Code: the code returned by sd_send_scsi_cmd()
28639  *		EFAULT if ddi_copyxxx() fails
28640  *		ENXIO if fail ddi_get_soft_state
28641  *		EINVAL if data pointer is NULL
28642  */
28643 
28644 static int
28645 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
28646 {
28647 	struct sd_lun		*un;
28648 	struct uscsi_cmd	*com;
28649 	struct cdrom_tochdr	toc_header;
28650 	struct cdrom_tochdr	*hdr = &toc_header;
28651 	char			cdb[CDB_GROUP1];
28652 	int			rval;
28653 	caddr_t			buffer;
28654 
28655 	if (data == NULL) {
28656 		return (EINVAL);
28657 	}
28658 
28659 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28660 	    (un->un_state == SD_STATE_OFFLINE)) {
28661 		return (ENXIO);
28662 	}
28663 
28664 	buffer = kmem_zalloc(4, KM_SLEEP);
28665 	bzero(cdb, CDB_GROUP1);
28666 	cdb[0] = SCMD_READ_TOC;
28667 	/*
28668 	 * Specifying a track number of 0x00 in the READ TOC command indicates
28669 	 * that the TOC header should be returned
28670 	 */
28671 	cdb[6] = 0x00;
28672 	/*
28673 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28674 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28675 	 */
28676 	cdb[8] = 0x04;
28677 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28678 	com->uscsi_cdb	   = cdb;
28679 	com->uscsi_cdblen  = CDB_GROUP1;
28680 	com->uscsi_bufaddr = buffer;
28681 	com->uscsi_buflen  = 0x04;
28682 	com->uscsi_timeout = 300;
28683 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28684 
28685 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28686 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28687 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28688 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28689 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28690 	} else {
28691 		hdr->cdth_trk0 = buffer[2];
28692 		hdr->cdth_trk1 = buffer[3];
28693 	}
28694 	kmem_free(buffer, 4);
28695 	kmem_free(com, sizeof (*com));
28696 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28697 		return (EFAULT);
28698 	}
28699 	return (rval);
28700 }
28701 
28702 
28703 /*
28704  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28705  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28706  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28707  * digital audio and extended architecture digital audio. These modes are
28708  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28709  * MMC specs.
28710  *
28711  * In addition to support for the various data formats these routines also
28712  * include support for devices that implement only the direct access READ
28713  * commands (0x08, 0x28), devices that implement the READ_CD commands
28714  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28715  * READ CDXA commands (0xD8, 0xDB)
28716  */
28717 
28718 /*
28719  *    Function: sr_read_mode1()
28720  *
28721  * Description: This routine is the driver entry point for handling CD-ROM
28722  *		ioctl read mode1 requests (CDROMREADMODE1).
28723  *
28724  *   Arguments: dev	- the device 'dev_t'
28725  *		data	- pointer to user provided cd read structure specifying
28726  *			  the lba buffer address and length.
28727  *		flag	- this argument is a pass through to ddi_copyxxx()
28728  *			  directly from the mode argument of ioctl().
28729  *
28730  * Return Code: the code returned by sd_send_scsi_cmd()
28731  *		EFAULT if ddi_copyxxx() fails
28732  *		ENXIO if fail ddi_get_soft_state
28733  *		EINVAL if data pointer is NULL
28734  */
28735 
28736 static int
28737 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28738 {
28739 	struct sd_lun		*un;
28740 	struct cdrom_read	mode1_struct;
28741 	struct cdrom_read	*mode1 = &mode1_struct;
28742 	int			rval;
28743 #ifdef _MULTI_DATAMODEL
28744 	/* To support ILP32 applications in an LP64 world */
28745 	struct cdrom_read32	cdrom_read32;
28746 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28747 #endif /* _MULTI_DATAMODEL */
28748 
28749 	if (data == NULL) {
28750 		return (EINVAL);
28751 	}
28752 
28753 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28754 	    (un->un_state == SD_STATE_OFFLINE)) {
28755 		return (ENXIO);
28756 	}
28757 
28758 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28759 	    "sd_read_mode1: entry: un:0x%p\n", un);
28760 
28761 #ifdef _MULTI_DATAMODEL
28762 	switch (ddi_model_convert_from(flag & FMODELS)) {
28763 	case DDI_MODEL_ILP32:
28764 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28765 			return (EFAULT);
28766 		}
28767 		/* Convert the ILP32 uscsi data from the application to LP64 */
28768 		cdrom_read32tocdrom_read(cdrd32, mode1);
28769 		break;
28770 	case DDI_MODEL_NONE:
28771 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28772 			return (EFAULT);
28773 		}
28774 	}
28775 #else /* ! _MULTI_DATAMODEL */
28776 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28777 		return (EFAULT);
28778 	}
28779 #endif /* _MULTI_DATAMODEL */
28780 
28781 	rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr,
28782 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28783 
28784 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28785 	    "sd_read_mode1: exit: un:0x%p\n", un);
28786 
28787 	return (rval);
28788 }
28789 
28790 
28791 /*
28792  *    Function: sr_read_cd_mode2()
28793  *
28794  * Description: This routine is the driver entry point for handling CD-ROM
28795  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28796  *		support the READ CD (0xBE) command or the 1st generation
28797  *		READ CD (0xD4) command.
28798  *
28799  *   Arguments: dev	- the device 'dev_t'
28800  *		data	- pointer to user provided cd read structure specifying
28801  *			  the lba buffer address and length.
28802  *		flag	- this argument is a pass through to ddi_copyxxx()
28803  *			  directly from the mode argument of ioctl().
28804  *
28805  * Return Code: the code returned by sd_send_scsi_cmd()
28806  *		EFAULT if ddi_copyxxx() fails
28807  *		ENXIO if fail ddi_get_soft_state
28808  *		EINVAL if data pointer is NULL
28809  */
28810 
28811 static int
28812 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28813 {
28814 	struct sd_lun		*un;
28815 	struct uscsi_cmd	*com;
28816 	struct cdrom_read	mode2_struct;
28817 	struct cdrom_read	*mode2 = &mode2_struct;
28818 	uchar_t			cdb[CDB_GROUP5];
28819 	int			nblocks;
28820 	int			rval;
28821 #ifdef _MULTI_DATAMODEL
28822 	/*  To support ILP32 applications in an LP64 world */
28823 	struct cdrom_read32	cdrom_read32;
28824 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28825 #endif /* _MULTI_DATAMODEL */
28826 
28827 	if (data == NULL) {
28828 		return (EINVAL);
28829 	}
28830 
28831 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28832 	    (un->un_state == SD_STATE_OFFLINE)) {
28833 		return (ENXIO);
28834 	}
28835 
28836 #ifdef _MULTI_DATAMODEL
28837 	switch (ddi_model_convert_from(flag & FMODELS)) {
28838 	case DDI_MODEL_ILP32:
28839 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28840 			return (EFAULT);
28841 		}
28842 		/* Convert the ILP32 uscsi data from the application to LP64 */
28843 		cdrom_read32tocdrom_read(cdrd32, mode2);
28844 		break;
28845 	case DDI_MODEL_NONE:
28846 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28847 			return (EFAULT);
28848 		}
28849 		break;
28850 	}
28851 
28852 #else /* ! _MULTI_DATAMODEL */
28853 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28854 		return (EFAULT);
28855 	}
28856 #endif /* _MULTI_DATAMODEL */
28857 
28858 	bzero(cdb, sizeof (cdb));
28859 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28860 		/* Read command supported by 1st generation atapi drives */
28861 		cdb[0] = SCMD_READ_CDD4;
28862 	} else {
28863 		/* Universal CD Access Command */
28864 		cdb[0] = SCMD_READ_CD;
28865 	}
28866 
28867 	/*
28868 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28869 	 */
28870 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28871 
28872 	/* set the start address */
28873 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28874 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28875 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28876 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28877 
28878 	/* set the transfer length */
28879 	nblocks = mode2->cdread_buflen / 2336;
28880 	cdb[6] = (uchar_t)(nblocks >> 16);
28881 	cdb[7] = (uchar_t)(nblocks >> 8);
28882 	cdb[8] = (uchar_t)nblocks;
28883 
28884 	/* set the filter bits */
28885 	cdb[9] = CDROM_READ_CD_USERDATA;
28886 
28887 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28888 	com->uscsi_cdb = (caddr_t)cdb;
28889 	com->uscsi_cdblen = sizeof (cdb);
28890 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28891 	com->uscsi_buflen = mode2->cdread_buflen;
28892 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28893 
28894 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28895 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28896 	kmem_free(com, sizeof (*com));
28897 	return (rval);
28898 }
28899 
28900 
28901 /*
28902  *    Function: sr_read_mode2()
28903  *
28904  * Description: This routine is the driver entry point for handling CD-ROM
28905  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28906  *		do not support the READ CD (0xBE) command.
28907  *
28908  *   Arguments: dev	- the device 'dev_t'
28909  *		data	- pointer to user provided cd read structure specifying
28910  *			  the lba buffer address and length.
28911  *		flag	- this argument is a pass through to ddi_copyxxx()
28912  *			  directly from the mode argument of ioctl().
28913  *
28914  * Return Code: the code returned by sd_send_scsi_cmd()
28915  *		EFAULT if ddi_copyxxx() fails
28916  *		ENXIO if fail ddi_get_soft_state
28917  *		EINVAL if data pointer is NULL
28918  *		EIO if fail to reset block size
28919  *		EAGAIN if commands are in progress in the driver
28920  */
28921 
28922 static int
28923 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28924 {
28925 	struct sd_lun		*un;
28926 	struct cdrom_read	mode2_struct;
28927 	struct cdrom_read	*mode2 = &mode2_struct;
28928 	int			rval;
28929 	uint32_t		restore_blksize;
28930 	struct uscsi_cmd	*com;
28931 	uchar_t			cdb[CDB_GROUP0];
28932 	int			nblocks;
28933 
28934 #ifdef _MULTI_DATAMODEL
28935 	/* To support ILP32 applications in an LP64 world */
28936 	struct cdrom_read32	cdrom_read32;
28937 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28938 #endif /* _MULTI_DATAMODEL */
28939 
28940 	if (data == NULL) {
28941 		return (EINVAL);
28942 	}
28943 
28944 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28945 	    (un->un_state == SD_STATE_OFFLINE)) {
28946 		return (ENXIO);
28947 	}
28948 
28949 	/*
28950 	 * Because this routine will update the device and driver block size
28951 	 * being used we want to make sure there are no commands in progress.
28952 	 * If commands are in progress the user will have to try again.
28953 	 *
28954 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28955 	 * in sdioctl to protect commands from sdioctl through to the top of
28956 	 * sd_uscsi_strategy. See sdioctl for details.
28957 	 */
28958 	mutex_enter(SD_MUTEX(un));
28959 	if (un->un_ncmds_in_driver != 1) {
28960 		mutex_exit(SD_MUTEX(un));
28961 		return (EAGAIN);
28962 	}
28963 	mutex_exit(SD_MUTEX(un));
28964 
28965 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28966 	    "sd_read_mode2: entry: un:0x%p\n", un);
28967 
28968 #ifdef _MULTI_DATAMODEL
28969 	switch (ddi_model_convert_from(flag & FMODELS)) {
28970 	case DDI_MODEL_ILP32:
28971 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28972 			return (EFAULT);
28973 		}
28974 		/* Convert the ILP32 uscsi data from the application to LP64 */
28975 		cdrom_read32tocdrom_read(cdrd32, mode2);
28976 		break;
28977 	case DDI_MODEL_NONE:
28978 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28979 			return (EFAULT);
28980 		}
28981 		break;
28982 	}
28983 #else /* ! _MULTI_DATAMODEL */
28984 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28985 		return (EFAULT);
28986 	}
28987 #endif /* _MULTI_DATAMODEL */
28988 
28989 	/* Store the current target block size for restoration later */
28990 	restore_blksize = un->un_tgt_blocksize;
28991 
28992 	/* Change the device and soft state target block size to 2336 */
28993 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28994 		rval = EIO;
28995 		goto done;
28996 	}
28997 
28998 
28999 	bzero(cdb, sizeof (cdb));
29000 
29001 	/* set READ operation */
29002 	cdb[0] = SCMD_READ;
29003 
29004 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
29005 	mode2->cdread_lba >>= 2;
29006 
29007 	/* set the start address */
29008 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
29009 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
29010 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
29011 
29012 	/* set the transfer length */
29013 	nblocks = mode2->cdread_buflen / 2336;
29014 	cdb[4] = (uchar_t)nblocks & 0xFF;
29015 
29016 	/* build command */
29017 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29018 	com->uscsi_cdb = (caddr_t)cdb;
29019 	com->uscsi_cdblen = sizeof (cdb);
29020 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
29021 	com->uscsi_buflen = mode2->cdread_buflen;
29022 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29023 
29024 	/*
29025 	 * Issue SCSI command with user space address for read buffer.
29026 	 *
29027 	 * This sends the command through main channel in the driver.
29028 	 *
29029 	 * Since this is accessed via an IOCTL call, we go through the
29030 	 * standard path, so that if the device was powered down, then
29031 	 * it would be 'awakened' to handle the command.
29032 	 */
29033 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
29034 	    UIO_SYSSPACE, SD_PATH_STANDARD);
29035 
29036 	kmem_free(com, sizeof (*com));
29037 
29038 	/* Restore the device and soft state target block size */
29039 	if (sr_sector_mode(dev, restore_blksize) != 0) {
29040 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29041 		    "can't do switch back to mode 1\n");
29042 		/*
29043 		 * If sd_send_scsi_READ succeeded we still need to report
29044 		 * an error because we failed to reset the block size
29045 		 */
29046 		if (rval == 0) {
29047 			rval = EIO;
29048 		}
29049 	}
29050 
29051 done:
29052 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
29053 	    "sd_read_mode2: exit: un:0x%p\n", un);
29054 
29055 	return (rval);
29056 }
29057 
29058 
29059 /*
29060  *    Function: sr_sector_mode()
29061  *
29062  * Description: This utility function is used by sr_read_mode2 to set the target
29063  *		block size based on the user specified size. This is a legacy
29064  *		implementation based upon a vendor specific mode page
29065  *
29066  *   Arguments: dev	- the device 'dev_t'
29067  *		data	- flag indicating if block size is being set to 2336 or
29068  *			  512.
29069  *
29070  * Return Code: the code returned by sd_send_scsi_cmd()
29071  *		EFAULT if ddi_copyxxx() fails
29072  *		ENXIO if fail ddi_get_soft_state
29073  *		EINVAL if data pointer is NULL
29074  */
29075 
29076 static int
29077 sr_sector_mode(dev_t dev, uint32_t blksize)
29078 {
29079 	struct sd_lun	*un;
29080 	uchar_t		*sense;
29081 	uchar_t		*select;
29082 	int		rval;
29083 
29084 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29085 	    (un->un_state == SD_STATE_OFFLINE)) {
29086 		return (ENXIO);
29087 	}
29088 
29089 	sense = kmem_zalloc(20, KM_SLEEP);
29090 
29091 	/* Note: This is a vendor specific mode page (0x81) */
29092 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81,
29093 	    SD_PATH_STANDARD)) != 0) {
29094 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29095 		    "sr_sector_mode: Mode Sense failed\n");
29096 		kmem_free(sense, 20);
29097 		return (rval);
29098 	}
29099 	select = kmem_zalloc(20, KM_SLEEP);
29100 	select[3] = 0x08;
29101 	select[10] = ((blksize >> 8) & 0xff);
29102 	select[11] = (blksize & 0xff);
29103 	select[12] = 0x01;
29104 	select[13] = 0x06;
29105 	select[14] = sense[14];
29106 	select[15] = sense[15];
29107 	if (blksize == SD_MODE2_BLKSIZE) {
29108 		select[14] |= 0x01;
29109 	}
29110 
29111 	if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20,
29112 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
29113 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29114 		    "sr_sector_mode: Mode Select failed\n");
29115 	} else {
29116 		/*
29117 		 * Only update the softstate block size if we successfully
29118 		 * changed the device block mode.
29119 		 */
29120 		mutex_enter(SD_MUTEX(un));
29121 		sd_update_block_info(un, blksize, 0);
29122 		mutex_exit(SD_MUTEX(un));
29123 	}
29124 	kmem_free(sense, 20);
29125 	kmem_free(select, 20);
29126 	return (rval);
29127 }
29128 
29129 
29130 /*
29131  *    Function: sr_read_cdda()
29132  *
29133  * Description: This routine is the driver entry point for handling CD-ROM
29134  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
29135  *		the target supports CDDA these requests are handled via a vendor
29136  *		specific command (0xD8) If the target does not support CDDA
29137  *		these requests are handled via the READ CD command (0xBE).
29138  *
29139  *   Arguments: dev	- the device 'dev_t'
29140  *		data	- pointer to user provided CD-DA structure specifying
29141  *			  the track starting address, transfer length, and
29142  *			  subcode options.
29143  *		flag	- this argument is a pass through to ddi_copyxxx()
29144  *			  directly from the mode argument of ioctl().
29145  *
29146  * Return Code: the code returned by sd_send_scsi_cmd()
29147  *		EFAULT if ddi_copyxxx() fails
29148  *		ENXIO if fail ddi_get_soft_state
29149  *		EINVAL if invalid arguments are provided
29150  *		ENOTTY
29151  */
29152 
29153 static int
29154 sr_read_cdda(dev_t dev, caddr_t data, int flag)
29155 {
29156 	struct sd_lun			*un;
29157 	struct uscsi_cmd		*com;
29158 	struct cdrom_cdda		*cdda;
29159 	int				rval;
29160 	size_t				buflen;
29161 	char				cdb[CDB_GROUP5];
29162 
29163 #ifdef _MULTI_DATAMODEL
29164 	/* To support ILP32 applications in an LP64 world */
29165 	struct cdrom_cdda32	cdrom_cdda32;
29166 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
29167 #endif /* _MULTI_DATAMODEL */
29168 
29169 	if (data == NULL) {
29170 		return (EINVAL);
29171 	}
29172 
29173 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
29174 		return (ENXIO);
29175 	}
29176 
29177 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
29178 
29179 #ifdef _MULTI_DATAMODEL
29180 	switch (ddi_model_convert_from(flag & FMODELS)) {
29181 	case DDI_MODEL_ILP32:
29182 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
29183 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29184 			    "sr_read_cdda: ddi_copyin Failed\n");
29185 			kmem_free(cdda, sizeof (struct cdrom_cdda));
29186 			return (EFAULT);
29187 		}
29188 		/* Convert the ILP32 uscsi data from the application to LP64 */
29189 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
29190 		break;
29191 	case DDI_MODEL_NONE:
29192 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
29193 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29194 			    "sr_read_cdda: ddi_copyin Failed\n");
29195 			kmem_free(cdda, sizeof (struct cdrom_cdda));
29196 			return (EFAULT);
29197 		}
29198 		break;
29199 	}
29200 #else /* ! _MULTI_DATAMODEL */
29201 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
29202 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29203 		    "sr_read_cdda: ddi_copyin Failed\n");
29204 		kmem_free(cdda, sizeof (struct cdrom_cdda));
29205 		return (EFAULT);
29206 	}
29207 #endif /* _MULTI_DATAMODEL */
29208 
29209 	/*
29210 	 * Since MMC-2 expects max 3 bytes for length, check if the
29211 	 * length input is greater than 3 bytes
29212 	 */
29213 	if ((cdda->cdda_length & 0xFF000000) != 0) {
29214 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
29215 		    "cdrom transfer length too large: %d (limit %d)\n",
29216 		    cdda->cdda_length, 0xFFFFFF);
29217 		kmem_free(cdda, sizeof (struct cdrom_cdda));
29218 		return (EINVAL);
29219 	}
29220 
29221 	switch (cdda->cdda_subcode) {
29222 	case CDROM_DA_NO_SUBCODE:
29223 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
29224 		break;
29225 	case CDROM_DA_SUBQ:
29226 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
29227 		break;
29228 	case CDROM_DA_ALL_SUBCODE:
29229 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
29230 		break;
29231 	case CDROM_DA_SUBCODE_ONLY:
29232 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
29233 		break;
29234 	default:
29235 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29236 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
29237 		    cdda->cdda_subcode);
29238 		kmem_free(cdda, sizeof (struct cdrom_cdda));
29239 		return (EINVAL);
29240 	}
29241 
29242 	/* Build and send the command */
29243 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29244 	bzero(cdb, CDB_GROUP5);
29245 
29246 	if (un->un_f_cfg_cdda == TRUE) {
29247 		cdb[0] = (char)SCMD_READ_CD;
29248 		cdb[1] = 0x04;
29249 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
29250 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
29251 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
29252 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
29253 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
29254 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
29255 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
29256 		cdb[9] = 0x10;
29257 		switch (cdda->cdda_subcode) {
29258 		case CDROM_DA_NO_SUBCODE :
29259 			cdb[10] = 0x0;
29260 			break;
29261 		case CDROM_DA_SUBQ :
29262 			cdb[10] = 0x2;
29263 			break;
29264 		case CDROM_DA_ALL_SUBCODE :
29265 			cdb[10] = 0x1;
29266 			break;
29267 		case CDROM_DA_SUBCODE_ONLY :
29268 			/* FALLTHROUGH */
29269 		default :
29270 			kmem_free(cdda, sizeof (struct cdrom_cdda));
29271 			kmem_free(com, sizeof (*com));
29272 			return (ENOTTY);
29273 		}
29274 	} else {
29275 		cdb[0] = (char)SCMD_READ_CDDA;
29276 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
29277 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
29278 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
29279 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
29280 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
29281 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
29282 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
29283 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
29284 		cdb[10] = cdda->cdda_subcode;
29285 	}
29286 
29287 	com->uscsi_cdb = cdb;
29288 	com->uscsi_cdblen = CDB_GROUP5;
29289 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
29290 	com->uscsi_buflen = buflen;
29291 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29292 
29293 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
29294 	    UIO_SYSSPACE, SD_PATH_STANDARD);
29295 
29296 	kmem_free(cdda, sizeof (struct cdrom_cdda));
29297 	kmem_free(com, sizeof (*com));
29298 	return (rval);
29299 }
29300 
29301 
29302 /*
29303  *    Function: sr_read_cdxa()
29304  *
29305  * Description: This routine is the driver entry point for handling CD-ROM
29306  *		ioctl requests to return CD-XA (Extended Architecture) data.
29307  *		(CDROMCDXA).
29308  *
29309  *   Arguments: dev	- the device 'dev_t'
29310  *		data	- pointer to user provided CD-XA structure specifying
29311  *			  the data starting address, transfer length, and format
29312  *		flag	- this argument is a pass through to ddi_copyxxx()
29313  *			  directly from the mode argument of ioctl().
29314  *
29315  * Return Code: the code returned by sd_send_scsi_cmd()
29316  *		EFAULT if ddi_copyxxx() fails
29317  *		ENXIO if fail ddi_get_soft_state
29318  *		EINVAL if data pointer is NULL
29319  */
29320 
29321 static int
29322 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
29323 {
29324 	struct sd_lun		*un;
29325 	struct uscsi_cmd	*com;
29326 	struct cdrom_cdxa	*cdxa;
29327 	int			rval;
29328 	size_t			buflen;
29329 	char			cdb[CDB_GROUP5];
29330 	uchar_t			read_flags;
29331 
29332 #ifdef _MULTI_DATAMODEL
29333 	/* To support ILP32 applications in an LP64 world */
29334 	struct cdrom_cdxa32		cdrom_cdxa32;
29335 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
29336 #endif /* _MULTI_DATAMODEL */
29337 
29338 	if (data == NULL) {
29339 		return (EINVAL);
29340 	}
29341 
29342 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
29343 		return (ENXIO);
29344 	}
29345 
29346 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
29347 
29348 #ifdef _MULTI_DATAMODEL
29349 	switch (ddi_model_convert_from(flag & FMODELS)) {
29350 	case DDI_MODEL_ILP32:
29351 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
29352 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29353 			return (EFAULT);
29354 		}
29355 		/*
29356 		 * Convert the ILP32 uscsi data from the
29357 		 * application to LP64 for internal use.
29358 		 */
29359 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
29360 		break;
29361 	case DDI_MODEL_NONE:
29362 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
29363 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29364 			return (EFAULT);
29365 		}
29366 		break;
29367 	}
29368 #else /* ! _MULTI_DATAMODEL */
29369 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
29370 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29371 		return (EFAULT);
29372 	}
29373 #endif /* _MULTI_DATAMODEL */
29374 
29375 	/*
29376 	 * Since MMC-2 expects max 3 bytes for length, check if the
29377 	 * length input is greater than 3 bytes
29378 	 */
29379 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
29380 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
29381 		    "cdrom transfer length too large: %d (limit %d)\n",
29382 		    cdxa->cdxa_length, 0xFFFFFF);
29383 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29384 		return (EINVAL);
29385 	}
29386 
29387 	switch (cdxa->cdxa_format) {
29388 	case CDROM_XA_DATA:
29389 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
29390 		read_flags = 0x10;
29391 		break;
29392 	case CDROM_XA_SECTOR_DATA:
29393 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
29394 		read_flags = 0xf8;
29395 		break;
29396 	case CDROM_XA_DATA_W_ERROR:
29397 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
29398 		read_flags = 0xfc;
29399 		break;
29400 	default:
29401 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29402 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
29403 		    cdxa->cdxa_format);
29404 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29405 		return (EINVAL);
29406 	}
29407 
29408 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29409 	bzero(cdb, CDB_GROUP5);
29410 	if (un->un_f_mmc_cap == TRUE) {
29411 		cdb[0] = (char)SCMD_READ_CD;
29412 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
29413 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
29414 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
29415 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
29416 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
29417 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
29418 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
29419 		cdb[9] = (char)read_flags;
29420 	} else {
29421 		/*
29422 		 * Note: A vendor specific command (0xDB) is being used her to
29423 		 * request a read of all subcodes.
29424 		 */
29425 		cdb[0] = (char)SCMD_READ_CDXA;
29426 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
29427 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
29428 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
29429 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
29430 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
29431 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
29432 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
29433 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
29434 		cdb[10] = cdxa->cdxa_format;
29435 	}
29436 	com->uscsi_cdb	   = cdb;
29437 	com->uscsi_cdblen  = CDB_GROUP5;
29438 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
29439 	com->uscsi_buflen  = buflen;
29440 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29441 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
29442 	    UIO_SYSSPACE, SD_PATH_STANDARD);
29443 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29444 	kmem_free(com, sizeof (*com));
29445 	return (rval);
29446 }
29447 
29448 
29449 /*
29450  *    Function: sr_eject()
29451  *
29452  * Description: This routine is the driver entry point for handling CD-ROM
29453  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
29454  *
29455  *   Arguments: dev	- the device 'dev_t'
29456  *
29457  * Return Code: the code returned by sd_send_scsi_cmd()
29458  */
29459 
29460 static int
29461 sr_eject(dev_t dev)
29462 {
29463 	struct sd_lun	*un;
29464 	int		rval;
29465 
29466 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29467 	    (un->un_state == SD_STATE_OFFLINE)) {
29468 		return (ENXIO);
29469 	}
29470 	if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
29471 	    SD_PATH_STANDARD)) != 0) {
29472 		return (rval);
29473 	}
29474 
29475 	rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT,
29476 	    SD_PATH_STANDARD);
29477 
29478 	if (rval == 0) {
29479 		mutex_enter(SD_MUTEX(un));
29480 		sr_ejected(un);
29481 		un->un_mediastate = DKIO_EJECTED;
29482 		cv_broadcast(&un->un_state_cv);
29483 		mutex_exit(SD_MUTEX(un));
29484 	}
29485 	return (rval);
29486 }
29487 
29488 
29489 /*
29490  *    Function: sr_ejected()
29491  *
29492  * Description: This routine updates the soft state structure to invalidate the
29493  *		geometry information after the media has been ejected or a
29494  *		media eject has been detected.
29495  *
29496  *   Arguments: un - driver soft state (unit) structure
29497  */
29498 
29499 static void
29500 sr_ejected(struct sd_lun *un)
29501 {
29502 	struct sd_errstats *stp;
29503 
29504 	ASSERT(un != NULL);
29505 	ASSERT(mutex_owned(SD_MUTEX(un)));
29506 
29507 	un->un_f_blockcount_is_valid	= FALSE;
29508 	un->un_f_tgt_blocksize_is_valid	= FALSE;
29509 	un->un_f_geometry_is_valid	= FALSE;
29510 
29511 	if (un->un_errstats != NULL) {
29512 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
29513 		stp->sd_capacity.value.ui64 = 0;
29514 	}
29515 }
29516 
29517 
29518 /*
29519  *    Function: sr_check_wp()
29520  *
29521  * Description: This routine checks the write protection of a removable
29522  *      media disk and hotpluggable devices via the write protect bit of
29523  *      the Mode Page Header device specific field. Some devices choke
29524  *      on unsupported mode page. In order to workaround this issue,
29525  *      this routine has been implemented to use 0x3f mode page(request
29526  *      for all pages) for all device types.
29527  *
29528  *   Arguments: dev		- the device 'dev_t'
29529  *
29530  * Return Code: int indicating if the device is write protected (1) or not (0)
29531  *
29532  *     Context: Kernel thread.
29533  *
29534  */
29535 
29536 static int
29537 sr_check_wp(dev_t dev)
29538 {
29539 	struct sd_lun	*un;
29540 	uchar_t		device_specific;
29541 	uchar_t		*sense;
29542 	int		hdrlen;
29543 	int		rval = FALSE;
29544 
29545 	/*
29546 	 * Note: The return codes for this routine should be reworked to
29547 	 * properly handle the case of a NULL softstate.
29548 	 */
29549 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
29550 		return (FALSE);
29551 	}
29552 
29553 	if (un->un_f_cfg_is_atapi == TRUE) {
29554 		/*
29555 		 * The mode page contents are not required; set the allocation
29556 		 * length for the mode page header only
29557 		 */
29558 		hdrlen = MODE_HEADER_LENGTH_GRP2;
29559 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29560 		if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen,
29561 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0)
29562 			goto err_exit;
29563 		device_specific =
29564 		    ((struct mode_header_grp2 *)sense)->device_specific;
29565 	} else {
29566 		hdrlen = MODE_HEADER_LENGTH;
29567 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29568 		if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen,
29569 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0)
29570 			goto err_exit;
29571 		device_specific =
29572 		    ((struct mode_header *)sense)->device_specific;
29573 	}
29574 
29575 	/*
29576 	 * Write protect mode sense failed; not all disks
29577 	 * understand this query. Return FALSE assuming that
29578 	 * these devices are not writable.
29579 	 */
29580 	if (device_specific & WRITE_PROTECT) {
29581 		rval = TRUE;
29582 	}
29583 
29584 err_exit:
29585 	kmem_free(sense, hdrlen);
29586 	return (rval);
29587 }
29588 
29589 /*
29590  *    Function: sr_volume_ctrl()
29591  *
29592  * Description: This routine is the driver entry point for handling CD-ROM
29593  *		audio output volume ioctl requests. (CDROMVOLCTRL)
29594  *
29595  *   Arguments: dev	- the device 'dev_t'
29596  *		data	- pointer to user audio volume control structure
29597  *		flag	- this argument is a pass through to ddi_copyxxx()
29598  *			  directly from the mode argument of ioctl().
29599  *
29600  * Return Code: the code returned by sd_send_scsi_cmd()
29601  *		EFAULT if ddi_copyxxx() fails
29602  *		ENXIO if fail ddi_get_soft_state
29603  *		EINVAL if data pointer is NULL
29604  *
29605  */
29606 
29607 static int
29608 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
29609 {
29610 	struct sd_lun		*un;
29611 	struct cdrom_volctrl    volume;
29612 	struct cdrom_volctrl    *vol = &volume;
29613 	uchar_t			*sense_page;
29614 	uchar_t			*select_page;
29615 	uchar_t			*sense;
29616 	uchar_t			*select;
29617 	int			sense_buflen;
29618 	int			select_buflen;
29619 	int			rval;
29620 
29621 	if (data == NULL) {
29622 		return (EINVAL);
29623 	}
29624 
29625 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29626 	    (un->un_state == SD_STATE_OFFLINE)) {
29627 		return (ENXIO);
29628 	}
29629 
29630 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
29631 		return (EFAULT);
29632 	}
29633 
29634 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29635 		struct mode_header_grp2		*sense_mhp;
29636 		struct mode_header_grp2		*select_mhp;
29637 		int				bd_len;
29638 
29639 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29640 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
29641 		    MODEPAGE_AUDIO_CTRL_LEN;
29642 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29643 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29644 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
29645 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29646 		    SD_PATH_STANDARD)) != 0) {
29647 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29648 			    "sr_volume_ctrl: Mode Sense Failed\n");
29649 			kmem_free(sense, sense_buflen);
29650 			kmem_free(select, select_buflen);
29651 			return (rval);
29652 		}
29653 		sense_mhp = (struct mode_header_grp2 *)sense;
29654 		select_mhp = (struct mode_header_grp2 *)select;
29655 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
29656 		    sense_mhp->bdesc_length_lo;
29657 		if (bd_len > MODE_BLK_DESC_LENGTH) {
29658 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29659 			    "sr_volume_ctrl: Mode Sense returned invalid "
29660 			    "block descriptor length\n");
29661 			kmem_free(sense, sense_buflen);
29662 			kmem_free(select, select_buflen);
29663 			return (EIO);
29664 		}
29665 		sense_page = (uchar_t *)
29666 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29667 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29668 		select_mhp->length_msb = 0;
29669 		select_mhp->length_lsb = 0;
29670 		select_mhp->bdesc_length_hi = 0;
29671 		select_mhp->bdesc_length_lo = 0;
29672 	} else {
29673 		struct mode_header		*sense_mhp, *select_mhp;
29674 
29675 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29676 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29677 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29678 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29679 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
29680 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29681 		    SD_PATH_STANDARD)) != 0) {
29682 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29683 			    "sr_volume_ctrl: Mode Sense Failed\n");
29684 			kmem_free(sense, sense_buflen);
29685 			kmem_free(select, select_buflen);
29686 			return (rval);
29687 		}
29688 		sense_mhp  = (struct mode_header *)sense;
29689 		select_mhp = (struct mode_header *)select;
29690 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29691 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29692 			    "sr_volume_ctrl: Mode Sense returned invalid "
29693 			    "block descriptor length\n");
29694 			kmem_free(sense, sense_buflen);
29695 			kmem_free(select, select_buflen);
29696 			return (EIO);
29697 		}
29698 		sense_page = (uchar_t *)
29699 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29700 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29701 		select_mhp->length = 0;
29702 		select_mhp->bdesc_length = 0;
29703 	}
29704 	/*
29705 	 * Note: An audio control data structure could be created and overlayed
29706 	 * on the following in place of the array indexing method implemented.
29707 	 */
29708 
29709 	/* Build the select data for the user volume data */
29710 	select_page[0] = MODEPAGE_AUDIO_CTRL;
29711 	select_page[1] = 0xE;
29712 	/* Set the immediate bit */
29713 	select_page[2] = 0x04;
29714 	/* Zero out reserved fields */
29715 	select_page[3] = 0x00;
29716 	select_page[4] = 0x00;
29717 	/* Return sense data for fields not to be modified */
29718 	select_page[5] = sense_page[5];
29719 	select_page[6] = sense_page[6];
29720 	select_page[7] = sense_page[7];
29721 	/* Set the user specified volume levels for channel 0 and 1 */
29722 	select_page[8] = 0x01;
29723 	select_page[9] = vol->channel0;
29724 	select_page[10] = 0x02;
29725 	select_page[11] = vol->channel1;
29726 	/* Channel 2 and 3 are currently unsupported so return the sense data */
29727 	select_page[12] = sense_page[12];
29728 	select_page[13] = sense_page[13];
29729 	select_page[14] = sense_page[14];
29730 	select_page[15] = sense_page[15];
29731 
29732 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29733 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select,
29734 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29735 	} else {
29736 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
29737 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29738 	}
29739 
29740 	kmem_free(sense, sense_buflen);
29741 	kmem_free(select, select_buflen);
29742 	return (rval);
29743 }
29744 
29745 
29746 /*
29747  *    Function: sr_read_sony_session_offset()
29748  *
29749  * Description: This routine is the driver entry point for handling CD-ROM
29750  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29751  *		The address of the first track in the last session of a
29752  *		multi-session CD-ROM is returned
29753  *
29754  *		Note: This routine uses a vendor specific key value in the
29755  *		command control field without implementing any vendor check here
29756  *		or in the ioctl routine.
29757  *
29758  *   Arguments: dev	- the device 'dev_t'
29759  *		data	- pointer to an int to hold the requested address
29760  *		flag	- this argument is a pass through to ddi_copyxxx()
29761  *			  directly from the mode argument of ioctl().
29762  *
29763  * Return Code: the code returned by sd_send_scsi_cmd()
29764  *		EFAULT if ddi_copyxxx() fails
29765  *		ENXIO if fail ddi_get_soft_state
29766  *		EINVAL if data pointer is NULL
29767  */
29768 
29769 static int
29770 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29771 {
29772 	struct sd_lun		*un;
29773 	struct uscsi_cmd	*com;
29774 	caddr_t			buffer;
29775 	char			cdb[CDB_GROUP1];
29776 	int			session_offset = 0;
29777 	int			rval;
29778 
29779 	if (data == NULL) {
29780 		return (EINVAL);
29781 	}
29782 
29783 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29784 	    (un->un_state == SD_STATE_OFFLINE)) {
29785 		return (ENXIO);
29786 	}
29787 
29788 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29789 	bzero(cdb, CDB_GROUP1);
29790 	cdb[0] = SCMD_READ_TOC;
29791 	/*
29792 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29793 	 * (4 byte TOC response header + 8 byte response data)
29794 	 */
29795 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29796 	/* Byte 9 is the control byte. A vendor specific value is used */
29797 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29798 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29799 	com->uscsi_cdb = cdb;
29800 	com->uscsi_cdblen = CDB_GROUP1;
29801 	com->uscsi_bufaddr = buffer;
29802 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29803 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29804 
29805 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
29806 	    UIO_SYSSPACE, SD_PATH_STANDARD);
29807 	if (rval != 0) {
29808 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29809 		kmem_free(com, sizeof (*com));
29810 		return (rval);
29811 	}
29812 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29813 		session_offset =
29814 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29815 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29816 		/*
29817 		 * Offset returned offset in current lbasize block's. Convert to
29818 		 * 2k block's to return to the user
29819 		 */
29820 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29821 			session_offset >>= 2;
29822 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29823 			session_offset >>= 1;
29824 		}
29825 	}
29826 
29827 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29828 		rval = EFAULT;
29829 	}
29830 
29831 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29832 	kmem_free(com, sizeof (*com));
29833 	return (rval);
29834 }
29835 
29836 
29837 /*
29838  *    Function: sd_wm_cache_constructor()
29839  *
29840  * Description: Cache Constructor for the wmap cache for the read/modify/write
29841  * 		devices.
29842  *
29843  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29844  *		un	- sd_lun structure for the device.
29845  *		flag	- the km flags passed to constructor
29846  *
29847  * Return Code: 0 on success.
29848  *		-1 on failure.
29849  */
29850 
29851 /*ARGSUSED*/
29852 static int
29853 sd_wm_cache_constructor(void *wm, void *un, int flags)
29854 {
29855 	bzero(wm, sizeof (struct sd_w_map));
29856 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29857 	return (0);
29858 }
29859 
29860 
29861 /*
29862  *    Function: sd_wm_cache_destructor()
29863  *
29864  * Description: Cache destructor for the wmap cache for the read/modify/write
29865  * 		devices.
29866  *
29867  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29868  *		un	- sd_lun structure for the device.
29869  */
29870 /*ARGSUSED*/
29871 static void
29872 sd_wm_cache_destructor(void *wm, void *un)
29873 {
29874 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29875 }
29876 
29877 
29878 /*
29879  *    Function: sd_range_lock()
29880  *
29881  * Description: Lock the range of blocks specified as parameter to ensure
29882  *		that read, modify write is atomic and no other i/o writes
29883  *		to the same location. The range is specified in terms
29884  *		of start and end blocks. Block numbers are the actual
29885  *		media block numbers and not system.
29886  *
29887  *   Arguments: un	- sd_lun structure for the device.
29888  *		startb - The starting block number
29889  *		endb - The end block number
29890  *		typ - type of i/o - simple/read_modify_write
29891  *
29892  * Return Code: wm  - pointer to the wmap structure.
29893  *
29894  *     Context: This routine can sleep.
29895  */
29896 
29897 static struct sd_w_map *
29898 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29899 {
29900 	struct sd_w_map *wmp = NULL;
29901 	struct sd_w_map *sl_wmp = NULL;
29902 	struct sd_w_map *tmp_wmp;
29903 	wm_state state = SD_WM_CHK_LIST;
29904 
29905 
29906 	ASSERT(un != NULL);
29907 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29908 
29909 	mutex_enter(SD_MUTEX(un));
29910 
29911 	while (state != SD_WM_DONE) {
29912 
29913 		switch (state) {
29914 		case SD_WM_CHK_LIST:
29915 			/*
29916 			 * This is the starting state. Check the wmap list
29917 			 * to see if the range is currently available.
29918 			 */
29919 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29920 				/*
29921 				 * If this is a simple write and no rmw
29922 				 * i/o is pending then try to lock the
29923 				 * range as the range should be available.
29924 				 */
29925 				state = SD_WM_LOCK_RANGE;
29926 			} else {
29927 				tmp_wmp = sd_get_range(un, startb, endb);
29928 				if (tmp_wmp != NULL) {
29929 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29930 						/*
29931 						 * Should not keep onlist wmps
29932 						 * while waiting this macro
29933 						 * will also do wmp = NULL;
29934 						 */
29935 						FREE_ONLIST_WMAP(un, wmp);
29936 					}
29937 					/*
29938 					 * sl_wmp is the wmap on which wait
29939 					 * is done, since the tmp_wmp points
29940 					 * to the inuse wmap, set sl_wmp to
29941 					 * tmp_wmp and change the state to sleep
29942 					 */
29943 					sl_wmp = tmp_wmp;
29944 					state = SD_WM_WAIT_MAP;
29945 				} else {
29946 					state = SD_WM_LOCK_RANGE;
29947 				}
29948 
29949 			}
29950 			break;
29951 
29952 		case SD_WM_LOCK_RANGE:
29953 			ASSERT(un->un_wm_cache);
29954 			/*
29955 			 * The range need to be locked, try to get a wmap.
29956 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29957 			 * if possible as we will have to release the sd mutex
29958 			 * if we have to sleep.
29959 			 */
29960 			if (wmp == NULL)
29961 				wmp = kmem_cache_alloc(un->un_wm_cache,
29962 				    KM_NOSLEEP);
29963 			if (wmp == NULL) {
29964 				mutex_exit(SD_MUTEX(un));
29965 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29966 				    (sd_lun::un_wm_cache))
29967 				wmp = kmem_cache_alloc(un->un_wm_cache,
29968 				    KM_SLEEP);
29969 				mutex_enter(SD_MUTEX(un));
29970 				/*
29971 				 * we released the mutex so recheck and go to
29972 				 * check list state.
29973 				 */
29974 				state = SD_WM_CHK_LIST;
29975 			} else {
29976 				/*
29977 				 * We exit out of state machine since we
29978 				 * have the wmap. Do the housekeeping first.
29979 				 * place the wmap on the wmap list if it is not
29980 				 * on it already and then set the state to done.
29981 				 */
29982 				wmp->wm_start = startb;
29983 				wmp->wm_end = endb;
29984 				wmp->wm_flags = typ | SD_WM_BUSY;
29985 				if (typ & SD_WTYPE_RMW) {
29986 					un->un_rmw_count++;
29987 				}
29988 				/*
29989 				 * If not already on the list then link
29990 				 */
29991 				if (!ONLIST(un, wmp)) {
29992 					wmp->wm_next = un->un_wm;
29993 					wmp->wm_prev = NULL;
29994 					if (wmp->wm_next)
29995 						wmp->wm_next->wm_prev = wmp;
29996 					un->un_wm = wmp;
29997 				}
29998 				state = SD_WM_DONE;
29999 			}
30000 			break;
30001 
30002 		case SD_WM_WAIT_MAP:
30003 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
30004 			/*
30005 			 * Wait is done on sl_wmp, which is set in the
30006 			 * check_list state.
30007 			 */
30008 			sl_wmp->wm_wanted_count++;
30009 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
30010 			sl_wmp->wm_wanted_count--;
30011 			/*
30012 			 * We can reuse the memory from the completed sl_wmp
30013 			 * lock range for our new lock, but only if noone is
30014 			 * waiting for it.
30015 			 */
30016 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
30017 			if (sl_wmp->wm_wanted_count == 0) {
30018 				if (wmp != NULL)
30019 					CHK_N_FREEWMP(un, wmp);
30020 				wmp = sl_wmp;
30021 			}
30022 			sl_wmp = NULL;
30023 			/*
30024 			 * After waking up, need to recheck for availability of
30025 			 * range.
30026 			 */
30027 			state = SD_WM_CHK_LIST;
30028 			break;
30029 
30030 		default:
30031 			panic("sd_range_lock: "
30032 			    "Unknown state %d in sd_range_lock", state);
30033 			/*NOTREACHED*/
30034 		} /* switch(state) */
30035 
30036 	} /* while(state != SD_WM_DONE) */
30037 
30038 	mutex_exit(SD_MUTEX(un));
30039 
30040 	ASSERT(wmp != NULL);
30041 
30042 	return (wmp);
30043 }
30044 
30045 
30046 /*
30047  *    Function: sd_get_range()
30048  *
30049  * Description: Find if there any overlapping I/O to this one
30050  *		Returns the write-map of 1st such I/O, NULL otherwise.
30051  *
30052  *   Arguments: un	- sd_lun structure for the device.
30053  *		startb - The starting block number
30054  *		endb - The end block number
30055  *
30056  * Return Code: wm  - pointer to the wmap structure.
30057  */
30058 
30059 static struct sd_w_map *
30060 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
30061 {
30062 	struct sd_w_map *wmp;
30063 
30064 	ASSERT(un != NULL);
30065 
30066 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
30067 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
30068 			continue;
30069 		}
30070 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
30071 			break;
30072 		}
30073 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
30074 			break;
30075 		}
30076 	}
30077 
30078 	return (wmp);
30079 }
30080 
30081 
30082 /*
30083  *    Function: sd_free_inlist_wmap()
30084  *
30085  * Description: Unlink and free a write map struct.
30086  *
30087  *   Arguments: un      - sd_lun structure for the device.
30088  *		wmp	- sd_w_map which needs to be unlinked.
30089  */
30090 
30091 static void
30092 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
30093 {
30094 	ASSERT(un != NULL);
30095 
30096 	if (un->un_wm == wmp) {
30097 		un->un_wm = wmp->wm_next;
30098 	} else {
30099 		wmp->wm_prev->wm_next = wmp->wm_next;
30100 	}
30101 
30102 	if (wmp->wm_next) {
30103 		wmp->wm_next->wm_prev = wmp->wm_prev;
30104 	}
30105 
30106 	wmp->wm_next = wmp->wm_prev = NULL;
30107 
30108 	kmem_cache_free(un->un_wm_cache, wmp);
30109 }
30110 
30111 
30112 /*
30113  *    Function: sd_range_unlock()
30114  *
30115  * Description: Unlock the range locked by wm.
30116  *		Free write map if nobody else is waiting on it.
30117  *
30118  *   Arguments: un      - sd_lun structure for the device.
30119  *              wmp     - sd_w_map which needs to be unlinked.
30120  */
30121 
30122 static void
30123 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
30124 {
30125 	ASSERT(un != NULL);
30126 	ASSERT(wm != NULL);
30127 	ASSERT(!mutex_owned(SD_MUTEX(un)));
30128 
30129 	mutex_enter(SD_MUTEX(un));
30130 
30131 	if (wm->wm_flags & SD_WTYPE_RMW) {
30132 		un->un_rmw_count--;
30133 	}
30134 
30135 	if (wm->wm_wanted_count) {
30136 		wm->wm_flags = 0;
30137 		/*
30138 		 * Broadcast that the wmap is available now.
30139 		 */
30140 		cv_broadcast(&wm->wm_avail);
30141 	} else {
30142 		/*
30143 		 * If no one is waiting on the map, it should be free'ed.
30144 		 */
30145 		sd_free_inlist_wmap(un, wm);
30146 	}
30147 
30148 	mutex_exit(SD_MUTEX(un));
30149 }
30150 
30151 
30152 /*
30153  *    Function: sd_read_modify_write_task
30154  *
30155  * Description: Called from a taskq thread to initiate the write phase of
30156  *		a read-modify-write request.  This is used for targets where
30157  *		un->un_sys_blocksize != un->un_tgt_blocksize.
30158  *
30159  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
30160  *
30161  *     Context: Called under taskq thread context.
30162  */
30163 
30164 static void
30165 sd_read_modify_write_task(void *arg)
30166 {
30167 	struct sd_mapblocksize_info	*bsp;
30168 	struct buf	*bp;
30169 	struct sd_xbuf	*xp;
30170 	struct sd_lun	*un;
30171 
30172 	bp = arg;	/* The bp is given in arg */
30173 	ASSERT(bp != NULL);
30174 
30175 	/* Get the pointer to the layer-private data struct */
30176 	xp = SD_GET_XBUF(bp);
30177 	ASSERT(xp != NULL);
30178 	bsp = xp->xb_private;
30179 	ASSERT(bsp != NULL);
30180 
30181 	un = SD_GET_UN(bp);
30182 	ASSERT(un != NULL);
30183 	ASSERT(!mutex_owned(SD_MUTEX(un)));
30184 
30185 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
30186 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
30187 
30188 	/*
30189 	 * This is the write phase of a read-modify-write request, called
30190 	 * under the context of a taskq thread in response to the completion
30191 	 * of the read portion of the rmw request completing under interrupt
30192 	 * context. The write request must be sent from here down the iostart
30193 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
30194 	 * we use the layer index saved in the layer-private data area.
30195 	 */
30196 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
30197 
30198 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
30199 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
30200 }
30201 
30202 
30203 /*
30204  *    Function: sddump_do_read_of_rmw()
30205  *
30206  * Description: This routine will be called from sddump, If sddump is called
30207  *		with an I/O which not aligned on device blocksize boundary
30208  *		then the write has to be converted to read-modify-write.
30209  *		Do the read part here in order to keep sddump simple.
30210  *		Note - That the sd_mutex is held across the call to this
30211  *		routine.
30212  *
30213  *   Arguments: un	- sd_lun
30214  *		blkno	- block number in terms of media block size.
30215  *		nblk	- number of blocks.
30216  *		bpp	- pointer to pointer to the buf structure. On return
30217  *			from this function, *bpp points to the valid buffer
30218  *			to which the write has to be done.
30219  *
30220  * Return Code: 0 for success or errno-type return code
30221  */
30222 
30223 static int
30224 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
30225 	struct buf **bpp)
30226 {
30227 	int err;
30228 	int i;
30229 	int rval;
30230 	struct buf *bp;
30231 	struct scsi_pkt *pkt = NULL;
30232 	uint32_t target_blocksize;
30233 
30234 	ASSERT(un != NULL);
30235 	ASSERT(mutex_owned(SD_MUTEX(un)));
30236 
30237 	target_blocksize = un->un_tgt_blocksize;
30238 
30239 	mutex_exit(SD_MUTEX(un));
30240 
30241 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
30242 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
30243 	if (bp == NULL) {
30244 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
30245 		    "no resources for dumping; giving up");
30246 		err = ENOMEM;
30247 		goto done;
30248 	}
30249 
30250 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
30251 	    blkno, nblk);
30252 	if (rval != 0) {
30253 		scsi_free_consistent_buf(bp);
30254 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
30255 		    "no resources for dumping; giving up");
30256 		err = ENOMEM;
30257 		goto done;
30258 	}
30259 
30260 	pkt->pkt_flags |= FLAG_NOINTR;
30261 
30262 	err = EIO;
30263 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
30264 
30265 		/*
30266 		 * Scsi_poll returns 0 (success) if the command completes and
30267 		 * the status block is STATUS_GOOD.  We should only check
30268 		 * errors if this condition is not true.  Even then we should
30269 		 * send our own request sense packet only if we have a check
30270 		 * condition and auto request sense has not been performed by
30271 		 * the hba.
30272 		 */
30273 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
30274 
30275 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
30276 			err = 0;
30277 			break;
30278 		}
30279 
30280 		/*
30281 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
30282 		 * no need to read RQS data.
30283 		 */
30284 		if (pkt->pkt_reason == CMD_DEV_GONE) {
30285 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
30286 			    "Device is gone\n");
30287 			break;
30288 		}
30289 
30290 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
30291 			SD_INFO(SD_LOG_DUMP, un,
30292 			    "sddump: read failed with CHECK, try # %d\n", i);
30293 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
30294 				(void) sd_send_polled_RQS(un);
30295 			}
30296 
30297 			continue;
30298 		}
30299 
30300 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
30301 			int reset_retval = 0;
30302 
30303 			SD_INFO(SD_LOG_DUMP, un,
30304 			    "sddump: read failed with BUSY, try # %d\n", i);
30305 
30306 			if (un->un_f_lun_reset_enabled == TRUE) {
30307 				reset_retval = scsi_reset(SD_ADDRESS(un),
30308 				    RESET_LUN);
30309 			}
30310 			if (reset_retval == 0) {
30311 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
30312 			}
30313 			(void) sd_send_polled_RQS(un);
30314 
30315 		} else {
30316 			SD_INFO(SD_LOG_DUMP, un,
30317 			    "sddump: read failed with 0x%x, try # %d\n",
30318 			    SD_GET_PKT_STATUS(pkt), i);
30319 			mutex_enter(SD_MUTEX(un));
30320 			sd_reset_target(un, pkt);
30321 			mutex_exit(SD_MUTEX(un));
30322 		}
30323 
30324 		/*
30325 		 * If we are not getting anywhere with lun/target resets,
30326 		 * let's reset the bus.
30327 		 */
30328 		if (i > SD_NDUMP_RETRIES/2) {
30329 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
30330 			(void) sd_send_polled_RQS(un);
30331 		}
30332 
30333 	}
30334 	scsi_destroy_pkt(pkt);
30335 
30336 	if (err != 0) {
30337 		scsi_free_consistent_buf(bp);
30338 		*bpp = NULL;
30339 	} else {
30340 		*bpp = bp;
30341 	}
30342 
30343 done:
30344 	mutex_enter(SD_MUTEX(un));
30345 	return (err);
30346 }
30347 
30348 
30349 /*
30350  *    Function: sd_failfast_flushq
30351  *
30352  * Description: Take all bp's on the wait queue that have B_FAILFAST set
30353  *		in b_flags and move them onto the failfast queue, then kick
30354  *		off a thread to return all bp's on the failfast queue to
30355  *		their owners with an error set.
30356  *
30357  *   Arguments: un - pointer to the soft state struct for the instance.
30358  *
30359  *     Context: may execute in interrupt context.
30360  */
30361 
30362 static void
30363 sd_failfast_flushq(struct sd_lun *un)
30364 {
30365 	struct buf *bp;
30366 	struct buf *next_waitq_bp;
30367 	struct buf *prev_waitq_bp = NULL;
30368 
30369 	ASSERT(un != NULL);
30370 	ASSERT(mutex_owned(SD_MUTEX(un)));
30371 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
30372 	ASSERT(un->un_failfast_bp == NULL);
30373 
30374 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
30375 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
30376 
30377 	/*
30378 	 * Check if we should flush all bufs when entering failfast state, or
30379 	 * just those with B_FAILFAST set.
30380 	 */
30381 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
30382 		/*
30383 		 * Move *all* bp's on the wait queue to the failfast flush
30384 		 * queue, including those that do NOT have B_FAILFAST set.
30385 		 */
30386 		if (un->un_failfast_headp == NULL) {
30387 			ASSERT(un->un_failfast_tailp == NULL);
30388 			un->un_failfast_headp = un->un_waitq_headp;
30389 		} else {
30390 			ASSERT(un->un_failfast_tailp != NULL);
30391 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
30392 		}
30393 
30394 		un->un_failfast_tailp = un->un_waitq_tailp;
30395 
30396 		/* update kstat for each bp moved out of the waitq */
30397 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
30398 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
30399 		}
30400 
30401 		/* empty the waitq */
30402 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
30403 
30404 	} else {
30405 		/*
30406 		 * Go thru the wait queue, pick off all entries with
30407 		 * B_FAILFAST set, and move these onto the failfast queue.
30408 		 */
30409 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
30410 			/*
30411 			 * Save the pointer to the next bp on the wait queue,
30412 			 * so we get to it on the next iteration of this loop.
30413 			 */
30414 			next_waitq_bp = bp->av_forw;
30415 
30416 			/*
30417 			 * If this bp from the wait queue does NOT have
30418 			 * B_FAILFAST set, just move on to the next element
30419 			 * in the wait queue. Note, this is the only place
30420 			 * where it is correct to set prev_waitq_bp.
30421 			 */
30422 			if ((bp->b_flags & B_FAILFAST) == 0) {
30423 				prev_waitq_bp = bp;
30424 				continue;
30425 			}
30426 
30427 			/*
30428 			 * Remove the bp from the wait queue.
30429 			 */
30430 			if (bp == un->un_waitq_headp) {
30431 				/* The bp is the first element of the waitq. */
30432 				un->un_waitq_headp = next_waitq_bp;
30433 				if (un->un_waitq_headp == NULL) {
30434 					/* The wait queue is now empty */
30435 					un->un_waitq_tailp = NULL;
30436 				}
30437 			} else {
30438 				/*
30439 				 * The bp is either somewhere in the middle
30440 				 * or at the end of the wait queue.
30441 				 */
30442 				ASSERT(un->un_waitq_headp != NULL);
30443 				ASSERT(prev_waitq_bp != NULL);
30444 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
30445 				    == 0);
30446 				if (bp == un->un_waitq_tailp) {
30447 					/* bp is the last entry on the waitq. */
30448 					ASSERT(next_waitq_bp == NULL);
30449 					un->un_waitq_tailp = prev_waitq_bp;
30450 				}
30451 				prev_waitq_bp->av_forw = next_waitq_bp;
30452 			}
30453 			bp->av_forw = NULL;
30454 
30455 			/*
30456 			 * update kstat since the bp is moved out of
30457 			 * the waitq
30458 			 */
30459 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
30460 
30461 			/*
30462 			 * Now put the bp onto the failfast queue.
30463 			 */
30464 			if (un->un_failfast_headp == NULL) {
30465 				/* failfast queue is currently empty */
30466 				ASSERT(un->un_failfast_tailp == NULL);
30467 				un->un_failfast_headp =
30468 				    un->un_failfast_tailp = bp;
30469 			} else {
30470 				/* Add the bp to the end of the failfast q */
30471 				ASSERT(un->un_failfast_tailp != NULL);
30472 				ASSERT(un->un_failfast_tailp->b_flags &
30473 				    B_FAILFAST);
30474 				un->un_failfast_tailp->av_forw = bp;
30475 				un->un_failfast_tailp = bp;
30476 			}
30477 		}
30478 	}
30479 
30480 	/*
30481 	 * Now return all bp's on the failfast queue to their owners.
30482 	 */
30483 	while ((bp = un->un_failfast_headp) != NULL) {
30484 
30485 		un->un_failfast_headp = bp->av_forw;
30486 		if (un->un_failfast_headp == NULL) {
30487 			un->un_failfast_tailp = NULL;
30488 		}
30489 
30490 		/*
30491 		 * We want to return the bp with a failure error code, but
30492 		 * we do not want a call to sd_start_cmds() to occur here,
30493 		 * so use sd_return_failed_command_no_restart() instead of
30494 		 * sd_return_failed_command().
30495 		 */
30496 		sd_return_failed_command_no_restart(un, bp, EIO);
30497 	}
30498 
30499 	/* Flush the xbuf queues if required. */
30500 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
30501 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
30502 	}
30503 
30504 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
30505 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
30506 }
30507 
30508 
30509 /*
30510  *    Function: sd_failfast_flushq_callback
30511  *
30512  * Description: Return TRUE if the given bp meets the criteria for failfast
30513  *		flushing. Used with ddi_xbuf_flushq(9F).
30514  *
30515  *   Arguments: bp - ptr to buf struct to be examined.
30516  *
30517  *     Context: Any
30518  */
30519 
30520 static int
30521 sd_failfast_flushq_callback(struct buf *bp)
30522 {
30523 	/*
30524 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
30525 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
30526 	 */
30527 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
30528 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
30529 }
30530 
30531 
30532 
30533 #if defined(__i386) || defined(__amd64)
30534 /*
30535  * Function: sd_setup_next_xfer
30536  *
30537  * Description: Prepare next I/O operation using DMA_PARTIAL
30538  *
30539  */
30540 
30541 static int
30542 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
30543     struct scsi_pkt *pkt, struct sd_xbuf *xp)
30544 {
30545 	ssize_t	num_blks_not_xfered;
30546 	daddr_t	strt_blk_num;
30547 	ssize_t	bytes_not_xfered;
30548 	int	rval;
30549 
30550 	ASSERT(pkt->pkt_resid == 0);
30551 
30552 	/*
30553 	 * Calculate next block number and amount to be transferred.
30554 	 *
30555 	 * How much data NOT transfered to the HBA yet.
30556 	 */
30557 	bytes_not_xfered = xp->xb_dma_resid;
30558 
30559 	/*
30560 	 * figure how many blocks NOT transfered to the HBA yet.
30561 	 */
30562 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
30563 
30564 	/*
30565 	 * set starting block number to the end of what WAS transfered.
30566 	 */
30567 	strt_blk_num = xp->xb_blkno +
30568 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
30569 
30570 	/*
30571 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
30572 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
30573 	 * the disk mutex here.
30574 	 */
30575 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
30576 	    strt_blk_num, num_blks_not_xfered);
30577 
30578 	if (rval == 0) {
30579 
30580 		/*
30581 		 * Success.
30582 		 *
30583 		 * Adjust things if there are still more blocks to be
30584 		 * transfered.
30585 		 */
30586 		xp->xb_dma_resid = pkt->pkt_resid;
30587 		pkt->pkt_resid = 0;
30588 
30589 		return (1);
30590 	}
30591 
30592 	/*
30593 	 * There's really only one possible return value from
30594 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
30595 	 * returns NULL.
30596 	 */
30597 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
30598 
30599 	bp->b_resid = bp->b_bcount;
30600 	bp->b_flags |= B_ERROR;
30601 
30602 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30603 	    "Error setting up next portion of DMA transfer\n");
30604 
30605 	return (0);
30606 }
30607 #endif
30608 
30609 /*
30610  *    Function: sd_panic_for_res_conflict
30611  *
30612  * Description: Call panic with a string formated with "Reservation Conflict"
30613  *		and a human readable identifier indicating the SD instance
30614  *		that experienced the reservation conflict.
30615  *
30616  *   Arguments: un - pointer to the soft state struct for the instance.
30617  *
30618  *     Context: may execute in interrupt context.
30619  */
30620 
30621 #define	SD_RESV_CONFLICT_FMT_LEN 40
30622 void
30623 sd_panic_for_res_conflict(struct sd_lun *un)
30624 {
30625 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30626 	char path_str[MAXPATHLEN];
30627 
30628 	(void) snprintf(panic_str, sizeof (panic_str),
30629 	    "Reservation Conflict\nDisk: %s",
30630 	    ddi_pathname(SD_DEVINFO(un), path_str));
30631 
30632 	panic(panic_str);
30633 }
30634 
30635 /*
30636  * Note: The following sd_faultinjection_ioctl( ) routines implement
30637  * driver support for handling fault injection for error analysis
30638  * causing faults in multiple layers of the driver.
30639  *
30640  */
30641 
30642 #ifdef SD_FAULT_INJECTION
30643 static uint_t   sd_fault_injection_on = 0;
30644 
30645 /*
30646  *    Function: sd_faultinjection_ioctl()
30647  *
30648  * Description: This routine is the driver entry point for handling
30649  *              faultinjection ioctls to inject errors into the
30650  *              layer model
30651  *
30652  *   Arguments: cmd	- the ioctl cmd recieved
30653  *		arg	- the arguments from user and returns
30654  */
30655 
30656 static void
30657 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
30658 
30659 	uint_t i;
30660 	uint_t rval;
30661 
30662 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30663 
30664 	mutex_enter(SD_MUTEX(un));
30665 
30666 	switch (cmd) {
30667 	case SDIOCRUN:
30668 		/* Allow pushed faults to be injected */
30669 		SD_INFO(SD_LOG_SDTEST, un,
30670 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
30671 
30672 		sd_fault_injection_on = 1;
30673 
30674 		SD_INFO(SD_LOG_IOERR, un,
30675 		    "sd_faultinjection_ioctl: run finished\n");
30676 		break;
30677 
30678 	case SDIOCSTART:
30679 		/* Start Injection Session */
30680 		SD_INFO(SD_LOG_SDTEST, un,
30681 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
30682 
30683 		sd_fault_injection_on = 0;
30684 		un->sd_injection_mask = 0xFFFFFFFF;
30685 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30686 			un->sd_fi_fifo_pkt[i] = NULL;
30687 			un->sd_fi_fifo_xb[i] = NULL;
30688 			un->sd_fi_fifo_un[i] = NULL;
30689 			un->sd_fi_fifo_arq[i] = NULL;
30690 		}
30691 		un->sd_fi_fifo_start = 0;
30692 		un->sd_fi_fifo_end = 0;
30693 
30694 		mutex_enter(&(un->un_fi_mutex));
30695 		un->sd_fi_log[0] = '\0';
30696 		un->sd_fi_buf_len = 0;
30697 		mutex_exit(&(un->un_fi_mutex));
30698 
30699 		SD_INFO(SD_LOG_IOERR, un,
30700 		    "sd_faultinjection_ioctl: start finished\n");
30701 		break;
30702 
30703 	case SDIOCSTOP:
30704 		/* Stop Injection Session */
30705 		SD_INFO(SD_LOG_SDTEST, un,
30706 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30707 		sd_fault_injection_on = 0;
30708 		un->sd_injection_mask = 0x0;
30709 
30710 		/* Empty stray or unuseds structs from fifo */
30711 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30712 			if (un->sd_fi_fifo_pkt[i] != NULL) {
30713 				kmem_free(un->sd_fi_fifo_pkt[i],
30714 				    sizeof (struct sd_fi_pkt));
30715 			}
30716 			if (un->sd_fi_fifo_xb[i] != NULL) {
30717 				kmem_free(un->sd_fi_fifo_xb[i],
30718 				    sizeof (struct sd_fi_xb));
30719 			}
30720 			if (un->sd_fi_fifo_un[i] != NULL) {
30721 				kmem_free(un->sd_fi_fifo_un[i],
30722 				    sizeof (struct sd_fi_un));
30723 			}
30724 			if (un->sd_fi_fifo_arq[i] != NULL) {
30725 				kmem_free(un->sd_fi_fifo_arq[i],
30726 				    sizeof (struct sd_fi_arq));
30727 			}
30728 			un->sd_fi_fifo_pkt[i] = NULL;
30729 			un->sd_fi_fifo_un[i] = NULL;
30730 			un->sd_fi_fifo_xb[i] = NULL;
30731 			un->sd_fi_fifo_arq[i] = NULL;
30732 		}
30733 		un->sd_fi_fifo_start = 0;
30734 		un->sd_fi_fifo_end = 0;
30735 
30736 		SD_INFO(SD_LOG_IOERR, un,
30737 		    "sd_faultinjection_ioctl: stop finished\n");
30738 		break;
30739 
30740 	case SDIOCINSERTPKT:
30741 		/* Store a packet struct to be pushed onto fifo */
30742 		SD_INFO(SD_LOG_SDTEST, un,
30743 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30744 
30745 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30746 
30747 		sd_fault_injection_on = 0;
30748 
30749 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30750 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30751 			kmem_free(un->sd_fi_fifo_pkt[i],
30752 			    sizeof (struct sd_fi_pkt));
30753 		}
30754 		if (arg != NULL) {
30755 			un->sd_fi_fifo_pkt[i] =
30756 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30757 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30758 				/* Alloc failed don't store anything */
30759 				break;
30760 			}
30761 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30762 			    sizeof (struct sd_fi_pkt), 0);
30763 			if (rval == -1) {
30764 				kmem_free(un->sd_fi_fifo_pkt[i],
30765 				    sizeof (struct sd_fi_pkt));
30766 				un->sd_fi_fifo_pkt[i] = NULL;
30767 			}
30768 		} else {
30769 			SD_INFO(SD_LOG_IOERR, un,
30770 			    "sd_faultinjection_ioctl: pkt null\n");
30771 		}
30772 		break;
30773 
30774 	case SDIOCINSERTXB:
30775 		/* Store a xb struct to be pushed onto fifo */
30776 		SD_INFO(SD_LOG_SDTEST, un,
30777 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30778 
30779 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30780 
30781 		sd_fault_injection_on = 0;
30782 
30783 		if (un->sd_fi_fifo_xb[i] != NULL) {
30784 			kmem_free(un->sd_fi_fifo_xb[i],
30785 			    sizeof (struct sd_fi_xb));
30786 			un->sd_fi_fifo_xb[i] = NULL;
30787 		}
30788 		if (arg != NULL) {
30789 			un->sd_fi_fifo_xb[i] =
30790 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30791 			if (un->sd_fi_fifo_xb[i] == NULL) {
30792 				/* Alloc failed don't store anything */
30793 				break;
30794 			}
30795 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30796 			    sizeof (struct sd_fi_xb), 0);
30797 
30798 			if (rval == -1) {
30799 				kmem_free(un->sd_fi_fifo_xb[i],
30800 				    sizeof (struct sd_fi_xb));
30801 				un->sd_fi_fifo_xb[i] = NULL;
30802 			}
30803 		} else {
30804 			SD_INFO(SD_LOG_IOERR, un,
30805 			    "sd_faultinjection_ioctl: xb null\n");
30806 		}
30807 		break;
30808 
30809 	case SDIOCINSERTUN:
30810 		/* Store a un struct to be pushed onto fifo */
30811 		SD_INFO(SD_LOG_SDTEST, un,
30812 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30813 
30814 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30815 
30816 		sd_fault_injection_on = 0;
30817 
30818 		if (un->sd_fi_fifo_un[i] != NULL) {
30819 			kmem_free(un->sd_fi_fifo_un[i],
30820 			    sizeof (struct sd_fi_un));
30821 			un->sd_fi_fifo_un[i] = NULL;
30822 		}
30823 		if (arg != NULL) {
30824 			un->sd_fi_fifo_un[i] =
30825 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30826 			if (un->sd_fi_fifo_un[i] == NULL) {
30827 				/* Alloc failed don't store anything */
30828 				break;
30829 			}
30830 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30831 			    sizeof (struct sd_fi_un), 0);
30832 			if (rval == -1) {
30833 				kmem_free(un->sd_fi_fifo_un[i],
30834 				    sizeof (struct sd_fi_un));
30835 				un->sd_fi_fifo_un[i] = NULL;
30836 			}
30837 
30838 		} else {
30839 			SD_INFO(SD_LOG_IOERR, un,
30840 			    "sd_faultinjection_ioctl: un null\n");
30841 		}
30842 
30843 		break;
30844 
30845 	case SDIOCINSERTARQ:
30846 		/* Store a arq struct to be pushed onto fifo */
30847 		SD_INFO(SD_LOG_SDTEST, un,
30848 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30849 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30850 
30851 		sd_fault_injection_on = 0;
30852 
30853 		if (un->sd_fi_fifo_arq[i] != NULL) {
30854 			kmem_free(un->sd_fi_fifo_arq[i],
30855 			    sizeof (struct sd_fi_arq));
30856 			un->sd_fi_fifo_arq[i] = NULL;
30857 		}
30858 		if (arg != NULL) {
30859 			un->sd_fi_fifo_arq[i] =
30860 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30861 			if (un->sd_fi_fifo_arq[i] == NULL) {
30862 				/* Alloc failed don't store anything */
30863 				break;
30864 			}
30865 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30866 			    sizeof (struct sd_fi_arq), 0);
30867 			if (rval == -1) {
30868 				kmem_free(un->sd_fi_fifo_arq[i],
30869 				    sizeof (struct sd_fi_arq));
30870 				un->sd_fi_fifo_arq[i] = NULL;
30871 			}
30872 
30873 		} else {
30874 			SD_INFO(SD_LOG_IOERR, un,
30875 			    "sd_faultinjection_ioctl: arq null\n");
30876 		}
30877 
30878 		break;
30879 
30880 	case SDIOCPUSH:
30881 		/* Push stored xb, pkt, un, and arq onto fifo */
30882 		sd_fault_injection_on = 0;
30883 
30884 		if (arg != NULL) {
30885 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30886 			if (rval != -1 &&
30887 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30888 				un->sd_fi_fifo_end += i;
30889 			}
30890 		} else {
30891 			SD_INFO(SD_LOG_IOERR, un,
30892 			    "sd_faultinjection_ioctl: push arg null\n");
30893 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30894 				un->sd_fi_fifo_end++;
30895 			}
30896 		}
30897 		SD_INFO(SD_LOG_IOERR, un,
30898 		    "sd_faultinjection_ioctl: push to end=%d\n",
30899 		    un->sd_fi_fifo_end);
30900 		break;
30901 
30902 	case SDIOCRETRIEVE:
30903 		/* Return buffer of log from Injection session */
30904 		SD_INFO(SD_LOG_SDTEST, un,
30905 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30906 
30907 		sd_fault_injection_on = 0;
30908 
30909 		mutex_enter(&(un->un_fi_mutex));
30910 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30911 		    un->sd_fi_buf_len+1, 0);
30912 		mutex_exit(&(un->un_fi_mutex));
30913 
30914 		if (rval == -1) {
30915 			/*
30916 			 * arg is possibly invalid setting
30917 			 * it to NULL for return
30918 			 */
30919 			arg = NULL;
30920 		}
30921 		break;
30922 	}
30923 
30924 	mutex_exit(SD_MUTEX(un));
30925 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30926 			    " exit\n");
30927 }
30928 
30929 
30930 /*
30931  *    Function: sd_injection_log()
30932  *
30933  * Description: This routine adds buff to the already existing injection log
30934  *              for retrieval via faultinjection_ioctl for use in fault
30935  *              detection and recovery
30936  *
30937  *   Arguments: buf - the string to add to the log
30938  */
30939 
30940 static void
30941 sd_injection_log(char *buf, struct sd_lun *un)
30942 {
30943 	uint_t len;
30944 
30945 	ASSERT(un != NULL);
30946 	ASSERT(buf != NULL);
30947 
30948 	mutex_enter(&(un->un_fi_mutex));
30949 
30950 	len = min(strlen(buf), 255);
30951 	/* Add logged value to Injection log to be returned later */
30952 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30953 		uint_t	offset = strlen((char *)un->sd_fi_log);
30954 		char *destp = (char *)un->sd_fi_log + offset;
30955 		int i;
30956 		for (i = 0; i < len; i++) {
30957 			*destp++ = *buf++;
30958 		}
30959 		un->sd_fi_buf_len += len;
30960 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30961 	}
30962 
30963 	mutex_exit(&(un->un_fi_mutex));
30964 }
30965 
30966 
30967 /*
30968  *    Function: sd_faultinjection()
30969  *
30970  * Description: This routine takes the pkt and changes its
30971  *		content based on error injection scenerio.
30972  *
30973  *   Arguments: pktp	- packet to be changed
30974  */
30975 
30976 static void
30977 sd_faultinjection(struct scsi_pkt *pktp)
30978 {
30979 	uint_t i;
30980 	struct sd_fi_pkt *fi_pkt;
30981 	struct sd_fi_xb *fi_xb;
30982 	struct sd_fi_un *fi_un;
30983 	struct sd_fi_arq *fi_arq;
30984 	struct buf *bp;
30985 	struct sd_xbuf *xb;
30986 	struct sd_lun *un;
30987 
30988 	ASSERT(pktp != NULL);
30989 
30990 	/* pull bp xb and un from pktp */
30991 	bp = (struct buf *)pktp->pkt_private;
30992 	xb = SD_GET_XBUF(bp);
30993 	un = SD_GET_UN(bp);
30994 
30995 	ASSERT(un != NULL);
30996 
30997 	mutex_enter(SD_MUTEX(un));
30998 
30999 	SD_TRACE(SD_LOG_SDTEST, un,
31000 	    "sd_faultinjection: entry Injection from sdintr\n");
31001 
31002 	/* if injection is off return */
31003 	if (sd_fault_injection_on == 0 ||
31004 		un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
31005 		mutex_exit(SD_MUTEX(un));
31006 		return;
31007 	}
31008 
31009 
31010 	/* take next set off fifo */
31011 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
31012 
31013 	fi_pkt = un->sd_fi_fifo_pkt[i];
31014 	fi_xb = un->sd_fi_fifo_xb[i];
31015 	fi_un = un->sd_fi_fifo_un[i];
31016 	fi_arq = un->sd_fi_fifo_arq[i];
31017 
31018 
31019 	/* set variables accordingly */
31020 	/* set pkt if it was on fifo */
31021 	if (fi_pkt != NULL) {
31022 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
31023 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
31024 		SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
31025 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
31026 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
31027 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
31028 
31029 	}
31030 
31031 	/* set xb if it was on fifo */
31032 	if (fi_xb != NULL) {
31033 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
31034 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
31035 		SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
31036 		SD_CONDSET(xb, xb, xb_victim_retry_count,
31037 		    "xb_victim_retry_count");
31038 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
31039 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
31040 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
31041 
31042 		/* copy in block data from sense */
31043 		if (fi_xb->xb_sense_data[0] != -1) {
31044 			bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
31045 			    SENSE_LENGTH);
31046 		}
31047 
31048 		/* copy in extended sense codes */
31049 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code,
31050 		    "es_code");
31051 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key,
31052 		    "es_key");
31053 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code,
31054 		    "es_add_code");
31055 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb,
31056 		    es_qual_code, "es_qual_code");
31057 	}
31058 
31059 	/* set un if it was on fifo */
31060 	if (fi_un != NULL) {
31061 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
31062 		SD_CONDSET(un, un, un_ctype, "un_ctype");
31063 		SD_CONDSET(un, un, un_reset_retry_count,
31064 		    "un_reset_retry_count");
31065 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
31066 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
31067 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
31068 		SD_CONDSET(un, un, un_f_geometry_is_valid,
31069 		    "un_f_geometry_is_valid");
31070 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
31071 		    "un_f_allow_bus_device_reset");
31072 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
31073 
31074 	}
31075 
31076 	/* copy in auto request sense if it was on fifo */
31077 	if (fi_arq != NULL) {
31078 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
31079 	}
31080 
31081 	/* free structs */
31082 	if (un->sd_fi_fifo_pkt[i] != NULL) {
31083 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
31084 	}
31085 	if (un->sd_fi_fifo_xb[i] != NULL) {
31086 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
31087 	}
31088 	if (un->sd_fi_fifo_un[i] != NULL) {
31089 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
31090 	}
31091 	if (un->sd_fi_fifo_arq[i] != NULL) {
31092 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
31093 	}
31094 
31095 	/*
31096 	 * kmem_free does not gurantee to set to NULL
31097 	 * since we uses these to determine if we set
31098 	 * values or not lets confirm they are always
31099 	 * NULL after free
31100 	 */
31101 	un->sd_fi_fifo_pkt[i] = NULL;
31102 	un->sd_fi_fifo_un[i] = NULL;
31103 	un->sd_fi_fifo_xb[i] = NULL;
31104 	un->sd_fi_fifo_arq[i] = NULL;
31105 
31106 	un->sd_fi_fifo_start++;
31107 
31108 	mutex_exit(SD_MUTEX(un));
31109 
31110 	SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
31111 }
31112 
31113 #endif /* SD_FAULT_INJECTION */
31114 
31115 /*
31116  * This routine is invoked in sd_unit_attach(). Before calling it, the
31117  * properties in conf file should be processed already, and "hotpluggable"
31118  * property was processed also.
31119  *
31120  * The sd driver distinguishes 3 different type of devices: removable media,
31121  * non-removable media, and hotpluggable. Below the differences are defined:
31122  *
31123  * 1. Device ID
31124  *
31125  *     The device ID of a device is used to identify this device. Refer to
31126  *     ddi_devid_register(9F).
31127  *
31128  *     For a non-removable media disk device which can provide 0x80 or 0x83
31129  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
31130  *     device ID is created to identify this device. For other non-removable
31131  *     media devices, a default device ID is created only if this device has
31132  *     at least 2 alter cylinders. Otherwise, this device has no devid.
31133  *
31134  *     -------------------------------------------------------
31135  *     removable media   hotpluggable  | Can Have Device ID
31136  *     -------------------------------------------------------
31137  *         false             false     |     Yes
31138  *         false             true      |     Yes
31139  *         true                x       |     No
31140  *     ------------------------------------------------------
31141  *
31142  *
31143  * 2. SCSI group 4 commands
31144  *
31145  *     In SCSI specs, only some commands in group 4 command set can use
31146  *     8-byte addresses that can be used to access >2TB storage spaces.
31147  *     Other commands have no such capability. Without supporting group4,
31148  *     it is impossible to make full use of storage spaces of a disk with
31149  *     capacity larger than 2TB.
31150  *
31151  *     -----------------------------------------------
31152  *     removable media   hotpluggable   LP64  |  Group
31153  *     -----------------------------------------------
31154  *           false          false       false |   1
31155  *           false          false       true  |   4
31156  *           false          true        false |   1
31157  *           false          true        true  |   4
31158  *           true             x           x   |   5
31159  *     -----------------------------------------------
31160  *
31161  *
31162  * 3. Check for VTOC Label
31163  *
31164  *     If a direct-access disk has no EFI label, sd will check if it has a
31165  *     valid VTOC label. Now, sd also does that check for removable media
31166  *     and hotpluggable devices.
31167  *
31168  *     --------------------------------------------------------------
31169  *     Direct-Access   removable media    hotpluggable |  Check Label
31170  *     -------------------------------------------------------------
31171  *         false          false           false        |   No
31172  *         false          false           true         |   No
31173  *         false          true            false        |   Yes
31174  *         false          true            true         |   Yes
31175  *         true            x                x          |   Yes
31176  *     --------------------------------------------------------------
31177  *
31178  *
31179  * 4. Building default VTOC label
31180  *
31181  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
31182  *     If those devices have no valid VTOC label, sd(7d) will attempt to
31183  *     create default VTOC for them. Currently sd creates default VTOC label
31184  *     for all devices on x86 platform (VTOC_16), but only for removable
31185  *     media devices on SPARC (VTOC_8).
31186  *
31187  *     -----------------------------------------------------------
31188  *       removable media hotpluggable platform   |   Default Label
31189  *     -----------------------------------------------------------
31190  *             false          false    sparc     |     No
31191  *             false          true      x86      |     Yes
31192  *             false          true     sparc     |     Yes
31193  *             true             x        x       |     Yes
31194  *     ----------------------------------------------------------
31195  *
31196  *
31197  * 5. Supported blocksizes of target devices
31198  *
31199  *     Sd supports non-512-byte blocksize for removable media devices only.
31200  *     For other devices, only 512-byte blocksize is supported. This may be
31201  *     changed in near future because some RAID devices require non-512-byte
31202  *     blocksize
31203  *
31204  *     -----------------------------------------------------------
31205  *     removable media    hotpluggable    | non-512-byte blocksize
31206  *     -----------------------------------------------------------
31207  *           false          false         |   No
31208  *           false          true          |   No
31209  *           true             x           |   Yes
31210  *     -----------------------------------------------------------
31211  *
31212  *
31213  * 6. Automatic mount & unmount (i.e. vold)
31214  *
31215  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
31216  *     if a device is removable media device. It return 1 for removable media
31217  *     devices, and 0 for others.
31218  *
31219  *     Vold treats a device as removable one only if DKIOREMOVABLE returns 1.
31220  *     And it does automounting only for removable media devices. In order to
31221  *     preserve users' experience and let vold continue to do automounting for
31222  *     USB disk devices, DKIOCREMOVABLE ioctl still returns 1 for USB/1394 disk
31223  *     devices.
31224  *
31225  *      ------------------------------------------------------
31226  *       removable media    hotpluggable   |  automatic mount
31227  *      ------------------------------------------------------
31228  *             false          false        |   No
31229  *             false          true         |   Yes
31230  *             true             x          |   Yes
31231  *      ------------------------------------------------------
31232  *
31233  *
31234  * 7. fdisk partition management
31235  *
31236  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
31237  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
31238  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
31239  *     fdisk partitions on both x86 and SPARC platform.
31240  *
31241  *     -----------------------------------------------------------
31242  *       platform   removable media  USB/1394  |  fdisk supported
31243  *     -----------------------------------------------------------
31244  *        x86         X               X        |       true
31245  *     ------------------------------------------------------------
31246  *        sparc       X               X        |       false
31247  *     ------------------------------------------------------------
31248  *
31249  *
31250  * 8. MBOOT/MBR
31251  *
31252  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
31253  *     read/write mboot for removable media devices on sparc platform.
31254  *
31255  *     -----------------------------------------------------------
31256  *       platform   removable media  USB/1394  |  mboot supported
31257  *     -----------------------------------------------------------
31258  *        x86         X               X        |       true
31259  *     ------------------------------------------------------------
31260  *        sparc      false           false     |       false
31261  *        sparc      false           true      |       true
31262  *        sparc      true            false     |       true
31263  *        sparc      true            true      |       true
31264  *     ------------------------------------------------------------
31265  *
31266  *
31267  * 9.  error handling during opening device
31268  *
31269  *     If failed to open a disk device, an errno is returned. For some kinds
31270  *     of errors, different errno is returned depending on if this device is
31271  *     a removable media device. This brings USB/1394 hard disks in line with
31272  *     expected hard disk behavior. It is not expected that this breaks any
31273  *     application.
31274  *
31275  *     ------------------------------------------------------
31276  *       removable media    hotpluggable   |  errno
31277  *     ------------------------------------------------------
31278  *             false          false        |   EIO
31279  *             false          true         |   EIO
31280  *             true             x          |   ENXIO
31281  *     ------------------------------------------------------
31282  *
31283  *
31284  * 11. ioctls: DKIOCEJECT, CDROMEJECT
31285  *
31286  *     These IOCTLs are applicable only to removable media devices.
31287  *
31288  *     -----------------------------------------------------------
31289  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
31290  *     -----------------------------------------------------------
31291  *             false          false        |     No
31292  *             false          true         |     No
31293  *             true            x           |     Yes
31294  *     -----------------------------------------------------------
31295  *
31296  *
31297  * 12. Kstats for partitions
31298  *
31299  *     sd creates partition kstat for non-removable media devices. USB and
31300  *     Firewire hard disks now have partition kstats
31301  *
31302  *      ------------------------------------------------------
31303  *       removable media    hotplugable    |   kstat
31304  *      ------------------------------------------------------
31305  *             false          false        |    Yes
31306  *             false          true         |    Yes
31307  *             true             x          |    No
31308  *       ------------------------------------------------------
31309  *
31310  *
31311  * 13. Removable media & hotpluggable properties
31312  *
31313  *     Sd driver creates a "removable-media" property for removable media
31314  *     devices. Parent nexus drivers create a "hotpluggable" property if
31315  *     it supports hotplugging.
31316  *
31317  *     ---------------------------------------------------------------------
31318  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
31319  *     ---------------------------------------------------------------------
31320  *       false            false       |    No                   No
31321  *       false            true        |    No                   Yes
31322  *       true             false       |    Yes                  No
31323  *       true             true        |    Yes                  Yes
31324  *     ---------------------------------------------------------------------
31325  *
31326  *
31327  * 14. Power Management
31328  *
31329  *     sd only power manages removable media devices or devices that support
31330  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
31331  *
31332  *     A parent nexus that supports hotplugging can also set "pm-capable"
31333  *     if the disk can be power managed.
31334  *
31335  *     ------------------------------------------------------------
31336  *       removable media hotpluggable pm-capable  |   power manage
31337  *     ------------------------------------------------------------
31338  *             false          false     false     |     No
31339  *             false          false     true      |     Yes
31340  *             false          true      false     |     No
31341  *             false          true      true      |     Yes
31342  *             true             x        x        |     Yes
31343  *     ------------------------------------------------------------
31344  *
31345  *      USB and firewire hard disks can now be power managed independently
31346  *      of the framebuffer
31347  *
31348  *
31349  * 15. Support for USB disks with capacity larger than 1TB
31350  *
31351  *     Currently, sd doesn't permit a fixed disk device with capacity
31352  *     larger than 1TB to be used in a 32-bit operating system environment.
31353  *     However, sd doesn't do that for removable media devices. Instead, it
31354  *     assumes that removable media devices cannot have a capacity larger
31355  *     than 1TB. Therefore, using those devices on 32-bit system is partially
31356  *     supported, which can cause some unexpected results.
31357  *
31358  *     ---------------------------------------------------------------------
31359  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
31360  *     ---------------------------------------------------------------------
31361  *             false          false  |   true         |     no
31362  *             false          true   |   true         |     no
31363  *             true           false  |   true         |     Yes
31364  *             true           true   |   true         |     Yes
31365  *     ---------------------------------------------------------------------
31366  *
31367  *
31368  * 16. Check write-protection at open time
31369  *
31370  *     When a removable media device is being opened for writing without NDELAY
31371  *     flag, sd will check if this device is writable. If attempting to open
31372  *     without NDELAY flag a write-protected device, this operation will abort.
31373  *
31374  *     ------------------------------------------------------------
31375  *       removable media    USB/1394   |   WP Check
31376  *     ------------------------------------------------------------
31377  *             false          false    |     No
31378  *             false          true     |     No
31379  *             true           false    |     Yes
31380  *             true           true     |     Yes
31381  *     ------------------------------------------------------------
31382  *
31383  *
31384  * 17. syslog when corrupted VTOC is encountered
31385  *
31386  *      Currently, if an invalid VTOC is encountered, sd only print syslog
31387  *      for fixed SCSI disks.
31388  *     ------------------------------------------------------------
31389  *       removable media    USB/1394   |   print syslog
31390  *     ------------------------------------------------------------
31391  *             false          false    |     Yes
31392  *             false          true     |     No
31393  *             true           false    |     No
31394  *             true           true     |     No
31395  *     ------------------------------------------------------------
31396  */
31397 static void
31398 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
31399 {
31400 	int	pm_capable_prop;
31401 
31402 	ASSERT(un->un_sd);
31403 	ASSERT(un->un_sd->sd_inq);
31404 
31405 #if defined(_SUNOS_VTOC_16)
31406 	/*
31407 	 * For VTOC_16 devices, the default label will be created for all
31408 	 * devices. (see sd_build_default_label)
31409 	 */
31410 	un->un_f_default_vtoc_supported = TRUE;
31411 #endif
31412 
31413 	if (un->un_sd->sd_inq->inq_rmb) {
31414 		/*
31415 		 * The media of this device is removable. And for this kind
31416 		 * of devices, it is possible to change medium after openning
31417 		 * devices. Thus we should support this operation.
31418 		 */
31419 		un->un_f_has_removable_media = TRUE;
31420 
31421 #if defined(_SUNOS_VTOC_8)
31422 		/*
31423 		 * Note: currently, for VTOC_8 devices, default label is
31424 		 * created for removable and hotpluggable devices only.
31425 		 */
31426 		un->un_f_default_vtoc_supported = TRUE;
31427 #endif
31428 		/*
31429 		 * support non-512-byte blocksize of removable media devices
31430 		 */
31431 		un->un_f_non_devbsize_supported = TRUE;
31432 
31433 		/*
31434 		 * Assume that all removable media devices support DOOR_LOCK
31435 		 */
31436 		un->un_f_doorlock_supported = TRUE;
31437 
31438 		/*
31439 		 * For a removable media device, it is possible to be opened
31440 		 * with NDELAY flag when there is no media in drive, in this
31441 		 * case we don't care if device is writable. But if without
31442 		 * NDELAY flag, we need to check if media is write-protected.
31443 		 */
31444 		un->un_f_chk_wp_open = TRUE;
31445 
31446 		/*
31447 		 * need to start a SCSI watch thread to monitor media state,
31448 		 * when media is being inserted or ejected, notify syseventd.
31449 		 */
31450 		un->un_f_monitor_media_state = TRUE;
31451 
31452 		/*
31453 		 * Some devices don't support START_STOP_UNIT command.
31454 		 * Therefore, we'd better check if a device supports it
31455 		 * before sending it.
31456 		 */
31457 		un->un_f_check_start_stop = TRUE;
31458 
31459 		/*
31460 		 * support eject media ioctl:
31461 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
31462 		 */
31463 		un->un_f_eject_media_supported = TRUE;
31464 
31465 		/*
31466 		 * Because many removable-media devices don't support
31467 		 * LOG_SENSE, we couldn't use this command to check if
31468 		 * a removable media device support power-management.
31469 		 * We assume that they support power-management via
31470 		 * START_STOP_UNIT command and can be spun up and down
31471 		 * without limitations.
31472 		 */
31473 		un->un_f_pm_supported = TRUE;
31474 
31475 		/*
31476 		 * Need to create a zero length (Boolean) property
31477 		 * removable-media for the removable media devices.
31478 		 * Note that the return value of the property is not being
31479 		 * checked, since if unable to create the property
31480 		 * then do not want the attach to fail altogether. Consistent
31481 		 * with other property creation in attach.
31482 		 */
31483 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
31484 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
31485 
31486 	} else {
31487 		/*
31488 		 * create device ID for device
31489 		 */
31490 		un->un_f_devid_supported = TRUE;
31491 
31492 		/*
31493 		 * Spin up non-removable-media devices once it is attached
31494 		 */
31495 		un->un_f_attach_spinup = TRUE;
31496 
31497 		/*
31498 		 * According to SCSI specification, Sense data has two kinds of
31499 		 * format: fixed format, and descriptor format. At present, we
31500 		 * don't support descriptor format sense data for removable
31501 		 * media.
31502 		 */
31503 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
31504 			un->un_f_descr_format_supported = TRUE;
31505 		}
31506 
31507 		/*
31508 		 * kstats are created only for non-removable media devices.
31509 		 *
31510 		 * Set this in sd.conf to 0 in order to disable kstats.  The
31511 		 * default is 1, so they are enabled by default.
31512 		 */
31513 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
31514 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
31515 			"enable-partition-kstats", 1));
31516 
31517 		/*
31518 		 * Check if HBA has set the "pm-capable" property.
31519 		 * If "pm-capable" exists and is non-zero then we can
31520 		 * power manage the device without checking the start/stop
31521 		 * cycle count log sense page.
31522 		 *
31523 		 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0)
31524 		 * then we should not power manage the device.
31525 		 *
31526 		 * If "pm-capable" doesn't exist then pm_capable_prop will
31527 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
31528 		 * sd will check the start/stop cycle count log sense page
31529 		 * and power manage the device if the cycle count limit has
31530 		 * not been exceeded.
31531 		 */
31532 		pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
31533 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
31534 		if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) {
31535 			un->un_f_log_sense_supported = TRUE;
31536 		} else {
31537 			/*
31538 			 * pm-capable property exists.
31539 			 *
31540 			 * Convert "TRUE" values for pm_capable_prop to
31541 			 * SD_PM_CAPABLE_TRUE (1) to make it easier to check
31542 			 * later. "TRUE" values are any values except
31543 			 * SD_PM_CAPABLE_FALSE (0) and
31544 			 * SD_PM_CAPABLE_UNDEFINED (-1)
31545 			 */
31546 			if (pm_capable_prop == SD_PM_CAPABLE_FALSE) {
31547 				un->un_f_log_sense_supported = FALSE;
31548 			} else {
31549 				un->un_f_pm_supported = TRUE;
31550 			}
31551 
31552 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
31553 			    "sd_unit_attach: un:0x%p pm-capable "
31554 			    "property set to %d.\n", un, un->un_f_pm_supported);
31555 		}
31556 	}
31557 
31558 	if (un->un_f_is_hotpluggable) {
31559 #if defined(_SUNOS_VTOC_8)
31560 		/*
31561 		 * Note: currently, for VTOC_8 devices, default label is
31562 		 * created for removable and hotpluggable devices only.
31563 		 */
31564 		un->un_f_default_vtoc_supported = TRUE;
31565 #endif
31566 
31567 		/*
31568 		 * Temporarily, let hotpluggable devices pretend to be
31569 		 * removable-media devices for vold.
31570 		 */
31571 		un->un_f_monitor_media_state = TRUE;
31572 
31573 		un->un_f_check_start_stop = TRUE;
31574 
31575 	}
31576 
31577 	/*
31578 	 * By default, only DIRECT ACCESS devices and CDs will have Sun
31579 	 * labels.
31580 	 */
31581 	if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) ||
31582 	    (un->un_sd->sd_inq->inq_rmb)) {
31583 		/*
31584 		 * Direct access devices have disk label
31585 		 */
31586 		un->un_f_vtoc_label_supported = TRUE;
31587 	}
31588 
31589 	/*
31590 	 * Fdisk partitions are supported for all direct access devices on
31591 	 * x86 platform, and just for removable media and hotpluggable
31592 	 * devices on SPARC platform. Later, we will set the following flag
31593 	 * to FALSE if current device is not removable media or hotpluggable
31594 	 * device and if sd works on SAPRC platform.
31595 	 */
31596 	if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
31597 		un->un_f_mboot_supported = TRUE;
31598 	}
31599 
31600 	if (!un->un_f_is_hotpluggable &&
31601 	    !un->un_sd->sd_inq->inq_rmb) {
31602 
31603 #if defined(_SUNOS_VTOC_8)
31604 		/*
31605 		 * Don't support fdisk on fixed disk
31606 		 */
31607 		un->un_f_mboot_supported = FALSE;
31608 #endif
31609 
31610 		/*
31611 		 * Fixed disk support SYNC CACHE
31612 		 */
31613 		un->un_f_sync_cache_supported = TRUE;
31614 
31615 		/*
31616 		 * For fixed disk, if its VTOC is not valid, we will write
31617 		 * errlog into system log
31618 		 */
31619 		if (un->un_f_vtoc_label_supported)
31620 			un->un_f_vtoc_errlog_supported = TRUE;
31621 	}
31622 }
31623