xref: /titanic_51/usr/src/uts/common/io/scsi/targets/sd.c (revision 3c785c4c5ac4bb72eb0cff3cda1255dfc9613aa1)
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 2007 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/kstat.h>
41 #include <sys/vtrace.h>
42 #include <sys/note.h>
43 #include <sys/thread.h>
44 #include <sys/proc.h>
45 #include <sys/efi_partition.h>
46 #include <sys/var.h>
47 #include <sys/aio_req.h>
48 
49 #ifdef __lock_lint
50 #define	_LP64
51 #define	__amd64
52 #endif
53 
54 #if (defined(__fibre))
55 /* Note: is there a leadville version of the following? */
56 #include <sys/fc4/fcal_linkapp.h>
57 #endif
58 #include <sys/taskq.h>
59 #include <sys/uuid.h>
60 #include <sys/byteorder.h>
61 #include <sys/sdt.h>
62 
63 #include "sd_xbuf.h"
64 
65 #include <sys/scsi/targets/sddef.h>
66 
67 
68 /*
69  * Loadable module info.
70  */
71 #if (defined(__fibre))
72 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver %I%"
73 char _depends_on[]	= "misc/scsi drv/fcp";
74 #else
75 #define	SD_MODULE_NAME	"SCSI Disk Driver %I%"
76 char _depends_on[]	= "misc/scsi";
77 #endif
78 
79 /*
80  * Define the interconnect type, to allow the driver to distinguish
81  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
82  *
83  * This is really for backward compatability. In the future, the driver
84  * should actually check the "interconnect-type" property as reported by
85  * the HBA; however at present this property is not defined by all HBAs,
86  * so we will use this #define (1) to permit the driver to run in
87  * backward-compatability mode; and (2) to print a notification message
88  * if an FC HBA does not support the "interconnect-type" property.  The
89  * behavior of the driver will be to assume parallel SCSI behaviors unless
90  * the "interconnect-type" property is defined by the HBA **AND** has a
91  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
92  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
93  * Channel behaviors (as per the old ssd).  (Note that the
94  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
95  * will result in the driver assuming parallel SCSI behaviors.)
96  *
97  * (see common/sys/scsi/impl/services.h)
98  *
99  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
100  * since some FC HBAs may already support that, and there is some code in
101  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
102  * default would confuse that code, and besides things should work fine
103  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
104  * "interconnect_type" property.
105  *
106  * Notes for off-by-1 workaround:
107  * -----------------------------
108  *
109  *    SCSI READ_CAPACITY command returns the LBA number of the
110  *    last logical block, but sd once treated this number as
111  *    disks' capacity on x86 platform. And LBAs are addressed
112  *    based 0. So the last block was lost on x86 platform.
113  *
114  *    Now, we remove this workaround. In order for present sd
115  *    driver to work with disks which are labeled/partitioned
116  *    via previous sd, we add workaround as follows:
117  *
118  *    1) Locate backup EFI label: sd searches the next to last
119  *       block for legacy backup EFI label. If fails, it will
120  *       turn to the last block for backup EFI label;
121  *    2) Clear backup EFI label: sd first search the last block
122  *       for backup EFI label, and will search the next to last
123  *       block only if failed for the last block.
124  *    3) Calculate geometry: refer to sd_convert_geometry(), If
125  *       capacity increasing by 1 causes disks' capacity to cross
126  *       over the limits in table CHS_values, geometry info will
127  *       change. This will raise an issue: In case that primary
128  *       VTOC label is destroyed, format commandline can restore
129  *       it via backup VTOC labels. And format locates backup VTOC
130  *       labels by use of geometry from sd driver. So changing
131  *       geometry will prevent format from finding backup VTOC
132  *       labels. To eliminate this side effect for compatibility,
133  *       sd uses (capacity -1) to calculate geometry;
134  *    4) 1TB disks: some important data structures use 32-bit
135  *       signed long/int (for example, daddr_t), so that sd doesn't
136  *       support a disk with capacity larger than 1TB on 32-bit
137  *       platform. However, for exactly 1TB disk, it was treated as
138  *       (1T - 512)B in the past, and could have valid solaris
139  *       partitions. To workaround this, if an exactly 1TB disk has
140  *       solaris fdisk partition, it will be allowed to work with sd.
141  */
142 #if (defined(__fibre))
143 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
144 #else
145 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
146 #endif
147 
148 /*
149  * The name of the driver, established from the module name in _init.
150  */
151 static	char *sd_label			= NULL;
152 
153 /*
154  * Driver name is unfortunately prefixed on some driver.conf properties.
155  */
156 #if (defined(__fibre))
157 #define	sd_max_xfer_size		ssd_max_xfer_size
158 #define	sd_config_list			ssd_config_list
159 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
160 static	char *sd_config_list		= "ssd-config-list";
161 #else
162 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
163 static	char *sd_config_list		= "sd-config-list";
164 #endif
165 
166 /*
167  * Driver global variables
168  */
169 
170 #if (defined(__fibre))
171 /*
172  * These #defines are to avoid namespace collisions that occur because this
173  * code is currently used to compile two seperate driver modules: sd and ssd.
174  * All global variables need to be treated this way (even if declared static)
175  * in order to allow the debugger to resolve the names properly.
176  * It is anticipated that in the near future the ssd module will be obsoleted,
177  * at which time this namespace issue should go away.
178  */
179 #define	sd_state			ssd_state
180 #define	sd_io_time			ssd_io_time
181 #define	sd_failfast_enable		ssd_failfast_enable
182 #define	sd_ua_retry_count		ssd_ua_retry_count
183 #define	sd_report_pfa			ssd_report_pfa
184 #define	sd_max_throttle			ssd_max_throttle
185 #define	sd_min_throttle			ssd_min_throttle
186 #define	sd_rot_delay			ssd_rot_delay
187 
188 #define	sd_retry_on_reservation_conflict	\
189 					ssd_retry_on_reservation_conflict
190 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
191 #define	sd_resv_conflict_name		ssd_resv_conflict_name
192 
193 #define	sd_component_mask		ssd_component_mask
194 #define	sd_level_mask			ssd_level_mask
195 #define	sd_debug_un			ssd_debug_un
196 #define	sd_error_level			ssd_error_level
197 
198 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
199 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
200 
201 #define	sd_tr				ssd_tr
202 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
203 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
204 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
205 #define	sd_check_media_time		ssd_check_media_time
206 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
207 #define	sd_label_mutex			ssd_label_mutex
208 #define	sd_detach_mutex			ssd_detach_mutex
209 #define	sd_log_buf			ssd_log_buf
210 #define	sd_log_mutex			ssd_log_mutex
211 
212 #define	sd_disk_table			ssd_disk_table
213 #define	sd_disk_table_size		ssd_disk_table_size
214 #define	sd_sense_mutex			ssd_sense_mutex
215 #define	sd_cdbtab			ssd_cdbtab
216 
217 #define	sd_cb_ops			ssd_cb_ops
218 #define	sd_ops				ssd_ops
219 #define	sd_additional_codes		ssd_additional_codes
220 
221 #define	sd_minor_data			ssd_minor_data
222 #define	sd_minor_data_efi		ssd_minor_data_efi
223 
224 #define	sd_tq				ssd_tq
225 #define	sd_wmr_tq			ssd_wmr_tq
226 #define	sd_taskq_name			ssd_taskq_name
227 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
228 #define	sd_taskq_minalloc		ssd_taskq_minalloc
229 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
230 
231 #define	sd_dump_format_string		ssd_dump_format_string
232 
233 #define	sd_iostart_chain		ssd_iostart_chain
234 #define	sd_iodone_chain			ssd_iodone_chain
235 
236 #define	sd_pm_idletime			ssd_pm_idletime
237 
238 #define	sd_force_pm_supported		ssd_force_pm_supported
239 
240 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
241 
242 #endif
243 
244 
245 #ifdef	SDDEBUG
246 int	sd_force_pm_supported		= 0;
247 #endif	/* SDDEBUG */
248 
249 void *sd_state				= NULL;
250 int sd_io_time				= SD_IO_TIME;
251 int sd_failfast_enable			= 1;
252 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
253 int sd_report_pfa			= 1;
254 int sd_max_throttle			= SD_MAX_THROTTLE;
255 int sd_min_throttle			= SD_MIN_THROTTLE;
256 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
257 int sd_qfull_throttle_enable		= TRUE;
258 
259 int sd_retry_on_reservation_conflict	= 1;
260 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
261 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
262 
263 static int sd_dtype_optical_bind	= -1;
264 
265 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
266 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
267 
268 /*
269  * Global data for debug logging. To enable debug printing, sd_component_mask
270  * and sd_level_mask should be set to the desired bit patterns as outlined in
271  * sddef.h.
272  */
273 uint_t	sd_component_mask		= 0x0;
274 uint_t	sd_level_mask			= 0x0;
275 struct	sd_lun *sd_debug_un		= NULL;
276 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
277 
278 /* Note: these may go away in the future... */
279 static uint32_t	sd_xbuf_active_limit	= 512;
280 static uint32_t sd_xbuf_reserve_limit	= 16;
281 
282 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
283 
284 /*
285  * Timer value used to reset the throttle after it has been reduced
286  * (typically in response to TRAN_BUSY or STATUS_QFULL)
287  */
288 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
289 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
290 
291 /*
292  * Interval value associated with the media change scsi watch.
293  */
294 static int sd_check_media_time		= 3000000;
295 
296 /*
297  * Wait value used for in progress operations during a DDI_SUSPEND
298  */
299 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
300 
301 /*
302  * sd_label_mutex protects a static buffer used in the disk label
303  * component of the driver
304  */
305 static kmutex_t sd_label_mutex;
306 
307 /*
308  * sd_detach_mutex protects un_layer_count, un_detach_count, and
309  * un_opens_in_progress in the sd_lun structure.
310  */
311 static kmutex_t sd_detach_mutex;
312 
313 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
314 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
315 
316 /*
317  * Global buffer and mutex for debug logging
318  */
319 static char	sd_log_buf[1024];
320 static kmutex_t	sd_log_mutex;
321 
322 /*
323  * Structs and globals for recording attached lun information.
324  * This maintains a chain. Each node in the chain represents a SCSI controller.
325  * The structure records the number of luns attached to each target connected
326  * with the controller.
327  * For parallel scsi device only.
328  */
329 struct sd_scsi_hba_tgt_lun {
330 	struct sd_scsi_hba_tgt_lun	*next;
331 	dev_info_t			*pdip;
332 	int				nlun[NTARGETS_WIDE];
333 };
334 
335 /*
336  * Flag to indicate the lun is attached or detached
337  */
338 #define	SD_SCSI_LUN_ATTACH	0
339 #define	SD_SCSI_LUN_DETACH	1
340 
341 static kmutex_t	sd_scsi_target_lun_mutex;
342 static struct sd_scsi_hba_tgt_lun	*sd_scsi_target_lun_head = NULL;
343 
344 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
345     sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
346 
347 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
348     sd_scsi_target_lun_head))
349 
350 /*
351  * "Smart" Probe Caching structs, globals, #defines, etc.
352  * For parallel scsi and non-self-identify device only.
353  */
354 
355 /*
356  * The following resources and routines are implemented to support
357  * "smart" probing, which caches the scsi_probe() results in an array,
358  * in order to help avoid long probe times.
359  */
360 struct sd_scsi_probe_cache {
361 	struct	sd_scsi_probe_cache	*next;
362 	dev_info_t	*pdip;
363 	int		cache[NTARGETS_WIDE];
364 };
365 
366 static kmutex_t	sd_scsi_probe_cache_mutex;
367 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
368 
369 /*
370  * Really we only need protection on the head of the linked list, but
371  * better safe than sorry.
372  */
373 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
374     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
375 
376 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
377     sd_scsi_probe_cache_head))
378 
379 
380 /*
381  * Vendor specific data name property declarations
382  */
383 
384 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
385 
386 static sd_tunables seagate_properties = {
387 	SEAGATE_THROTTLE_VALUE,
388 	0,
389 	0,
390 	0,
391 	0,
392 	0,
393 	0,
394 	0,
395 	0
396 };
397 
398 
399 static sd_tunables fujitsu_properties = {
400 	FUJITSU_THROTTLE_VALUE,
401 	0,
402 	0,
403 	0,
404 	0,
405 	0,
406 	0,
407 	0,
408 	0
409 };
410 
411 static sd_tunables ibm_properties = {
412 	IBM_THROTTLE_VALUE,
413 	0,
414 	0,
415 	0,
416 	0,
417 	0,
418 	0,
419 	0,
420 	0
421 };
422 
423 static sd_tunables purple_properties = {
424 	PURPLE_THROTTLE_VALUE,
425 	0,
426 	0,
427 	PURPLE_BUSY_RETRIES,
428 	PURPLE_RESET_RETRY_COUNT,
429 	PURPLE_RESERVE_RELEASE_TIME,
430 	0,
431 	0,
432 	0
433 };
434 
435 static sd_tunables sve_properties = {
436 	SVE_THROTTLE_VALUE,
437 	0,
438 	0,
439 	SVE_BUSY_RETRIES,
440 	SVE_RESET_RETRY_COUNT,
441 	SVE_RESERVE_RELEASE_TIME,
442 	SVE_MIN_THROTTLE_VALUE,
443 	SVE_DISKSORT_DISABLED_FLAG,
444 	0
445 };
446 
447 static sd_tunables maserati_properties = {
448 	0,
449 	0,
450 	0,
451 	0,
452 	0,
453 	0,
454 	0,
455 	MASERATI_DISKSORT_DISABLED_FLAG,
456 	MASERATI_LUN_RESET_ENABLED_FLAG
457 };
458 
459 static sd_tunables pirus_properties = {
460 	PIRUS_THROTTLE_VALUE,
461 	0,
462 	PIRUS_NRR_COUNT,
463 	PIRUS_BUSY_RETRIES,
464 	PIRUS_RESET_RETRY_COUNT,
465 	0,
466 	PIRUS_MIN_THROTTLE_VALUE,
467 	PIRUS_DISKSORT_DISABLED_FLAG,
468 	PIRUS_LUN_RESET_ENABLED_FLAG
469 };
470 
471 #endif
472 
473 #if (defined(__sparc) && !defined(__fibre)) || \
474 	(defined(__i386) || defined(__amd64))
475 
476 
477 static sd_tunables elite_properties = {
478 	ELITE_THROTTLE_VALUE,
479 	0,
480 	0,
481 	0,
482 	0,
483 	0,
484 	0,
485 	0,
486 	0
487 };
488 
489 static sd_tunables st31200n_properties = {
490 	ST31200N_THROTTLE_VALUE,
491 	0,
492 	0,
493 	0,
494 	0,
495 	0,
496 	0,
497 	0,
498 	0
499 };
500 
501 #endif /* Fibre or not */
502 
503 static sd_tunables lsi_properties_scsi = {
504 	LSI_THROTTLE_VALUE,
505 	0,
506 	LSI_NOTREADY_RETRIES,
507 	0,
508 	0,
509 	0,
510 	0,
511 	0,
512 	0
513 };
514 
515 static sd_tunables symbios_properties = {
516 	SYMBIOS_THROTTLE_VALUE,
517 	0,
518 	SYMBIOS_NOTREADY_RETRIES,
519 	0,
520 	0,
521 	0,
522 	0,
523 	0,
524 	0
525 };
526 
527 static sd_tunables lsi_properties = {
528 	0,
529 	0,
530 	LSI_NOTREADY_RETRIES,
531 	0,
532 	0,
533 	0,
534 	0,
535 	0,
536 	0
537 };
538 
539 static sd_tunables lsi_oem_properties = {
540 	0,
541 	0,
542 	LSI_OEM_NOTREADY_RETRIES,
543 	0,
544 	0,
545 	0,
546 	0,
547 	0,
548 	0
549 };
550 
551 
552 
553 #if (defined(SD_PROP_TST))
554 
555 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
556 #define	SD_TST_THROTTLE_VAL	16
557 #define	SD_TST_NOTREADY_VAL	12
558 #define	SD_TST_BUSY_VAL		60
559 #define	SD_TST_RST_RETRY_VAL	36
560 #define	SD_TST_RSV_REL_TIME	60
561 
562 static sd_tunables tst_properties = {
563 	SD_TST_THROTTLE_VAL,
564 	SD_TST_CTYPE_VAL,
565 	SD_TST_NOTREADY_VAL,
566 	SD_TST_BUSY_VAL,
567 	SD_TST_RST_RETRY_VAL,
568 	SD_TST_RSV_REL_TIME,
569 	0,
570 	0,
571 	0
572 };
573 #endif
574 
575 /* This is similiar to the ANSI toupper implementation */
576 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
577 
578 /*
579  * Static Driver Configuration Table
580  *
581  * This is the table of disks which need throttle adjustment (or, perhaps
582  * something else as defined by the flags at a future time.)  device_id
583  * is a string consisting of concatenated vid (vendor), pid (product/model)
584  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
585  * the parts of the string are as defined by the sizes in the scsi_inquiry
586  * structure.  Device type is searched as far as the device_id string is
587  * defined.  Flags defines which values are to be set in the driver from the
588  * properties list.
589  *
590  * Entries below which begin and end with a "*" are a special case.
591  * These do not have a specific vendor, and the string which follows
592  * can appear anywhere in the 16 byte PID portion of the inquiry data.
593  *
594  * Entries below which begin and end with a " " (blank) are a special
595  * case. The comparison function will treat multiple consecutive blanks
596  * as equivalent to a single blank. For example, this causes a
597  * sd_disk_table entry of " NEC CDROM " to match a device's id string
598  * of  "NEC       CDROM".
599  *
600  * Note: The MD21 controller type has been obsoleted.
601  *	 ST318202F is a Legacy device
602  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
603  *	 made with an FC connection. The entries here are a legacy.
604  */
605 static sd_disk_config_t sd_disk_table[] = {
606 #if defined(__fibre) || defined(__i386) || defined(__amd64)
607 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
608 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
609 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
610 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
611 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
612 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
613 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
614 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
615 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
616 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
617 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
618 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
619 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
620 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
621 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
622 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
623 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
624 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
625 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
626 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
627 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
628 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
629 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
630 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
631 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
632 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
633 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
634 	{ "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
635 	{ "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
636 	{ "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
637 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
638 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
639 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
640 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
641 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
642 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
643 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
644 	{ "IBM     1814",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
645 	{ "IBM     1814-200",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
646 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
647 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
648 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652 	{ "Fujitsu SX300",	SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
653 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
654 	{ "SUN     T3", 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 		&purple_properties },
659 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
660 		SD_CONF_BSET_BSY_RETRY_COUNT|
661 		SD_CONF_BSET_RST_RETRIES|
662 		SD_CONF_BSET_RSV_REL_TIME|
663 		SD_CONF_BSET_MIN_THROTTLE|
664 		SD_CONF_BSET_DISKSORT_DISABLED,
665 		&sve_properties },
666 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
667 			SD_CONF_BSET_BSY_RETRY_COUNT|
668 			SD_CONF_BSET_RST_RETRIES|
669 			SD_CONF_BSET_RSV_REL_TIME,
670 		&purple_properties },
671 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
672 		SD_CONF_BSET_LUN_RESET_ENABLED,
673 		&maserati_properties },
674 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
675 		SD_CONF_BSET_NRR_COUNT|
676 		SD_CONF_BSET_BSY_RETRY_COUNT|
677 		SD_CONF_BSET_RST_RETRIES|
678 		SD_CONF_BSET_MIN_THROTTLE|
679 		SD_CONF_BSET_DISKSORT_DISABLED|
680 		SD_CONF_BSET_LUN_RESET_ENABLED,
681 		&pirus_properties },
682 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
683 		SD_CONF_BSET_NRR_COUNT|
684 		SD_CONF_BSET_BSY_RETRY_COUNT|
685 		SD_CONF_BSET_RST_RETRIES|
686 		SD_CONF_BSET_MIN_THROTTLE|
687 		SD_CONF_BSET_DISKSORT_DISABLED|
688 		SD_CONF_BSET_LUN_RESET_ENABLED,
689 		&pirus_properties },
690 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
691 		SD_CONF_BSET_NRR_COUNT|
692 		SD_CONF_BSET_BSY_RETRY_COUNT|
693 		SD_CONF_BSET_RST_RETRIES|
694 		SD_CONF_BSET_MIN_THROTTLE|
695 		SD_CONF_BSET_DISKSORT_DISABLED|
696 		SD_CONF_BSET_LUN_RESET_ENABLED,
697 		&pirus_properties },
698 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
699 		SD_CONF_BSET_NRR_COUNT|
700 		SD_CONF_BSET_BSY_RETRY_COUNT|
701 		SD_CONF_BSET_RST_RETRIES|
702 		SD_CONF_BSET_MIN_THROTTLE|
703 		SD_CONF_BSET_DISKSORT_DISABLED|
704 		SD_CONF_BSET_LUN_RESET_ENABLED,
705 		&pirus_properties },
706 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
707 		SD_CONF_BSET_NRR_COUNT|
708 		SD_CONF_BSET_BSY_RETRY_COUNT|
709 		SD_CONF_BSET_RST_RETRIES|
710 		SD_CONF_BSET_MIN_THROTTLE|
711 		SD_CONF_BSET_DISKSORT_DISABLED|
712 		SD_CONF_BSET_LUN_RESET_ENABLED,
713 		&pirus_properties },
714 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
715 		SD_CONF_BSET_NRR_COUNT|
716 		SD_CONF_BSET_BSY_RETRY_COUNT|
717 		SD_CONF_BSET_RST_RETRIES|
718 		SD_CONF_BSET_MIN_THROTTLE|
719 		SD_CONF_BSET_DISKSORT_DISABLED|
720 		SD_CONF_BSET_LUN_RESET_ENABLED,
721 		&pirus_properties },
722 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
723 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
724 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
725 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
726 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
727 #endif /* fibre or NON-sparc platforms */
728 #if ((defined(__sparc) && !defined(__fibre)) ||\
729 	(defined(__i386) || defined(__amd64)))
730 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
731 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
732 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
733 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
734 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
735 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
736 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
737 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
738 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
739 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
740 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
741 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
742 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
743 	    &symbios_properties },
744 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
745 	    &lsi_properties_scsi },
746 #if defined(__i386) || defined(__amd64)
747 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
748 				    | SD_CONF_BSET_READSUB_BCD
749 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
750 				    | SD_CONF_BSET_NO_READ_HEADER
751 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
752 
753 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
754 				    | SD_CONF_BSET_READSUB_BCD
755 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
756 				    | SD_CONF_BSET_NO_READ_HEADER
757 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
758 #endif /* __i386 || __amd64 */
759 #endif /* sparc NON-fibre or NON-sparc platforms */
760 
761 #if (defined(SD_PROP_TST))
762 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
763 				| SD_CONF_BSET_CTYPE
764 				| SD_CONF_BSET_NRR_COUNT
765 				| SD_CONF_BSET_FAB_DEVID
766 				| SD_CONF_BSET_NOCACHE
767 				| SD_CONF_BSET_BSY_RETRY_COUNT
768 				| SD_CONF_BSET_PLAYMSF_BCD
769 				| SD_CONF_BSET_READSUB_BCD
770 				| SD_CONF_BSET_READ_TOC_TRK_BCD
771 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
772 				| SD_CONF_BSET_NO_READ_HEADER
773 				| SD_CONF_BSET_READ_CD_XD4
774 				| SD_CONF_BSET_RST_RETRIES
775 				| SD_CONF_BSET_RSV_REL_TIME
776 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
777 #endif
778 };
779 
780 static const int sd_disk_table_size =
781 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
782 
783 
784 /*
785  * Return codes of sd_uselabel().
786  */
787 #define	SD_LABEL_IS_VALID		0
788 #define	SD_LABEL_IS_INVALID		1
789 
790 #define	SD_INTERCONNECT_PARALLEL	0
791 #define	SD_INTERCONNECT_FABRIC		1
792 #define	SD_INTERCONNECT_FIBRE		2
793 #define	SD_INTERCONNECT_SSA		3
794 #define	SD_INTERCONNECT_SATA		4
795 #define	SD_IS_PARALLEL_SCSI(un)		\
796 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
797 #define	SD_IS_SERIAL(un)		\
798 	((un)->un_interconnect_type == SD_INTERCONNECT_SATA)
799 
800 /*
801  * Definitions used by device id registration routines
802  */
803 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
804 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
805 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
806 #define	WD_NODE			7	/* the whole disk minor */
807 
808 static kmutex_t sd_sense_mutex = {0};
809 
810 /*
811  * Macros for updates of the driver state
812  */
813 #define	New_state(un, s)        \
814 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
815 #define	Restore_state(un)	\
816 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
817 
818 static struct sd_cdbinfo sd_cdbtab[] = {
819 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
820 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
821 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
822 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
823 };
824 
825 /*
826  * Specifies the number of seconds that must have elapsed since the last
827  * cmd. has completed for a device to be declared idle to the PM framework.
828  */
829 static int sd_pm_idletime = 1;
830 
831 /*
832  * Internal function prototypes
833  */
834 
835 #if (defined(__fibre))
836 /*
837  * These #defines are to avoid namespace collisions that occur because this
838  * code is currently used to compile two seperate driver modules: sd and ssd.
839  * All function names need to be treated this way (even if declared static)
840  * in order to allow the debugger to resolve the names properly.
841  * It is anticipated that in the near future the ssd module will be obsoleted,
842  * at which time this ugliness should go away.
843  */
844 #define	sd_log_trace			ssd_log_trace
845 #define	sd_log_info			ssd_log_info
846 #define	sd_log_err			ssd_log_err
847 #define	sdprobe				ssdprobe
848 #define	sdinfo				ssdinfo
849 #define	sd_prop_op			ssd_prop_op
850 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
851 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
852 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
853 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
854 #define	sd_scsi_target_lun_init		ssd_scsi_target_lun_init
855 #define	sd_scsi_target_lun_fini		ssd_scsi_target_lun_fini
856 #define	sd_scsi_get_target_lun_count	ssd_scsi_get_target_lun_count
857 #define	sd_scsi_update_lun_on_target	ssd_scsi_update_lun_on_target
858 #define	sd_spin_up_unit			ssd_spin_up_unit
859 #define	sd_enable_descr_sense		ssd_enable_descr_sense
860 #define	sd_reenable_dsense_task		ssd_reenable_dsense_task
861 #define	sd_set_mmc_caps			ssd_set_mmc_caps
862 #define	sd_read_unit_properties		ssd_read_unit_properties
863 #define	sd_process_sdconf_file		ssd_process_sdconf_file
864 #define	sd_process_sdconf_table		ssd_process_sdconf_table
865 #define	sd_sdconf_id_match		ssd_sdconf_id_match
866 #define	sd_blank_cmp			ssd_blank_cmp
867 #define	sd_chk_vers1_data		ssd_chk_vers1_data
868 #define	sd_set_vers1_properties		ssd_set_vers1_properties
869 #define	sd_validate_geometry		ssd_validate_geometry
870 
871 #if defined(_SUNOS_VTOC_16)
872 #define	sd_convert_geometry		ssd_convert_geometry
873 #endif
874 
875 #define	sd_resync_geom_caches		ssd_resync_geom_caches
876 #define	sd_read_fdisk			ssd_read_fdisk
877 #define	sd_get_physical_geometry	ssd_get_physical_geometry
878 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
879 #define	sd_update_block_info		ssd_update_block_info
880 #define	sd_swap_efi_gpt			ssd_swap_efi_gpt
881 #define	sd_swap_efi_gpe			ssd_swap_efi_gpe
882 #define	sd_validate_efi			ssd_validate_efi
883 #define	sd_use_efi			ssd_use_efi
884 #define	sd_uselabel			ssd_uselabel
885 #define	sd_build_default_label		ssd_build_default_label
886 #define	sd_has_max_chs_vals		ssd_has_max_chs_vals
887 #define	sd_inq_fill			ssd_inq_fill
888 #define	sd_register_devid		ssd_register_devid
889 #define	sd_get_devid_block		ssd_get_devid_block
890 #define	sd_get_devid			ssd_get_devid
891 #define	sd_create_devid			ssd_create_devid
892 #define	sd_write_deviceid		ssd_write_deviceid
893 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
894 #define	sd_setup_pm			ssd_setup_pm
895 #define	sd_create_pm_components		ssd_create_pm_components
896 #define	sd_ddi_suspend			ssd_ddi_suspend
897 #define	sd_ddi_pm_suspend		ssd_ddi_pm_suspend
898 #define	sd_ddi_resume			ssd_ddi_resume
899 #define	sd_ddi_pm_resume		ssd_ddi_pm_resume
900 #define	sdpower				ssdpower
901 #define	sdattach			ssdattach
902 #define	sddetach			ssddetach
903 #define	sd_unit_attach			ssd_unit_attach
904 #define	sd_unit_detach			ssd_unit_detach
905 #define	sd_set_unit_attributes		ssd_set_unit_attributes
906 #define	sd_create_minor_nodes		ssd_create_minor_nodes
907 #define	sd_create_errstats		ssd_create_errstats
908 #define	sd_set_errstats			ssd_set_errstats
909 #define	sd_set_pstats			ssd_set_pstats
910 #define	sddump				ssddump
911 #define	sd_scsi_poll			ssd_scsi_poll
912 #define	sd_send_polled_RQS		ssd_send_polled_RQS
913 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
914 #define	sd_init_event_callbacks		ssd_init_event_callbacks
915 #define	sd_event_callback		ssd_event_callback
916 #define	sd_cache_control		ssd_cache_control
917 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
918 #define	sd_make_device			ssd_make_device
919 #define	sdopen				ssdopen
920 #define	sdclose				ssdclose
921 #define	sd_ready_and_valid		ssd_ready_and_valid
922 #define	sdmin				ssdmin
923 #define	sdread				ssdread
924 #define	sdwrite				ssdwrite
925 #define	sdaread				ssdaread
926 #define	sdawrite			ssdawrite
927 #define	sdstrategy			ssdstrategy
928 #define	sdioctl				ssdioctl
929 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
930 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
931 #define	sd_checksum_iostart		ssd_checksum_iostart
932 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
933 #define	sd_pm_iostart			ssd_pm_iostart
934 #define	sd_core_iostart			ssd_core_iostart
935 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
936 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
937 #define	sd_checksum_iodone		ssd_checksum_iodone
938 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
939 #define	sd_pm_iodone			ssd_pm_iodone
940 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
941 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
942 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
943 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
944 #define	sd_buf_iodone			ssd_buf_iodone
945 #define	sd_uscsi_strategy		ssd_uscsi_strategy
946 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
947 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
948 #define	sd_uscsi_iodone			ssd_uscsi_iodone
949 #define	sd_xbuf_strategy		ssd_xbuf_strategy
950 #define	sd_xbuf_init			ssd_xbuf_init
951 #define	sd_pm_entry			ssd_pm_entry
952 #define	sd_pm_exit			ssd_pm_exit
953 
954 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
955 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
956 
957 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
958 #define	sdintr				ssdintr
959 #define	sd_start_cmds			ssd_start_cmds
960 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
961 #define	sd_bioclone_alloc		ssd_bioclone_alloc
962 #define	sd_bioclone_free		ssd_bioclone_free
963 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
964 #define	sd_shadow_buf_free		ssd_shadow_buf_free
965 #define	sd_print_transport_rejected_message	\
966 					ssd_print_transport_rejected_message
967 #define	sd_retry_command		ssd_retry_command
968 #define	sd_set_retry_bp			ssd_set_retry_bp
969 #define	sd_send_request_sense_command	ssd_send_request_sense_command
970 #define	sd_start_retry_command		ssd_start_retry_command
971 #define	sd_start_direct_priority_command	\
972 					ssd_start_direct_priority_command
973 #define	sd_return_failed_command	ssd_return_failed_command
974 #define	sd_return_failed_command_no_restart	\
975 					ssd_return_failed_command_no_restart
976 #define	sd_return_command		ssd_return_command
977 #define	sd_sync_with_callback		ssd_sync_with_callback
978 #define	sdrunout			ssdrunout
979 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
980 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
981 #define	sd_reduce_throttle		ssd_reduce_throttle
982 #define	sd_restore_throttle		ssd_restore_throttle
983 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
984 #define	sd_init_cdb_limits		ssd_init_cdb_limits
985 #define	sd_pkt_status_good		ssd_pkt_status_good
986 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
987 #define	sd_pkt_status_busy		ssd_pkt_status_busy
988 #define	sd_pkt_status_reservation_conflict	\
989 					ssd_pkt_status_reservation_conflict
990 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
991 #define	sd_handle_request_sense		ssd_handle_request_sense
992 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
993 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
994 #define	sd_validate_sense_data		ssd_validate_sense_data
995 #define	sd_decode_sense			ssd_decode_sense
996 #define	sd_print_sense_msg		ssd_print_sense_msg
997 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
998 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
999 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
1000 #define	sd_sense_key_medium_or_hardware_error	\
1001 					ssd_sense_key_medium_or_hardware_error
1002 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
1003 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
1004 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
1005 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
1006 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
1007 #define	sd_sense_key_default		ssd_sense_key_default
1008 #define	sd_print_retry_msg		ssd_print_retry_msg
1009 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
1010 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
1011 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
1012 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
1013 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
1014 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
1015 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
1016 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
1017 #define	sd_pkt_reason_default		ssd_pkt_reason_default
1018 #define	sd_reset_target			ssd_reset_target
1019 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
1020 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
1021 #define	sd_taskq_create			ssd_taskq_create
1022 #define	sd_taskq_delete			ssd_taskq_delete
1023 #define	sd_media_change_task		ssd_media_change_task
1024 #define	sd_handle_mchange		ssd_handle_mchange
1025 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
1026 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
1027 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
1028 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
1029 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
1030 					sd_send_scsi_feature_GET_CONFIGURATION
1031 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
1032 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
1033 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
1034 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
1035 					ssd_send_scsi_PERSISTENT_RESERVE_IN
1036 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
1037 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
1038 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
1039 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
1040 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1041 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
1042 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
1043 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
1044 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
1045 #define	sd_alloc_rqs			ssd_alloc_rqs
1046 #define	sd_free_rqs			ssd_free_rqs
1047 #define	sd_dump_memory			ssd_dump_memory
1048 #define	sd_get_media_info		ssd_get_media_info
1049 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
1050 #define	sd_dkio_get_geometry		ssd_dkio_get_geometry
1051 #define	sd_dkio_set_geometry		ssd_dkio_set_geometry
1052 #define	sd_dkio_get_partition		ssd_dkio_get_partition
1053 #define	sd_dkio_set_partition		ssd_dkio_set_partition
1054 #define	sd_dkio_partition		ssd_dkio_partition
1055 #define	sd_dkio_get_vtoc		ssd_dkio_get_vtoc
1056 #define	sd_dkio_get_efi			ssd_dkio_get_efi
1057 #define	sd_build_user_vtoc		ssd_build_user_vtoc
1058 #define	sd_dkio_set_vtoc		ssd_dkio_set_vtoc
1059 #define	sd_dkio_set_efi			ssd_dkio_set_efi
1060 #define	sd_build_label_vtoc		ssd_build_label_vtoc
1061 #define	sd_write_label			ssd_write_label
1062 #define	sd_clear_vtoc			ssd_clear_vtoc
1063 #define	sd_clear_efi			ssd_clear_efi
1064 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
1065 #define	sd_setup_next_xfer		ssd_setup_next_xfer
1066 #define	sd_dkio_get_temp		ssd_dkio_get_temp
1067 #define	sd_dkio_get_mboot		ssd_dkio_get_mboot
1068 #define	sd_dkio_set_mboot		ssd_dkio_set_mboot
1069 #define	sd_setup_default_geometry	ssd_setup_default_geometry
1070 #define	sd_update_fdisk_and_vtoc	ssd_update_fdisk_and_vtoc
1071 #define	sd_check_mhd			ssd_check_mhd
1072 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1073 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1074 #define	sd_sname			ssd_sname
1075 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1076 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1077 #define	sd_take_ownership		ssd_take_ownership
1078 #define	sd_reserve_release		ssd_reserve_release
1079 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1080 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1081 #define	sd_persistent_reservation_in_read_keys	\
1082 					ssd_persistent_reservation_in_read_keys
1083 #define	sd_persistent_reservation_in_read_resv	\
1084 					ssd_persistent_reservation_in_read_resv
1085 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1086 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1087 #define	sd_mhdioc_release		ssd_mhdioc_release
1088 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1089 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1090 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1091 #define	sr_change_blkmode		ssr_change_blkmode
1092 #define	sr_change_speed			ssr_change_speed
1093 #define	sr_atapi_change_speed		ssr_atapi_change_speed
1094 #define	sr_pause_resume			ssr_pause_resume
1095 #define	sr_play_msf			ssr_play_msf
1096 #define	sr_play_trkind			ssr_play_trkind
1097 #define	sr_read_all_subcodes		ssr_read_all_subcodes
1098 #define	sr_read_subchannel		ssr_read_subchannel
1099 #define	sr_read_tocentry		ssr_read_tocentry
1100 #define	sr_read_tochdr			ssr_read_tochdr
1101 #define	sr_read_cdda			ssr_read_cdda
1102 #define	sr_read_cdxa			ssr_read_cdxa
1103 #define	sr_read_mode1			ssr_read_mode1
1104 #define	sr_read_mode2			ssr_read_mode2
1105 #define	sr_read_cd_mode2		ssr_read_cd_mode2
1106 #define	sr_sector_mode			ssr_sector_mode
1107 #define	sr_eject			ssr_eject
1108 #define	sr_ejected			ssr_ejected
1109 #define	sr_check_wp			ssr_check_wp
1110 #define	sd_check_media			ssd_check_media
1111 #define	sd_media_watch_cb		ssd_media_watch_cb
1112 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1113 #define	sr_volume_ctrl			ssr_volume_ctrl
1114 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1115 #define	sd_log_page_supported		ssd_log_page_supported
1116 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1117 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1118 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1119 #define	sd_range_lock			ssd_range_lock
1120 #define	sd_get_range			ssd_get_range
1121 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1122 #define	sd_range_unlock			ssd_range_unlock
1123 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1124 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1125 
1126 #define	sd_iostart_chain		ssd_iostart_chain
1127 #define	sd_iodone_chain			ssd_iodone_chain
1128 #define	sd_initpkt_map			ssd_initpkt_map
1129 #define	sd_destroypkt_map		ssd_destroypkt_map
1130 #define	sd_chain_type_map		ssd_chain_type_map
1131 #define	sd_chain_index_map		ssd_chain_index_map
1132 
1133 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1134 #define	sd_failfast_flushq		ssd_failfast_flushq
1135 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1136 
1137 #define	sd_is_lsi			ssd_is_lsi
1138 
1139 #endif	/* #if (defined(__fibre)) */
1140 
1141 
1142 int _init(void);
1143 int _fini(void);
1144 int _info(struct modinfo *modinfop);
1145 
1146 /*PRINTFLIKE3*/
1147 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1148 /*PRINTFLIKE3*/
1149 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1150 /*PRINTFLIKE3*/
1151 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1152 
1153 static int sdprobe(dev_info_t *devi);
1154 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1155     void **result);
1156 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1157     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1158 
1159 /*
1160  * Smart probe for parallel scsi
1161  */
1162 static void sd_scsi_probe_cache_init(void);
1163 static void sd_scsi_probe_cache_fini(void);
1164 static void sd_scsi_clear_probe_cache(void);
1165 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1166 
1167 /*
1168  * Attached luns on target for parallel scsi
1169  */
1170 static void sd_scsi_target_lun_init(void);
1171 static void sd_scsi_target_lun_fini(void);
1172 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1173 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1174 
1175 static int	sd_spin_up_unit(struct sd_lun *un);
1176 #ifdef _LP64
1177 static void	sd_enable_descr_sense(struct sd_lun *un);
1178 static void	sd_reenable_dsense_task(void *arg);
1179 #endif /* _LP64 */
1180 
1181 static void	sd_set_mmc_caps(struct sd_lun *un);
1182 
1183 static void sd_read_unit_properties(struct sd_lun *un);
1184 static int  sd_process_sdconf_file(struct sd_lun *un);
1185 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1186     int *data_list, sd_tunables *values);
1187 static void sd_process_sdconf_table(struct sd_lun *un);
1188 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1189 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1190 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1191 	int list_len, char *dataname_ptr);
1192 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1193     sd_tunables *prop_list);
1194 static int  sd_validate_geometry(struct sd_lun *un, int path_flag);
1195 
1196 #if defined(_SUNOS_VTOC_16)
1197 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g);
1198 #endif
1199 
1200 static void sd_resync_geom_caches(struct sd_lun *un, uint64_t capacity,
1201 	int lbasize, int path_flag);
1202 static int  sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize,
1203 	int path_flag);
1204 static void sd_get_physical_geometry(struct sd_lun *un,
1205 	struct geom_cache *pgeom_p, uint64_t capacity, int lbasize,
1206 	int path_flag);
1207 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity,
1208 	int lbasize);
1209 static int  sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag);
1210 static void sd_swap_efi_gpt(efi_gpt_t *);
1211 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *);
1212 static int sd_validate_efi(efi_gpt_t *);
1213 static int sd_use_efi(struct sd_lun *, int);
1214 static void sd_build_default_label(struct sd_lun *un);
1215 
1216 #if defined(_FIRMWARE_NEEDS_FDISK)
1217 static int  sd_has_max_chs_vals(struct ipart *fdp);
1218 #endif
1219 static void sd_inq_fill(char *p, int l, char *s);
1220 
1221 
1222 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi,
1223     int reservation_flag);
1224 static daddr_t  sd_get_devid_block(struct sd_lun *un);
1225 static int  sd_get_devid(struct sd_lun *un);
1226 static int  sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len);
1227 static ddi_devid_t sd_create_devid(struct sd_lun *un);
1228 static int  sd_write_deviceid(struct sd_lun *un);
1229 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1230 static int  sd_check_vpd_page_support(struct sd_lun *un);
1231 
1232 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi);
1233 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1234 
1235 static int  sd_ddi_suspend(dev_info_t *devi);
1236 static int  sd_ddi_pm_suspend(struct sd_lun *un);
1237 static int  sd_ddi_resume(dev_info_t *devi);
1238 static int  sd_ddi_pm_resume(struct sd_lun *un);
1239 static int  sdpower(dev_info_t *devi, int component, int level);
1240 
1241 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1242 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1243 static int  sd_unit_attach(dev_info_t *devi);
1244 static int  sd_unit_detach(dev_info_t *devi);
1245 
1246 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1247 static int  sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi);
1248 static void sd_create_errstats(struct sd_lun *un, int instance);
1249 static void sd_set_errstats(struct sd_lun *un);
1250 static void sd_set_pstats(struct sd_lun *un);
1251 
1252 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1253 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1254 static int  sd_send_polled_RQS(struct sd_lun *un);
1255 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1256 
1257 #if (defined(__fibre))
1258 /*
1259  * Event callbacks (photon)
1260  */
1261 static void sd_init_event_callbacks(struct sd_lun *un);
1262 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1263 #endif
1264 
1265 /*
1266  * Defines for sd_cache_control
1267  */
1268 
1269 #define	SD_CACHE_ENABLE		1
1270 #define	SD_CACHE_DISABLE	0
1271 #define	SD_CACHE_NOCHANGE	-1
1272 
1273 static int   sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag);
1274 static int   sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled);
1275 static dev_t sd_make_device(dev_info_t *devi);
1276 
1277 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1278 	uint64_t capacity);
1279 
1280 /*
1281  * Driver entry point functions.
1282  */
1283 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1284 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1285 static int  sd_ready_and_valid(struct sd_lun *un);
1286 
1287 static void sdmin(struct buf *bp);
1288 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1289 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1290 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1291 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1292 
1293 static int sdstrategy(struct buf *bp);
1294 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1295 
1296 /*
1297  * Function prototypes for layering functions in the iostart chain.
1298  */
1299 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1300 	struct buf *bp);
1301 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1302 	struct buf *bp);
1303 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1304 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1305 	struct buf *bp);
1306 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1307 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1308 
1309 /*
1310  * Function prototypes for layering functions in the iodone chain.
1311  */
1312 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1313 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1314 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1315 	struct buf *bp);
1316 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1317 	struct buf *bp);
1318 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1319 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1320 	struct buf *bp);
1321 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1322 
1323 /*
1324  * Prototypes for functions to support buf(9S) based IO.
1325  */
1326 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1327 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1328 static void sd_destroypkt_for_buf(struct buf *);
1329 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1330 	struct buf *bp, int flags,
1331 	int (*callback)(caddr_t), caddr_t callback_arg,
1332 	diskaddr_t lba, uint32_t blockcount);
1333 #if defined(__i386) || defined(__amd64)
1334 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1335 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1336 #endif /* defined(__i386) || defined(__amd64) */
1337 
1338 /*
1339  * Prototypes for functions to support USCSI IO.
1340  */
1341 static int sd_uscsi_strategy(struct buf *bp);
1342 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1343 static void sd_destroypkt_for_uscsi(struct buf *);
1344 
1345 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1346 	uchar_t chain_type, void *pktinfop);
1347 
1348 static int  sd_pm_entry(struct sd_lun *un);
1349 static void sd_pm_exit(struct sd_lun *un);
1350 
1351 static void sd_pm_idletimeout_handler(void *arg);
1352 
1353 /*
1354  * sd_core internal functions (used at the sd_core_io layer).
1355  */
1356 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1357 static void sdintr(struct scsi_pkt *pktp);
1358 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1359 
1360 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1361 	enum uio_seg dataspace, int path_flag);
1362 
1363 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1364 	daddr_t blkno, int (*func)(struct buf *));
1365 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1366 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1367 static void sd_bioclone_free(struct buf *bp);
1368 static void sd_shadow_buf_free(struct buf *bp);
1369 
1370 static void sd_print_transport_rejected_message(struct sd_lun *un,
1371 	struct sd_xbuf *xp, int code);
1372 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1373     void *arg, int code);
1374 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1375     void *arg, int code);
1376 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1377     void *arg, int code);
1378 
1379 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1380 	int retry_check_flag,
1381 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1382 		int c),
1383 	void *user_arg, int failure_code,  clock_t retry_delay,
1384 	void (*statp)(kstat_io_t *));
1385 
1386 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1387 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1388 
1389 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1390 	struct scsi_pkt *pktp);
1391 static void sd_start_retry_command(void *arg);
1392 static void sd_start_direct_priority_command(void *arg);
1393 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1394 	int errcode);
1395 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1396 	struct buf *bp, int errcode);
1397 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1398 static void sd_sync_with_callback(struct sd_lun *un);
1399 static int sdrunout(caddr_t arg);
1400 
1401 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1402 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1403 
1404 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1405 static void sd_restore_throttle(void *arg);
1406 
1407 static void sd_init_cdb_limits(struct sd_lun *un);
1408 
1409 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1410 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1411 
1412 /*
1413  * Error handling functions
1414  */
1415 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1416 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1417 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1418 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1419 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1420 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1421 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1422 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1423 
1424 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1425 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1426 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1427 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1428 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1429 	struct sd_xbuf *xp);
1430 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1431 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1432 
1433 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1434 	void *arg, int code);
1435 
1436 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1437 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1438 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1439 	uint8_t *sense_datap,
1440 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1441 static void sd_sense_key_not_ready(struct sd_lun *un,
1442 	uint8_t *sense_datap,
1443 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1444 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1445 	uint8_t *sense_datap,
1446 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1447 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1448 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1449 static void sd_sense_key_unit_attention(struct sd_lun *un,
1450 	uint8_t *sense_datap,
1451 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1452 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1453 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1454 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1455 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1456 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1457 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1458 static void sd_sense_key_default(struct sd_lun *un,
1459 	uint8_t *sense_datap,
1460 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1461 
1462 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1463 	void *arg, int flag);
1464 
1465 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1466 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1467 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1468 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1469 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1470 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1471 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1472 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1473 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1474 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1475 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1476 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1477 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1478 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1479 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1480 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1481 
1482 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1483 
1484 static void sd_start_stop_unit_callback(void *arg);
1485 static void sd_start_stop_unit_task(void *arg);
1486 
1487 static void sd_taskq_create(void);
1488 static void sd_taskq_delete(void);
1489 static void sd_media_change_task(void *arg);
1490 
1491 static int sd_handle_mchange(struct sd_lun *un);
1492 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag);
1493 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp,
1494 	uint32_t *lbap, int path_flag);
1495 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
1496 	uint32_t *lbap, int path_flag);
1497 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag,
1498 	int path_flag);
1499 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr,
1500 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1501 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag);
1502 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un,
1503 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1504 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un,
1505 	uchar_t usr_cmd, uchar_t *usr_bufp);
1506 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1507 	struct dk_callback *dkc);
1508 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1509 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un,
1510 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1511 	uchar_t *bufaddr, uint_t buflen);
1512 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
1513 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1514 	uchar_t *bufaddr, uint_t buflen, char feature);
1515 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize,
1516 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1517 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize,
1518 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1519 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
1520 	size_t buflen, daddr_t start_block, int path_flag);
1521 #define	sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag)	\
1522 	sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \
1523 	path_flag)
1524 #define	sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag)	\
1525 	sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\
1526 	path_flag)
1527 
1528 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr,
1529 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1530 	uint16_t param_ptr, int path_flag);
1531 
1532 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1533 static void sd_free_rqs(struct sd_lun *un);
1534 
1535 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1536 	uchar_t *data, int len, int fmt);
1537 static void sd_panic_for_res_conflict(struct sd_lun *un);
1538 
1539 /*
1540  * Disk Ioctl Function Prototypes
1541  */
1542 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1543 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1544 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag,
1545 	int geom_validated);
1546 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag);
1547 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag,
1548 	int geom_validated);
1549 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag);
1550 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag,
1551 	int geom_validated);
1552 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag);
1553 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag);
1554 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1555 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag);
1556 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag);
1557 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1558 static int sd_write_label(dev_t dev);
1559 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl);
1560 static void sd_clear_vtoc(struct sd_lun *un);
1561 static void sd_clear_efi(struct sd_lun *un);
1562 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1563 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag);
1564 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag);
1565 static void sd_setup_default_geometry(struct sd_lun *un);
1566 #if defined(__i386) || defined(__amd64)
1567 static int sd_update_fdisk_and_vtoc(struct sd_lun *un);
1568 #endif
1569 
1570 /*
1571  * Multi-host Ioctl Prototypes
1572  */
1573 static int sd_check_mhd(dev_t dev, int interval);
1574 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1575 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1576 static char *sd_sname(uchar_t status);
1577 static void sd_mhd_resvd_recover(void *arg);
1578 static void sd_resv_reclaim_thread();
1579 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1580 static int sd_reserve_release(dev_t dev, int cmd);
1581 static void sd_rmv_resv_reclaim_req(dev_t dev);
1582 static void sd_mhd_reset_notify_cb(caddr_t arg);
1583 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1584 	mhioc_inkeys_t *usrp, int flag);
1585 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1586 	mhioc_inresvs_t *usrp, int flag);
1587 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1588 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1589 static int sd_mhdioc_release(dev_t dev);
1590 static int sd_mhdioc_register_devid(dev_t dev);
1591 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1592 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1593 
1594 /*
1595  * SCSI removable prototypes
1596  */
1597 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1598 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1599 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1600 static int sr_pause_resume(dev_t dev, int mode);
1601 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1602 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1603 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1604 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1605 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1606 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1607 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1608 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1609 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1610 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1611 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1612 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1613 static int sr_eject(dev_t dev);
1614 static void sr_ejected(register struct sd_lun *un);
1615 static int sr_check_wp(dev_t dev);
1616 static int sd_check_media(dev_t dev, enum dkio_state state);
1617 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1618 static void sd_delayed_cv_broadcast(void *arg);
1619 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1620 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1621 
1622 static int sd_log_page_supported(struct sd_lun *un, int log_page);
1623 
1624 /*
1625  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1626  */
1627 static void sd_check_for_writable_cd(struct sd_lun *un);
1628 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1629 static void sd_wm_cache_destructor(void *wm, void *un);
1630 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1631 	daddr_t endb, ushort_t typ);
1632 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1633 	daddr_t endb);
1634 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1635 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1636 static void sd_read_modify_write_task(void * arg);
1637 static int
1638 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1639 	struct buf **bpp);
1640 
1641 
1642 /*
1643  * Function prototypes for failfast support.
1644  */
1645 static void sd_failfast_flushq(struct sd_lun *un);
1646 static int sd_failfast_flushq_callback(struct buf *bp);
1647 
1648 /*
1649  * Function prototypes to check for lsi devices
1650  */
1651 static void sd_is_lsi(struct sd_lun *un);
1652 
1653 /*
1654  * Function prototypes for x86 support
1655  */
1656 #if defined(__i386) || defined(__amd64)
1657 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1658 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1659 #endif
1660 
1661 /*
1662  * Constants for failfast support:
1663  *
1664  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1665  * failfast processing being performed.
1666  *
1667  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1668  * failfast processing on all bufs with B_FAILFAST set.
1669  */
1670 
1671 #define	SD_FAILFAST_INACTIVE		0
1672 #define	SD_FAILFAST_ACTIVE		1
1673 
1674 /*
1675  * Bitmask to control behavior of buf(9S) flushes when a transition to
1676  * the failfast state occurs. Optional bits include:
1677  *
1678  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1679  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1680  * be flushed.
1681  *
1682  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1683  * driver, in addition to the regular wait queue. This includes the xbuf
1684  * queues. When clear, only the driver's wait queue will be flushed.
1685  */
1686 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1687 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1688 
1689 /*
1690  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1691  * to flush all queues within the driver.
1692  */
1693 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1694 
1695 
1696 /*
1697  * SD Testing Fault Injection
1698  */
1699 #ifdef SD_FAULT_INJECTION
1700 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1701 static void sd_faultinjection(struct scsi_pkt *pktp);
1702 static void sd_injection_log(char *buf, struct sd_lun *un);
1703 #endif
1704 
1705 /*
1706  * Device driver ops vector
1707  */
1708 static struct cb_ops sd_cb_ops = {
1709 	sdopen,			/* open */
1710 	sdclose,		/* close */
1711 	sdstrategy,		/* strategy */
1712 	nodev,			/* print */
1713 	sddump,			/* dump */
1714 	sdread,			/* read */
1715 	sdwrite,		/* write */
1716 	sdioctl,		/* ioctl */
1717 	nodev,			/* devmap */
1718 	nodev,			/* mmap */
1719 	nodev,			/* segmap */
1720 	nochpoll,		/* poll */
1721 	sd_prop_op,		/* cb_prop_op */
1722 	0,			/* streamtab  */
1723 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1724 	CB_REV,			/* cb_rev */
1725 	sdaread, 		/* async I/O read entry point */
1726 	sdawrite		/* async I/O write entry point */
1727 };
1728 
1729 static struct dev_ops sd_ops = {
1730 	DEVO_REV,		/* devo_rev, */
1731 	0,			/* refcnt  */
1732 	sdinfo,			/* info */
1733 	nulldev,		/* identify */
1734 	sdprobe,		/* probe */
1735 	sdattach,		/* attach */
1736 	sddetach,		/* detach */
1737 	nodev,			/* reset */
1738 	&sd_cb_ops,		/* driver operations */
1739 	NULL,			/* bus operations */
1740 	sdpower			/* power */
1741 };
1742 
1743 
1744 /*
1745  * This is the loadable module wrapper.
1746  */
1747 #include <sys/modctl.h>
1748 
1749 static struct modldrv modldrv = {
1750 	&mod_driverops,		/* Type of module. This one is a driver */
1751 	SD_MODULE_NAME,		/* Module name. */
1752 	&sd_ops			/* driver ops */
1753 };
1754 
1755 
1756 static struct modlinkage modlinkage = {
1757 	MODREV_1,
1758 	&modldrv,
1759 	NULL
1760 };
1761 
1762 
1763 static struct scsi_asq_key_strings sd_additional_codes[] = {
1764 	0x81, 0, "Logical Unit is Reserved",
1765 	0x85, 0, "Audio Address Not Valid",
1766 	0xb6, 0, "Media Load Mechanism Failed",
1767 	0xB9, 0, "Audio Play Operation Aborted",
1768 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1769 	0x53, 2, "Medium removal prevented",
1770 	0x6f, 0, "Authentication failed during key exchange",
1771 	0x6f, 1, "Key not present",
1772 	0x6f, 2, "Key not established",
1773 	0x6f, 3, "Read without proper authentication",
1774 	0x6f, 4, "Mismatched region to this logical unit",
1775 	0x6f, 5, "Region reset count error",
1776 	0xffff, 0x0, NULL
1777 };
1778 
1779 
1780 /*
1781  * Struct for passing printing information for sense data messages
1782  */
1783 struct sd_sense_info {
1784 	int	ssi_severity;
1785 	int	ssi_pfa_flag;
1786 };
1787 
1788 /*
1789  * Table of function pointers for iostart-side routines. Seperate "chains"
1790  * of layered function calls are formed by placing the function pointers
1791  * sequentially in the desired order. Functions are called according to an
1792  * incrementing table index ordering. The last function in each chain must
1793  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1794  * in the sd_iodone_chain[] array.
1795  *
1796  * Note: It may seem more natural to organize both the iostart and iodone
1797  * functions together, into an array of structures (or some similar
1798  * organization) with a common index, rather than two seperate arrays which
1799  * must be maintained in synchronization. The purpose of this division is
1800  * to achiece improved performance: individual arrays allows for more
1801  * effective cache line utilization on certain platforms.
1802  */
1803 
1804 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1805 
1806 
1807 static sd_chain_t sd_iostart_chain[] = {
1808 
1809 	/* Chain for buf IO for disk drive targets (PM enabled) */
1810 	sd_mapblockaddr_iostart,	/* Index: 0 */
1811 	sd_pm_iostart,			/* Index: 1 */
1812 	sd_core_iostart,		/* Index: 2 */
1813 
1814 	/* Chain for buf IO for disk drive targets (PM disabled) */
1815 	sd_mapblockaddr_iostart,	/* Index: 3 */
1816 	sd_core_iostart,		/* Index: 4 */
1817 
1818 	/* Chain for buf IO for removable-media targets (PM enabled) */
1819 	sd_mapblockaddr_iostart,	/* Index: 5 */
1820 	sd_mapblocksize_iostart,	/* Index: 6 */
1821 	sd_pm_iostart,			/* Index: 7 */
1822 	sd_core_iostart,		/* Index: 8 */
1823 
1824 	/* Chain for buf IO for removable-media targets (PM disabled) */
1825 	sd_mapblockaddr_iostart,	/* Index: 9 */
1826 	sd_mapblocksize_iostart,	/* Index: 10 */
1827 	sd_core_iostart,		/* Index: 11 */
1828 
1829 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1830 	sd_mapblockaddr_iostart,	/* Index: 12 */
1831 	sd_checksum_iostart,		/* Index: 13 */
1832 	sd_pm_iostart,			/* Index: 14 */
1833 	sd_core_iostart,		/* Index: 15 */
1834 
1835 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1836 	sd_mapblockaddr_iostart,	/* Index: 16 */
1837 	sd_checksum_iostart,		/* Index: 17 */
1838 	sd_core_iostart,		/* Index: 18 */
1839 
1840 	/* Chain for USCSI commands (all targets) */
1841 	sd_pm_iostart,			/* Index: 19 */
1842 	sd_core_iostart,		/* Index: 20 */
1843 
1844 	/* Chain for checksumming USCSI commands (all targets) */
1845 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1846 	sd_pm_iostart,			/* Index: 22 */
1847 	sd_core_iostart,		/* Index: 23 */
1848 
1849 	/* Chain for "direct" USCSI commands (all targets) */
1850 	sd_core_iostart,		/* Index: 24 */
1851 
1852 	/* Chain for "direct priority" USCSI commands (all targets) */
1853 	sd_core_iostart,		/* Index: 25 */
1854 };
1855 
1856 /*
1857  * Macros to locate the first function of each iostart chain in the
1858  * sd_iostart_chain[] array. These are located by the index in the array.
1859  */
1860 #define	SD_CHAIN_DISK_IOSTART			0
1861 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1862 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1863 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1864 #define	SD_CHAIN_CHKSUM_IOSTART			12
1865 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1866 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1867 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1868 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1869 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1870 
1871 
1872 /*
1873  * Table of function pointers for the iodone-side routines for the driver-
1874  * internal layering mechanism.  The calling sequence for iodone routines
1875  * uses a decrementing table index, so the last routine called in a chain
1876  * must be at the lowest array index location for that chain.  The last
1877  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1878  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1879  * of the functions in an iodone side chain must correspond to the ordering
1880  * of the iostart routines for that chain.  Note that there is no iodone
1881  * side routine that corresponds to sd_core_iostart(), so there is no
1882  * entry in the table for this.
1883  */
1884 
1885 static sd_chain_t sd_iodone_chain[] = {
1886 
1887 	/* Chain for buf IO for disk drive targets (PM enabled) */
1888 	sd_buf_iodone,			/* Index: 0 */
1889 	sd_mapblockaddr_iodone,		/* Index: 1 */
1890 	sd_pm_iodone,			/* Index: 2 */
1891 
1892 	/* Chain for buf IO for disk drive targets (PM disabled) */
1893 	sd_buf_iodone,			/* Index: 3 */
1894 	sd_mapblockaddr_iodone,		/* Index: 4 */
1895 
1896 	/* Chain for buf IO for removable-media targets (PM enabled) */
1897 	sd_buf_iodone,			/* Index: 5 */
1898 	sd_mapblockaddr_iodone,		/* Index: 6 */
1899 	sd_mapblocksize_iodone,		/* Index: 7 */
1900 	sd_pm_iodone,			/* Index: 8 */
1901 
1902 	/* Chain for buf IO for removable-media targets (PM disabled) */
1903 	sd_buf_iodone,			/* Index: 9 */
1904 	sd_mapblockaddr_iodone,		/* Index: 10 */
1905 	sd_mapblocksize_iodone,		/* Index: 11 */
1906 
1907 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1908 	sd_buf_iodone,			/* Index: 12 */
1909 	sd_mapblockaddr_iodone,		/* Index: 13 */
1910 	sd_checksum_iodone,		/* Index: 14 */
1911 	sd_pm_iodone,			/* Index: 15 */
1912 
1913 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1914 	sd_buf_iodone,			/* Index: 16 */
1915 	sd_mapblockaddr_iodone,		/* Index: 17 */
1916 	sd_checksum_iodone,		/* Index: 18 */
1917 
1918 	/* Chain for USCSI commands (non-checksum targets) */
1919 	sd_uscsi_iodone,		/* Index: 19 */
1920 	sd_pm_iodone,			/* Index: 20 */
1921 
1922 	/* Chain for USCSI commands (checksum targets) */
1923 	sd_uscsi_iodone,		/* Index: 21 */
1924 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1925 	sd_pm_iodone,			/* Index: 22 */
1926 
1927 	/* Chain for "direct" USCSI commands (all targets) */
1928 	sd_uscsi_iodone,		/* Index: 24 */
1929 
1930 	/* Chain for "direct priority" USCSI commands (all targets) */
1931 	sd_uscsi_iodone,		/* Index: 25 */
1932 };
1933 
1934 
1935 /*
1936  * Macros to locate the "first" function in the sd_iodone_chain[] array for
1937  * each iodone-side chain. These are located by the array index, but as the
1938  * iodone side functions are called in a decrementing-index order, the
1939  * highest index number in each chain must be specified (as these correspond
1940  * to the first function in the iodone chain that will be called by the core
1941  * at IO completion time).
1942  */
1943 
1944 #define	SD_CHAIN_DISK_IODONE			2
1945 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
1946 #define	SD_CHAIN_RMMEDIA_IODONE			8
1947 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
1948 #define	SD_CHAIN_CHKSUM_IODONE			15
1949 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
1950 #define	SD_CHAIN_USCSI_CMD_IODONE		20
1951 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
1952 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
1953 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
1954 
1955 
1956 
1957 
1958 /*
1959  * Array to map a layering chain index to the appropriate initpkt routine.
1960  * The redundant entries are present so that the index used for accessing
1961  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1962  * with this table as well.
1963  */
1964 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
1965 
1966 static sd_initpkt_t	sd_initpkt_map[] = {
1967 
1968 	/* Chain for buf IO for disk drive targets (PM enabled) */
1969 	sd_initpkt_for_buf,		/* Index: 0 */
1970 	sd_initpkt_for_buf,		/* Index: 1 */
1971 	sd_initpkt_for_buf,		/* Index: 2 */
1972 
1973 	/* Chain for buf IO for disk drive targets (PM disabled) */
1974 	sd_initpkt_for_buf,		/* Index: 3 */
1975 	sd_initpkt_for_buf,		/* Index: 4 */
1976 
1977 	/* Chain for buf IO for removable-media targets (PM enabled) */
1978 	sd_initpkt_for_buf,		/* Index: 5 */
1979 	sd_initpkt_for_buf,		/* Index: 6 */
1980 	sd_initpkt_for_buf,		/* Index: 7 */
1981 	sd_initpkt_for_buf,		/* Index: 8 */
1982 
1983 	/* Chain for buf IO for removable-media targets (PM disabled) */
1984 	sd_initpkt_for_buf,		/* Index: 9 */
1985 	sd_initpkt_for_buf,		/* Index: 10 */
1986 	sd_initpkt_for_buf,		/* Index: 11 */
1987 
1988 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1989 	sd_initpkt_for_buf,		/* Index: 12 */
1990 	sd_initpkt_for_buf,		/* Index: 13 */
1991 	sd_initpkt_for_buf,		/* Index: 14 */
1992 	sd_initpkt_for_buf,		/* Index: 15 */
1993 
1994 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1995 	sd_initpkt_for_buf,		/* Index: 16 */
1996 	sd_initpkt_for_buf,		/* Index: 17 */
1997 	sd_initpkt_for_buf,		/* Index: 18 */
1998 
1999 	/* Chain for USCSI commands (non-checksum targets) */
2000 	sd_initpkt_for_uscsi,		/* Index: 19 */
2001 	sd_initpkt_for_uscsi,		/* Index: 20 */
2002 
2003 	/* Chain for USCSI commands (checksum targets) */
2004 	sd_initpkt_for_uscsi,		/* Index: 21 */
2005 	sd_initpkt_for_uscsi,		/* Index: 22 */
2006 	sd_initpkt_for_uscsi,		/* Index: 22 */
2007 
2008 	/* Chain for "direct" USCSI commands (all targets) */
2009 	sd_initpkt_for_uscsi,		/* Index: 24 */
2010 
2011 	/* Chain for "direct priority" USCSI commands (all targets) */
2012 	sd_initpkt_for_uscsi,		/* Index: 25 */
2013 
2014 };
2015 
2016 
2017 /*
2018  * Array to map a layering chain index to the appropriate destroypktpkt routine.
2019  * The redundant entries are present so that the index used for accessing
2020  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2021  * with this table as well.
2022  */
2023 typedef void (*sd_destroypkt_t)(struct buf *);
2024 
2025 static sd_destroypkt_t	sd_destroypkt_map[] = {
2026 
2027 	/* Chain for buf IO for disk drive targets (PM enabled) */
2028 	sd_destroypkt_for_buf,		/* Index: 0 */
2029 	sd_destroypkt_for_buf,		/* Index: 1 */
2030 	sd_destroypkt_for_buf,		/* Index: 2 */
2031 
2032 	/* Chain for buf IO for disk drive targets (PM disabled) */
2033 	sd_destroypkt_for_buf,		/* Index: 3 */
2034 	sd_destroypkt_for_buf,		/* Index: 4 */
2035 
2036 	/* Chain for buf IO for removable-media targets (PM enabled) */
2037 	sd_destroypkt_for_buf,		/* Index: 5 */
2038 	sd_destroypkt_for_buf,		/* Index: 6 */
2039 	sd_destroypkt_for_buf,		/* Index: 7 */
2040 	sd_destroypkt_for_buf,		/* Index: 8 */
2041 
2042 	/* Chain for buf IO for removable-media targets (PM disabled) */
2043 	sd_destroypkt_for_buf,		/* Index: 9 */
2044 	sd_destroypkt_for_buf,		/* Index: 10 */
2045 	sd_destroypkt_for_buf,		/* Index: 11 */
2046 
2047 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2048 	sd_destroypkt_for_buf,		/* Index: 12 */
2049 	sd_destroypkt_for_buf,		/* Index: 13 */
2050 	sd_destroypkt_for_buf,		/* Index: 14 */
2051 	sd_destroypkt_for_buf,		/* Index: 15 */
2052 
2053 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2054 	sd_destroypkt_for_buf,		/* Index: 16 */
2055 	sd_destroypkt_for_buf,		/* Index: 17 */
2056 	sd_destroypkt_for_buf,		/* Index: 18 */
2057 
2058 	/* Chain for USCSI commands (non-checksum targets) */
2059 	sd_destroypkt_for_uscsi,	/* Index: 19 */
2060 	sd_destroypkt_for_uscsi,	/* Index: 20 */
2061 
2062 	/* Chain for USCSI commands (checksum targets) */
2063 	sd_destroypkt_for_uscsi,	/* Index: 21 */
2064 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2065 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2066 
2067 	/* Chain for "direct" USCSI commands (all targets) */
2068 	sd_destroypkt_for_uscsi,	/* Index: 24 */
2069 
2070 	/* Chain for "direct priority" USCSI commands (all targets) */
2071 	sd_destroypkt_for_uscsi,	/* Index: 25 */
2072 
2073 };
2074 
2075 
2076 
2077 /*
2078  * Array to map a layering chain index to the appropriate chain "type".
2079  * The chain type indicates a specific property/usage of the chain.
2080  * The redundant entries are present so that the index used for accessing
2081  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2082  * with this table as well.
2083  */
2084 
2085 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2086 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2087 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2088 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2089 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2090 						/* (for error recovery) */
2091 
2092 static int sd_chain_type_map[] = {
2093 
2094 	/* Chain for buf IO for disk drive targets (PM enabled) */
2095 	SD_CHAIN_BUFIO,			/* Index: 0 */
2096 	SD_CHAIN_BUFIO,			/* Index: 1 */
2097 	SD_CHAIN_BUFIO,			/* Index: 2 */
2098 
2099 	/* Chain for buf IO for disk drive targets (PM disabled) */
2100 	SD_CHAIN_BUFIO,			/* Index: 3 */
2101 	SD_CHAIN_BUFIO,			/* Index: 4 */
2102 
2103 	/* Chain for buf IO for removable-media targets (PM enabled) */
2104 	SD_CHAIN_BUFIO,			/* Index: 5 */
2105 	SD_CHAIN_BUFIO,			/* Index: 6 */
2106 	SD_CHAIN_BUFIO,			/* Index: 7 */
2107 	SD_CHAIN_BUFIO,			/* Index: 8 */
2108 
2109 	/* Chain for buf IO for removable-media targets (PM disabled) */
2110 	SD_CHAIN_BUFIO,			/* Index: 9 */
2111 	SD_CHAIN_BUFIO,			/* Index: 10 */
2112 	SD_CHAIN_BUFIO,			/* Index: 11 */
2113 
2114 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2115 	SD_CHAIN_BUFIO,			/* Index: 12 */
2116 	SD_CHAIN_BUFIO,			/* Index: 13 */
2117 	SD_CHAIN_BUFIO,			/* Index: 14 */
2118 	SD_CHAIN_BUFIO,			/* Index: 15 */
2119 
2120 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2121 	SD_CHAIN_BUFIO,			/* Index: 16 */
2122 	SD_CHAIN_BUFIO,			/* Index: 17 */
2123 	SD_CHAIN_BUFIO,			/* Index: 18 */
2124 
2125 	/* Chain for USCSI commands (non-checksum targets) */
2126 	SD_CHAIN_USCSI,			/* Index: 19 */
2127 	SD_CHAIN_USCSI,			/* Index: 20 */
2128 
2129 	/* Chain for USCSI commands (checksum targets) */
2130 	SD_CHAIN_USCSI,			/* Index: 21 */
2131 	SD_CHAIN_USCSI,			/* Index: 22 */
2132 	SD_CHAIN_USCSI,			/* Index: 22 */
2133 
2134 	/* Chain for "direct" USCSI commands (all targets) */
2135 	SD_CHAIN_DIRECT,		/* Index: 24 */
2136 
2137 	/* Chain for "direct priority" USCSI commands (all targets) */
2138 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2139 };
2140 
2141 
2142 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2143 #define	SD_IS_BUFIO(xp)			\
2144 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2145 
2146 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2147 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2148 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2149 
2150 
2151 
2152 /*
2153  * Struct, array, and macros to map a specific chain to the appropriate
2154  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2155  *
2156  * The sd_chain_index_map[] array is used at attach time to set the various
2157  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2158  * chain to be used with the instance. This allows different instances to use
2159  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2160  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2161  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2162  * dynamically & without the use of locking; and (2) a layer may update the
2163  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2164  * to allow for deferred processing of an IO within the same chain from a
2165  * different execution context.
2166  */
2167 
2168 struct sd_chain_index {
2169 	int	sci_iostart_index;
2170 	int	sci_iodone_index;
2171 };
2172 
2173 static struct sd_chain_index	sd_chain_index_map[] = {
2174 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2175 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2176 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2177 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2178 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2179 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2180 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2181 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2182 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2183 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2184 };
2185 
2186 
2187 /*
2188  * The following are indexes into the sd_chain_index_map[] array.
2189  */
2190 
2191 /* un->un_buf_chain_type must be set to one of these */
2192 #define	SD_CHAIN_INFO_DISK		0
2193 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2194 #define	SD_CHAIN_INFO_RMMEDIA		2
2195 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2196 #define	SD_CHAIN_INFO_CHKSUM		4
2197 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2198 
2199 /* un->un_uscsi_chain_type must be set to one of these */
2200 #define	SD_CHAIN_INFO_USCSI_CMD		6
2201 /* USCSI with PM disabled is the same as DIRECT */
2202 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2203 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2204 
2205 /* un->un_direct_chain_type must be set to one of these */
2206 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2207 
2208 /* un->un_priority_chain_type must be set to one of these */
2209 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2210 
2211 /* size for devid inquiries */
2212 #define	MAX_INQUIRY_SIZE		0xF0
2213 
2214 /*
2215  * Macros used by functions to pass a given buf(9S) struct along to the
2216  * next function in the layering chain for further processing.
2217  *
2218  * In the following macros, passing more than three arguments to the called
2219  * routines causes the optimizer for the SPARC compiler to stop doing tail
2220  * call elimination which results in significant performance degradation.
2221  */
2222 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2223 	((*(sd_iostart_chain[index]))(index, un, bp))
2224 
2225 #define	SD_BEGIN_IODONE(index, un, bp)	\
2226 	((*(sd_iodone_chain[index]))(index, un, bp))
2227 
2228 #define	SD_NEXT_IOSTART(index, un, bp)				\
2229 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2230 
2231 #define	SD_NEXT_IODONE(index, un, bp)				\
2232 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2233 
2234 /*
2235  *    Function: _init
2236  *
2237  * Description: This is the driver _init(9E) entry point.
2238  *
2239  * Return Code: Returns the value from mod_install(9F) or
2240  *		ddi_soft_state_init(9F) as appropriate.
2241  *
2242  *     Context: Called when driver module loaded.
2243  */
2244 
2245 int
2246 _init(void)
2247 {
2248 	int	err;
2249 
2250 	/* establish driver name from module name */
2251 	sd_label = mod_modname(&modlinkage);
2252 
2253 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2254 		SD_MAXUNIT);
2255 
2256 	if (err != 0) {
2257 		return (err);
2258 	}
2259 
2260 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2261 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2262 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2263 
2264 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2265 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2266 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2267 
2268 	/*
2269 	 * it's ok to init here even for fibre device
2270 	 */
2271 	sd_scsi_probe_cache_init();
2272 
2273 	sd_scsi_target_lun_init();
2274 
2275 	/*
2276 	 * Creating taskq before mod_install ensures that all callers (threads)
2277 	 * that enter the module after a successfull mod_install encounter
2278 	 * a valid taskq.
2279 	 */
2280 	sd_taskq_create();
2281 
2282 	err = mod_install(&modlinkage);
2283 	if (err != 0) {
2284 		/* delete taskq if install fails */
2285 		sd_taskq_delete();
2286 
2287 		mutex_destroy(&sd_detach_mutex);
2288 		mutex_destroy(&sd_log_mutex);
2289 		mutex_destroy(&sd_label_mutex);
2290 
2291 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2292 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2293 		cv_destroy(&sd_tr.srq_inprocess_cv);
2294 
2295 		sd_scsi_probe_cache_fini();
2296 
2297 		sd_scsi_target_lun_fini();
2298 
2299 		ddi_soft_state_fini(&sd_state);
2300 		return (err);
2301 	}
2302 
2303 	return (err);
2304 }
2305 
2306 
2307 /*
2308  *    Function: _fini
2309  *
2310  * Description: This is the driver _fini(9E) entry point.
2311  *
2312  * Return Code: Returns the value from mod_remove(9F)
2313  *
2314  *     Context: Called when driver module is unloaded.
2315  */
2316 
2317 int
2318 _fini(void)
2319 {
2320 	int err;
2321 
2322 	if ((err = mod_remove(&modlinkage)) != 0) {
2323 		return (err);
2324 	}
2325 
2326 	sd_taskq_delete();
2327 
2328 	mutex_destroy(&sd_detach_mutex);
2329 	mutex_destroy(&sd_log_mutex);
2330 	mutex_destroy(&sd_label_mutex);
2331 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2332 
2333 	sd_scsi_probe_cache_fini();
2334 
2335 	sd_scsi_target_lun_fini();
2336 
2337 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2338 	cv_destroy(&sd_tr.srq_inprocess_cv);
2339 
2340 	ddi_soft_state_fini(&sd_state);
2341 
2342 	return (err);
2343 }
2344 
2345 
2346 /*
2347  *    Function: _info
2348  *
2349  * Description: This is the driver _info(9E) entry point.
2350  *
2351  *   Arguments: modinfop - pointer to the driver modinfo structure
2352  *
2353  * Return Code: Returns the value from mod_info(9F).
2354  *
2355  *     Context: Kernel thread context
2356  */
2357 
2358 int
2359 _info(struct modinfo *modinfop)
2360 {
2361 	return (mod_info(&modlinkage, modinfop));
2362 }
2363 
2364 
2365 /*
2366  * The following routines implement the driver message logging facility.
2367  * They provide component- and level- based debug output filtering.
2368  * Output may also be restricted to messages for a single instance by
2369  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2370  * to NULL, then messages for all instances are printed.
2371  *
2372  * These routines have been cloned from each other due to the language
2373  * constraints of macros and variable argument list processing.
2374  */
2375 
2376 
2377 /*
2378  *    Function: sd_log_err
2379  *
2380  * Description: This routine is called by the SD_ERROR macro for debug
2381  *		logging of error conditions.
2382  *
2383  *   Arguments: comp - driver component being logged
2384  *		dev  - pointer to driver info structure
2385  *		fmt  - error string and format to be logged
2386  */
2387 
2388 static void
2389 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2390 {
2391 	va_list		ap;
2392 	dev_info_t	*dev;
2393 
2394 	ASSERT(un != NULL);
2395 	dev = SD_DEVINFO(un);
2396 	ASSERT(dev != NULL);
2397 
2398 	/*
2399 	 * Filter messages based on the global component and level masks.
2400 	 * Also print if un matches the value of sd_debug_un, or if
2401 	 * sd_debug_un is set to NULL.
2402 	 */
2403 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2404 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2405 		mutex_enter(&sd_log_mutex);
2406 		va_start(ap, fmt);
2407 		(void) vsprintf(sd_log_buf, fmt, ap);
2408 		va_end(ap);
2409 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2410 		mutex_exit(&sd_log_mutex);
2411 	}
2412 #ifdef SD_FAULT_INJECTION
2413 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2414 	if (un->sd_injection_mask & comp) {
2415 		mutex_enter(&sd_log_mutex);
2416 		va_start(ap, fmt);
2417 		(void) vsprintf(sd_log_buf, fmt, ap);
2418 		va_end(ap);
2419 		sd_injection_log(sd_log_buf, un);
2420 		mutex_exit(&sd_log_mutex);
2421 	}
2422 #endif
2423 }
2424 
2425 
2426 /*
2427  *    Function: sd_log_info
2428  *
2429  * Description: This routine is called by the SD_INFO macro for debug
2430  *		logging of general purpose informational conditions.
2431  *
2432  *   Arguments: comp - driver component being logged
2433  *		dev  - pointer to driver info structure
2434  *		fmt  - info string and format to be logged
2435  */
2436 
2437 static void
2438 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2439 {
2440 	va_list		ap;
2441 	dev_info_t	*dev;
2442 
2443 	ASSERT(un != NULL);
2444 	dev = SD_DEVINFO(un);
2445 	ASSERT(dev != NULL);
2446 
2447 	/*
2448 	 * Filter messages based on the global component and level masks.
2449 	 * Also print if un matches the value of sd_debug_un, or if
2450 	 * sd_debug_un is set to NULL.
2451 	 */
2452 	if ((sd_component_mask & component) &&
2453 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2454 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2455 		mutex_enter(&sd_log_mutex);
2456 		va_start(ap, fmt);
2457 		(void) vsprintf(sd_log_buf, fmt, ap);
2458 		va_end(ap);
2459 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2460 		mutex_exit(&sd_log_mutex);
2461 	}
2462 #ifdef SD_FAULT_INJECTION
2463 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2464 	if (un->sd_injection_mask & component) {
2465 		mutex_enter(&sd_log_mutex);
2466 		va_start(ap, fmt);
2467 		(void) vsprintf(sd_log_buf, fmt, ap);
2468 		va_end(ap);
2469 		sd_injection_log(sd_log_buf, un);
2470 		mutex_exit(&sd_log_mutex);
2471 	}
2472 #endif
2473 }
2474 
2475 
2476 /*
2477  *    Function: sd_log_trace
2478  *
2479  * Description: This routine is called by the SD_TRACE macro for debug
2480  *		logging of trace conditions (i.e. function entry/exit).
2481  *
2482  *   Arguments: comp - driver component being logged
2483  *		dev  - pointer to driver info structure
2484  *		fmt  - trace string and format to be logged
2485  */
2486 
2487 static void
2488 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2489 {
2490 	va_list		ap;
2491 	dev_info_t	*dev;
2492 
2493 	ASSERT(un != NULL);
2494 	dev = SD_DEVINFO(un);
2495 	ASSERT(dev != NULL);
2496 
2497 	/*
2498 	 * Filter messages based on the global component and level masks.
2499 	 * Also print if un matches the value of sd_debug_un, or if
2500 	 * sd_debug_un is set to NULL.
2501 	 */
2502 	if ((sd_component_mask & component) &&
2503 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2504 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2505 		mutex_enter(&sd_log_mutex);
2506 		va_start(ap, fmt);
2507 		(void) vsprintf(sd_log_buf, fmt, ap);
2508 		va_end(ap);
2509 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2510 		mutex_exit(&sd_log_mutex);
2511 	}
2512 #ifdef SD_FAULT_INJECTION
2513 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2514 	if (un->sd_injection_mask & component) {
2515 		mutex_enter(&sd_log_mutex);
2516 		va_start(ap, fmt);
2517 		(void) vsprintf(sd_log_buf, fmt, ap);
2518 		va_end(ap);
2519 		sd_injection_log(sd_log_buf, un);
2520 		mutex_exit(&sd_log_mutex);
2521 	}
2522 #endif
2523 }
2524 
2525 
2526 /*
2527  *    Function: sdprobe
2528  *
2529  * Description: This is the driver probe(9e) entry point function.
2530  *
2531  *   Arguments: devi - opaque device info handle
2532  *
2533  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2534  *              DDI_PROBE_FAILURE: If the probe failed.
2535  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2536  *				   but may be present in the future.
2537  */
2538 
2539 static int
2540 sdprobe(dev_info_t *devi)
2541 {
2542 	struct scsi_device	*devp;
2543 	int			rval;
2544 	int			instance;
2545 
2546 	/*
2547 	 * if it wasn't for pln, sdprobe could actually be nulldev
2548 	 * in the "__fibre" case.
2549 	 */
2550 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2551 		return (DDI_PROBE_DONTCARE);
2552 	}
2553 
2554 	devp = ddi_get_driver_private(devi);
2555 
2556 	if (devp == NULL) {
2557 		/* Ooops... nexus driver is mis-configured... */
2558 		return (DDI_PROBE_FAILURE);
2559 	}
2560 
2561 	instance = ddi_get_instance(devi);
2562 
2563 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2564 		return (DDI_PROBE_PARTIAL);
2565 	}
2566 
2567 	/*
2568 	 * Call the SCSA utility probe routine to see if we actually
2569 	 * have a target at this SCSI nexus.
2570 	 */
2571 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2572 	case SCSIPROBE_EXISTS:
2573 		switch (devp->sd_inq->inq_dtype) {
2574 		case DTYPE_DIRECT:
2575 			rval = DDI_PROBE_SUCCESS;
2576 			break;
2577 		case DTYPE_RODIRECT:
2578 			/* CDs etc. Can be removable media */
2579 			rval = DDI_PROBE_SUCCESS;
2580 			break;
2581 		case DTYPE_OPTICAL:
2582 			/*
2583 			 * Rewritable optical driver HP115AA
2584 			 * Can also be removable media
2585 			 */
2586 
2587 			/*
2588 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2589 			 * pre solaris 9 sparc sd behavior is required
2590 			 *
2591 			 * If first time through and sd_dtype_optical_bind
2592 			 * has not been set in /etc/system check properties
2593 			 */
2594 
2595 			if (sd_dtype_optical_bind  < 0) {
2596 			    sd_dtype_optical_bind = ddi_prop_get_int
2597 				(DDI_DEV_T_ANY,	devi,	0,
2598 				"optical-device-bind",	1);
2599 			}
2600 
2601 			if (sd_dtype_optical_bind == 0) {
2602 				rval = DDI_PROBE_FAILURE;
2603 			} else {
2604 				rval = DDI_PROBE_SUCCESS;
2605 			}
2606 			break;
2607 
2608 		case DTYPE_NOTPRESENT:
2609 		default:
2610 			rval = DDI_PROBE_FAILURE;
2611 			break;
2612 		}
2613 		break;
2614 	default:
2615 		rval = DDI_PROBE_PARTIAL;
2616 		break;
2617 	}
2618 
2619 	/*
2620 	 * This routine checks for resource allocation prior to freeing,
2621 	 * so it will take care of the "smart probing" case where a
2622 	 * scsi_probe() may or may not have been issued and will *not*
2623 	 * free previously-freed resources.
2624 	 */
2625 	scsi_unprobe(devp);
2626 	return (rval);
2627 }
2628 
2629 
2630 /*
2631  *    Function: sdinfo
2632  *
2633  * Description: This is the driver getinfo(9e) entry point function.
2634  * 		Given the device number, return the devinfo pointer from
2635  *		the scsi_device structure or the instance number
2636  *		associated with the dev_t.
2637  *
2638  *   Arguments: dip     - pointer to device info structure
2639  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2640  *			  DDI_INFO_DEVT2INSTANCE)
2641  *		arg     - driver dev_t
2642  *		resultp - user buffer for request response
2643  *
2644  * Return Code: DDI_SUCCESS
2645  *              DDI_FAILURE
2646  */
2647 /* ARGSUSED */
2648 static int
2649 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2650 {
2651 	struct sd_lun	*un;
2652 	dev_t		dev;
2653 	int		instance;
2654 	int		error;
2655 
2656 	switch (infocmd) {
2657 	case DDI_INFO_DEVT2DEVINFO:
2658 		dev = (dev_t)arg;
2659 		instance = SDUNIT(dev);
2660 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2661 			return (DDI_FAILURE);
2662 		}
2663 		*result = (void *) SD_DEVINFO(un);
2664 		error = DDI_SUCCESS;
2665 		break;
2666 	case DDI_INFO_DEVT2INSTANCE:
2667 		dev = (dev_t)arg;
2668 		instance = SDUNIT(dev);
2669 		*result = (void *)(uintptr_t)instance;
2670 		error = DDI_SUCCESS;
2671 		break;
2672 	default:
2673 		error = DDI_FAILURE;
2674 	}
2675 	return (error);
2676 }
2677 
2678 /*
2679  *    Function: sd_prop_op
2680  *
2681  * Description: This is the driver prop_op(9e) entry point function.
2682  *		Return the number of blocks for the partition in question
2683  *		or forward the request to the property facilities.
2684  *
2685  *   Arguments: dev       - device number
2686  *		dip       - pointer to device info structure
2687  *		prop_op   - property operator
2688  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2689  *		name      - pointer to property name
2690  *		valuep    - pointer or address of the user buffer
2691  *		lengthp   - property length
2692  *
2693  * Return Code: DDI_PROP_SUCCESS
2694  *              DDI_PROP_NOT_FOUND
2695  *              DDI_PROP_UNDEFINED
2696  *              DDI_PROP_NO_MEMORY
2697  *              DDI_PROP_BUF_TOO_SMALL
2698  */
2699 
2700 static int
2701 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2702 	char *name, caddr_t valuep, int *lengthp)
2703 {
2704 	int		instance = ddi_get_instance(dip);
2705 	struct sd_lun	*un;
2706 	uint64_t	nblocks64;
2707 
2708 	/*
2709 	 * Our dynamic properties are all device specific and size oriented.
2710 	 * Requests issued under conditions where size is valid are passed
2711 	 * to ddi_prop_op_nblocks with the size information, otherwise the
2712 	 * request is passed to ddi_prop_op. Size depends on valid geometry.
2713 	 */
2714 	un = ddi_get_soft_state(sd_state, instance);
2715 	if ((dev == DDI_DEV_T_ANY) || (un == NULL) ||
2716 	    (un->un_f_geometry_is_valid == FALSE)) {
2717 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2718 		    name, valuep, lengthp));
2719 	} else {
2720 		/* get nblocks value */
2721 		ASSERT(!mutex_owned(SD_MUTEX(un)));
2722 		mutex_enter(SD_MUTEX(un));
2723 		nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk;
2724 		mutex_exit(SD_MUTEX(un));
2725 
2726 		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
2727 		    name, valuep, lengthp, nblocks64));
2728 	}
2729 }
2730 
2731 /*
2732  * The following functions are for smart probing:
2733  * sd_scsi_probe_cache_init()
2734  * sd_scsi_probe_cache_fini()
2735  * sd_scsi_clear_probe_cache()
2736  * sd_scsi_probe_with_cache()
2737  */
2738 
2739 /*
2740  *    Function: sd_scsi_probe_cache_init
2741  *
2742  * Description: Initializes the probe response cache mutex and head pointer.
2743  *
2744  *     Context: Kernel thread context
2745  */
2746 
2747 static void
2748 sd_scsi_probe_cache_init(void)
2749 {
2750 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2751 	sd_scsi_probe_cache_head = NULL;
2752 }
2753 
2754 
2755 /*
2756  *    Function: sd_scsi_probe_cache_fini
2757  *
2758  * Description: Frees all resources associated with the probe response cache.
2759  *
2760  *     Context: Kernel thread context
2761  */
2762 
2763 static void
2764 sd_scsi_probe_cache_fini(void)
2765 {
2766 	struct sd_scsi_probe_cache *cp;
2767 	struct sd_scsi_probe_cache *ncp;
2768 
2769 	/* Clean up our smart probing linked list */
2770 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2771 		ncp = cp->next;
2772 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2773 	}
2774 	sd_scsi_probe_cache_head = NULL;
2775 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2776 }
2777 
2778 
2779 /*
2780  *    Function: sd_scsi_clear_probe_cache
2781  *
2782  * Description: This routine clears the probe response cache. This is
2783  *		done when open() returns ENXIO so that when deferred
2784  *		attach is attempted (possibly after a device has been
2785  *		turned on) we will retry the probe. Since we don't know
2786  *		which target we failed to open, we just clear the
2787  *		entire cache.
2788  *
2789  *     Context: Kernel thread context
2790  */
2791 
2792 static void
2793 sd_scsi_clear_probe_cache(void)
2794 {
2795 	struct sd_scsi_probe_cache	*cp;
2796 	int				i;
2797 
2798 	mutex_enter(&sd_scsi_probe_cache_mutex);
2799 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2800 		/*
2801 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2802 		 * force probing to be performed the next time
2803 		 * sd_scsi_probe_with_cache is called.
2804 		 */
2805 		for (i = 0; i < NTARGETS_WIDE; i++) {
2806 			cp->cache[i] = SCSIPROBE_EXISTS;
2807 		}
2808 	}
2809 	mutex_exit(&sd_scsi_probe_cache_mutex);
2810 }
2811 
2812 
2813 /*
2814  *    Function: sd_scsi_probe_with_cache
2815  *
2816  * Description: This routine implements support for a scsi device probe
2817  *		with cache. The driver maintains a cache of the target
2818  *		responses to scsi probes. If we get no response from a
2819  *		target during a probe inquiry, we remember that, and we
2820  *		avoid additional calls to scsi_probe on non-zero LUNs
2821  *		on the same target until the cache is cleared. By doing
2822  *		so we avoid the 1/4 sec selection timeout for nonzero
2823  *		LUNs. lun0 of a target is always probed.
2824  *
2825  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2826  *              waitfunc - indicates what the allocator routines should
2827  *			   do when resources are not available. This value
2828  *			   is passed on to scsi_probe() when that routine
2829  *			   is called.
2830  *
2831  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2832  *		otherwise the value returned by scsi_probe(9F).
2833  *
2834  *     Context: Kernel thread context
2835  */
2836 
2837 static int
2838 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2839 {
2840 	struct sd_scsi_probe_cache	*cp;
2841 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
2842 	int		lun, tgt;
2843 
2844 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2845 	    SCSI_ADDR_PROP_LUN, 0);
2846 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2847 	    SCSI_ADDR_PROP_TARGET, -1);
2848 
2849 	/* Make sure caching enabled and target in range */
2850 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
2851 		/* do it the old way (no cache) */
2852 		return (scsi_probe(devp, waitfn));
2853 	}
2854 
2855 	mutex_enter(&sd_scsi_probe_cache_mutex);
2856 
2857 	/* Find the cache for this scsi bus instance */
2858 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2859 		if (cp->pdip == pdip) {
2860 			break;
2861 		}
2862 	}
2863 
2864 	/* If we can't find a cache for this pdip, create one */
2865 	if (cp == NULL) {
2866 		int i;
2867 
2868 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
2869 		    KM_SLEEP);
2870 		cp->pdip = pdip;
2871 		cp->next = sd_scsi_probe_cache_head;
2872 		sd_scsi_probe_cache_head = cp;
2873 		for (i = 0; i < NTARGETS_WIDE; i++) {
2874 			cp->cache[i] = SCSIPROBE_EXISTS;
2875 		}
2876 	}
2877 
2878 	mutex_exit(&sd_scsi_probe_cache_mutex);
2879 
2880 	/* Recompute the cache for this target if LUN zero */
2881 	if (lun == 0) {
2882 		cp->cache[tgt] = SCSIPROBE_EXISTS;
2883 	}
2884 
2885 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
2886 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
2887 		return (SCSIPROBE_NORESP);
2888 	}
2889 
2890 	/* Do the actual probe; save & return the result */
2891 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
2892 }
2893 
2894 
2895 /*
2896  *    Function: sd_scsi_target_lun_init
2897  *
2898  * Description: Initializes the attached lun chain mutex and head pointer.
2899  *
2900  *     Context: Kernel thread context
2901  */
2902 
2903 static void
2904 sd_scsi_target_lun_init(void)
2905 {
2906 	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
2907 	sd_scsi_target_lun_head = NULL;
2908 }
2909 
2910 
2911 /*
2912  *    Function: sd_scsi_target_lun_fini
2913  *
2914  * Description: Frees all resources associated with the attached lun
2915  *              chain
2916  *
2917  *     Context: Kernel thread context
2918  */
2919 
2920 static void
2921 sd_scsi_target_lun_fini(void)
2922 {
2923 	struct sd_scsi_hba_tgt_lun	*cp;
2924 	struct sd_scsi_hba_tgt_lun	*ncp;
2925 
2926 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
2927 		ncp = cp->next;
2928 		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
2929 	}
2930 	sd_scsi_target_lun_head = NULL;
2931 	mutex_destroy(&sd_scsi_target_lun_mutex);
2932 }
2933 
2934 
2935 /*
2936  *    Function: sd_scsi_get_target_lun_count
2937  *
2938  * Description: This routine will check in the attached lun chain to see
2939  * 		how many luns are attached on the required SCSI controller
2940  * 		and target. Currently, some capabilities like tagged queue
2941  *		are supported per target based by HBA. So all luns in a
2942  *		target have the same capabilities. Based on this assumption,
2943  * 		sd should only set these capabilities once per target. This
2944  *		function is called when sd needs to decide how many luns
2945  *		already attached on a target.
2946  *
2947  *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
2948  *			  controller device.
2949  *              target	- The target ID on the controller's SCSI bus.
2950  *
2951  * Return Code: The number of luns attached on the required target and
2952  *		controller.
2953  *		-1 if target ID is not in parallel SCSI scope or the given
2954  * 		dip is not in the chain.
2955  *
2956  *     Context: Kernel thread context
2957  */
2958 
2959 static int
2960 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
2961 {
2962 	struct sd_scsi_hba_tgt_lun	*cp;
2963 
2964 	if ((target < 0) || (target >= NTARGETS_WIDE)) {
2965 		return (-1);
2966 	}
2967 
2968 	mutex_enter(&sd_scsi_target_lun_mutex);
2969 
2970 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
2971 		if (cp->pdip == dip) {
2972 			break;
2973 		}
2974 	}
2975 
2976 	mutex_exit(&sd_scsi_target_lun_mutex);
2977 
2978 	if (cp == NULL) {
2979 		return (-1);
2980 	}
2981 
2982 	return (cp->nlun[target]);
2983 }
2984 
2985 
2986 /*
2987  *    Function: sd_scsi_update_lun_on_target
2988  *
2989  * Description: This routine is used to update the attached lun chain when a
2990  *		lun is attached or detached on a target.
2991  *
2992  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
2993  *                        controller device.
2994  *              target  - The target ID on the controller's SCSI bus.
2995  *		flag	- Indicate the lun is attached or detached.
2996  *
2997  *     Context: Kernel thread context
2998  */
2999 
3000 static void
3001 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3002 {
3003 	struct sd_scsi_hba_tgt_lun	*cp;
3004 
3005 	mutex_enter(&sd_scsi_target_lun_mutex);
3006 
3007 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3008 		if (cp->pdip == dip) {
3009 			break;
3010 		}
3011 	}
3012 
3013 	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3014 		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3015 		    KM_SLEEP);
3016 		cp->pdip = dip;
3017 		cp->next = sd_scsi_target_lun_head;
3018 		sd_scsi_target_lun_head = cp;
3019 	}
3020 
3021 	mutex_exit(&sd_scsi_target_lun_mutex);
3022 
3023 	if (cp != NULL) {
3024 		if (flag == SD_SCSI_LUN_ATTACH) {
3025 			cp->nlun[target] ++;
3026 		} else {
3027 			cp->nlun[target] --;
3028 		}
3029 	}
3030 }
3031 
3032 
3033 /*
3034  *    Function: sd_spin_up_unit
3035  *
3036  * Description: Issues the following commands to spin-up the device:
3037  *		START STOP UNIT, and INQUIRY.
3038  *
3039  *   Arguments: un - driver soft state (unit) structure
3040  *
3041  * Return Code: 0 - success
3042  *		EIO - failure
3043  *		EACCES - reservation conflict
3044  *
3045  *     Context: Kernel thread context
3046  */
3047 
3048 static int
3049 sd_spin_up_unit(struct sd_lun *un)
3050 {
3051 	size_t	resid		= 0;
3052 	int	has_conflict	= FALSE;
3053 	uchar_t *bufaddr;
3054 
3055 	ASSERT(un != NULL);
3056 
3057 	/*
3058 	 * Send a throwaway START UNIT command.
3059 	 *
3060 	 * If we fail on this, we don't care presently what precisely
3061 	 * is wrong.  EMC's arrays will also fail this with a check
3062 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
3063 	 * we don't want to fail the attach because it may become
3064 	 * "active" later.
3065 	 */
3066 	if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT)
3067 	    == EACCES)
3068 		has_conflict = TRUE;
3069 
3070 	/*
3071 	 * Send another INQUIRY command to the target. This is necessary for
3072 	 * non-removable media direct access devices because their INQUIRY data
3073 	 * may not be fully qualified until they are spun up (perhaps via the
3074 	 * START command above).  Note: This seems to be needed for some
3075 	 * legacy devices only.) The INQUIRY command should succeed even if a
3076 	 * Reservation Conflict is present.
3077 	 */
3078 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3079 	if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) {
3080 		kmem_free(bufaddr, SUN_INQSIZE);
3081 		return (EIO);
3082 	}
3083 
3084 	/*
3085 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3086 	 * Note that this routine does not return a failure here even if the
3087 	 * INQUIRY command did not return any data.  This is a legacy behavior.
3088 	 */
3089 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3090 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3091 	}
3092 
3093 	kmem_free(bufaddr, SUN_INQSIZE);
3094 
3095 	/* If we hit a reservation conflict above, tell the caller. */
3096 	if (has_conflict == TRUE) {
3097 		return (EACCES);
3098 	}
3099 
3100 	return (0);
3101 }
3102 
3103 #ifdef _LP64
3104 /*
3105  *    Function: sd_enable_descr_sense
3106  *
3107  * Description: This routine attempts to select descriptor sense format
3108  *		using the Control mode page.  Devices that support 64 bit
3109  *		LBAs (for >2TB luns) should also implement descriptor
3110  *		sense data so we will call this function whenever we see
3111  *		a lun larger than 2TB.  If for some reason the device
3112  *		supports 64 bit LBAs but doesn't support descriptor sense
3113  *		presumably the mode select will fail.  Everything will
3114  *		continue to work normally except that we will not get
3115  *		complete sense data for commands that fail with an LBA
3116  *		larger than 32 bits.
3117  *
3118  *   Arguments: un - driver soft state (unit) structure
3119  *
3120  *     Context: Kernel thread context only
3121  */
3122 
3123 static void
3124 sd_enable_descr_sense(struct sd_lun *un)
3125 {
3126 	uchar_t			*header;
3127 	struct mode_control_scsi3 *ctrl_bufp;
3128 	size_t			buflen;
3129 	size_t			bd_len;
3130 
3131 	/*
3132 	 * Read MODE SENSE page 0xA, Control Mode Page
3133 	 */
3134 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3135 	    sizeof (struct mode_control_scsi3);
3136 	header = kmem_zalloc(buflen, KM_SLEEP);
3137 	if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
3138 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) {
3139 		SD_ERROR(SD_LOG_COMMON, un,
3140 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
3141 		goto eds_exit;
3142 	}
3143 
3144 	/*
3145 	 * Determine size of Block Descriptors in order to locate
3146 	 * the mode page data. ATAPI devices return 0, SCSI devices
3147 	 * should return MODE_BLK_DESC_LENGTH.
3148 	 */
3149 	bd_len  = ((struct mode_header *)header)->bdesc_length;
3150 
3151 	ctrl_bufp = (struct mode_control_scsi3 *)
3152 	    (header + MODE_HEADER_LENGTH + bd_len);
3153 
3154 	/*
3155 	 * Clear PS bit for MODE SELECT
3156 	 */
3157 	ctrl_bufp->mode_page.ps = 0;
3158 
3159 	/*
3160 	 * Set D_SENSE to enable descriptor sense format.
3161 	 */
3162 	ctrl_bufp->d_sense = 1;
3163 
3164 	/*
3165 	 * Use MODE SELECT to commit the change to the D_SENSE bit
3166 	 */
3167 	if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
3168 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) {
3169 		SD_INFO(SD_LOG_COMMON, un,
3170 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
3171 		goto eds_exit;
3172 	}
3173 
3174 eds_exit:
3175 	kmem_free(header, buflen);
3176 }
3177 
3178 /*
3179  *    Function: sd_reenable_dsense_task
3180  *
3181  * Description: Re-enable descriptor sense after device or bus reset
3182  *
3183  *     Context: Executes in a taskq() thread context
3184  */
3185 static void
3186 sd_reenable_dsense_task(void *arg)
3187 {
3188 	struct	sd_lun	*un = arg;
3189 
3190 	ASSERT(un != NULL);
3191 	sd_enable_descr_sense(un);
3192 }
3193 #endif /* _LP64 */
3194 
3195 /*
3196  *    Function: sd_set_mmc_caps
3197  *
3198  * Description: This routine determines if the device is MMC compliant and if
3199  *		the device supports CDDA via a mode sense of the CDVD
3200  *		capabilities mode page. Also checks if the device is a
3201  *		dvdram writable device.
3202  *
3203  *   Arguments: un - driver soft state (unit) structure
3204  *
3205  *     Context: Kernel thread context only
3206  */
3207 
3208 static void
3209 sd_set_mmc_caps(struct sd_lun *un)
3210 {
3211 	struct mode_header_grp2		*sense_mhp;
3212 	uchar_t				*sense_page;
3213 	caddr_t				buf;
3214 	int				bd_len;
3215 	int				status;
3216 	struct uscsi_cmd		com;
3217 	int				rtn;
3218 	uchar_t				*out_data_rw, *out_data_hd;
3219 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3220 
3221 	ASSERT(un != NULL);
3222 
3223 	/*
3224 	 * The flags which will be set in this function are - mmc compliant,
3225 	 * dvdram writable device, cdda support. Initialize them to FALSE
3226 	 * and if a capability is detected - it will be set to TRUE.
3227 	 */
3228 	un->un_f_mmc_cap = FALSE;
3229 	un->un_f_dvdram_writable_device = FALSE;
3230 	un->un_f_cfg_cdda = FALSE;
3231 
3232 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3233 	status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf,
3234 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3235 
3236 	if (status != 0) {
3237 		/* command failed; just return */
3238 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3239 		return;
3240 	}
3241 	/*
3242 	 * If the mode sense request for the CDROM CAPABILITIES
3243 	 * page (0x2A) succeeds the device is assumed to be MMC.
3244 	 */
3245 	un->un_f_mmc_cap = TRUE;
3246 
3247 	/* Get to the page data */
3248 	sense_mhp = (struct mode_header_grp2 *)buf;
3249 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3250 	    sense_mhp->bdesc_length_lo;
3251 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3252 		/*
3253 		 * We did not get back the expected block descriptor
3254 		 * length so we cannot determine if the device supports
3255 		 * CDDA. However, we still indicate the device is MMC
3256 		 * according to the successful response to the page
3257 		 * 0x2A mode sense request.
3258 		 */
3259 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3260 		    "sd_set_mmc_caps: Mode Sense returned "
3261 		    "invalid block descriptor length\n");
3262 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3263 		return;
3264 	}
3265 
3266 	/* See if read CDDA is supported */
3267 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3268 	    bd_len);
3269 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3270 
3271 	/* See if writing DVD RAM is supported. */
3272 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3273 	if (un->un_f_dvdram_writable_device == TRUE) {
3274 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3275 		return;
3276 	}
3277 
3278 	/*
3279 	 * If the device presents DVD or CD capabilities in the mode
3280 	 * page, we can return here since a RRD will not have
3281 	 * these capabilities.
3282 	 */
3283 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3284 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3285 		return;
3286 	}
3287 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3288 
3289 	/*
3290 	 * If un->un_f_dvdram_writable_device is still FALSE,
3291 	 * check for a Removable Rigid Disk (RRD).  A RRD
3292 	 * device is identified by the features RANDOM_WRITABLE and
3293 	 * HARDWARE_DEFECT_MANAGEMENT.
3294 	 */
3295 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3296 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3297 
3298 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
3299 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3300 	    RANDOM_WRITABLE);
3301 	if (rtn != 0) {
3302 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3303 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3304 		return;
3305 	}
3306 
3307 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3308 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3309 
3310 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
3311 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3312 	    HARDWARE_DEFECT_MANAGEMENT);
3313 	if (rtn == 0) {
3314 		/*
3315 		 * We have good information, check for random writable
3316 		 * and hardware defect features.
3317 		 */
3318 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3319 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3320 			un->un_f_dvdram_writable_device = TRUE;
3321 		}
3322 	}
3323 
3324 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3325 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3326 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3327 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3328 }
3329 
3330 /*
3331  *    Function: sd_check_for_writable_cd
3332  *
3333  * Description: This routine determines if the media in the device is
3334  *		writable or not. It uses the get configuration command (0x46)
3335  *		to determine if the media is writable
3336  *
3337  *   Arguments: un - driver soft state (unit) structure
3338  *
3339  *     Context: Never called at interrupt context.
3340  */
3341 
3342 static void
3343 sd_check_for_writable_cd(struct sd_lun *un)
3344 {
3345 	struct uscsi_cmd		com;
3346 	uchar_t				*out_data;
3347 	uchar_t				*rqbuf;
3348 	int				rtn;
3349 	uchar_t				*out_data_rw, *out_data_hd;
3350 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3351 	struct mode_header_grp2		*sense_mhp;
3352 	uchar_t				*sense_page;
3353 	caddr_t				buf;
3354 	int				bd_len;
3355 	int				status;
3356 
3357 	ASSERT(un != NULL);
3358 	ASSERT(mutex_owned(SD_MUTEX(un)));
3359 
3360 	/*
3361 	 * Initialize the writable media to false, if configuration info.
3362 	 * tells us otherwise then only we will set it.
3363 	 */
3364 	un->un_f_mmc_writable_media = FALSE;
3365 	mutex_exit(SD_MUTEX(un));
3366 
3367 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3368 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3369 
3370 	rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH,
3371 	    out_data, SD_PROFILE_HEADER_LEN);
3372 
3373 	mutex_enter(SD_MUTEX(un));
3374 	if (rtn == 0) {
3375 		/*
3376 		 * We have good information, check for writable DVD.
3377 		 */
3378 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3379 			un->un_f_mmc_writable_media = TRUE;
3380 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3381 			kmem_free(rqbuf, SENSE_LENGTH);
3382 			return;
3383 		}
3384 	}
3385 
3386 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3387 	kmem_free(rqbuf, SENSE_LENGTH);
3388 
3389 	/*
3390 	 * Determine if this is a RRD type device.
3391 	 */
3392 	mutex_exit(SD_MUTEX(un));
3393 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3394 	status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf,
3395 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3396 	mutex_enter(SD_MUTEX(un));
3397 	if (status != 0) {
3398 		/* command failed; just return */
3399 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3400 		return;
3401 	}
3402 
3403 	/* Get to the page data */
3404 	sense_mhp = (struct mode_header_grp2 *)buf;
3405 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3406 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3407 		/*
3408 		 * We did not get back the expected block descriptor length so
3409 		 * we cannot check the mode page.
3410 		 */
3411 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3412 		    "sd_check_for_writable_cd: Mode Sense returned "
3413 		    "invalid block descriptor length\n");
3414 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3415 		return;
3416 	}
3417 
3418 	/*
3419 	 * If the device presents DVD or CD capabilities in the mode
3420 	 * page, we can return here since a RRD device will not have
3421 	 * these capabilities.
3422 	 */
3423 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3424 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3425 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3426 		return;
3427 	}
3428 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3429 
3430 	/*
3431 	 * If un->un_f_mmc_writable_media is still FALSE,
3432 	 * check for RRD type media.  A RRD device is identified
3433 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3434 	 */
3435 	mutex_exit(SD_MUTEX(un));
3436 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3437 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3438 
3439 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
3440 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3441 	    RANDOM_WRITABLE);
3442 	if (rtn != 0) {
3443 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3444 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3445 		mutex_enter(SD_MUTEX(un));
3446 		return;
3447 	}
3448 
3449 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3450 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3451 
3452 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
3453 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3454 	    HARDWARE_DEFECT_MANAGEMENT);
3455 	mutex_enter(SD_MUTEX(un));
3456 	if (rtn == 0) {
3457 		/*
3458 		 * We have good information, check for random writable
3459 		 * and hardware defect features as current.
3460 		 */
3461 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3462 		    (out_data_rw[10] & 0x1) &&
3463 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3464 		    (out_data_hd[10] & 0x1)) {
3465 			un->un_f_mmc_writable_media = TRUE;
3466 		}
3467 	}
3468 
3469 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3470 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3471 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3472 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3473 }
3474 
3475 /*
3476  *    Function: sd_read_unit_properties
3477  *
3478  * Description: The following implements a property lookup mechanism.
3479  *		Properties for particular disks (keyed on vendor, model
3480  *		and rev numbers) are sought in the sd.conf file via
3481  *		sd_process_sdconf_file(), and if not found there, are
3482  *		looked for in a list hardcoded in this driver via
3483  *		sd_process_sdconf_table() Once located the properties
3484  *		are used to update the driver unit structure.
3485  *
3486  *   Arguments: un - driver soft state (unit) structure
3487  */
3488 
3489 static void
3490 sd_read_unit_properties(struct sd_lun *un)
3491 {
3492 	/*
3493 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3494 	 * the "sd-config-list" property (from the sd.conf file) or if
3495 	 * there was not a match for the inquiry vid/pid. If this event
3496 	 * occurs the static driver configuration table is searched for
3497 	 * a match.
3498 	 */
3499 	ASSERT(un != NULL);
3500 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3501 		sd_process_sdconf_table(un);
3502 	}
3503 
3504 	/* check for LSI device */
3505 	sd_is_lsi(un);
3506 
3507 
3508 }
3509 
3510 
3511 /*
3512  *    Function: sd_process_sdconf_file
3513  *
3514  * Description: Use ddi_getlongprop to obtain the properties from the
3515  *		driver's config file (ie, sd.conf) and update the driver
3516  *		soft state structure accordingly.
3517  *
3518  *   Arguments: un - driver soft state (unit) structure
3519  *
3520  * Return Code: SD_SUCCESS - The properties were successfully set according
3521  *			     to the driver configuration file.
3522  *		SD_FAILURE - The driver config list was not obtained or
3523  *			     there was no vid/pid match. This indicates that
3524  *			     the static config table should be used.
3525  *
3526  * The config file has a property, "sd-config-list", which consists of
3527  * one or more duplets as follows:
3528  *
3529  *  sd-config-list=
3530  *	<duplet>,
3531  *	[<duplet>,]
3532  *	[<duplet>];
3533  *
3534  * The structure of each duplet is as follows:
3535  *
3536  *  <duplet>:= <vid+pid>,<data-property-name_list>
3537  *
3538  * The first entry of the duplet is the device ID string (the concatenated
3539  * vid & pid; not to be confused with a device_id).  This is defined in
3540  * the same way as in the sd_disk_table.
3541  *
3542  * The second part of the duplet is a string that identifies a
3543  * data-property-name-list. The data-property-name-list is defined as
3544  * follows:
3545  *
3546  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3547  *
3548  * The syntax of <data-property-name> depends on the <version> field.
3549  *
3550  * If version = SD_CONF_VERSION_1 we have the following syntax:
3551  *
3552  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3553  *
3554  * where the prop0 value will be used to set prop0 if bit0 set in the
3555  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3556  *
3557  */
3558 
3559 static int
3560 sd_process_sdconf_file(struct sd_lun *un)
3561 {
3562 	char	*config_list = NULL;
3563 	int	config_list_len;
3564 	int	len;
3565 	int	dupletlen = 0;
3566 	char	*vidptr;
3567 	int	vidlen;
3568 	char	*dnlist_ptr;
3569 	char	*dataname_ptr;
3570 	int	dnlist_len;
3571 	int	dataname_len;
3572 	int	*data_list;
3573 	int	data_list_len;
3574 	int	rval = SD_FAILURE;
3575 	int	i;
3576 
3577 	ASSERT(un != NULL);
3578 
3579 	/* Obtain the configuration list associated with the .conf file */
3580 	if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS,
3581 	    sd_config_list, (caddr_t)&config_list, &config_list_len)
3582 	    != DDI_PROP_SUCCESS) {
3583 		return (SD_FAILURE);
3584 	}
3585 
3586 	/*
3587 	 * Compare vids in each duplet to the inquiry vid - if a match is
3588 	 * made, get the data value and update the soft state structure
3589 	 * accordingly.
3590 	 *
3591 	 * Note: This algorithm is complex and difficult to maintain. It should
3592 	 * be replaced with a more robust implementation.
3593 	 */
3594 	for (len = config_list_len, vidptr = config_list; len > 0;
3595 	    vidptr += dupletlen, len -= dupletlen) {
3596 		/*
3597 		 * Note: The assumption here is that each vid entry is on
3598 		 * a unique line from its associated duplet.
3599 		 */
3600 		vidlen = dupletlen = (int)strlen(vidptr);
3601 		if ((vidlen == 0) ||
3602 		    (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
3603 			dupletlen++;
3604 			continue;
3605 		}
3606 
3607 		/*
3608 		 * dnlist contains 1 or more blank separated
3609 		 * data-property-name entries
3610 		 */
3611 		dnlist_ptr = vidptr + vidlen + 1;
3612 		dnlist_len = (int)strlen(dnlist_ptr);
3613 		dupletlen += dnlist_len + 2;
3614 
3615 		/*
3616 		 * Set a pointer for the first data-property-name
3617 		 * entry in the list
3618 		 */
3619 		dataname_ptr = dnlist_ptr;
3620 		dataname_len = 0;
3621 
3622 		/*
3623 		 * Loop through all data-property-name entries in the
3624 		 * data-property-name-list setting the properties for each.
3625 		 */
3626 		while (dataname_len < dnlist_len) {
3627 			int version;
3628 
3629 			/*
3630 			 * Determine the length of the current
3631 			 * data-property-name entry by indexing until a
3632 			 * blank or NULL is encountered. When the space is
3633 			 * encountered reset it to a NULL for compliance
3634 			 * with ddi_getlongprop().
3635 			 */
3636 			for (i = 0; ((dataname_ptr[i] != ' ') &&
3637 			    (dataname_ptr[i] != '\0')); i++) {
3638 				;
3639 			}
3640 
3641 			dataname_len += i;
3642 			/* If not null terminated, Make it so */
3643 			if (dataname_ptr[i] == ' ') {
3644 				dataname_ptr[i] = '\0';
3645 			}
3646 			dataname_len++;
3647 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3648 			    "sd_process_sdconf_file: disk:%s, data:%s\n",
3649 			    vidptr, dataname_ptr);
3650 
3651 			/* Get the data list */
3652 			if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0,
3653 			    dataname_ptr, (caddr_t)&data_list, &data_list_len)
3654 			    != DDI_PROP_SUCCESS) {
3655 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3656 				    "sd_process_sdconf_file: data property (%s)"
3657 				    " has no value\n", dataname_ptr);
3658 				dataname_ptr = dnlist_ptr + dataname_len;
3659 				continue;
3660 			}
3661 
3662 			version = data_list[0];
3663 
3664 			if (version == SD_CONF_VERSION_1) {
3665 				sd_tunables values;
3666 
3667 				/* Set the properties */
3668 				if (sd_chk_vers1_data(un, data_list[1],
3669 				    &data_list[2], data_list_len, dataname_ptr)
3670 				    == SD_SUCCESS) {
3671 					sd_get_tunables_from_conf(un,
3672 					    data_list[1], &data_list[2],
3673 					    &values);
3674 					sd_set_vers1_properties(un,
3675 					    data_list[1], &values);
3676 					rval = SD_SUCCESS;
3677 				} else {
3678 					rval = SD_FAILURE;
3679 				}
3680 			} else {
3681 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3682 				    "data property %s version 0x%x is invalid.",
3683 				    dataname_ptr, version);
3684 				rval = SD_FAILURE;
3685 			}
3686 			kmem_free(data_list, data_list_len);
3687 			dataname_ptr = dnlist_ptr + dataname_len;
3688 		}
3689 	}
3690 
3691 	/* free up the memory allocated by ddi_getlongprop */
3692 	if (config_list) {
3693 		kmem_free(config_list, config_list_len);
3694 	}
3695 
3696 	return (rval);
3697 }
3698 
3699 /*
3700  *    Function: sd_get_tunables_from_conf()
3701  *
3702  *
3703  *    This function reads the data list from the sd.conf file and pulls
3704  *    the values that can have numeric values as arguments and places
3705  *    the values in the apropriate sd_tunables member.
3706  *    Since the order of the data list members varies across platforms
3707  *    This function reads them from the data list in a platform specific
3708  *    order and places them into the correct sd_tunable member that is
3709  *    a consistant across all platforms.
3710  */
3711 static void
3712 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
3713     sd_tunables *values)
3714 {
3715 	int i;
3716 	int mask;
3717 
3718 	bzero(values, sizeof (sd_tunables));
3719 
3720 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3721 
3722 		mask = 1 << i;
3723 		if (mask > flags) {
3724 			break;
3725 		}
3726 
3727 		switch (mask & flags) {
3728 		case 0:	/* This mask bit not set in flags */
3729 			continue;
3730 		case SD_CONF_BSET_THROTTLE:
3731 			values->sdt_throttle = data_list[i];
3732 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3733 			    "sd_get_tunables_from_conf: throttle = %d\n",
3734 			    values->sdt_throttle);
3735 			break;
3736 		case SD_CONF_BSET_CTYPE:
3737 			values->sdt_ctype = data_list[i];
3738 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3739 			    "sd_get_tunables_from_conf: ctype = %d\n",
3740 			    values->sdt_ctype);
3741 			break;
3742 		case SD_CONF_BSET_NRR_COUNT:
3743 			values->sdt_not_rdy_retries = data_list[i];
3744 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3745 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
3746 			    values->sdt_not_rdy_retries);
3747 			break;
3748 		case SD_CONF_BSET_BSY_RETRY_COUNT:
3749 			values->sdt_busy_retries = data_list[i];
3750 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3751 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
3752 			    values->sdt_busy_retries);
3753 			break;
3754 		case SD_CONF_BSET_RST_RETRIES:
3755 			values->sdt_reset_retries = data_list[i];
3756 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3757 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
3758 			    values->sdt_reset_retries);
3759 			break;
3760 		case SD_CONF_BSET_RSV_REL_TIME:
3761 			values->sdt_reserv_rel_time = data_list[i];
3762 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3763 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
3764 			    values->sdt_reserv_rel_time);
3765 			break;
3766 		case SD_CONF_BSET_MIN_THROTTLE:
3767 			values->sdt_min_throttle = data_list[i];
3768 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3769 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
3770 			    values->sdt_min_throttle);
3771 			break;
3772 		case SD_CONF_BSET_DISKSORT_DISABLED:
3773 			values->sdt_disk_sort_dis = data_list[i];
3774 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3775 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
3776 			    values->sdt_disk_sort_dis);
3777 			break;
3778 		case SD_CONF_BSET_LUN_RESET_ENABLED:
3779 			values->sdt_lun_reset_enable = data_list[i];
3780 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3781 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
3782 			    "\n", values->sdt_lun_reset_enable);
3783 			break;
3784 		}
3785 	}
3786 }
3787 
3788 /*
3789  *    Function: sd_process_sdconf_table
3790  *
3791  * Description: Search the static configuration table for a match on the
3792  *		inquiry vid/pid and update the driver soft state structure
3793  *		according to the table property values for the device.
3794  *
3795  *		The form of a configuration table entry is:
3796  *		  <vid+pid>,<flags>,<property-data>
3797  *		  "SEAGATE ST42400N",1,63,0,0			(Fibre)
3798  *		  "SEAGATE ST42400N",1,63,0,0,0,0		(Sparc)
3799  *		  "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0	(Intel)
3800  *
3801  *   Arguments: un - driver soft state (unit) structure
3802  */
3803 
3804 static void
3805 sd_process_sdconf_table(struct sd_lun *un)
3806 {
3807 	char	*id = NULL;
3808 	int	table_index;
3809 	int	idlen;
3810 
3811 	ASSERT(un != NULL);
3812 	for (table_index = 0; table_index < sd_disk_table_size;
3813 	    table_index++) {
3814 		id = sd_disk_table[table_index].device_id;
3815 		idlen = strlen(id);
3816 		if (idlen == 0) {
3817 			continue;
3818 		}
3819 
3820 		/*
3821 		 * The static configuration table currently does not
3822 		 * implement version 10 properties. Additionally,
3823 		 * multiple data-property-name entries are not
3824 		 * implemented in the static configuration table.
3825 		 */
3826 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
3827 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3828 			    "sd_process_sdconf_table: disk %s\n", id);
3829 			sd_set_vers1_properties(un,
3830 			    sd_disk_table[table_index].flags,
3831 			    sd_disk_table[table_index].properties);
3832 			break;
3833 		}
3834 	}
3835 }
3836 
3837 
3838 /*
3839  *    Function: sd_sdconf_id_match
3840  *
3841  * Description: This local function implements a case sensitive vid/pid
3842  *		comparison as well as the boundary cases of wild card and
3843  *		multiple blanks.
3844  *
3845  *		Note: An implicit assumption made here is that the scsi
3846  *		inquiry structure will always keep the vid, pid and
3847  *		revision strings in consecutive sequence, so they can be
3848  *		read as a single string. If this assumption is not the
3849  *		case, a separate string, to be used for the check, needs
3850  *		to be built with these strings concatenated.
3851  *
3852  *   Arguments: un - driver soft state (unit) structure
3853  *		id - table or config file vid/pid
3854  *		idlen  - length of the vid/pid (bytes)
3855  *
3856  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3857  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3858  */
3859 
3860 static int
3861 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
3862 {
3863 	struct scsi_inquiry	*sd_inq;
3864 	int 			rval = SD_SUCCESS;
3865 
3866 	ASSERT(un != NULL);
3867 	sd_inq = un->un_sd->sd_inq;
3868 	ASSERT(id != NULL);
3869 
3870 	/*
3871 	 * We use the inq_vid as a pointer to a buffer containing the
3872 	 * vid and pid and use the entire vid/pid length of the table
3873 	 * entry for the comparison. This works because the inq_pid
3874 	 * data member follows inq_vid in the scsi_inquiry structure.
3875 	 */
3876 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
3877 		/*
3878 		 * The user id string is compared to the inquiry vid/pid
3879 		 * using a case insensitive comparison and ignoring
3880 		 * multiple spaces.
3881 		 */
3882 		rval = sd_blank_cmp(un, id, idlen);
3883 		if (rval != SD_SUCCESS) {
3884 			/*
3885 			 * User id strings that start and end with a "*"
3886 			 * are a special case. These do not have a
3887 			 * specific vendor, and the product string can
3888 			 * appear anywhere in the 16 byte PID portion of
3889 			 * the inquiry data. This is a simple strstr()
3890 			 * type search for the user id in the inquiry data.
3891 			 */
3892 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
3893 				char	*pidptr = &id[1];
3894 				int	i;
3895 				int	j;
3896 				int	pidstrlen = idlen - 2;
3897 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
3898 				    pidstrlen;
3899 
3900 				if (j < 0) {
3901 					return (SD_FAILURE);
3902 				}
3903 				for (i = 0; i < j; i++) {
3904 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
3905 					    pidptr, pidstrlen) == 0) {
3906 						rval = SD_SUCCESS;
3907 						break;
3908 					}
3909 				}
3910 			}
3911 		}
3912 	}
3913 	return (rval);
3914 }
3915 
3916 
3917 /*
3918  *    Function: sd_blank_cmp
3919  *
3920  * Description: If the id string starts and ends with a space, treat
3921  *		multiple consecutive spaces as equivalent to a single
3922  *		space. For example, this causes a sd_disk_table entry
3923  *		of " NEC CDROM " to match a device's id string of
3924  *		"NEC       CDROM".
3925  *
3926  *		Note: The success exit condition for this routine is if
3927  *		the pointer to the table entry is '\0' and the cnt of
3928  *		the inquiry length is zero. This will happen if the inquiry
3929  *		string returned by the device is padded with spaces to be
3930  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
3931  *		SCSI spec states that the inquiry string is to be padded with
3932  *		spaces.
3933  *
3934  *   Arguments: un - driver soft state (unit) structure
3935  *		id - table or config file vid/pid
3936  *		idlen  - length of the vid/pid (bytes)
3937  *
3938  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3939  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3940  */
3941 
3942 static int
3943 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
3944 {
3945 	char		*p1;
3946 	char		*p2;
3947 	int		cnt;
3948 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
3949 	    sizeof (SD_INQUIRY(un)->inq_pid);
3950 
3951 	ASSERT(un != NULL);
3952 	p2 = un->un_sd->sd_inq->inq_vid;
3953 	ASSERT(id != NULL);
3954 	p1 = id;
3955 
3956 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
3957 		/*
3958 		 * Note: string p1 is terminated by a NUL but string p2
3959 		 * isn't.  The end of p2 is determined by cnt.
3960 		 */
3961 		for (;;) {
3962 			/* skip over any extra blanks in both strings */
3963 			while ((*p1 != '\0') && (*p1 == ' ')) {
3964 				p1++;
3965 			}
3966 			while ((cnt != 0) && (*p2 == ' ')) {
3967 				p2++;
3968 				cnt--;
3969 			}
3970 
3971 			/* compare the two strings */
3972 			if ((cnt == 0) ||
3973 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
3974 				break;
3975 			}
3976 			while ((cnt > 0) &&
3977 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
3978 				p1++;
3979 				p2++;
3980 				cnt--;
3981 			}
3982 		}
3983 	}
3984 
3985 	/* return SD_SUCCESS if both strings match */
3986 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
3987 }
3988 
3989 
3990 /*
3991  *    Function: sd_chk_vers1_data
3992  *
3993  * Description: Verify the version 1 device properties provided by the
3994  *		user via the configuration file
3995  *
3996  *   Arguments: un	     - driver soft state (unit) structure
3997  *		flags	     - integer mask indicating properties to be set
3998  *		prop_list    - integer list of property values
3999  *		list_len     - length of user provided data
4000  *
4001  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4002  *		SD_FAILURE - Indicates the user provided data is invalid
4003  */
4004 
4005 static int
4006 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4007     int list_len, char *dataname_ptr)
4008 {
4009 	int i;
4010 	int mask = 1;
4011 	int index = 0;
4012 
4013 	ASSERT(un != NULL);
4014 
4015 	/* Check for a NULL property name and list */
4016 	if (dataname_ptr == NULL) {
4017 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4018 		    "sd_chk_vers1_data: NULL data property name.");
4019 		return (SD_FAILURE);
4020 	}
4021 	if (prop_list == NULL) {
4022 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4023 		    "sd_chk_vers1_data: %s NULL data property list.",
4024 		    dataname_ptr);
4025 		return (SD_FAILURE);
4026 	}
4027 
4028 	/* Display a warning if undefined bits are set in the flags */
4029 	if (flags & ~SD_CONF_BIT_MASK) {
4030 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4031 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4032 		    "Properties not set.",
4033 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4034 		return (SD_FAILURE);
4035 	}
4036 
4037 	/*
4038 	 * Verify the length of the list by identifying the highest bit set
4039 	 * in the flags and validating that the property list has a length
4040 	 * up to the index of this bit.
4041 	 */
4042 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4043 		if (flags & mask) {
4044 			index++;
4045 		}
4046 		mask = 1 << i;
4047 	}
4048 	if ((list_len / sizeof (int)) < (index + 2)) {
4049 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4050 		    "sd_chk_vers1_data: "
4051 		    "Data property list %s size is incorrect. "
4052 		    "Properties not set.", dataname_ptr);
4053 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4054 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4055 		return (SD_FAILURE);
4056 	}
4057 	return (SD_SUCCESS);
4058 }
4059 
4060 
4061 /*
4062  *    Function: sd_set_vers1_properties
4063  *
4064  * Description: Set version 1 device properties based on a property list
4065  *		retrieved from the driver configuration file or static
4066  *		configuration table. Version 1 properties have the format:
4067  *
4068  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4069  *
4070  *		where the prop0 value will be used to set prop0 if bit0
4071  *		is set in the flags
4072  *
4073  *   Arguments: un	     - driver soft state (unit) structure
4074  *		flags	     - integer mask indicating properties to be set
4075  *		prop_list    - integer list of property values
4076  */
4077 
4078 static void
4079 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4080 {
4081 	ASSERT(un != NULL);
4082 
4083 	/*
4084 	 * Set the flag to indicate cache is to be disabled. An attempt
4085 	 * to disable the cache via sd_cache_control() will be made
4086 	 * later during attach once the basic initialization is complete.
4087 	 */
4088 	if (flags & SD_CONF_BSET_NOCACHE) {
4089 		un->un_f_opt_disable_cache = TRUE;
4090 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4091 		    "sd_set_vers1_properties: caching disabled flag set\n");
4092 	}
4093 
4094 	/* CD-specific configuration parameters */
4095 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4096 		un->un_f_cfg_playmsf_bcd = TRUE;
4097 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4098 		    "sd_set_vers1_properties: playmsf_bcd set\n");
4099 	}
4100 	if (flags & SD_CONF_BSET_READSUB_BCD) {
4101 		un->un_f_cfg_readsub_bcd = TRUE;
4102 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4103 		    "sd_set_vers1_properties: readsub_bcd set\n");
4104 	}
4105 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4106 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
4107 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4108 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4109 	}
4110 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4111 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
4112 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4113 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4114 	}
4115 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4116 		un->un_f_cfg_no_read_header = TRUE;
4117 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4118 			    "sd_set_vers1_properties: no_read_header set\n");
4119 	}
4120 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
4121 		un->un_f_cfg_read_cd_xd4 = TRUE;
4122 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4123 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
4124 	}
4125 
4126 	/* Support for devices which do not have valid/unique serial numbers */
4127 	if (flags & SD_CONF_BSET_FAB_DEVID) {
4128 		un->un_f_opt_fab_devid = TRUE;
4129 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4130 		    "sd_set_vers1_properties: fab_devid bit set\n");
4131 	}
4132 
4133 	/* Support for user throttle configuration */
4134 	if (flags & SD_CONF_BSET_THROTTLE) {
4135 		ASSERT(prop_list != NULL);
4136 		un->un_saved_throttle = un->un_throttle =
4137 		    prop_list->sdt_throttle;
4138 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4139 		    "sd_set_vers1_properties: throttle set to %d\n",
4140 		    prop_list->sdt_throttle);
4141 	}
4142 
4143 	/* Set the per disk retry count according to the conf file or table. */
4144 	if (flags & SD_CONF_BSET_NRR_COUNT) {
4145 		ASSERT(prop_list != NULL);
4146 		if (prop_list->sdt_not_rdy_retries) {
4147 			un->un_notready_retry_count =
4148 				prop_list->sdt_not_rdy_retries;
4149 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4150 			    "sd_set_vers1_properties: not ready retry count"
4151 			    " set to %d\n", un->un_notready_retry_count);
4152 		}
4153 	}
4154 
4155 	/* The controller type is reported for generic disk driver ioctls */
4156 	if (flags & SD_CONF_BSET_CTYPE) {
4157 		ASSERT(prop_list != NULL);
4158 		switch (prop_list->sdt_ctype) {
4159 		case CTYPE_CDROM:
4160 			un->un_ctype = prop_list->sdt_ctype;
4161 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4162 			    "sd_set_vers1_properties: ctype set to "
4163 			    "CTYPE_CDROM\n");
4164 			break;
4165 		case CTYPE_CCS:
4166 			un->un_ctype = prop_list->sdt_ctype;
4167 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4168 				"sd_set_vers1_properties: ctype set to "
4169 				"CTYPE_CCS\n");
4170 			break;
4171 		case CTYPE_ROD:		/* RW optical */
4172 			un->un_ctype = prop_list->sdt_ctype;
4173 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4174 			    "sd_set_vers1_properties: ctype set to "
4175 			    "CTYPE_ROD\n");
4176 			break;
4177 		default:
4178 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4179 			    "sd_set_vers1_properties: Could not set "
4180 			    "invalid ctype value (%d)",
4181 			    prop_list->sdt_ctype);
4182 		}
4183 	}
4184 
4185 	/* Purple failover timeout */
4186 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4187 		ASSERT(prop_list != NULL);
4188 		un->un_busy_retry_count =
4189 			prop_list->sdt_busy_retries;
4190 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4191 		    "sd_set_vers1_properties: "
4192 		    "busy retry count set to %d\n",
4193 		    un->un_busy_retry_count);
4194 	}
4195 
4196 	/* Purple reset retry count */
4197 	if (flags & SD_CONF_BSET_RST_RETRIES) {
4198 		ASSERT(prop_list != NULL);
4199 		un->un_reset_retry_count =
4200 			prop_list->sdt_reset_retries;
4201 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4202 		    "sd_set_vers1_properties: "
4203 		    "reset retry count set to %d\n",
4204 		    un->un_reset_retry_count);
4205 	}
4206 
4207 	/* Purple reservation release timeout */
4208 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4209 		ASSERT(prop_list != NULL);
4210 		un->un_reserve_release_time =
4211 			prop_list->sdt_reserv_rel_time;
4212 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4213 		    "sd_set_vers1_properties: "
4214 		    "reservation release timeout set to %d\n",
4215 		    un->un_reserve_release_time);
4216 	}
4217 
4218 	/*
4219 	 * Driver flag telling the driver to verify that no commands are pending
4220 	 * for a device before issuing a Test Unit Ready. This is a workaround
4221 	 * for a firmware bug in some Seagate eliteI drives.
4222 	 */
4223 	if (flags & SD_CONF_BSET_TUR_CHECK) {
4224 		un->un_f_cfg_tur_check = TRUE;
4225 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4226 		    "sd_set_vers1_properties: tur queue check set\n");
4227 	}
4228 
4229 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4230 		un->un_min_throttle = prop_list->sdt_min_throttle;
4231 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4232 		    "sd_set_vers1_properties: min throttle set to %d\n",
4233 		    un->un_min_throttle);
4234 	}
4235 
4236 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4237 		un->un_f_disksort_disabled =
4238 		    (prop_list->sdt_disk_sort_dis != 0) ?
4239 		    TRUE : FALSE;
4240 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4241 		    "sd_set_vers1_properties: disksort disabled "
4242 		    "flag set to %d\n",
4243 		    prop_list->sdt_disk_sort_dis);
4244 	}
4245 
4246 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4247 		un->un_f_lun_reset_enabled =
4248 		    (prop_list->sdt_lun_reset_enable != 0) ?
4249 		    TRUE : FALSE;
4250 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4251 		    "sd_set_vers1_properties: lun reset enabled "
4252 		    "flag set to %d\n",
4253 		    prop_list->sdt_lun_reset_enable);
4254 	}
4255 
4256 	/*
4257 	 * Validate the throttle values.
4258 	 * If any of the numbers are invalid, set everything to defaults.
4259 	 */
4260 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4261 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4262 	    (un->un_min_throttle > un->un_throttle)) {
4263 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4264 		un->un_min_throttle = sd_min_throttle;
4265 	}
4266 }
4267 
4268 /*
4269  *   Function: sd_is_lsi()
4270  *
4271  *   Description: Check for lsi devices, step throught the static device
4272  *	table to match vid/pid.
4273  *
4274  *   Args: un - ptr to sd_lun
4275  *
4276  *   Notes:  When creating new LSI property, need to add the new LSI property
4277  *		to this function.
4278  */
4279 static void
4280 sd_is_lsi(struct sd_lun *un)
4281 {
4282 	char	*id = NULL;
4283 	int	table_index;
4284 	int	idlen;
4285 	void	*prop;
4286 
4287 	ASSERT(un != NULL);
4288 	for (table_index = 0; table_index < sd_disk_table_size;
4289 	    table_index++) {
4290 		id = sd_disk_table[table_index].device_id;
4291 		idlen = strlen(id);
4292 		if (idlen == 0) {
4293 			continue;
4294 		}
4295 
4296 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4297 			prop = sd_disk_table[table_index].properties;
4298 			if (prop == &lsi_properties ||
4299 			    prop == &lsi_oem_properties ||
4300 			    prop == &lsi_properties_scsi ||
4301 			    prop == &symbios_properties) {
4302 				un->un_f_cfg_is_lsi = TRUE;
4303 			}
4304 			break;
4305 		}
4306 	}
4307 }
4308 
4309 
4310 /*
4311  * The following routines support reading and interpretation of disk labels,
4312  * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and
4313  * fdisk tables.
4314  */
4315 
4316 /*
4317  *    Function: sd_validate_geometry
4318  *
4319  * Description: Read the label from the disk (if present). Update the unit's
4320  *		geometry and vtoc information from the data in the label.
4321  *		Verify that the label is valid.
4322  *
4323  *   Arguments: un - driver soft state (unit) structure
4324  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4325  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4326  *			to use the USCSI "direct" chain and bypass the normal
4327  *			command waitq.
4328  *
4329  * Return Code: 0 - Successful completion
4330  *		EINVAL  - Invalid value in un->un_tgt_blocksize or
4331  *			  un->un_blockcount; or label on disk is corrupted
4332  *			  or unreadable.
4333  *		EACCES  - Reservation conflict at the device.
4334  *		ENOMEM  - Resource allocation error
4335  *		ENOTSUP - geometry not applicable
4336  *
4337  *     Context: Kernel thread only (can sleep).
4338  */
4339 
4340 static int
4341 sd_validate_geometry(struct sd_lun *un, int path_flag)
4342 {
4343 	static	char		labelstring[128];
4344 	static	char		buf[256];
4345 	char	*label		= NULL;
4346 	int	label_error = 0;
4347 	int	gvalid		= un->un_f_geometry_is_valid;
4348 	int	lbasize;
4349 	uint64_t	capacity;
4350 	int	count;
4351 #if defined(__i386) || defined(__amd64)
4352 	int forced_under_1t = 0;
4353 #endif
4354 
4355 	ASSERT(un != NULL);
4356 	ASSERT(mutex_owned(SD_MUTEX(un)));
4357 
4358 	/*
4359 	 * If the required values are not valid, then try getting them
4360 	 * once via read capacity. If that fails, then fail this call.
4361 	 * This is necessary with the new mpxio failover behavior in
4362 	 * the T300 where we can get an attach for the inactive path
4363 	 * before the active path. The inactive path fails commands with
4364 	 * sense data of 02,04,88 which happens to the read capacity
4365 	 * before mpxio has had sufficient knowledge to know if it should
4366 	 * force a fail over or not. (Which it won't do at attach anyhow).
4367 	 * If the read capacity at attach time fails, un_tgt_blocksize and
4368 	 * un_blockcount won't be valid.
4369 	 */
4370 	if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
4371 	    (un->un_f_blockcount_is_valid != TRUE)) {
4372 		uint64_t	cap;
4373 		uint32_t	lbasz;
4374 		int		rval;
4375 
4376 		mutex_exit(SD_MUTEX(un));
4377 		rval = sd_send_scsi_READ_CAPACITY(un, &cap,
4378 		    &lbasz, SD_PATH_DIRECT);
4379 		mutex_enter(SD_MUTEX(un));
4380 		if (rval == 0) {
4381 			/*
4382 			 * The following relies on
4383 			 * sd_send_scsi_READ_CAPACITY never
4384 			 * returning 0 for capacity and/or lbasize.
4385 			 */
4386 			sd_update_block_info(un, lbasz, cap);
4387 		}
4388 
4389 		if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
4390 		    (un->un_f_blockcount_is_valid != TRUE)) {
4391 			return (EINVAL);
4392 		}
4393 	}
4394 
4395 	/*
4396 	 * Copy the lbasize and capacity so that if they're reset while we're
4397 	 * not holding the SD_MUTEX, we will continue to use valid values
4398 	 * after the SD_MUTEX is reacquired. (4119659)
4399 	 */
4400 	lbasize  = un->un_tgt_blocksize;
4401 	capacity = un->un_blockcount;
4402 
4403 #if defined(_SUNOS_VTOC_16)
4404 	/*
4405 	 * Set up the "whole disk" fdisk partition; this should always
4406 	 * exist, regardless of whether the disk contains an fdisk table
4407 	 * or vtoc.
4408 	 */
4409 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
4410 	un->un_map[P0_RAW_DISK].dkl_nblk  = capacity;
4411 #endif
4412 
4413 	/*
4414 	 * Refresh the logical and physical geometry caches.
4415 	 * (data from MODE SENSE format/rigid disk geometry pages,
4416 	 * and scsi_ifgetcap("geometry").
4417 	 */
4418 	sd_resync_geom_caches(un, capacity, lbasize, path_flag);
4419 
4420 	label_error = sd_use_efi(un, path_flag);
4421 	if (label_error == 0) {
4422 		/* found a valid EFI label */
4423 		SD_TRACE(SD_LOG_IO_PARTITION, un,
4424 			"sd_validate_geometry: found EFI label\n");
4425 		un->un_solaris_offset = 0;
4426 		un->un_solaris_size = capacity;
4427 		return (ENOTSUP);
4428 	}
4429 	if (un->un_blockcount > DK_MAX_BLOCKS) {
4430 		if (label_error == ESRCH) {
4431 			/*
4432 			 * they've configured a LUN over 1TB, but used
4433 			 * format.dat to restrict format's view of the
4434 			 * capacity to be under 1TB
4435 			 */
4436 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4437 "is >1TB and has a VTOC label: use format(1M) to either decrease the");
4438 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
4439 "size to be < 1TB or relabel the disk with an EFI label");
4440 #if defined(__i386) || defined(__amd64)
4441 			forced_under_1t = 1;
4442 #endif
4443 		} else {
4444 			/* unlabeled disk over 1TB */
4445 #if defined(__i386) || defined(__amd64)
4446 			/*
4447 			 * Refer to comments on off-by-1 at the head of the file
4448 			 * A 1TB disk was treated as (1T - 512)B in the past,
4449 			 * thus, it might have valid solaris partition. We
4450 			 * will return ENOTSUP later only if this disk has no
4451 			 * valid solaris partition.
4452 			 */
4453 			if ((un->un_tgt_blocksize != un->un_sys_blocksize) ||
4454 			    (un->un_blockcount - 1 > DK_MAX_BLOCKS) ||
4455 			    un->un_f_has_removable_media ||
4456 			    un->un_f_is_hotpluggable)
4457 #endif
4458 				return (ENOTSUP);
4459 		}
4460 	}
4461 	label_error = 0;
4462 
4463 	/*
4464 	 * at this point it is either labeled with a VTOC or it is
4465 	 * under 1TB (<= 1TB actually for off-by-1)
4466 	 */
4467 	if (un->un_f_vtoc_label_supported) {
4468 		struct	dk_label *dkl;
4469 		offset_t dkl1;
4470 		offset_t label_addr, real_addr;
4471 		int	rval;
4472 		size_t	buffer_size;
4473 
4474 		/*
4475 		 * Note: This will set up un->un_solaris_size and
4476 		 * un->un_solaris_offset.
4477 		 */
4478 		switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) {
4479 		case SD_CMD_RESERVATION_CONFLICT:
4480 			ASSERT(mutex_owned(SD_MUTEX(un)));
4481 			return (EACCES);
4482 		case SD_CMD_FAILURE:
4483 			ASSERT(mutex_owned(SD_MUTEX(un)));
4484 			return (ENOMEM);
4485 		}
4486 
4487 		if (un->un_solaris_size <= DK_LABEL_LOC) {
4488 
4489 #if defined(__i386) || defined(__amd64)
4490 			/*
4491 			 * Refer to comments on off-by-1 at the head of the file
4492 			 * This is for 1TB disk only. Since that there is no
4493 			 * solaris partitions, return ENOTSUP as we do for
4494 			 * >1TB disk.
4495 			 */
4496 			if (un->un_blockcount > DK_MAX_BLOCKS)
4497 				return (ENOTSUP);
4498 #endif
4499 			/*
4500 			 * Found fdisk table but no Solaris partition entry,
4501 			 * so don't call sd_uselabel() and don't create
4502 			 * a default label.
4503 			 */
4504 			label_error = 0;
4505 			un->un_f_geometry_is_valid = TRUE;
4506 			goto no_solaris_partition;
4507 		}
4508 		label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC);
4509 
4510 #if defined(__i386) || defined(__amd64)
4511 		/*
4512 		 * Refer to comments on off-by-1 at the head of the file
4513 		 * Now, this 1TB disk has valid solaris partition. It
4514 		 * must be created by previous sd driver, we have to
4515 		 * treat it as (1T-512)B.
4516 		 */
4517 		if ((un->un_blockcount > DK_MAX_BLOCKS) &&
4518 		    (forced_under_1t != 1)) {
4519 			un->un_f_capacity_adjusted = 1;
4520 			un->un_blockcount = DK_MAX_BLOCKS;
4521 			un->un_map[P0_RAW_DISK].dkl_nblk  = DK_MAX_BLOCKS;
4522 
4523 			/*
4524 			 * Refer to sd_read_fdisk, when there is no
4525 			 * fdisk partition table, un_solaris_size is
4526 			 * set to disk's capacity. In this case, we
4527 			 * need to adjust it
4528 			 */
4529 			if (un->un_solaris_size > DK_MAX_BLOCKS)
4530 				un->un_solaris_size = DK_MAX_BLOCKS;
4531 			sd_resync_geom_caches(un, DK_MAX_BLOCKS,
4532 			    lbasize, path_flag);
4533 		}
4534 #endif
4535 
4536 		/*
4537 		 * sys_blocksize != tgt_blocksize, need to re-adjust
4538 		 * blkno and save the index to beginning of dk_label
4539 		 */
4540 		real_addr = SD_SYS2TGTBLOCK(un, label_addr);
4541 		buffer_size = SD_REQBYTES2TGTBYTES(un,
4542 		    sizeof (struct dk_label));
4543 
4544 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: "
4545 		    "label_addr: 0x%x allocation size: 0x%x\n",
4546 		    label_addr, buffer_size);
4547 		dkl = kmem_zalloc(buffer_size, KM_NOSLEEP);
4548 		if (dkl == NULL) {
4549 			return (ENOMEM);
4550 		}
4551 
4552 		mutex_exit(SD_MUTEX(un));
4553 		rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr,
4554 		    path_flag);
4555 		mutex_enter(SD_MUTEX(un));
4556 
4557 		switch (rval) {
4558 		case 0:
4559 			/*
4560 			 * sd_uselabel will establish that the geometry
4561 			 * is valid.
4562 			 * For sys_blocksize != tgt_blocksize, need
4563 			 * to index into the beginning of dk_label
4564 			 */
4565 			dkl1 = (daddr_t)dkl
4566 				+ SD_TGTBYTEOFFSET(un, label_addr, real_addr);
4567 			if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1,
4568 			    path_flag) != SD_LABEL_IS_VALID) {
4569 				label_error = EINVAL;
4570 			}
4571 			break;
4572 		case EACCES:
4573 			label_error = EACCES;
4574 			break;
4575 		default:
4576 			label_error = EINVAL;
4577 			break;
4578 		}
4579 
4580 		kmem_free(dkl, buffer_size);
4581 
4582 #if defined(_SUNOS_VTOC_8)
4583 		label = (char *)un->un_asciilabel;
4584 #elif defined(_SUNOS_VTOC_16)
4585 		label = (char *)un->un_vtoc.v_asciilabel;
4586 #else
4587 #error "No VTOC format defined."
4588 #endif
4589 	}
4590 
4591 	/*
4592 	 * If a valid label was not found, AND if no reservation conflict
4593 	 * was detected, then go ahead and create a default label (4069506).
4594 	 */
4595 	if (un->un_f_default_vtoc_supported && (label_error != EACCES)) {
4596 		if (un->un_f_geometry_is_valid == FALSE) {
4597 			sd_build_default_label(un);
4598 		}
4599 		label_error = 0;
4600 	}
4601 
4602 no_solaris_partition:
4603 	if ((!un->un_f_has_removable_media ||
4604 	    (un->un_f_has_removable_media &&
4605 		un->un_mediastate == DKIO_EJECTED)) &&
4606 		(un->un_state == SD_STATE_NORMAL && !gvalid)) {
4607 		/*
4608 		 * Print out a message indicating who and what we are.
4609 		 * We do this only when we happen to really validate the
4610 		 * geometry. We may call sd_validate_geometry() at other
4611 		 * times, e.g., ioctl()'s like Get VTOC in which case we
4612 		 * don't want to print the label.
4613 		 * If the geometry is valid, print the label string,
4614 		 * else print vendor and product info, if available
4615 		 */
4616 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
4617 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label);
4618 		} else {
4619 			mutex_enter(&sd_label_mutex);
4620 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
4621 			    labelstring);
4622 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
4623 			    &labelstring[64]);
4624 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
4625 			    labelstring, &labelstring[64]);
4626 			if (un->un_f_blockcount_is_valid == TRUE) {
4627 				(void) sprintf(&buf[strlen(buf)],
4628 				    ", %llu %u byte blocks\n",
4629 				    (longlong_t)un->un_blockcount,
4630 				    un->un_tgt_blocksize);
4631 			} else {
4632 				(void) sprintf(&buf[strlen(buf)],
4633 				    ", (unknown capacity)\n");
4634 			}
4635 			SD_INFO(SD_LOG_ATTACH_DETACH, un, buf);
4636 			mutex_exit(&sd_label_mutex);
4637 		}
4638 	}
4639 
4640 #if defined(_SUNOS_VTOC_16)
4641 	/*
4642 	 * If we have valid geometry, set up the remaining fdisk partitions.
4643 	 * Note that dkl_cylno is not used for the fdisk map entries, so
4644 	 * we set it to an entirely bogus value.
4645 	 */
4646 	for (count = 0; count < FD_NUMPART; count++) {
4647 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
4648 		un->un_map[FDISK_P1 + count].dkl_nblk =
4649 		    un->un_fmap[count].fmap_nblk;
4650 
4651 		un->un_offset[FDISK_P1 + count] =
4652 		    un->un_fmap[count].fmap_start;
4653 	}
4654 #endif
4655 
4656 	for (count = 0; count < NDKMAP; count++) {
4657 #if defined(_SUNOS_VTOC_8)
4658 		struct dk_map *lp  = &un->un_map[count];
4659 		un->un_offset[count] =
4660 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
4661 #elif defined(_SUNOS_VTOC_16)
4662 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
4663 
4664 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
4665 #else
4666 #error "No VTOC format defined."
4667 #endif
4668 	}
4669 
4670 	/*
4671 	 * For VTOC labeled disk, create and set the partition stats
4672 	 * at attach time, update the stats according to dynamic
4673 	 * partition changes during running time.
4674 	 */
4675 	if (label_error == 0 && un->un_f_pkstats_enabled) {
4676 		sd_set_pstats(un);
4677 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: "
4678 		    "un:0x%p pstats created and set, or updated\n", un);
4679 	}
4680 
4681 	return (label_error);
4682 }
4683 
4684 
4685 #if defined(_SUNOS_VTOC_16)
4686 /*
4687  * Macro: MAX_BLKS
4688  *
4689  *	This macro is used for table entries where we need to have the largest
4690  *	possible sector value for that head & SPT (sectors per track)
4691  *	combination.  Other entries for some smaller disk sizes are set by
4692  *	convention to match those used by X86 BIOS usage.
4693  */
4694 #define	MAX_BLKS(heads, spt)	UINT16_MAX * heads * spt, heads, spt
4695 
4696 /*
4697  *    Function: sd_convert_geometry
4698  *
4699  * Description: Convert physical geometry into a dk_geom structure. In
4700  *		other words, make sure we don't wrap 16-bit values.
4701  *		e.g. converting from geom_cache to dk_geom
4702  *
4703  *     Context: Kernel thread only
4704  */
4705 static void
4706 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g)
4707 {
4708 	int i;
4709 	static const struct chs_values {
4710 		uint_t max_cap;		/* Max Capacity for this HS. */
4711 		uint_t nhead;		/* Heads to use. */
4712 		uint_t nsect;		/* SPT to use. */
4713 	} CHS_values[] = {
4714 		{0x00200000,  64, 32},		/* 1GB or smaller disk. */
4715 		{0x01000000, 128, 32},		/* 8GB or smaller disk. */
4716 		{MAX_BLKS(255,  63)},		/* 502.02GB or smaller disk. */
4717 		{MAX_BLKS(255, 126)},		/* .98TB or smaller disk. */
4718 		{DK_MAX_BLOCKS, 255, 189}	/* Max size is just under 1TB */
4719 	};
4720 
4721 	/* Unlabeled SCSI floppy device */
4722 	if (capacity <= 0x1000) {
4723 		un_g->dkg_nhead = 2;
4724 		un_g->dkg_ncyl = 80;
4725 		un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl);
4726 		return;
4727 	}
4728 
4729 	/*
4730 	 * For all devices we calculate cylinders using the
4731 	 * heads and sectors we assign based on capacity of the
4732 	 * device.  The table is designed to be compatible with the
4733 	 * way other operating systems lay out fdisk tables for X86
4734 	 * and to insure that the cylinders never exceed 65535 to
4735 	 * prevent problems with X86 ioctls that report geometry.
4736 	 * We use SPT that are multiples of 63, since other OSes that
4737 	 * are not limited to 16-bits for cylinders stop at 63 SPT
4738 	 * we make do by using multiples of 63 SPT.
4739 	 *
4740 	 * Note than capacities greater than or equal to 1TB will simply
4741 	 * get the largest geometry from the table. This should be okay
4742 	 * since disks this large shouldn't be using CHS values anyway.
4743 	 */
4744 	for (i = 0; CHS_values[i].max_cap < capacity &&
4745 	    CHS_values[i].max_cap != DK_MAX_BLOCKS; i++)
4746 		;
4747 
4748 	un_g->dkg_nhead = CHS_values[i].nhead;
4749 	un_g->dkg_nsect = CHS_values[i].nsect;
4750 }
4751 #endif
4752 
4753 
4754 /*
4755  *    Function: sd_resync_geom_caches
4756  *
4757  * Description: (Re)initialize both geometry caches: the virtual geometry
4758  *		information is extracted from the HBA (the "geometry"
4759  *		capability), and the physical geometry cache data is
4760  *		generated by issuing MODE SENSE commands.
4761  *
4762  *   Arguments: un - driver soft state (unit) structure
4763  *		capacity - disk capacity in #blocks
4764  *		lbasize - disk block size in bytes
4765  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4766  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4767  *			to use the USCSI "direct" chain and bypass the normal
4768  *			command waitq.
4769  *
4770  *     Context: Kernel thread only (can sleep).
4771  */
4772 
4773 static void
4774 sd_resync_geom_caches(struct sd_lun *un, uint64_t capacity, int lbasize,
4775 	int path_flag)
4776 {
4777 	struct 	geom_cache 	pgeom;
4778 	struct 	geom_cache	*pgeom_p = &pgeom;
4779 	int 	spc;
4780 	unsigned short nhead;
4781 	unsigned short nsect;
4782 
4783 	ASSERT(un != NULL);
4784 	ASSERT(mutex_owned(SD_MUTEX(un)));
4785 
4786 	/*
4787 	 * Ask the controller for its logical geometry.
4788 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
4789 	 * then the lgeom cache will be invalid.
4790 	 */
4791 	sd_get_virtual_geometry(un, capacity, lbasize);
4792 
4793 	/*
4794 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
4795 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
4796 	 */
4797 	if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) {
4798 		/*
4799 		 * Note: Perhaps this needs to be more adaptive? The rationale
4800 		 * is that, if there's no HBA geometry from the HBA driver, any
4801 		 * guess is good, since this is the physical geometry. If MODE
4802 		 * SENSE fails this gives a max cylinder size for non-LBA access
4803 		 */
4804 		nhead = 255;
4805 		nsect = 63;
4806 	} else {
4807 		nhead = un->un_lgeom.g_nhead;
4808 		nsect = un->un_lgeom.g_nsect;
4809 	}
4810 
4811 	if (ISCD(un)) {
4812 		pgeom_p->g_nhead = 1;
4813 		pgeom_p->g_nsect = nsect * nhead;
4814 	} else {
4815 		pgeom_p->g_nhead = nhead;
4816 		pgeom_p->g_nsect = nsect;
4817 	}
4818 
4819 	spc = pgeom_p->g_nhead * pgeom_p->g_nsect;
4820 	pgeom_p->g_capacity = capacity;
4821 	pgeom_p->g_ncyl = pgeom_p->g_capacity / spc;
4822 	pgeom_p->g_acyl = 0;
4823 
4824 	/*
4825 	 * Retrieve fresh geometry data from the hardware, stash it
4826 	 * here temporarily before we rebuild the incore label.
4827 	 *
4828 	 * We want to use the MODE SENSE commands to derive the
4829 	 * physical geometry of the device, but if either command
4830 	 * fails, the logical geometry is used as the fallback for
4831 	 * disk label geometry.
4832 	 */
4833 	mutex_exit(SD_MUTEX(un));
4834 	sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag);
4835 	mutex_enter(SD_MUTEX(un));
4836 
4837 	/*
4838 	 * Now update the real copy while holding the mutex. This
4839 	 * way the global copy is never in an inconsistent state.
4840 	 */
4841 	bcopy(pgeom_p, &un->un_pgeom,  sizeof (un->un_pgeom));
4842 
4843 	SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: "
4844 	    "(cached from lgeom)\n");
4845 	SD_INFO(SD_LOG_COMMON, un,
4846 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
4847 	    un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl,
4848 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
4849 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
4850 	    "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize,
4851 	    un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv,
4852 	    un->un_pgeom.g_rpm);
4853 }
4854 
4855 
4856 /*
4857  *    Function: sd_read_fdisk
4858  *
4859  * Description: utility routine to read the fdisk table.
4860  *
4861  *   Arguments: un - driver soft state (unit) structure
4862  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4863  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4864  *			to use the USCSI "direct" chain and bypass the normal
4865  *			command waitq.
4866  *
4867  * Return Code: SD_CMD_SUCCESS
4868  *		SD_CMD_FAILURE
4869  *
4870  *     Context: Kernel thread only (can sleep).
4871  */
4872 /* ARGSUSED */
4873 static int
4874 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag)
4875 {
4876 #if defined(_NO_FDISK_PRESENT)
4877 
4878 	un->un_solaris_offset = 0;
4879 	un->un_solaris_size = capacity;
4880 	bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4881 	return (SD_CMD_SUCCESS);
4882 
4883 #elif defined(_FIRMWARE_NEEDS_FDISK)
4884 
4885 	struct ipart	*fdp;
4886 	struct mboot	*mbp;
4887 	struct ipart	fdisk[FD_NUMPART];
4888 	int		i;
4889 	char		sigbuf[2];
4890 	caddr_t		bufp;
4891 	int		uidx;
4892 	int		rval;
4893 	int		lba = 0;
4894 	uint_t		solaris_offset;	/* offset to solaris part. */
4895 	daddr_t		solaris_size;	/* size of solaris partition */
4896 	uint32_t	blocksize;
4897 
4898 	ASSERT(un != NULL);
4899 	ASSERT(mutex_owned(SD_MUTEX(un)));
4900 	ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE);
4901 
4902 	blocksize = un->un_tgt_blocksize;
4903 
4904 	/*
4905 	 * Start off assuming no fdisk table
4906 	 */
4907 	solaris_offset = 0;
4908 	solaris_size   = capacity;
4909 
4910 	mutex_exit(SD_MUTEX(un));
4911 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
4912 	rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag);
4913 	mutex_enter(SD_MUTEX(un));
4914 
4915 	if (rval != 0) {
4916 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4917 		    "sd_read_fdisk: fdisk read err\n");
4918 		kmem_free(bufp, blocksize);
4919 		return (SD_CMD_FAILURE);
4920 	}
4921 
4922 	mbp = (struct mboot *)bufp;
4923 
4924 	/*
4925 	 * The fdisk table does not begin on a 4-byte boundary within the
4926 	 * master boot record, so we copy it to an aligned structure to avoid
4927 	 * alignment exceptions on some processors.
4928 	 */
4929 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
4930 
4931 	/*
4932 	 * Check for lba support before verifying sig; sig might not be
4933 	 * there, say on a blank disk, but the max_chs mark may still
4934 	 * be present.
4935 	 *
4936 	 * Note: LBA support and BEFs are an x86-only concept but this
4937 	 * code should work OK on SPARC as well.
4938 	 */
4939 
4940 	/*
4941 	 * First, check for lba-access-ok on root node (or prom root node)
4942 	 * if present there, don't need to search fdisk table.
4943 	 */
4944 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
4945 	    "lba-access-ok", 0) != 0) {
4946 		/* All drives do LBA; don't search fdisk table */
4947 		lba = 1;
4948 	} else {
4949 		/* Okay, look for mark in fdisk table */
4950 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
4951 			/* accumulate "lba" value from all partitions */
4952 			lba = (lba || sd_has_max_chs_vals(fdp));
4953 		}
4954 	}
4955 
4956 	if (lba != 0) {
4957 		dev_t dev = sd_make_device(SD_DEVINFO(un));
4958 
4959 		if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS,
4960 		    "lba-access-ok", 0) == 0) {
4961 			/* not found; create it */
4962 			if (ddi_prop_create(dev, SD_DEVINFO(un), 0,
4963 			    "lba-access-ok", (caddr_t)NULL, 0) !=
4964 			    DDI_PROP_SUCCESS) {
4965 				SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4966 				    "sd_read_fdisk: Can't create lba property "
4967 				    "for instance %d\n",
4968 				    ddi_get_instance(SD_DEVINFO(un)));
4969 			}
4970 		}
4971 	}
4972 
4973 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
4974 
4975 	/*
4976 	 * Endian-independent signature check
4977 	 */
4978 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
4979 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
4980 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4981 		    "sd_read_fdisk: no fdisk\n");
4982 		bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4983 		rval = SD_CMD_SUCCESS;
4984 		goto done;
4985 	}
4986 
4987 #ifdef SDDEBUG
4988 	if (sd_level_mask & SD_LOGMASK_INFO) {
4989 		fdp = fdisk;
4990 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n");
4991 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "         relsect    "
4992 		    "numsect         sysid       bootid\n");
4993 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
4994 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4995 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
4996 			    i, fdp->relsect, fdp->numsect,
4997 			    fdp->systid, fdp->bootid);
4998 		}
4999 	}
5000 #endif
5001 
5002 	/*
5003 	 * Try to find the unix partition
5004 	 */
5005 	uidx = -1;
5006 	solaris_offset = 0;
5007 	solaris_size   = 0;
5008 
5009 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
5010 		int	relsect;
5011 		int	numsect;
5012 
5013 		if (fdp->numsect == 0) {
5014 			un->un_fmap[i].fmap_start = 0;
5015 			un->un_fmap[i].fmap_nblk  = 0;
5016 			continue;
5017 		}
5018 
5019 		/*
5020 		 * Data in the fdisk table is little-endian.
5021 		 */
5022 		relsect = LE_32(fdp->relsect);
5023 		numsect = LE_32(fdp->numsect);
5024 
5025 		un->un_fmap[i].fmap_start = relsect;
5026 		un->un_fmap[i].fmap_nblk  = numsect;
5027 
5028 		if (fdp->systid != SUNIXOS &&
5029 		    fdp->systid != SUNIXOS2 &&
5030 		    fdp->systid != EFI_PMBR) {
5031 			continue;
5032 		}
5033 
5034 		/*
5035 		 * use the last active solaris partition id found
5036 		 * (there should only be 1 active partition id)
5037 		 *
5038 		 * if there are no active solaris partition id
5039 		 * then use the first inactive solaris partition id
5040 		 */
5041 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
5042 			uidx = i;
5043 			solaris_offset = relsect;
5044 			solaris_size   = numsect;
5045 		}
5046 	}
5047 
5048 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx",
5049 	    un->un_solaris_offset, un->un_solaris_size);
5050 
5051 	rval = SD_CMD_SUCCESS;
5052 
5053 done:
5054 
5055 	/*
5056 	 * Clear the VTOC info, only if the Solaris partition entry
5057 	 * has moved, changed size, been deleted, or if the size of
5058 	 * the partition is too small to even fit the label sector.
5059 	 */
5060 	if ((un->un_solaris_offset != solaris_offset) ||
5061 	    (un->un_solaris_size != solaris_size) ||
5062 	    solaris_size <= DK_LABEL_LOC) {
5063 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx",
5064 			solaris_offset, solaris_size);
5065 		bzero(&un->un_g, sizeof (struct dk_geom));
5066 		bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
5067 		bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map)));
5068 		un->un_f_geometry_is_valid = FALSE;
5069 	}
5070 	un->un_solaris_offset = solaris_offset;
5071 	un->un_solaris_size = solaris_size;
5072 	kmem_free(bufp, blocksize);
5073 	return (rval);
5074 
5075 #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
5076 #error "fdisk table presence undetermined for this platform."
5077 #endif	/* #if defined(_NO_FDISK_PRESENT) */
5078 }
5079 
5080 
5081 /*
5082  *    Function: sd_get_physical_geometry
5083  *
5084  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
5085  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
5086  *		target, and use this information to initialize the physical
5087  *		geometry cache specified by pgeom_p.
5088  *
5089  *		MODE SENSE is an optional command, so failure in this case
5090  *		does not necessarily denote an error. We want to use the
5091  *		MODE SENSE commands to derive the physical geometry of the
5092  *		device, but if either command fails, the logical geometry is
5093  *		used as the fallback for disk label geometry.
5094  *
5095  *		This requires that un->un_blockcount and un->un_tgt_blocksize
5096  *		have already been initialized for the current target and
5097  *		that the current values be passed as args so that we don't
5098  *		end up ever trying to use -1 as a valid value. This could
5099  *		happen if either value is reset while we're not holding
5100  *		the mutex.
5101  *
5102  *   Arguments: un - driver soft state (unit) structure
5103  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
5104  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
5105  *			to use the USCSI "direct" chain and bypass the normal
5106  *			command waitq.
5107  *
5108  *     Context: Kernel thread only (can sleep).
5109  */
5110 
5111 static void
5112 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p,
5113 	uint64_t capacity, int lbasize, int path_flag)
5114 {
5115 	struct	mode_format	*page3p;
5116 	struct	mode_geometry	*page4p;
5117 	struct	mode_header	*headerp;
5118 	int	sector_size;
5119 	int	nsect;
5120 	int	nhead;
5121 	int	ncyl;
5122 	int	intrlv;
5123 	int	spc;
5124 	int	modesense_capacity;
5125 	int	rpm;
5126 	int	bd_len;
5127 	int	mode_header_length;
5128 	uchar_t	*p3bufp;
5129 	uchar_t	*p4bufp;
5130 	int	cdbsize;
5131 
5132 	ASSERT(un != NULL);
5133 	ASSERT(!(mutex_owned(SD_MUTEX(un))));
5134 
5135 	if (un->un_f_blockcount_is_valid != TRUE) {
5136 		return;
5137 	}
5138 
5139 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
5140 		return;
5141 	}
5142 
5143 	if (lbasize == 0) {
5144 		if (ISCD(un)) {
5145 			lbasize = 2048;
5146 		} else {
5147 			lbasize = un->un_sys_blocksize;
5148 		}
5149 	}
5150 	pgeom_p->g_secsize = (unsigned short)lbasize;
5151 
5152 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
5153 
5154 	/*
5155 	 * Retrieve MODE SENSE page 3 - Format Device Page
5156 	 */
5157 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
5158 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp,
5159 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag)
5160 	    != 0) {
5161 		SD_ERROR(SD_LOG_COMMON, un,
5162 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
5163 		goto page3_exit;
5164 	}
5165 
5166 	/*
5167 	 * Determine size of Block Descriptors in order to locate the mode
5168 	 * page data.  ATAPI devices return 0, SCSI devices should return
5169 	 * MODE_BLK_DESC_LENGTH.
5170 	 */
5171 	headerp = (struct mode_header *)p3bufp;
5172 	if (un->un_f_cfg_is_atapi == TRUE) {
5173 		struct mode_header_grp2 *mhp =
5174 		    (struct mode_header_grp2 *)headerp;
5175 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
5176 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5177 	} else {
5178 		mode_header_length = MODE_HEADER_LENGTH;
5179 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5180 	}
5181 
5182 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5183 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
5184 		    "received unexpected bd_len of %d, page3\n", bd_len);
5185 		goto page3_exit;
5186 	}
5187 
5188 	page3p = (struct mode_format *)
5189 	    ((caddr_t)headerp + mode_header_length + bd_len);
5190 
5191 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5192 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
5193 		    "mode sense pg3 code mismatch %d\n",
5194 		    page3p->mode_page.code);
5195 		goto page3_exit;
5196 	}
5197 
5198 	/*
5199 	 * Use this physical geometry data only if BOTH MODE SENSE commands
5200 	 * complete successfully; otherwise, revert to the logical geometry.
5201 	 * So, we need to save everything in temporary variables.
5202 	 */
5203 	sector_size = BE_16(page3p->data_bytes_sect);
5204 
5205 	/*
5206 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5207 	 */
5208 	if (sector_size == 0) {
5209 		sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize;
5210 	} else {
5211 		sector_size &= ~(un->un_sys_blocksize - 1);
5212 	}
5213 
5214 	nsect  = BE_16(page3p->sect_track);
5215 	intrlv = BE_16(page3p->interleave);
5216 
5217 	SD_INFO(SD_LOG_COMMON, un,
5218 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
5219 	SD_INFO(SD_LOG_COMMON, un,
5220 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
5221 	    page3p->mode_page.code, nsect, sector_size);
5222 	SD_INFO(SD_LOG_COMMON, un,
5223 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5224 	    BE_16(page3p->track_skew),
5225 	    BE_16(page3p->cylinder_skew));
5226 
5227 
5228 	/*
5229 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5230 	 */
5231 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5232 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp,
5233 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag)
5234 	    != 0) {
5235 		SD_ERROR(SD_LOG_COMMON, un,
5236 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
5237 		goto page4_exit;
5238 	}
5239 
5240 	/*
5241 	 * Determine size of Block Descriptors in order to locate the mode
5242 	 * page data.  ATAPI devices return 0, SCSI devices should return
5243 	 * MODE_BLK_DESC_LENGTH.
5244 	 */
5245 	headerp = (struct mode_header *)p4bufp;
5246 	if (un->un_f_cfg_is_atapi == TRUE) {
5247 		struct mode_header_grp2 *mhp =
5248 		    (struct mode_header_grp2 *)headerp;
5249 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5250 	} else {
5251 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5252 	}
5253 
5254 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5255 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
5256 		    "received unexpected bd_len of %d, page4\n", bd_len);
5257 		goto page4_exit;
5258 	}
5259 
5260 	page4p = (struct mode_geometry *)
5261 	    ((caddr_t)headerp + mode_header_length + bd_len);
5262 
5263 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5264 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
5265 		    "mode sense pg4 code mismatch %d\n",
5266 		    page4p->mode_page.code);
5267 		goto page4_exit;
5268 	}
5269 
5270 	/*
5271 	 * Stash the data now, after we know that both commands completed.
5272 	 */
5273 
5274 	mutex_enter(SD_MUTEX(un));
5275 
5276 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
5277 	spc   = nhead * nsect;
5278 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5279 	rpm   = BE_16(page4p->rpm);
5280 
5281 	modesense_capacity = spc * ncyl;
5282 
5283 	SD_INFO(SD_LOG_COMMON, un,
5284 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5285 	SD_INFO(SD_LOG_COMMON, un,
5286 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5287 	SD_INFO(SD_LOG_COMMON, un,
5288 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
5289 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
5290 	    (void *)pgeom_p, capacity);
5291 
5292 	/*
5293 	 * Compensate if the drive's geometry is not rectangular, i.e.,
5294 	 * the product of C * H * S returned by MODE SENSE >= that returned
5295 	 * by read capacity. This is an idiosyncrasy of the original x86
5296 	 * disk subsystem.
5297 	 */
5298 	if (modesense_capacity >= capacity) {
5299 		SD_INFO(SD_LOG_COMMON, un,
5300 		    "sd_get_physical_geometry: adjusting acyl; "
5301 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
5302 		    (modesense_capacity - capacity + spc - 1) / spc);
5303 		if (sector_size != 0) {
5304 			/* 1243403: NEC D38x7 drives don't support sec size */
5305 			pgeom_p->g_secsize = (unsigned short)sector_size;
5306 		}
5307 		pgeom_p->g_nsect    = (unsigned short)nsect;
5308 		pgeom_p->g_nhead    = (unsigned short)nhead;
5309 		pgeom_p->g_capacity = capacity;
5310 		pgeom_p->g_acyl	    =
5311 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5312 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5313 	}
5314 
5315 	pgeom_p->g_rpm    = (unsigned short)rpm;
5316 	pgeom_p->g_intrlv = (unsigned short)intrlv;
5317 
5318 	SD_INFO(SD_LOG_COMMON, un,
5319 	    "sd_get_physical_geometry: mode sense geometry:\n");
5320 	SD_INFO(SD_LOG_COMMON, un,
5321 	    "   nsect: %d; sector size: %d; interlv: %d\n",
5322 	    nsect, sector_size, intrlv);
5323 	SD_INFO(SD_LOG_COMMON, un,
5324 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5325 	    nhead, ncyl, rpm, modesense_capacity);
5326 	SD_INFO(SD_LOG_COMMON, un,
5327 	    "sd_get_physical_geometry: (cached)\n");
5328 	SD_INFO(SD_LOG_COMMON, un,
5329 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5330 	    un->un_pgeom.g_ncyl,  un->un_pgeom.g_acyl,
5331 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
5332 	SD_INFO(SD_LOG_COMMON, un,
5333 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5334 	    un->un_pgeom.g_secsize, un->un_pgeom.g_capacity,
5335 	    un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm);
5336 
5337 	mutex_exit(SD_MUTEX(un));
5338 
5339 page4_exit:
5340 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5341 page3_exit:
5342 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5343 }
5344 
5345 
5346 /*
5347  *    Function: sd_get_virtual_geometry
5348  *
5349  * Description: Ask the controller to tell us about the target device.
5350  *
5351  *   Arguments: un - pointer to softstate
5352  *		capacity - disk capacity in #blocks
5353  *		lbasize - disk block size in bytes
5354  *
5355  *     Context: Kernel thread only
5356  */
5357 
5358 static void
5359 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize)
5360 {
5361 	struct	geom_cache 	*lgeom_p = &un->un_lgeom;
5362 	uint_t	geombuf;
5363 	int	spc;
5364 
5365 	ASSERT(un != NULL);
5366 	ASSERT(mutex_owned(SD_MUTEX(un)));
5367 
5368 	mutex_exit(SD_MUTEX(un));
5369 
5370 	/* Set sector size, and total number of sectors */
5371 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5372 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5373 
5374 	/* Let the HBA tell us its geometry */
5375 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5376 
5377 	mutex_enter(SD_MUTEX(un));
5378 
5379 	/* A value of -1 indicates an undefined "geometry" property */
5380 	if (geombuf == (-1)) {
5381 		return;
5382 	}
5383 
5384 	/* Initialize the logical geometry cache. */
5385 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5386 	lgeom_p->g_nsect   = geombuf & 0xffff;
5387 	lgeom_p->g_secsize = un->un_sys_blocksize;
5388 
5389 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5390 
5391 	/*
5392 	 * Note: The driver originally converted the capacity value from
5393 	 * target blocks to system blocks. However, the capacity value passed
5394 	 * to this routine is already in terms of system blocks (this scaling
5395 	 * is done when the READ CAPACITY command is issued and processed).
5396 	 * This 'error' may have gone undetected because the usage of g_ncyl
5397 	 * (which is based upon g_capacity) is very limited within the driver
5398 	 */
5399 	lgeom_p->g_capacity = capacity;
5400 
5401 	/*
5402 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5403 	 * hba may return zero values if the device has been removed.
5404 	 */
5405 	if (spc == 0) {
5406 		lgeom_p->g_ncyl = 0;
5407 	} else {
5408 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5409 	}
5410 	lgeom_p->g_acyl = 0;
5411 
5412 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5413 	SD_INFO(SD_LOG_COMMON, un,
5414 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5415 	    un->un_lgeom.g_ncyl,  un->un_lgeom.g_acyl,
5416 	    un->un_lgeom.g_nhead, un->un_lgeom.g_nsect);
5417 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
5418 	    "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize,
5419 	    un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm);
5420 }
5421 
5422 
5423 /*
5424  *    Function: sd_update_block_info
5425  *
5426  * Description: Calculate a byte count to sector count bitshift value
5427  *		from sector size.
5428  *
5429  *   Arguments: un: unit struct.
5430  *		lbasize: new target sector size
5431  *		capacity: new target capacity, ie. block count
5432  *
5433  *     Context: Kernel thread context
5434  */
5435 
5436 static void
5437 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5438 {
5439 	if (lbasize != 0) {
5440 		un->un_tgt_blocksize = lbasize;
5441 		un->un_f_tgt_blocksize_is_valid	= TRUE;
5442 	}
5443 
5444 	if (capacity != 0) {
5445 		un->un_blockcount		= capacity;
5446 		un->un_f_blockcount_is_valid	= TRUE;
5447 	}
5448 }
5449 
5450 
5451 static void
5452 sd_swap_efi_gpt(efi_gpt_t *e)
5453 {
5454 	_NOTE(ASSUMING_PROTECTED(*e))
5455 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
5456 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
5457 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
5458 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
5459 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
5460 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
5461 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
5462 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
5463 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
5464 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
5465 	e->efi_gpt_NumberOfPartitionEntries =
5466 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
5467 	e->efi_gpt_SizeOfPartitionEntry =
5468 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
5469 	e->efi_gpt_PartitionEntryArrayCRC32 =
5470 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
5471 }
5472 
5473 static void
5474 sd_swap_efi_gpe(int nparts, efi_gpe_t *p)
5475 {
5476 	int i;
5477 
5478 	_NOTE(ASSUMING_PROTECTED(*p))
5479 	for (i = 0; i < nparts; i++) {
5480 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
5481 		    p[i].efi_gpe_PartitionTypeGUID);
5482 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
5483 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
5484 		/* PartitionAttrs */
5485 	}
5486 }
5487 
5488 static int
5489 sd_validate_efi(efi_gpt_t *labp)
5490 {
5491 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
5492 		return (EINVAL);
5493 	/* at least 96 bytes in this version of the spec. */
5494 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
5495 	    labp->efi_gpt_HeaderSize)
5496 		return (EINVAL);
5497 	/* this should be 128 bytes */
5498 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
5499 		return (EINVAL);
5500 	return (0);
5501 }
5502 
5503 static int
5504 sd_use_efi(struct sd_lun *un, int path_flag)
5505 {
5506 	int		i;
5507 	int		rval = 0;
5508 	efi_gpe_t	*partitions;
5509 	uchar_t		*buf;
5510 	uint_t		lbasize;
5511 	uint64_t	cap = 0;
5512 	uint_t		nparts;
5513 	diskaddr_t	gpe_lba;
5514 	struct uuid	uuid_type_reserved = EFI_RESERVED;
5515 
5516 	ASSERT(mutex_owned(SD_MUTEX(un)));
5517 	lbasize = un->un_tgt_blocksize;
5518 	un->un_reserved = -1;
5519 
5520 	mutex_exit(SD_MUTEX(un));
5521 
5522 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
5523 
5524 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
5525 		rval = EINVAL;
5526 		goto done_err;
5527 	}
5528 
5529 	rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag);
5530 	if (rval) {
5531 		goto done_err;
5532 	}
5533 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
5534 		/* not ours */
5535 		rval = ESRCH;
5536 		goto done_err;
5537 	}
5538 
5539 	rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag);
5540 	if (rval) {
5541 		goto done_err;
5542 	}
5543 	sd_swap_efi_gpt((efi_gpt_t *)buf);
5544 
5545 	if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) {
5546 		/*
5547 		 * Couldn't read the primary, try the backup.  Our
5548 		 * capacity at this point could be based on CHS, so
5549 		 * check what the device reports.
5550 		 */
5551 		rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
5552 		    path_flag);
5553 		if (rval) {
5554 			goto done_err;
5555 		}
5556 
5557 		/*
5558 		 * The MMC standard allows READ CAPACITY to be
5559 		 * inaccurate by a bounded amount (in the interest of
5560 		 * response latency).  As a result, failed READs are
5561 		 * commonplace (due to the reading of metadata and not
5562 		 * data). Depending on the per-Vendor/drive Sense data,
5563 		 * the failed READ can cause many (unnecessary) retries.
5564 		 */
5565 
5566 		/*
5567 		 * Refer to comments related to off-by-1 at the
5568 		 * header of this file. Search the next to last
5569 		 * block for backup EFI label.
5570 		 */
5571 		if ((rval = sd_send_scsi_READ(un, buf, lbasize,
5572 		    cap - 2, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY :
5573 			path_flag)) != 0) {
5574 				goto done_err;
5575 		}
5576 
5577 		sd_swap_efi_gpt((efi_gpt_t *)buf);
5578 		if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) {
5579 			if ((rval = sd_send_scsi_READ(un, buf, lbasize,
5580 			    cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY :
5581 			    path_flag)) != 0) {
5582 				goto done_err;
5583 			}
5584 			sd_swap_efi_gpt((efi_gpt_t *)buf);
5585 			if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0)
5586 				goto done_err;
5587 		}
5588 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5589 		    "primary label corrupt; using backup\n");
5590 	}
5591 
5592 	if (cap == 0)
5593 		rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
5594 		    path_flag);
5595 
5596 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
5597 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
5598 
5599 	rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba,
5600 	    path_flag);
5601 	if (rval) {
5602 		goto done_err;
5603 	}
5604 	partitions = (efi_gpe_t *)buf;
5605 
5606 	if (nparts > MAXPART) {
5607 		nparts = MAXPART;
5608 	}
5609 	sd_swap_efi_gpe(nparts, partitions);
5610 
5611 	mutex_enter(SD_MUTEX(un));
5612 
5613 	/* Fill in partition table. */
5614 	for (i = 0; i < nparts; i++) {
5615 		if (partitions->efi_gpe_StartingLBA != 0 ||
5616 		    partitions->efi_gpe_EndingLBA != 0) {
5617 			un->un_map[i].dkl_cylno =
5618 			    partitions->efi_gpe_StartingLBA;
5619 			un->un_map[i].dkl_nblk =
5620 			    partitions->efi_gpe_EndingLBA -
5621 			    partitions->efi_gpe_StartingLBA + 1;
5622 			un->un_offset[i] =
5623 			    partitions->efi_gpe_StartingLBA;
5624 		}
5625 		if (un->un_reserved == -1) {
5626 			if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
5627 			    &uuid_type_reserved, sizeof (struct uuid)) == 0) {
5628 				un->un_reserved = i;
5629 			}
5630 		}
5631 		if (i == WD_NODE) {
5632 			/*
5633 			 * minor number 7 corresponds to the whole disk
5634 			 */
5635 			un->un_map[i].dkl_cylno = 0;
5636 			un->un_map[i].dkl_nblk = un->un_blockcount;
5637 			un->un_offset[i] = 0;
5638 		}
5639 		partitions++;
5640 	}
5641 	un->un_solaris_offset = 0;
5642 	un->un_solaris_size = cap;
5643 	un->un_f_geometry_is_valid = TRUE;
5644 
5645 	/* clear the vtoc label */
5646 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
5647 
5648 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
5649 
5650 	/*
5651 	 * For EFI labeled disk, create and set the partition stats
5652 	 * at attach time, update the stats according to dynamic
5653 	 * partition changes during running time.
5654 	 */
5655 	if (un->un_f_pkstats_enabled) {
5656 		sd_set_pstats(un);
5657 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_use_efi: "
5658 		    "un:0x%p pstats created and set, or updated\n", un);
5659 	}
5660 	return (0);
5661 
5662 done_err:
5663 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
5664 	mutex_enter(SD_MUTEX(un));
5665 	/*
5666 	 * if we didn't find something that could look like a VTOC
5667 	 * and the disk is over 1TB, we know there isn't a valid label.
5668 	 * Otherwise let sd_uselabel decide what to do.  We only
5669 	 * want to invalidate this if we're certain the label isn't
5670 	 * valid because sd_prop_op will now fail, which in turn
5671 	 * causes things like opens and stats on the partition to fail.
5672 	 */
5673 	if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) {
5674 		un->un_f_geometry_is_valid = FALSE;
5675 	}
5676 	return (rval);
5677 }
5678 
5679 
5680 /*
5681  *    Function: sd_uselabel
5682  *
5683  * Description: Validate the disk label and update the relevant data (geometry,
5684  *		partition, vtoc, and capacity data) in the sd_lun struct.
5685  *		Marks the geometry of the unit as being valid.
5686  *
5687  *   Arguments: un: unit struct.
5688  *		dk_label: disk label
5689  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
5690  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
5691  *			to use the USCSI "direct" chain and bypass the normal
5692  *			command waitq.
5693  *
5694  * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry,
5695  *		partition, vtoc, and capacity data are good.
5696  *
5697  *		SD_LABEL_IS_INVALID: Magic number or checksum error in the
5698  *		label; or computed capacity does not jibe with capacity
5699  *		reported from the READ CAPACITY command.
5700  *
5701  *     Context: Kernel thread only (can sleep).
5702  */
5703 
5704 static int
5705 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag)
5706 {
5707 	short	*sp;
5708 	short	sum;
5709 	short	count;
5710 	int	label_error = SD_LABEL_IS_VALID;
5711 	int	i;
5712 	int	capacity;
5713 	int	part_end;
5714 	int	track_capacity;
5715 	int	err;
5716 #if defined(_SUNOS_VTOC_16)
5717 	struct	dkl_partition	*vpartp;
5718 #endif
5719 	ASSERT(un != NULL);
5720 	ASSERT(mutex_owned(SD_MUTEX(un)));
5721 
5722 	/* Validate the magic number of the label. */
5723 	if (labp->dkl_magic != DKL_MAGIC) {
5724 #if defined(__sparc)
5725 		if ((un->un_state == SD_STATE_NORMAL) &&
5726 			un->un_f_vtoc_errlog_supported) {
5727 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5728 			    "Corrupt label; wrong magic number\n");
5729 		}
5730 #endif
5731 		return (SD_LABEL_IS_INVALID);
5732 	}
5733 
5734 	/* Validate the checksum of the label. */
5735 	sp  = (short *)labp;
5736 	sum = 0;
5737 	count = sizeof (struct dk_label) / sizeof (short);
5738 	while (count--)	 {
5739 		sum ^= *sp++;
5740 	}
5741 
5742 	if (sum != 0) {
5743 #if	defined(_SUNOS_VTOC_16)
5744 		if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) {
5745 #elif defined(_SUNOS_VTOC_8)
5746 		if ((un->un_state == SD_STATE_NORMAL) &&
5747 		    un->un_f_vtoc_errlog_supported) {
5748 #endif
5749 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5750 			    "Corrupt label - label checksum failed\n");
5751 		}
5752 		return (SD_LABEL_IS_INVALID);
5753 	}
5754 
5755 
5756 	/*
5757 	 * Fill in geometry structure with data from label.
5758 	 */
5759 	bzero(&un->un_g, sizeof (struct dk_geom));
5760 	un->un_g.dkg_ncyl   = labp->dkl_ncyl;
5761 	un->un_g.dkg_acyl   = labp->dkl_acyl;
5762 	un->un_g.dkg_bcyl   = 0;
5763 	un->un_g.dkg_nhead  = labp->dkl_nhead;
5764 	un->un_g.dkg_nsect  = labp->dkl_nsect;
5765 	un->un_g.dkg_intrlv = labp->dkl_intrlv;
5766 
5767 #if defined(_SUNOS_VTOC_8)
5768 	un->un_g.dkg_gap1   = labp->dkl_gap1;
5769 	un->un_g.dkg_gap2   = labp->dkl_gap2;
5770 	un->un_g.dkg_bhead  = labp->dkl_bhead;
5771 #endif
5772 #if defined(_SUNOS_VTOC_16)
5773 	un->un_dkg_skew = labp->dkl_skew;
5774 #endif
5775 
5776 #if defined(__i386) || defined(__amd64)
5777 	un->un_g.dkg_apc = labp->dkl_apc;
5778 #endif
5779 
5780 	/*
5781 	 * Currently we rely on the values in the label being accurate. If
5782 	 * dlk_rpm or dlk_pcly are zero in the label, use a default value.
5783 	 *
5784 	 * Note: In the future a MODE SENSE may be used to retrieve this data,
5785 	 * although this command is optional in SCSI-2.
5786 	 */
5787 	un->un_g.dkg_rpm  = (labp->dkl_rpm  != 0) ? labp->dkl_rpm  : 3600;
5788 	un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
5789 	    (un->un_g.dkg_ncyl + un->un_g.dkg_acyl);
5790 
5791 	/*
5792 	 * The Read and Write reinstruct values may not be valid
5793 	 * for older disks.
5794 	 */
5795 	un->un_g.dkg_read_reinstruct  = labp->dkl_read_reinstruct;
5796 	un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
5797 
5798 	/* Fill in partition table. */
5799 #if defined(_SUNOS_VTOC_8)
5800 	for (i = 0; i < NDKMAP; i++) {
5801 		un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
5802 		un->un_map[i].dkl_nblk  = labp->dkl_map[i].dkl_nblk;
5803 	}
5804 #endif
5805 #if  defined(_SUNOS_VTOC_16)
5806 	vpartp		= labp->dkl_vtoc.v_part;
5807 	track_capacity	= labp->dkl_nhead * labp->dkl_nsect;
5808 
5809 	/* Prevent divide by zero */
5810 	if (track_capacity == 0) {
5811 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5812 		    "Corrupt label - zero nhead or nsect value\n");
5813 
5814 		return (SD_LABEL_IS_INVALID);
5815 	}
5816 
5817 	for (i = 0; i < NDKMAP; i++, vpartp++) {
5818 		un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity;
5819 		un->un_map[i].dkl_nblk  = vpartp->p_size;
5820 	}
5821 #endif
5822 
5823 	/* Fill in VTOC Structure. */
5824 	bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc));
5825 #if defined(_SUNOS_VTOC_8)
5826 	/*
5827 	 * The 8-slice vtoc does not include the ascii label; save it into
5828 	 * the device's soft state structure here.
5829 	 */
5830 	bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII);
5831 #endif
5832 
5833 	/* Now look for a valid capacity. */
5834 	track_capacity	= (un->un_g.dkg_nhead * un->un_g.dkg_nsect);
5835 	capacity	= (un->un_g.dkg_ncyl  * track_capacity);
5836 
5837 	if (un->un_g.dkg_acyl) {
5838 #if defined(__i386) || defined(__amd64)
5839 		/* we may have > 1 alts cylinder */
5840 		capacity += (track_capacity * un->un_g.dkg_acyl);
5841 #else
5842 		capacity += track_capacity;
5843 #endif
5844 	}
5845 
5846 	/*
5847 	 * Force check here to ensure the computed capacity is valid.
5848 	 * If capacity is zero, it indicates an invalid label and
5849 	 * we should abort updating the relevant data then.
5850 	 */
5851 	if (capacity == 0) {
5852 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5853 		    "Corrupt label - no valid capacity could be retrieved\n");
5854 
5855 		return (SD_LABEL_IS_INVALID);
5856 	}
5857 
5858 	/* Mark the geometry as valid. */
5859 	un->un_f_geometry_is_valid = TRUE;
5860 
5861 	/*
5862 	 * At this point, un->un_blockcount should contain valid data from
5863 	 * the READ CAPACITY command.
5864 	 */
5865 	if (un->un_f_blockcount_is_valid != TRUE) {
5866 		/*
5867 		 * We have a situation where the target didn't give us a good
5868 		 * READ CAPACITY value, yet there appears to be a valid label.
5869 		 * In this case, we'll fake the capacity.
5870 		 */
5871 		un->un_blockcount = capacity;
5872 		un->un_f_blockcount_is_valid = TRUE;
5873 		goto done;
5874 	}
5875 
5876 
5877 	if ((capacity <= un->un_blockcount) ||
5878 	    (un->un_state != SD_STATE_NORMAL)) {
5879 #if defined(_SUNOS_VTOC_8)
5880 		/*
5881 		 * We can't let this happen on drives that are subdivided
5882 		 * into logical disks (i.e., that have an fdisk table).
5883 		 * The un_blockcount field should always hold the full media
5884 		 * size in sectors, period.  This code would overwrite
5885 		 * un_blockcount with the size of the Solaris fdisk partition.
5886 		 */
5887 		SD_ERROR(SD_LOG_COMMON, un,
5888 		    "sd_uselabel: Label %d blocks; Drive %d blocks\n",
5889 		    capacity, un->un_blockcount);
5890 		un->un_blockcount = capacity;
5891 		un->un_f_blockcount_is_valid = TRUE;
5892 #endif	/* defined(_SUNOS_VTOC_8) */
5893 		goto done;
5894 	}
5895 
5896 	if (ISCD(un)) {
5897 		/* For CDROMs, we trust that the data in the label is OK. */
5898 #if defined(_SUNOS_VTOC_8)
5899 		for (i = 0; i < NDKMAP; i++) {
5900 			part_end = labp->dkl_nhead * labp->dkl_nsect *
5901 			    labp->dkl_map[i].dkl_cylno +
5902 			    labp->dkl_map[i].dkl_nblk  - 1;
5903 
5904 			if ((labp->dkl_map[i].dkl_nblk) &&
5905 			    (part_end > un->un_blockcount)) {
5906 				un->un_f_geometry_is_valid = FALSE;
5907 				break;
5908 			}
5909 		}
5910 #endif
5911 #if defined(_SUNOS_VTOC_16)
5912 		vpartp = &(labp->dkl_vtoc.v_part[0]);
5913 		for (i = 0; i < NDKMAP; i++, vpartp++) {
5914 			part_end = vpartp->p_start + vpartp->p_size;
5915 			if ((vpartp->p_size > 0) &&
5916 			    (part_end > un->un_blockcount)) {
5917 				un->un_f_geometry_is_valid = FALSE;
5918 				break;
5919 			}
5920 		}
5921 #endif
5922 	} else {
5923 		uint64_t t_capacity;
5924 		uint32_t t_lbasize;
5925 
5926 		mutex_exit(SD_MUTEX(un));
5927 		err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize,
5928 		    path_flag);
5929 		ASSERT(t_capacity <= DK_MAX_BLOCKS);
5930 		mutex_enter(SD_MUTEX(un));
5931 
5932 		if (err == 0) {
5933 			sd_update_block_info(un, t_lbasize, t_capacity);
5934 		}
5935 
5936 		if (capacity > un->un_blockcount) {
5937 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5938 			    "Corrupt label - bad geometry\n");
5939 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
5940 			    "Label says %u blocks; Drive says %llu blocks\n",
5941 			    capacity, (unsigned long long)un->un_blockcount);
5942 			un->un_f_geometry_is_valid = FALSE;
5943 			label_error = SD_LABEL_IS_INVALID;
5944 		}
5945 	}
5946 
5947 done:
5948 
5949 	SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n");
5950 	SD_INFO(SD_LOG_COMMON, un,
5951 	    "   ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
5952 	    un->un_g.dkg_ncyl,  un->un_g.dkg_acyl,
5953 	    un->un_g.dkg_nhead, un->un_g.dkg_nsect);
5954 	SD_INFO(SD_LOG_COMMON, un,
5955 	    "   lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n",
5956 	    un->un_tgt_blocksize, un->un_blockcount,
5957 	    un->un_g.dkg_intrlv, un->un_g.dkg_rpm);
5958 	SD_INFO(SD_LOG_COMMON, un, "   wrt_reinstr: %d; rd_reinstr: %d\n",
5959 	    un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct);
5960 
5961 	ASSERT(mutex_owned(SD_MUTEX(un)));
5962 
5963 	return (label_error);
5964 }
5965 
5966 
5967 /*
5968  *    Function: sd_build_default_label
5969  *
5970  * Description: Generate a default label for those devices that do not have
5971  *		one, e.g., new media, removable cartridges, etc..
5972  *
5973  *     Context: Kernel thread only
5974  */
5975 
5976 static void
5977 sd_build_default_label(struct sd_lun *un)
5978 {
5979 #if defined(_SUNOS_VTOC_16)
5980 	uint_t	phys_spc;
5981 	uint_t	disksize;
5982 	struct	dk_geom un_g;
5983 	uint64_t capacity;
5984 #endif
5985 
5986 	ASSERT(un != NULL);
5987 	ASSERT(mutex_owned(SD_MUTEX(un)));
5988 
5989 #if defined(_SUNOS_VTOC_8)
5990 	/*
5991 	 * Note: This is a legacy check for non-removable devices on VTOC_8
5992 	 * only. This may be a valid check for VTOC_16 as well.
5993 	 * Once we understand why there is this difference between SPARC and
5994 	 * x86 platform, we could remove this legacy check.
5995 	 */
5996 	ASSERT(un->un_f_default_vtoc_supported);
5997 #endif
5998 
5999 	bzero(&un->un_g, sizeof (struct dk_geom));
6000 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
6001 	bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map)));
6002 
6003 #if defined(_SUNOS_VTOC_8)
6004 
6005 	/*
6006 	 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
6007 	 * But it is still necessary to set up various geometry information,
6008 	 * and we are doing this here.
6009 	 */
6010 
6011 	/*
6012 	 * For the rpm, we use the minimum for the disk.  For the head, cyl,
6013 	 * and number of sector per track, if the capacity <= 1GB, head = 64,
6014 	 * sect = 32.  else head = 255, sect 63 Note: the capacity should be
6015 	 * equal to C*H*S values.  This will cause some truncation of size due
6016 	 * to round off errors. For CD-ROMs, this truncation can have adverse
6017 	 * side effects, so returning ncyl and nhead as 1. The nsect will
6018 	 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
6019 	 */
6020 	if (ISCD(un)) {
6021 		/*
6022 		 * Preserve the old behavior for non-writable
6023 		 * medias. Since dkg_nsect is a ushort, it
6024 		 * will lose bits as cdroms have more than
6025 		 * 65536 sectors. So if we recalculate
6026 		 * capacity, it will become much shorter.
6027 		 * But the dkg_* information is not
6028 		 * used for CDROMs so it is OK. But for
6029 		 * Writable CDs we need this information
6030 		 * to be valid (for newfs say). So we
6031 		 * make nsect and nhead > 1 that way
6032 		 * nsect can still stay within ushort limit
6033 		 * without losing any bits.
6034 		 */
6035 		if (un->un_f_mmc_writable_media == TRUE) {
6036 			un->un_g.dkg_nhead = 64;
6037 			un->un_g.dkg_nsect = 32;
6038 			un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32);
6039 			un->un_blockcount = un->un_g.dkg_ncyl *
6040 			    un->un_g.dkg_nhead * un->un_g.dkg_nsect;
6041 		} else {
6042 			un->un_g.dkg_ncyl  = 1;
6043 			un->un_g.dkg_nhead = 1;
6044 			un->un_g.dkg_nsect = un->un_blockcount;
6045 		}
6046 	} else {
6047 		if (un->un_blockcount <= 0x1000) {
6048 			/* unlabeled SCSI floppy device */
6049 			un->un_g.dkg_nhead = 2;
6050 			un->un_g.dkg_ncyl = 80;
6051 			un->un_g.dkg_nsect = un->un_blockcount / (2 * 80);
6052 		} else if (un->un_blockcount <= 0x200000) {
6053 			un->un_g.dkg_nhead = 64;
6054 			un->un_g.dkg_nsect = 32;
6055 			un->un_g.dkg_ncyl  = un->un_blockcount / (64 * 32);
6056 		} else {
6057 			un->un_g.dkg_nhead = 255;
6058 			un->un_g.dkg_nsect = 63;
6059 			un->un_g.dkg_ncyl  = un->un_blockcount / (255 * 63);
6060 		}
6061 		un->un_blockcount =
6062 		    un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect;
6063 	}
6064 
6065 	un->un_g.dkg_acyl	= 0;
6066 	un->un_g.dkg_bcyl	= 0;
6067 	un->un_g.dkg_rpm	= 200;
6068 	un->un_asciilabel[0]	= '\0';
6069 	un->un_g.dkg_pcyl	= un->un_g.dkg_ncyl;
6070 
6071 	un->un_map[0].dkl_cylno = 0;
6072 	un->un_map[0].dkl_nblk  = un->un_blockcount;
6073 	un->un_map[2].dkl_cylno = 0;
6074 	un->un_map[2].dkl_nblk  = un->un_blockcount;
6075 
6076 #elif defined(_SUNOS_VTOC_16)
6077 
6078 	if (un->un_solaris_size == 0) {
6079 		/*
6080 		 * Got fdisk table but no solaris entry therefore
6081 		 * don't create a default label
6082 		 */
6083 		un->un_f_geometry_is_valid = TRUE;
6084 		return;
6085 	}
6086 
6087 	/*
6088 	 * For CDs we continue to use the physical geometry to calculate
6089 	 * number of cylinders. All other devices must convert the
6090 	 * physical geometry (geom_cache) to values that will fit
6091 	 * in a dk_geom structure.
6092 	 */
6093 	if (ISCD(un)) {
6094 		phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect;
6095 	} else {
6096 		/* Convert physical geometry to disk geometry */
6097 		bzero(&un_g, sizeof (struct dk_geom));
6098 
6099 		/*
6100 		 * Refer to comments related to off-by-1 at the
6101 		 * header of this file.
6102 		 * Before caculating geometry, capacity should be
6103 		 * decreased by 1. That un_f_capacity_adjusted is
6104 		 * TRUE means that we are treating a 1TB disk as
6105 		 * (1T - 512)B. And the capacity of disks is already
6106 		 * decreased by 1.
6107 		 */
6108 		if (!un->un_f_capacity_adjusted &&
6109 		    !un->un_f_has_removable_media &&
6110 		    !un->un_f_is_hotpluggable &&
6111 			un->un_tgt_blocksize == un->un_sys_blocksize)
6112 			capacity = un->un_blockcount - 1;
6113 		else
6114 			capacity = un->un_blockcount;
6115 
6116 		sd_convert_geometry(capacity, &un_g);
6117 		bcopy(&un_g, &un->un_g, sizeof (un->un_g));
6118 		phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect;
6119 	}
6120 
6121 	ASSERT(phys_spc != 0);
6122 	un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc;
6123 	un->un_g.dkg_acyl = DK_ACYL;
6124 	un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL;
6125 	disksize = un->un_g.dkg_ncyl * phys_spc;
6126 
6127 	if (ISCD(un)) {
6128 		/*
6129 		 * CD's don't use the "heads * sectors * cyls"-type of
6130 		 * geometry, but instead use the entire capacity of the media.
6131 		 */
6132 		disksize = un->un_solaris_size;
6133 		un->un_g.dkg_nhead = 1;
6134 		un->un_g.dkg_nsect = 1;
6135 		un->un_g.dkg_rpm =
6136 		    (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm;
6137 
6138 		un->un_vtoc.v_part[0].p_start = 0;
6139 		un->un_vtoc.v_part[0].p_size  = disksize;
6140 		un->un_vtoc.v_part[0].p_tag   = V_BACKUP;
6141 		un->un_vtoc.v_part[0].p_flag  = V_UNMNT;
6142 
6143 		un->un_map[0].dkl_cylno = 0;
6144 		un->un_map[0].dkl_nblk  = disksize;
6145 		un->un_offset[0] = 0;
6146 
6147 	} else {
6148 		/*
6149 		 * Hard disks and removable media cartridges
6150 		 */
6151 		un->un_g.dkg_rpm =
6152 		    (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm;
6153 		un->un_vtoc.v_sectorsz = un->un_sys_blocksize;
6154 
6155 		/* Add boot slice */
6156 		un->un_vtoc.v_part[8].p_start = 0;
6157 		un->un_vtoc.v_part[8].p_size  = phys_spc;
6158 		un->un_vtoc.v_part[8].p_tag   = V_BOOT;
6159 		un->un_vtoc.v_part[8].p_flag  = V_UNMNT;
6160 
6161 		un->un_map[8].dkl_cylno = 0;
6162 		un->un_map[8].dkl_nblk  = phys_spc;
6163 		un->un_offset[8] = 0;
6164 	}
6165 
6166 	un->un_g.dkg_apc = 0;
6167 	un->un_vtoc.v_nparts = V_NUMPAR;
6168 	un->un_vtoc.v_version = V_VERSION;
6169 
6170 	/* Add backup slice */
6171 	un->un_vtoc.v_part[2].p_start = 0;
6172 	un->un_vtoc.v_part[2].p_size  = disksize;
6173 	un->un_vtoc.v_part[2].p_tag   = V_BACKUP;
6174 	un->un_vtoc.v_part[2].p_flag  = V_UNMNT;
6175 
6176 	un->un_map[2].dkl_cylno = 0;
6177 	un->un_map[2].dkl_nblk  = disksize;
6178 	un->un_offset[2] = 0;
6179 
6180 	(void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
6181 	    " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl,
6182 	    un->un_g.dkg_nhead, un->un_g.dkg_nsect);
6183 
6184 #else
6185 #error "No VTOC format defined."
6186 #endif
6187 
6188 	un->un_g.dkg_read_reinstruct  = 0;
6189 	un->un_g.dkg_write_reinstruct = 0;
6190 
6191 	un->un_g.dkg_intrlv = 1;
6192 
6193 	un->un_vtoc.v_sanity  = VTOC_SANE;
6194 
6195 	un->un_f_geometry_is_valid = TRUE;
6196 
6197 	SD_INFO(SD_LOG_COMMON, un,
6198 	    "sd_build_default_label: Default label created: "
6199 	    "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
6200 	    un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead,
6201 	    un->un_g.dkg_nsect, un->un_blockcount);
6202 }
6203 
6204 
6205 #if defined(_FIRMWARE_NEEDS_FDISK)
6206 /*
6207  * Max CHS values, as they are encoded into bytes, for 1022/254/63
6208  */
6209 #define	LBA_MAX_SECT	(63 | ((1022 & 0x300) >> 2))
6210 #define	LBA_MAX_CYL	(1022 & 0xFF)
6211 #define	LBA_MAX_HEAD	(254)
6212 
6213 
6214 /*
6215  *    Function: sd_has_max_chs_vals
6216  *
6217  * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum.
6218  *
6219  *   Arguments: fdp - ptr to CHS info
6220  *
6221  * Return Code: True or false
6222  *
6223  *     Context: Any.
6224  */
6225 
6226 static int
6227 sd_has_max_chs_vals(struct ipart *fdp)
6228 {
6229 	return ((fdp->begcyl  == LBA_MAX_CYL)	&&
6230 	    (fdp->beghead == LBA_MAX_HEAD)	&&
6231 	    (fdp->begsect == LBA_MAX_SECT)	&&
6232 	    (fdp->endcyl  == LBA_MAX_CYL)	&&
6233 	    (fdp->endhead == LBA_MAX_HEAD)	&&
6234 	    (fdp->endsect == LBA_MAX_SECT));
6235 }
6236 #endif
6237 
6238 
6239 /*
6240  *    Function: sd_inq_fill
6241  *
6242  * Description: Print a piece of inquiry data, cleaned up for non-printable
6243  *		characters and stopping at the first space character after
6244  *		the beginning of the passed string;
6245  *
6246  *   Arguments: p - source string
6247  *		l - maximum length to copy
6248  *		s - destination string
6249  *
6250  *     Context: Any.
6251  */
6252 
6253 static void
6254 sd_inq_fill(char *p, int l, char *s)
6255 {
6256 	unsigned i = 0;
6257 	char c;
6258 
6259 	while (i++ < l) {
6260 		if ((c = *p++) < ' ' || c >= 0x7F) {
6261 			c = '*';
6262 		} else if (i != 1 && c == ' ') {
6263 			break;
6264 		}
6265 		*s++ = c;
6266 	}
6267 	*s++ = 0;
6268 }
6269 
6270 
6271 /*
6272  *    Function: sd_register_devid
6273  *
6274  * Description: This routine will obtain the device id information from the
6275  *		target, obtain the serial number, and register the device
6276  *		id with the ddi framework.
6277  *
6278  *   Arguments: devi - the system's dev_info_t for the device.
6279  *		un - driver soft state (unit) structure
6280  *		reservation_flag - indicates if a reservation conflict
6281  *		occurred during attach
6282  *
6283  *     Context: Kernel Thread
6284  */
6285 static void
6286 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag)
6287 {
6288 	int		rval		= 0;
6289 	uchar_t		*inq80		= NULL;
6290 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
6291 	size_t		inq80_resid	= 0;
6292 	uchar_t		*inq83		= NULL;
6293 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
6294 	size_t		inq83_resid	= 0;
6295 
6296 	ASSERT(un != NULL);
6297 	ASSERT(mutex_owned(SD_MUTEX(un)));
6298 	ASSERT((SD_DEVINFO(un)) == devi);
6299 
6300 	/*
6301 	 * This is the case of antiquated Sun disk drives that have the
6302 	 * FAB_DEVID property set in the disk_table.  These drives
6303 	 * manage the devid's by storing them in last 2 available sectors
6304 	 * on the drive and have them fabricated by the ddi layer by calling
6305 	 * ddi_devid_init and passing the DEVID_FAB flag.
6306 	 */
6307 	if (un->un_f_opt_fab_devid == TRUE) {
6308 		/*
6309 		 * Depending on EINVAL isn't reliable, since a reserved disk
6310 		 * may result in invalid geometry, so check to make sure a
6311 		 * reservation conflict did not occur during attach.
6312 		 */
6313 		if ((sd_get_devid(un) == EINVAL) &&
6314 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
6315 			/*
6316 			 * The devid is invalid AND there is no reservation
6317 			 * conflict.  Fabricate a new devid.
6318 			 */
6319 			(void) sd_create_devid(un);
6320 		}
6321 
6322 		/* Register the devid if it exists */
6323 		if (un->un_devid != NULL) {
6324 			(void) ddi_devid_register(SD_DEVINFO(un),
6325 			    un->un_devid);
6326 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
6327 			    "sd_register_devid: Devid Fabricated\n");
6328 		}
6329 		return;
6330 	}
6331 
6332 	/*
6333 	 * We check the availibility of the World Wide Name (0x83) and Unit
6334 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
6335 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
6336 	 * 0x83 is availible, that is the best choice.  Our next choice is
6337 	 * 0x80.  If neither are availible, we munge the devid from the device
6338 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
6339 	 * to fabricate a devid for non-Sun qualified disks.
6340 	 */
6341 	if (sd_check_vpd_page_support(un) == 0) {
6342 		/* collect page 80 data if available */
6343 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
6344 
6345 			mutex_exit(SD_MUTEX(un));
6346 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
6347 			rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len,
6348 			    0x01, 0x80, &inq80_resid);
6349 
6350 			if (rval != 0) {
6351 				kmem_free(inq80, inq80_len);
6352 				inq80 = NULL;
6353 				inq80_len = 0;
6354 			}
6355 			mutex_enter(SD_MUTEX(un));
6356 		}
6357 
6358 		/* collect page 83 data if available */
6359 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
6360 			mutex_exit(SD_MUTEX(un));
6361 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
6362 			rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len,
6363 			    0x01, 0x83, &inq83_resid);
6364 
6365 			if (rval != 0) {
6366 				kmem_free(inq83, inq83_len);
6367 				inq83 = NULL;
6368 				inq83_len = 0;
6369 			}
6370 			mutex_enter(SD_MUTEX(un));
6371 		}
6372 	}
6373 
6374 	/* encode best devid possible based on data available */
6375 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
6376 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
6377 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
6378 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
6379 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
6380 
6381 		/* devid successfully encoded, register devid */
6382 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
6383 
6384 	} else {
6385 		/*
6386 		 * Unable to encode a devid based on data available.
6387 		 * This is not a Sun qualified disk.  Older Sun disk
6388 		 * drives that have the SD_FAB_DEVID property
6389 		 * set in the disk_table and non Sun qualified
6390 		 * disks are treated in the same manner.  These
6391 		 * drives manage the devid's by storing them in
6392 		 * last 2 available sectors on the drive and
6393 		 * have them fabricated by the ddi layer by
6394 		 * calling ddi_devid_init and passing the
6395 		 * DEVID_FAB flag.
6396 		 * Create a fabricate devid only if there's no
6397 		 * fabricate devid existed.
6398 		 */
6399 		if (sd_get_devid(un) == EINVAL) {
6400 			(void) sd_create_devid(un);
6401 		}
6402 		un->un_f_opt_fab_devid = TRUE;
6403 
6404 		/* Register the devid if it exists */
6405 		if (un->un_devid != NULL) {
6406 			(void) ddi_devid_register(SD_DEVINFO(un),
6407 			    un->un_devid);
6408 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
6409 			    "sd_register_devid: devid fabricated using "
6410 			    "ddi framework\n");
6411 		}
6412 	}
6413 
6414 	/* clean up resources */
6415 	if (inq80 != NULL) {
6416 		kmem_free(inq80, inq80_len);
6417 	}
6418 	if (inq83 != NULL) {
6419 		kmem_free(inq83, inq83_len);
6420 	}
6421 }
6422 
6423 static daddr_t
6424 sd_get_devid_block(struct sd_lun *un)
6425 {
6426 	daddr_t			spc, blk, head, cyl;
6427 
6428 	if ((un->un_f_geometry_is_valid == FALSE) ||
6429 	    (un->un_solaris_size < DK_LABEL_LOC))
6430 		return (-1);
6431 
6432 	if (un->un_vtoc.v_sanity != VTOC_SANE) {
6433 		/* EFI labeled */
6434 		if (un->un_reserved != -1) {
6435 			blk = un->un_map[un->un_reserved].dkl_cylno;
6436 		} else {
6437 			return (-1);
6438 		}
6439 	} else {
6440 		/* SMI labeled */
6441 		/* this geometry doesn't allow us to write a devid */
6442 		if (un->un_g.dkg_acyl < 2) {
6443 			return (-1);
6444 		}
6445 
6446 		/*
6447 		 * Subtract 2 guarantees that the next to last cylinder
6448 		 * is used
6449 		 */
6450 		cyl  = un->un_g.dkg_ncyl  + un->un_g.dkg_acyl - 2;
6451 		spc  = un->un_g.dkg_nhead * un->un_g.dkg_nsect;
6452 		head = un->un_g.dkg_nhead - 1;
6453 		blk  = (cyl * (spc - un->un_g.dkg_apc)) +
6454 		    (head * un->un_g.dkg_nsect) + 1;
6455 	}
6456 	return (blk);
6457 }
6458 
6459 /*
6460  *    Function: sd_get_devid
6461  *
6462  * Description: This routine will return 0 if a valid device id has been
6463  *		obtained from the target and stored in the soft state. If a
6464  *		valid device id has not been previously read and stored, a
6465  *		read attempt will be made.
6466  *
6467  *   Arguments: un - driver soft state (unit) structure
6468  *
6469  * Return Code: 0 if we successfully get the device id
6470  *
6471  *     Context: Kernel Thread
6472  */
6473 
6474 static int
6475 sd_get_devid(struct sd_lun *un)
6476 {
6477 	struct dk_devid		*dkdevid;
6478 	ddi_devid_t		tmpid;
6479 	uint_t			*ip;
6480 	size_t			sz;
6481 	daddr_t			blk;
6482 	int			status;
6483 	int			chksum;
6484 	int			i;
6485 	size_t			buffer_size;
6486 
6487 	ASSERT(un != NULL);
6488 	ASSERT(mutex_owned(SD_MUTEX(un)));
6489 
6490 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
6491 	    un);
6492 
6493 	if (un->un_devid != NULL) {
6494 		return (0);
6495 	}
6496 
6497 	blk = sd_get_devid_block(un);
6498 	if (blk < 0)
6499 		return (EINVAL);
6500 
6501 	/*
6502 	 * Read and verify device id, stored in the reserved cylinders at the
6503 	 * end of the disk. Backup label is on the odd sectors of the last
6504 	 * track of the last cylinder. Device id will be on track of the next
6505 	 * to last cylinder.
6506 	 */
6507 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
6508 	mutex_exit(SD_MUTEX(un));
6509 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
6510 	status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk,
6511 	    SD_PATH_DIRECT);
6512 	if (status != 0) {
6513 		goto error;
6514 	}
6515 
6516 	/* Validate the revision */
6517 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
6518 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
6519 		status = EINVAL;
6520 		goto error;
6521 	}
6522 
6523 	/* Calculate the checksum */
6524 	chksum = 0;
6525 	ip = (uint_t *)dkdevid;
6526 	for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int));
6527 	    i++) {
6528 		chksum ^= ip[i];
6529 	}
6530 
6531 	/* Compare the checksums */
6532 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
6533 		status = EINVAL;
6534 		goto error;
6535 	}
6536 
6537 	/* Validate the device id */
6538 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
6539 		status = EINVAL;
6540 		goto error;
6541 	}
6542 
6543 	/*
6544 	 * Store the device id in the driver soft state
6545 	 */
6546 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
6547 	tmpid = kmem_alloc(sz, KM_SLEEP);
6548 
6549 	mutex_enter(SD_MUTEX(un));
6550 
6551 	un->un_devid = tmpid;
6552 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
6553 
6554 	kmem_free(dkdevid, buffer_size);
6555 
6556 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
6557 
6558 	return (status);
6559 error:
6560 	mutex_enter(SD_MUTEX(un));
6561 	kmem_free(dkdevid, buffer_size);
6562 	return (status);
6563 }
6564 
6565 
6566 /*
6567  *    Function: sd_create_devid
6568  *
6569  * Description: This routine will fabricate the device id and write it
6570  *		to the disk.
6571  *
6572  *   Arguments: un - driver soft state (unit) structure
6573  *
6574  * Return Code: value of the fabricated device id
6575  *
6576  *     Context: Kernel Thread
6577  */
6578 
6579 static ddi_devid_t
6580 sd_create_devid(struct sd_lun *un)
6581 {
6582 	ASSERT(un != NULL);
6583 
6584 	/* Fabricate the devid */
6585 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
6586 	    == DDI_FAILURE) {
6587 		return (NULL);
6588 	}
6589 
6590 	/* Write the devid to disk */
6591 	if (sd_write_deviceid(un) != 0) {
6592 		ddi_devid_free(un->un_devid);
6593 		un->un_devid = NULL;
6594 	}
6595 
6596 	return (un->un_devid);
6597 }
6598 
6599 
6600 /*
6601  *    Function: sd_write_deviceid
6602  *
6603  * Description: This routine will write the device id to the disk
6604  *		reserved sector.
6605  *
6606  *   Arguments: un - driver soft state (unit) structure
6607  *
6608  * Return Code: EINVAL
6609  *		value returned by sd_send_scsi_cmd
6610  *
6611  *     Context: Kernel Thread
6612  */
6613 
6614 static int
6615 sd_write_deviceid(struct sd_lun *un)
6616 {
6617 	struct dk_devid		*dkdevid;
6618 	daddr_t			blk;
6619 	uint_t			*ip, chksum;
6620 	int			status;
6621 	int			i;
6622 
6623 	ASSERT(mutex_owned(SD_MUTEX(un)));
6624 
6625 	blk = sd_get_devid_block(un);
6626 	if (blk < 0)
6627 		return (-1);
6628 	mutex_exit(SD_MUTEX(un));
6629 
6630 	/* Allocate the buffer */
6631 	dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
6632 
6633 	/* Fill in the revision */
6634 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
6635 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
6636 
6637 	/* Copy in the device id */
6638 	mutex_enter(SD_MUTEX(un));
6639 	bcopy(un->un_devid, &dkdevid->dkd_devid,
6640 	    ddi_devid_sizeof(un->un_devid));
6641 	mutex_exit(SD_MUTEX(un));
6642 
6643 	/* Calculate the checksum */
6644 	chksum = 0;
6645 	ip = (uint_t *)dkdevid;
6646 	for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int));
6647 	    i++) {
6648 		chksum ^= ip[i];
6649 	}
6650 
6651 	/* Fill-in checksum */
6652 	DKD_FORMCHKSUM(chksum, dkdevid);
6653 
6654 	/* Write the reserved sector */
6655 	status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk,
6656 	    SD_PATH_DIRECT);
6657 
6658 	kmem_free(dkdevid, un->un_sys_blocksize);
6659 
6660 	mutex_enter(SD_MUTEX(un));
6661 	return (status);
6662 }
6663 
6664 
6665 /*
6666  *    Function: sd_check_vpd_page_support
6667  *
6668  * Description: This routine sends an inquiry command with the EVPD bit set and
6669  *		a page code of 0x00 to the device. It is used to determine which
6670  *		vital product pages are availible to find the devid. We are
6671  *		looking for pages 0x83 or 0x80.  If we return a negative 1, the
6672  *		device does not support that command.
6673  *
6674  *   Arguments: un  - driver soft state (unit) structure
6675  *
6676  * Return Code: 0 - success
6677  *		1 - check condition
6678  *
6679  *     Context: This routine can sleep.
6680  */
6681 
6682 static int
6683 sd_check_vpd_page_support(struct sd_lun *un)
6684 {
6685 	uchar_t	*page_list	= NULL;
6686 	uchar_t	page_length	= 0xff;	/* Use max possible length */
6687 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
6688 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
6689 	int    	rval		= 0;
6690 	int	counter;
6691 
6692 	ASSERT(un != NULL);
6693 	ASSERT(mutex_owned(SD_MUTEX(un)));
6694 
6695 	mutex_exit(SD_MUTEX(un));
6696 
6697 	/*
6698 	 * We'll set the page length to the maximum to save figuring it out
6699 	 * with an additional call.
6700 	 */
6701 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
6702 
6703 	rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd,
6704 	    page_code, NULL);
6705 
6706 	mutex_enter(SD_MUTEX(un));
6707 
6708 	/*
6709 	 * Now we must validate that the device accepted the command, as some
6710 	 * drives do not support it.  If the drive does support it, we will
6711 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
6712 	 * not, we return -1.
6713 	 */
6714 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
6715 		/* Loop to find one of the 2 pages we need */
6716 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
6717 
6718 		/*
6719 		 * Pages are returned in ascending order, and 0x83 is what we
6720 		 * are hoping for.
6721 		 */
6722 		while ((page_list[counter] <= 0x83) &&
6723 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
6724 		    VPD_HEAD_OFFSET))) {
6725 			/*
6726 			 * Add 3 because page_list[3] is the number of
6727 			 * pages minus 3
6728 			 */
6729 
6730 			switch (page_list[counter]) {
6731 			case 0x00:
6732 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
6733 				break;
6734 			case 0x80:
6735 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
6736 				break;
6737 			case 0x81:
6738 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
6739 				break;
6740 			case 0x82:
6741 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
6742 				break;
6743 			case 0x83:
6744 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
6745 				break;
6746 			}
6747 			counter++;
6748 		}
6749 
6750 	} else {
6751 		rval = -1;
6752 
6753 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
6754 		    "sd_check_vpd_page_support: This drive does not implement "
6755 		    "VPD pages.\n");
6756 	}
6757 
6758 	kmem_free(page_list, page_length);
6759 
6760 	return (rval);
6761 }
6762 
6763 
6764 /*
6765  *    Function: sd_setup_pm
6766  *
6767  * Description: Initialize Power Management on the device
6768  *
6769  *     Context: Kernel Thread
6770  */
6771 
6772 static void
6773 sd_setup_pm(struct sd_lun *un, dev_info_t *devi)
6774 {
6775 	uint_t	log_page_size;
6776 	uchar_t	*log_page_data;
6777 	int	rval;
6778 
6779 	/*
6780 	 * Since we are called from attach, holding a mutex for
6781 	 * un is unnecessary. Because some of the routines called
6782 	 * from here require SD_MUTEX to not be held, assert this
6783 	 * right up front.
6784 	 */
6785 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6786 	/*
6787 	 * Since the sd device does not have the 'reg' property,
6788 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
6789 	 * The following code is to tell cpr that this device
6790 	 * DOES need to be suspended and resumed.
6791 	 */
6792 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
6793 	    "pm-hardware-state", "needs-suspend-resume");
6794 
6795 	/*
6796 	 * This complies with the new power management framework
6797 	 * for certain desktop machines. Create the pm_components
6798 	 * property as a string array property.
6799 	 */
6800 	if (un->un_f_pm_supported) {
6801 		/*
6802 		 * not all devices have a motor, try it first.
6803 		 * some devices may return ILLEGAL REQUEST, some
6804 		 * will hang
6805 		 * The following START_STOP_UNIT is used to check if target
6806 		 * device has a motor.
6807 		 */
6808 		un->un_f_start_stop_supported = TRUE;
6809 		if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
6810 		    SD_PATH_DIRECT) != 0) {
6811 			un->un_f_start_stop_supported = FALSE;
6812 		}
6813 
6814 		/*
6815 		 * create pm properties anyways otherwise the parent can't
6816 		 * go to sleep
6817 		 */
6818 		(void) sd_create_pm_components(devi, un);
6819 		un->un_f_pm_is_enabled = TRUE;
6820 		return;
6821 	}
6822 
6823 	if (!un->un_f_log_sense_supported) {
6824 		un->un_power_level = SD_SPINDLE_ON;
6825 		un->un_f_pm_is_enabled = FALSE;
6826 		return;
6827 	}
6828 
6829 	rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE);
6830 
6831 #ifdef	SDDEBUG
6832 	if (sd_force_pm_supported) {
6833 		/* Force a successful result */
6834 		rval = 1;
6835 	}
6836 #endif
6837 
6838 	/*
6839 	 * If the start-stop cycle counter log page is not supported
6840 	 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0)
6841 	 * then we should not create the pm_components property.
6842 	 */
6843 	if (rval == -1) {
6844 		/*
6845 		 * Error.
6846 		 * Reading log sense failed, most likely this is
6847 		 * an older drive that does not support log sense.
6848 		 * If this fails auto-pm is not supported.
6849 		 */
6850 		un->un_power_level = SD_SPINDLE_ON;
6851 		un->un_f_pm_is_enabled = FALSE;
6852 
6853 	} else if (rval == 0) {
6854 		/*
6855 		 * Page not found.
6856 		 * The start stop cycle counter is implemented as page
6857 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6858 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6859 		 */
6860 		if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) {
6861 			/*
6862 			 * Page found, use this one.
6863 			 */
6864 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6865 			un->un_f_pm_is_enabled = TRUE;
6866 		} else {
6867 			/*
6868 			 * Error or page not found.
6869 			 * auto-pm is not supported for this device.
6870 			 */
6871 			un->un_power_level = SD_SPINDLE_ON;
6872 			un->un_f_pm_is_enabled = FALSE;
6873 		}
6874 	} else {
6875 		/*
6876 		 * Page found, use it.
6877 		 */
6878 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6879 		un->un_f_pm_is_enabled = TRUE;
6880 	}
6881 
6882 
6883 	if (un->un_f_pm_is_enabled == TRUE) {
6884 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6885 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6886 
6887 		rval = sd_send_scsi_LOG_SENSE(un, log_page_data,
6888 		    log_page_size, un->un_start_stop_cycle_page,
6889 		    0x01, 0, SD_PATH_DIRECT);
6890 #ifdef	SDDEBUG
6891 		if (sd_force_pm_supported) {
6892 			/* Force a successful result */
6893 			rval = 0;
6894 		}
6895 #endif
6896 
6897 		/*
6898 		 * If the Log sense for Page( Start/stop cycle counter page)
6899 		 * succeeds, then power managment is supported and we can
6900 		 * enable auto-pm.
6901 		 */
6902 		if (rval == 0)  {
6903 			(void) sd_create_pm_components(devi, un);
6904 		} else {
6905 			un->un_power_level = SD_SPINDLE_ON;
6906 			un->un_f_pm_is_enabled = FALSE;
6907 		}
6908 
6909 		kmem_free(log_page_data, log_page_size);
6910 	}
6911 }
6912 
6913 
6914 /*
6915  *    Function: sd_create_pm_components
6916  *
6917  * Description: Initialize PM property.
6918  *
6919  *     Context: Kernel thread context
6920  */
6921 
6922 static void
6923 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6924 {
6925 	char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL };
6926 
6927 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6928 
6929 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6930 	    "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) {
6931 		/*
6932 		 * When components are initially created they are idle,
6933 		 * power up any non-removables.
6934 		 * Note: the return value of pm_raise_power can't be used
6935 		 * for determining if PM should be enabled for this device.
6936 		 * Even if you check the return values and remove this
6937 		 * property created above, the PM framework will not honor the
6938 		 * change after the first call to pm_raise_power. Hence,
6939 		 * removal of that property does not help if pm_raise_power
6940 		 * fails. In the case of removable media, the start/stop
6941 		 * will fail if the media is not present.
6942 		 */
6943 		if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6944 		    SD_SPINDLE_ON) == DDI_SUCCESS)) {
6945 			mutex_enter(SD_MUTEX(un));
6946 			un->un_power_level = SD_SPINDLE_ON;
6947 			mutex_enter(&un->un_pm_mutex);
6948 			/* Set to on and not busy. */
6949 			un->un_pm_count = 0;
6950 		} else {
6951 			mutex_enter(SD_MUTEX(un));
6952 			un->un_power_level = SD_SPINDLE_OFF;
6953 			mutex_enter(&un->un_pm_mutex);
6954 			/* Set to off. */
6955 			un->un_pm_count = -1;
6956 		}
6957 		mutex_exit(&un->un_pm_mutex);
6958 		mutex_exit(SD_MUTEX(un));
6959 	} else {
6960 		un->un_power_level = SD_SPINDLE_ON;
6961 		un->un_f_pm_is_enabled = FALSE;
6962 	}
6963 }
6964 
6965 
6966 /*
6967  *    Function: sd_ddi_suspend
6968  *
6969  * Description: Performs system power-down operations. This includes
6970  *		setting the drive state to indicate its suspended so
6971  *		that no new commands will be accepted. Also, wait for
6972  *		all commands that are in transport or queued to a timer
6973  *		for retry to complete. All timeout threads are cancelled.
6974  *
6975  * Return Code: DDI_FAILURE or DDI_SUCCESS
6976  *
6977  *     Context: Kernel thread context
6978  */
6979 
6980 static int
6981 sd_ddi_suspend(dev_info_t *devi)
6982 {
6983 	struct	sd_lun	*un;
6984 	clock_t		wait_cmds_complete;
6985 
6986 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6987 	if (un == NULL) {
6988 		return (DDI_FAILURE);
6989 	}
6990 
6991 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6992 
6993 	mutex_enter(SD_MUTEX(un));
6994 
6995 	/* Return success if the device is already suspended. */
6996 	if (un->un_state == SD_STATE_SUSPENDED) {
6997 		mutex_exit(SD_MUTEX(un));
6998 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6999 		    "device already suspended, exiting\n");
7000 		return (DDI_SUCCESS);
7001 	}
7002 
7003 	/* Return failure if the device is being used by HA */
7004 	if (un->un_resvd_status &
7005 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
7006 		mutex_exit(SD_MUTEX(un));
7007 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
7008 		    "device in use by HA, exiting\n");
7009 		return (DDI_FAILURE);
7010 	}
7011 
7012 	/*
7013 	 * Return failure if the device is in a resource wait
7014 	 * or power changing state.
7015 	 */
7016 	if ((un->un_state == SD_STATE_RWAIT) ||
7017 	    (un->un_state == SD_STATE_PM_CHANGING)) {
7018 		mutex_exit(SD_MUTEX(un));
7019 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
7020 		    "device in resource wait state, exiting\n");
7021 		return (DDI_FAILURE);
7022 	}
7023 
7024 
7025 	un->un_save_state = un->un_last_state;
7026 	New_state(un, SD_STATE_SUSPENDED);
7027 
7028 	/*
7029 	 * Wait for all commands that are in transport or queued to a timer
7030 	 * for retry to complete.
7031 	 *
7032 	 * While waiting, no new commands will be accepted or sent because of
7033 	 * the new state we set above.
7034 	 *
7035 	 * Wait till current operation has completed. If we are in the resource
7036 	 * wait state (with an intr outstanding) then we need to wait till the
7037 	 * intr completes and starts the next cmd. We want to wait for
7038 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
7039 	 */
7040 	wait_cmds_complete = ddi_get_lbolt() +
7041 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
7042 
7043 	while (un->un_ncmds_in_transport != 0) {
7044 		/*
7045 		 * Fail if commands do not finish in the specified time.
7046 		 */
7047 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
7048 		    wait_cmds_complete) == -1) {
7049 			/*
7050 			 * Undo the state changes made above. Everything
7051 			 * must go back to it's original value.
7052 			 */
7053 			Restore_state(un);
7054 			un->un_last_state = un->un_save_state;
7055 			/* Wake up any threads that might be waiting. */
7056 			cv_broadcast(&un->un_suspend_cv);
7057 			mutex_exit(SD_MUTEX(un));
7058 			SD_ERROR(SD_LOG_IO_PM, un,
7059 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
7060 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
7061 			return (DDI_FAILURE);
7062 		}
7063 	}
7064 
7065 	/*
7066 	 * Cancel SCSI watch thread and timeouts, if any are active
7067 	 */
7068 
7069 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
7070 		opaque_t temp_token = un->un_swr_token;
7071 		mutex_exit(SD_MUTEX(un));
7072 		scsi_watch_suspend(temp_token);
7073 		mutex_enter(SD_MUTEX(un));
7074 	}
7075 
7076 	if (un->un_reset_throttle_timeid != NULL) {
7077 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
7078 		un->un_reset_throttle_timeid = NULL;
7079 		mutex_exit(SD_MUTEX(un));
7080 		(void) untimeout(temp_id);
7081 		mutex_enter(SD_MUTEX(un));
7082 	}
7083 
7084 	if (un->un_dcvb_timeid != NULL) {
7085 		timeout_id_t temp_id = un->un_dcvb_timeid;
7086 		un->un_dcvb_timeid = NULL;
7087 		mutex_exit(SD_MUTEX(un));
7088 		(void) untimeout(temp_id);
7089 		mutex_enter(SD_MUTEX(un));
7090 	}
7091 
7092 	mutex_enter(&un->un_pm_mutex);
7093 	if (un->un_pm_timeid != NULL) {
7094 		timeout_id_t temp_id = un->un_pm_timeid;
7095 		un->un_pm_timeid = NULL;
7096 		mutex_exit(&un->un_pm_mutex);
7097 		mutex_exit(SD_MUTEX(un));
7098 		(void) untimeout(temp_id);
7099 		mutex_enter(SD_MUTEX(un));
7100 	} else {
7101 		mutex_exit(&un->un_pm_mutex);
7102 	}
7103 
7104 	if (un->un_retry_timeid != NULL) {
7105 		timeout_id_t temp_id = un->un_retry_timeid;
7106 		un->un_retry_timeid = NULL;
7107 		mutex_exit(SD_MUTEX(un));
7108 		(void) untimeout(temp_id);
7109 		mutex_enter(SD_MUTEX(un));
7110 	}
7111 
7112 	if (un->un_direct_priority_timeid != NULL) {
7113 		timeout_id_t temp_id = un->un_direct_priority_timeid;
7114 		un->un_direct_priority_timeid = NULL;
7115 		mutex_exit(SD_MUTEX(un));
7116 		(void) untimeout(temp_id);
7117 		mutex_enter(SD_MUTEX(un));
7118 	}
7119 
7120 	if (un->un_f_is_fibre == TRUE) {
7121 		/*
7122 		 * Remove callbacks for insert and remove events
7123 		 */
7124 		if (un->un_insert_event != NULL) {
7125 			mutex_exit(SD_MUTEX(un));
7126 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
7127 			mutex_enter(SD_MUTEX(un));
7128 			un->un_insert_event = NULL;
7129 		}
7130 
7131 		if (un->un_remove_event != NULL) {
7132 			mutex_exit(SD_MUTEX(un));
7133 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
7134 			mutex_enter(SD_MUTEX(un));
7135 			un->un_remove_event = NULL;
7136 		}
7137 	}
7138 
7139 	mutex_exit(SD_MUTEX(un));
7140 
7141 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
7142 
7143 	return (DDI_SUCCESS);
7144 }
7145 
7146 
7147 /*
7148  *    Function: sd_ddi_pm_suspend
7149  *
7150  * Description: Set the drive state to low power.
7151  *		Someone else is required to actually change the drive
7152  *		power level.
7153  *
7154  *   Arguments: un - driver soft state (unit) structure
7155  *
7156  * Return Code: DDI_FAILURE or DDI_SUCCESS
7157  *
7158  *     Context: Kernel thread context
7159  */
7160 
7161 static int
7162 sd_ddi_pm_suspend(struct sd_lun *un)
7163 {
7164 	ASSERT(un != NULL);
7165 	SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n");
7166 
7167 	ASSERT(!mutex_owned(SD_MUTEX(un)));
7168 	mutex_enter(SD_MUTEX(un));
7169 
7170 	/*
7171 	 * Exit if power management is not enabled for this device, or if
7172 	 * the device is being used by HA.
7173 	 */
7174 	if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
7175 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
7176 		mutex_exit(SD_MUTEX(un));
7177 		SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n");
7178 		return (DDI_SUCCESS);
7179 	}
7180 
7181 	SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n",
7182 	    un->un_ncmds_in_driver);
7183 
7184 	/*
7185 	 * See if the device is not busy, ie.:
7186 	 *    - we have no commands in the driver for this device
7187 	 *    - not waiting for resources
7188 	 */
7189 	if ((un->un_ncmds_in_driver == 0) &&
7190 	    (un->un_state != SD_STATE_RWAIT)) {
7191 		/*
7192 		 * The device is not busy, so it is OK to go to low power state.
7193 		 * Indicate low power, but rely on someone else to actually
7194 		 * change it.
7195 		 */
7196 		mutex_enter(&un->un_pm_mutex);
7197 		un->un_pm_count = -1;
7198 		mutex_exit(&un->un_pm_mutex);
7199 		un->un_power_level = SD_SPINDLE_OFF;
7200 	}
7201 
7202 	mutex_exit(SD_MUTEX(un));
7203 
7204 	SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n");
7205 
7206 	return (DDI_SUCCESS);
7207 }
7208 
7209 
7210 /*
7211  *    Function: sd_ddi_resume
7212  *
7213  * Description: Performs system power-up operations..
7214  *
7215  * Return Code: DDI_SUCCESS
7216  *		DDI_FAILURE
7217  *
7218  *     Context: Kernel thread context
7219  */
7220 
7221 static int
7222 sd_ddi_resume(dev_info_t *devi)
7223 {
7224 	struct	sd_lun	*un;
7225 
7226 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
7227 	if (un == NULL) {
7228 		return (DDI_FAILURE);
7229 	}
7230 
7231 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
7232 
7233 	mutex_enter(SD_MUTEX(un));
7234 	Restore_state(un);
7235 
7236 	/*
7237 	 * Restore the state which was saved to give the
7238 	 * the right state in un_last_state
7239 	 */
7240 	un->un_last_state = un->un_save_state;
7241 	/*
7242 	 * Note: throttle comes back at full.
7243 	 * Also note: this MUST be done before calling pm_raise_power
7244 	 * otherwise the system can get hung in biowait. The scenario where
7245 	 * this'll happen is under cpr suspend. Writing of the system
7246 	 * state goes through sddump, which writes 0 to un_throttle. If
7247 	 * writing the system state then fails, example if the partition is
7248 	 * too small, then cpr attempts a resume. If throttle isn't restored
7249 	 * from the saved value until after calling pm_raise_power then
7250 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
7251 	 * in biowait.
7252 	 */
7253 	un->un_throttle = un->un_saved_throttle;
7254 
7255 	/*
7256 	 * The chance of failure is very rare as the only command done in power
7257 	 * entry point is START command when you transition from 0->1 or
7258 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
7259 	 * which suspend was done. Ignore the return value as the resume should
7260 	 * not be failed. In the case of removable media the media need not be
7261 	 * inserted and hence there is a chance that raise power will fail with
7262 	 * media not present.
7263 	 */
7264 	if (un->un_f_attach_spinup) {
7265 		mutex_exit(SD_MUTEX(un));
7266 		(void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON);
7267 		mutex_enter(SD_MUTEX(un));
7268 	}
7269 
7270 	/*
7271 	 * Don't broadcast to the suspend cv and therefore possibly
7272 	 * start I/O until after power has been restored.
7273 	 */
7274 	cv_broadcast(&un->un_suspend_cv);
7275 	cv_broadcast(&un->un_state_cv);
7276 
7277 	/* restart thread */
7278 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
7279 		scsi_watch_resume(un->un_swr_token);
7280 	}
7281 
7282 #if (defined(__fibre))
7283 	if (un->un_f_is_fibre == TRUE) {
7284 		/*
7285 		 * Add callbacks for insert and remove events
7286 		 */
7287 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
7288 			sd_init_event_callbacks(un);
7289 		}
7290 	}
7291 #endif
7292 
7293 	/*
7294 	 * Transport any pending commands to the target.
7295 	 *
7296 	 * If this is a low-activity device commands in queue will have to wait
7297 	 * until new commands come in, which may take awhile. Also, we
7298 	 * specifically don't check un_ncmds_in_transport because we know that
7299 	 * there really are no commands in progress after the unit was
7300 	 * suspended and we could have reached the throttle level, been
7301 	 * suspended, and have no new commands coming in for awhile. Highly
7302 	 * unlikely, but so is the low-activity disk scenario.
7303 	 */
7304 	ddi_xbuf_dispatch(un->un_xbuf_attr);
7305 
7306 	sd_start_cmds(un, NULL);
7307 	mutex_exit(SD_MUTEX(un));
7308 
7309 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
7310 
7311 	return (DDI_SUCCESS);
7312 }
7313 
7314 
7315 /*
7316  *    Function: sd_ddi_pm_resume
7317  *
7318  * Description: Set the drive state to powered on.
7319  *		Someone else is required to actually change the drive
7320  *		power level.
7321  *
7322  *   Arguments: un - driver soft state (unit) structure
7323  *
7324  * Return Code: DDI_SUCCESS
7325  *
7326  *     Context: Kernel thread context
7327  */
7328 
7329 static int
7330 sd_ddi_pm_resume(struct sd_lun *un)
7331 {
7332 	ASSERT(un != NULL);
7333 
7334 	ASSERT(!mutex_owned(SD_MUTEX(un)));
7335 	mutex_enter(SD_MUTEX(un));
7336 	un->un_power_level = SD_SPINDLE_ON;
7337 
7338 	ASSERT(!mutex_owned(&un->un_pm_mutex));
7339 	mutex_enter(&un->un_pm_mutex);
7340 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
7341 		un->un_pm_count++;
7342 		ASSERT(un->un_pm_count == 0);
7343 		/*
7344 		 * Note: no longer do the cv_broadcast on un_suspend_cv. The
7345 		 * un_suspend_cv is for a system resume, not a power management
7346 		 * device resume. (4297749)
7347 		 *	 cv_broadcast(&un->un_suspend_cv);
7348 		 */
7349 	}
7350 	mutex_exit(&un->un_pm_mutex);
7351 	mutex_exit(SD_MUTEX(un));
7352 
7353 	return (DDI_SUCCESS);
7354 }
7355 
7356 
7357 /*
7358  *    Function: sd_pm_idletimeout_handler
7359  *
7360  * Description: A timer routine that's active only while a device is busy.
7361  *		The purpose is to extend slightly the pm framework's busy
7362  *		view of the device to prevent busy/idle thrashing for
7363  *		back-to-back commands. Do this by comparing the current time
7364  *		to the time at which the last command completed and when the
7365  *		difference is greater than sd_pm_idletime, call
7366  *		pm_idle_component. In addition to indicating idle to the pm
7367  *		framework, update the chain type to again use the internal pm
7368  *		layers of the driver.
7369  *
7370  *   Arguments: arg - driver soft state (unit) structure
7371  *
7372  *     Context: Executes in a timeout(9F) thread context
7373  */
7374 
7375 static void
7376 sd_pm_idletimeout_handler(void *arg)
7377 {
7378 	struct sd_lun *un = arg;
7379 
7380 	time_t	now;
7381 
7382 	mutex_enter(&sd_detach_mutex);
7383 	if (un->un_detach_count != 0) {
7384 		/* Abort if the instance is detaching */
7385 		mutex_exit(&sd_detach_mutex);
7386 		return;
7387 	}
7388 	mutex_exit(&sd_detach_mutex);
7389 
7390 	now = ddi_get_time();
7391 	/*
7392 	 * Grab both mutexes, in the proper order, since we're accessing
7393 	 * both PM and softstate variables.
7394 	 */
7395 	mutex_enter(SD_MUTEX(un));
7396 	mutex_enter(&un->un_pm_mutex);
7397 	if (((now - un->un_pm_idle_time) > sd_pm_idletime) &&
7398 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
7399 		/*
7400 		 * Update the chain types.
7401 		 * This takes affect on the next new command received.
7402 		 */
7403 		if (un->un_f_non_devbsize_supported) {
7404 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7405 		} else {
7406 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7407 		}
7408 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7409 
7410 		SD_TRACE(SD_LOG_IO_PM, un,
7411 		    "sd_pm_idletimeout_handler: idling device\n");
7412 		(void) pm_idle_component(SD_DEVINFO(un), 0);
7413 		un->un_pm_idle_timeid = NULL;
7414 	} else {
7415 		un->un_pm_idle_timeid =
7416 			timeout(sd_pm_idletimeout_handler, un,
7417 			(drv_usectohz((clock_t)300000))); /* 300 ms. */
7418 	}
7419 	mutex_exit(&un->un_pm_mutex);
7420 	mutex_exit(SD_MUTEX(un));
7421 }
7422 
7423 
7424 /*
7425  *    Function: sd_pm_timeout_handler
7426  *
7427  * Description: Callback to tell framework we are idle.
7428  *
7429  *     Context: timeout(9f) thread context.
7430  */
7431 
7432 static void
7433 sd_pm_timeout_handler(void *arg)
7434 {
7435 	struct sd_lun *un = arg;
7436 
7437 	(void) pm_idle_component(SD_DEVINFO(un), 0);
7438 	mutex_enter(&un->un_pm_mutex);
7439 	un->un_pm_timeid = NULL;
7440 	mutex_exit(&un->un_pm_mutex);
7441 }
7442 
7443 
7444 /*
7445  *    Function: sdpower
7446  *
7447  * Description: PM entry point.
7448  *
7449  * Return Code: DDI_SUCCESS
7450  *		DDI_FAILURE
7451  *
7452  *     Context: Kernel thread context
7453  */
7454 
7455 static int
7456 sdpower(dev_info_t *devi, int component, int level)
7457 {
7458 	struct sd_lun	*un;
7459 	int		instance;
7460 	int		rval = DDI_SUCCESS;
7461 	uint_t		i, log_page_size, maxcycles, ncycles;
7462 	uchar_t		*log_page_data;
7463 	int		log_sense_page;
7464 	int		medium_present;
7465 	time_t		intvlp;
7466 	dev_t		dev;
7467 	struct pm_trans_data	sd_pm_tran_data;
7468 	uchar_t		save_state;
7469 	int		sval;
7470 	uchar_t		state_before_pm;
7471 	int		got_semaphore_here;
7472 
7473 	instance = ddi_get_instance(devi);
7474 
7475 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
7476 	    (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) ||
7477 	    component != 0) {
7478 		return (DDI_FAILURE);
7479 	}
7480 
7481 	dev = sd_make_device(SD_DEVINFO(un));
7482 
7483 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
7484 
7485 	/*
7486 	 * Must synchronize power down with close.
7487 	 * Attempt to decrement/acquire the open/close semaphore,
7488 	 * but do NOT wait on it. If it's not greater than zero,
7489 	 * ie. it can't be decremented without waiting, then
7490 	 * someone else, either open or close, already has it
7491 	 * and the try returns 0. Use that knowledge here to determine
7492 	 * if it's OK to change the device power level.
7493 	 * Also, only increment it on exit if it was decremented, ie. gotten,
7494 	 * here.
7495 	 */
7496 	got_semaphore_here = sema_tryp(&un->un_semoclose);
7497 
7498 	mutex_enter(SD_MUTEX(un));
7499 
7500 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
7501 	    un->un_ncmds_in_driver);
7502 
7503 	/*
7504 	 * If un_ncmds_in_driver is non-zero it indicates commands are
7505 	 * already being processed in the driver, or if the semaphore was
7506 	 * not gotten here it indicates an open or close is being processed.
7507 	 * At the same time somebody is requesting to go low power which
7508 	 * can't happen, therefore we need to return failure.
7509 	 */
7510 	if ((level == SD_SPINDLE_OFF) &&
7511 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
7512 		mutex_exit(SD_MUTEX(un));
7513 
7514 		if (got_semaphore_here != 0) {
7515 			sema_v(&un->un_semoclose);
7516 		}
7517 		SD_TRACE(SD_LOG_IO_PM, un,
7518 		    "sdpower: exit, device has queued cmds.\n");
7519 		return (DDI_FAILURE);
7520 	}
7521 
7522 	/*
7523 	 * if it is OFFLINE that means the disk is completely dead
7524 	 * in our case we have to put the disk in on or off by sending commands
7525 	 * Of course that will fail anyway so return back here.
7526 	 *
7527 	 * Power changes to a device that's OFFLINE or SUSPENDED
7528 	 * are not allowed.
7529 	 */
7530 	if ((un->un_state == SD_STATE_OFFLINE) ||
7531 	    (un->un_state == SD_STATE_SUSPENDED)) {
7532 		mutex_exit(SD_MUTEX(un));
7533 
7534 		if (got_semaphore_here != 0) {
7535 			sema_v(&un->un_semoclose);
7536 		}
7537 		SD_TRACE(SD_LOG_IO_PM, un,
7538 		    "sdpower: exit, device is off-line.\n");
7539 		return (DDI_FAILURE);
7540 	}
7541 
7542 	/*
7543 	 * Change the device's state to indicate it's power level
7544 	 * is being changed. Do this to prevent a power off in the
7545 	 * middle of commands, which is especially bad on devices
7546 	 * that are really powered off instead of just spun down.
7547 	 */
7548 	state_before_pm = un->un_state;
7549 	un->un_state = SD_STATE_PM_CHANGING;
7550 
7551 	mutex_exit(SD_MUTEX(un));
7552 
7553 	/*
7554 	 * If "pm-capable" property is set to TRUE by HBA drivers,
7555 	 * bypass the following checking, otherwise, check the log
7556 	 * sense information for this device
7557 	 */
7558 	if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) {
7559 		/*
7560 		 * Get the log sense information to understand whether the
7561 		 * the powercycle counts have gone beyond the threshhold.
7562 		 */
7563 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
7564 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
7565 
7566 		mutex_enter(SD_MUTEX(un));
7567 		log_sense_page = un->un_start_stop_cycle_page;
7568 		mutex_exit(SD_MUTEX(un));
7569 
7570 		rval = sd_send_scsi_LOG_SENSE(un, log_page_data,
7571 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
7572 #ifdef	SDDEBUG
7573 		if (sd_force_pm_supported) {
7574 			/* Force a successful result */
7575 			rval = 0;
7576 		}
7577 #endif
7578 		if (rval != 0) {
7579 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
7580 			    "Log Sense Failed\n");
7581 			kmem_free(log_page_data, log_page_size);
7582 			/* Cannot support power management on those drives */
7583 
7584 			if (got_semaphore_here != 0) {
7585 				sema_v(&un->un_semoclose);
7586 			}
7587 			/*
7588 			 * On exit put the state back to it's original value
7589 			 * and broadcast to anyone waiting for the power
7590 			 * change completion.
7591 			 */
7592 			mutex_enter(SD_MUTEX(un));
7593 			un->un_state = state_before_pm;
7594 			cv_broadcast(&un->un_suspend_cv);
7595 			mutex_exit(SD_MUTEX(un));
7596 			SD_TRACE(SD_LOG_IO_PM, un,
7597 			    "sdpower: exit, Log Sense Failed.\n");
7598 			return (DDI_FAILURE);
7599 		}
7600 
7601 		/*
7602 		 * From the page data - Convert the essential information to
7603 		 * pm_trans_data
7604 		 */
7605 		maxcycles =
7606 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
7607 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
7608 
7609 		sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
7610 
7611 		ncycles =
7612 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
7613 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
7614 
7615 		sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
7616 
7617 		for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
7618 			sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
7619 			    log_page_data[8+i];
7620 		}
7621 
7622 		kmem_free(log_page_data, log_page_size);
7623 
7624 		/*
7625 		 * Call pm_trans_check routine to get the Ok from
7626 		 * the global policy
7627 		 */
7628 
7629 		sd_pm_tran_data.format = DC_SCSI_FORMAT;
7630 		sd_pm_tran_data.un.scsi_cycles.flag = 0;
7631 
7632 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
7633 #ifdef	SDDEBUG
7634 		if (sd_force_pm_supported) {
7635 			/* Force a successful result */
7636 			rval = 1;
7637 		}
7638 #endif
7639 		switch (rval) {
7640 		case 0:
7641 			/*
7642 			 * Not Ok to Power cycle or error in parameters passed
7643 			 * Would have given the advised time to consider power
7644 			 * cycle. Based on the new intvlp parameter we are
7645 			 * supposed to pretend we are busy so that pm framework
7646 			 * will never call our power entry point. Because of
7647 			 * that install a timeout handler and wait for the
7648 			 * recommended time to elapse so that power management
7649 			 * can be effective again.
7650 			 *
7651 			 * To effect this behavior, call pm_busy_component to
7652 			 * indicate to the framework this device is busy.
7653 			 * By not adjusting un_pm_count the rest of PM in
7654 			 * the driver will function normally, and independant
7655 			 * of this but because the framework is told the device
7656 			 * is busy it won't attempt powering down until it gets
7657 			 * a matching idle. The timeout handler sends this.
7658 			 * Note: sd_pm_entry can't be called here to do this
7659 			 * because sdpower may have been called as a result
7660 			 * of a call to pm_raise_power from within sd_pm_entry.
7661 			 *
7662 			 * If a timeout handler is already active then
7663 			 * don't install another.
7664 			 */
7665 			mutex_enter(&un->un_pm_mutex);
7666 			if (un->un_pm_timeid == NULL) {
7667 				un->un_pm_timeid =
7668 				    timeout(sd_pm_timeout_handler,
7669 				    un, intvlp * drv_usectohz(1000000));
7670 				mutex_exit(&un->un_pm_mutex);
7671 				(void) pm_busy_component(SD_DEVINFO(un), 0);
7672 			} else {
7673 				mutex_exit(&un->un_pm_mutex);
7674 			}
7675 			if (got_semaphore_here != 0) {
7676 				sema_v(&un->un_semoclose);
7677 			}
7678 			/*
7679 			 * On exit put the state back to it's original value
7680 			 * and broadcast to anyone waiting for the power
7681 			 * change completion.
7682 			 */
7683 			mutex_enter(SD_MUTEX(un));
7684 			un->un_state = state_before_pm;
7685 			cv_broadcast(&un->un_suspend_cv);
7686 			mutex_exit(SD_MUTEX(un));
7687 
7688 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
7689 			    "trans check Failed, not ok to power cycle.\n");
7690 			return (DDI_FAILURE);
7691 
7692 		case -1:
7693 			if (got_semaphore_here != 0) {
7694 				sema_v(&un->un_semoclose);
7695 			}
7696 			/*
7697 			 * On exit put the state back to it's original value
7698 			 * and broadcast to anyone waiting for the power
7699 			 * change completion.
7700 			 */
7701 			mutex_enter(SD_MUTEX(un));
7702 			un->un_state = state_before_pm;
7703 			cv_broadcast(&un->un_suspend_cv);
7704 			mutex_exit(SD_MUTEX(un));
7705 			SD_TRACE(SD_LOG_IO_PM, un,
7706 			    "sdpower: exit, trans check command Failed.\n");
7707 			return (DDI_FAILURE);
7708 		}
7709 	}
7710 
7711 	if (level == SD_SPINDLE_OFF) {
7712 		/*
7713 		 * Save the last state... if the STOP FAILS we need it
7714 		 * for restoring
7715 		 */
7716 		mutex_enter(SD_MUTEX(un));
7717 		save_state = un->un_last_state;
7718 		/*
7719 		 * There must not be any cmds. getting processed
7720 		 * in the driver when we get here. Power to the
7721 		 * device is potentially going off.
7722 		 */
7723 		ASSERT(un->un_ncmds_in_driver == 0);
7724 		mutex_exit(SD_MUTEX(un));
7725 
7726 		/*
7727 		 * For now suspend the device completely before spindle is
7728 		 * turned off
7729 		 */
7730 		if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) {
7731 			if (got_semaphore_here != 0) {
7732 				sema_v(&un->un_semoclose);
7733 			}
7734 			/*
7735 			 * On exit put the state back to it's original value
7736 			 * and broadcast to anyone waiting for the power
7737 			 * change completion.
7738 			 */
7739 			mutex_enter(SD_MUTEX(un));
7740 			un->un_state = state_before_pm;
7741 			cv_broadcast(&un->un_suspend_cv);
7742 			mutex_exit(SD_MUTEX(un));
7743 			SD_TRACE(SD_LOG_IO_PM, un,
7744 			    "sdpower: exit, PM suspend Failed.\n");
7745 			return (DDI_FAILURE);
7746 		}
7747 	}
7748 
7749 	/*
7750 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
7751 	 * close, or strategy. Dump no long uses this routine, it uses it's
7752 	 * own code so it can be done in polled mode.
7753 	 */
7754 
7755 	medium_present = TRUE;
7756 
7757 	/*
7758 	 * When powering up, issue a TUR in case the device is at unit
7759 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
7760 	 * a deadlock on un_pm_busy_cv will occur.
7761 	 */
7762 	if (level == SD_SPINDLE_ON) {
7763 		(void) sd_send_scsi_TEST_UNIT_READY(un,
7764 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
7765 	}
7766 
7767 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
7768 	    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
7769 
7770 	sval = sd_send_scsi_START_STOP_UNIT(un,
7771 	    ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP),
7772 	    SD_PATH_DIRECT);
7773 	/* Command failed, check for media present. */
7774 	if ((sval == ENXIO) && un->un_f_has_removable_media) {
7775 		medium_present = FALSE;
7776 	}
7777 
7778 	/*
7779 	 * The conditions of interest here are:
7780 	 *   if a spindle off with media present fails,
7781 	 *	then restore the state and return an error.
7782 	 *   else if a spindle on fails,
7783 	 *	then return an error (there's no state to restore).
7784 	 * In all other cases we setup for the new state
7785 	 * and return success.
7786 	 */
7787 	switch (level) {
7788 	case SD_SPINDLE_OFF:
7789 		if ((medium_present == TRUE) && (sval != 0)) {
7790 			/* The stop command from above failed */
7791 			rval = DDI_FAILURE;
7792 			/*
7793 			 * The stop command failed, and we have media
7794 			 * present. Put the level back by calling the
7795 			 * sd_pm_resume() and set the state back to
7796 			 * it's previous value.
7797 			 */
7798 			(void) sd_ddi_pm_resume(un);
7799 			mutex_enter(SD_MUTEX(un));
7800 			un->un_last_state = save_state;
7801 			mutex_exit(SD_MUTEX(un));
7802 			break;
7803 		}
7804 		/*
7805 		 * The stop command from above succeeded.
7806 		 */
7807 		if (un->un_f_monitor_media_state) {
7808 			/*
7809 			 * Terminate watch thread in case of removable media
7810 			 * devices going into low power state. This is as per
7811 			 * the requirements of pm framework, otherwise commands
7812 			 * will be generated for the device (through watch
7813 			 * thread), even when the device is in low power state.
7814 			 */
7815 			mutex_enter(SD_MUTEX(un));
7816 			un->un_f_watcht_stopped = FALSE;
7817 			if (un->un_swr_token != NULL) {
7818 				opaque_t temp_token = un->un_swr_token;
7819 				un->un_f_watcht_stopped = TRUE;
7820 				un->un_swr_token = NULL;
7821 				mutex_exit(SD_MUTEX(un));
7822 				(void) scsi_watch_request_terminate(temp_token,
7823 				    SCSI_WATCH_TERMINATE_WAIT);
7824 			} else {
7825 				mutex_exit(SD_MUTEX(un));
7826 			}
7827 		}
7828 		break;
7829 
7830 	default:	/* The level requested is spindle on... */
7831 		/*
7832 		 * Legacy behavior: return success on a failed spinup
7833 		 * if there is no media in the drive.
7834 		 * Do this by looking at medium_present here.
7835 		 */
7836 		if ((sval != 0) && medium_present) {
7837 			/* The start command from above failed */
7838 			rval = DDI_FAILURE;
7839 			break;
7840 		}
7841 		/*
7842 		 * The start command from above succeeded
7843 		 * Resume the devices now that we have
7844 		 * started the disks
7845 		 */
7846 		(void) sd_ddi_pm_resume(un);
7847 
7848 		/*
7849 		 * Resume the watch thread since it was suspended
7850 		 * when the device went into low power mode.
7851 		 */
7852 		if (un->un_f_monitor_media_state) {
7853 			mutex_enter(SD_MUTEX(un));
7854 			if (un->un_f_watcht_stopped == TRUE) {
7855 				opaque_t temp_token;
7856 
7857 				un->un_f_watcht_stopped = FALSE;
7858 				mutex_exit(SD_MUTEX(un));
7859 				temp_token = scsi_watch_request_submit(
7860 				    SD_SCSI_DEVP(un),
7861 				    sd_check_media_time,
7862 				    SENSE_LENGTH, sd_media_watch_cb,
7863 				    (caddr_t)dev);
7864 				mutex_enter(SD_MUTEX(un));
7865 				un->un_swr_token = temp_token;
7866 			}
7867 			mutex_exit(SD_MUTEX(un));
7868 		}
7869 	}
7870 	if (got_semaphore_here != 0) {
7871 		sema_v(&un->un_semoclose);
7872 	}
7873 	/*
7874 	 * On exit put the state back to it's original value
7875 	 * and broadcast to anyone waiting for the power
7876 	 * change completion.
7877 	 */
7878 	mutex_enter(SD_MUTEX(un));
7879 	un->un_state = state_before_pm;
7880 	cv_broadcast(&un->un_suspend_cv);
7881 	mutex_exit(SD_MUTEX(un));
7882 
7883 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7884 
7885 	return (rval);
7886 }
7887 
7888 
7889 
7890 /*
7891  *    Function: sdattach
7892  *
7893  * Description: Driver's attach(9e) entry point function.
7894  *
7895  *   Arguments: devi - opaque device info handle
7896  *		cmd  - attach  type
7897  *
7898  * Return Code: DDI_SUCCESS
7899  *		DDI_FAILURE
7900  *
7901  *     Context: Kernel thread context
7902  */
7903 
7904 static int
7905 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7906 {
7907 	switch (cmd) {
7908 	case DDI_ATTACH:
7909 		return (sd_unit_attach(devi));
7910 	case DDI_RESUME:
7911 		return (sd_ddi_resume(devi));
7912 	default:
7913 		break;
7914 	}
7915 	return (DDI_FAILURE);
7916 }
7917 
7918 
7919 /*
7920  *    Function: sddetach
7921  *
7922  * Description: Driver's detach(9E) entry point function.
7923  *
7924  *   Arguments: devi - opaque device info handle
7925  *		cmd  - detach  type
7926  *
7927  * Return Code: DDI_SUCCESS
7928  *		DDI_FAILURE
7929  *
7930  *     Context: Kernel thread context
7931  */
7932 
7933 static int
7934 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7935 {
7936 	switch (cmd) {
7937 	case DDI_DETACH:
7938 		return (sd_unit_detach(devi));
7939 	case DDI_SUSPEND:
7940 		return (sd_ddi_suspend(devi));
7941 	default:
7942 		break;
7943 	}
7944 	return (DDI_FAILURE);
7945 }
7946 
7947 
7948 /*
7949  *     Function: sd_sync_with_callback
7950  *
7951  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7952  *		 state while the callback routine is active.
7953  *
7954  *    Arguments: un: softstate structure for the instance
7955  *
7956  *	Context: Kernel thread context
7957  */
7958 
7959 static void
7960 sd_sync_with_callback(struct sd_lun *un)
7961 {
7962 	ASSERT(un != NULL);
7963 
7964 	mutex_enter(SD_MUTEX(un));
7965 
7966 	ASSERT(un->un_in_callback >= 0);
7967 
7968 	while (un->un_in_callback > 0) {
7969 		mutex_exit(SD_MUTEX(un));
7970 		delay(2);
7971 		mutex_enter(SD_MUTEX(un));
7972 	}
7973 
7974 	mutex_exit(SD_MUTEX(un));
7975 }
7976 
7977 /*
7978  *    Function: sd_unit_attach
7979  *
7980  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7981  *		the soft state structure for the device and performs
7982  *		all necessary structure and device initializations.
7983  *
7984  *   Arguments: devi: the system's dev_info_t for the device.
7985  *
7986  * Return Code: DDI_SUCCESS if attach is successful.
7987  *		DDI_FAILURE if any part of the attach fails.
7988  *
7989  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7990  *		Kernel thread context only.  Can sleep.
7991  */
7992 
7993 static int
7994 sd_unit_attach(dev_info_t *devi)
7995 {
7996 	struct	scsi_device	*devp;
7997 	struct	sd_lun		*un;
7998 	char			*variantp;
7999 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
8000 	int	instance;
8001 	int	rval;
8002 	int	wc_enabled;
8003 	int	tgt;
8004 	uint64_t	capacity;
8005 	uint_t		lbasize;
8006 	dev_info_t	*pdip = ddi_get_parent(devi);
8007 
8008 	/*
8009 	 * Retrieve the target driver's private data area. This was set
8010 	 * up by the HBA.
8011 	 */
8012 	devp = ddi_get_driver_private(devi);
8013 
8014 	/*
8015 	 * Retrieve the target ID of the device.
8016 	 */
8017 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8018 	    SCSI_ADDR_PROP_TARGET, -1);
8019 
8020 	/*
8021 	 * Since we have no idea what state things were left in by the last
8022 	 * user of the device, set up some 'default' settings, ie. turn 'em
8023 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
8024 	 * Do this before the scsi_probe, which sends an inquiry.
8025 	 * This is a fix for bug (4430280).
8026 	 * Of special importance is wide-xfer. The drive could have been left
8027 	 * in wide transfer mode by the last driver to communicate with it,
8028 	 * this includes us. If that's the case, and if the following is not
8029 	 * setup properly or we don't re-negotiate with the drive prior to
8030 	 * transferring data to/from the drive, it causes bus parity errors,
8031 	 * data overruns, and unexpected interrupts. This first occurred when
8032 	 * the fix for bug (4378686) was made.
8033 	 */
8034 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
8035 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
8036 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
8037 
8038 	/*
8039 	 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
8040 	 * on a target. Setting it per lun instance actually sets the
8041 	 * capability of this target, which affects those luns already
8042 	 * attached on the same target. So during attach, we can only disable
8043 	 * this capability only when no other lun has been attached on this
8044 	 * target. By doing this, we assume a target has the same tagged-qing
8045 	 * capability for every lun. The condition can be removed when HBA
8046 	 * is changed to support per lun based tagged-qing capability.
8047 	 */
8048 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8049 		(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
8050 	}
8051 
8052 	/*
8053 	 * Use scsi_probe() to issue an INQUIRY command to the device.
8054 	 * This call will allocate and fill in the scsi_inquiry structure
8055 	 * and point the sd_inq member of the scsi_device structure to it.
8056 	 * If the attach succeeds, then this memory will not be de-allocated
8057 	 * (via scsi_unprobe()) until the instance is detached.
8058 	 */
8059 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
8060 		goto probe_failed;
8061 	}
8062 
8063 	/*
8064 	 * Check the device type as specified in the inquiry data and
8065 	 * claim it if it is of a type that we support.
8066 	 */
8067 	switch (devp->sd_inq->inq_dtype) {
8068 	case DTYPE_DIRECT:
8069 		break;
8070 	case DTYPE_RODIRECT:
8071 		break;
8072 	case DTYPE_OPTICAL:
8073 		break;
8074 	case DTYPE_NOTPRESENT:
8075 	default:
8076 		/* Unsupported device type; fail the attach. */
8077 		goto probe_failed;
8078 	}
8079 
8080 	/*
8081 	 * Allocate the soft state structure for this unit.
8082 	 *
8083 	 * We rely upon this memory being set to all zeroes by
8084 	 * ddi_soft_state_zalloc().  We assume that any member of the
8085 	 * soft state structure that is not explicitly initialized by
8086 	 * this routine will have a value of zero.
8087 	 */
8088 	instance = ddi_get_instance(devp->sd_dev);
8089 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
8090 		goto probe_failed;
8091 	}
8092 
8093 	/*
8094 	 * Retrieve a pointer to the newly-allocated soft state.
8095 	 *
8096 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
8097 	 * was successful, unless something has gone horribly wrong and the
8098 	 * ddi's soft state internals are corrupt (in which case it is
8099 	 * probably better to halt here than just fail the attach....)
8100 	 */
8101 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
8102 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
8103 		    instance);
8104 		/*NOTREACHED*/
8105 	}
8106 
8107 	/*
8108 	 * Link the back ptr of the driver soft state to the scsi_device
8109 	 * struct for this lun.
8110 	 * Save a pointer to the softstate in the driver-private area of
8111 	 * the scsi_device struct.
8112 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
8113 	 * we first set un->un_sd below.
8114 	 */
8115 	un->un_sd = devp;
8116 	devp->sd_private = (opaque_t)un;
8117 
8118 	/*
8119 	 * The following must be after devp is stored in the soft state struct.
8120 	 */
8121 #ifdef SDDEBUG
8122 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8123 	    "%s_unit_attach: un:0x%p instance:%d\n",
8124 	    ddi_driver_name(devi), un, instance);
8125 #endif
8126 
8127 	/*
8128 	 * Set up the device type and node type (for the minor nodes).
8129 	 * By default we assume that the device can at least support the
8130 	 * Common Command Set. Call it a CD-ROM if it reports itself
8131 	 * as a RODIRECT device.
8132 	 */
8133 	switch (devp->sd_inq->inq_dtype) {
8134 	case DTYPE_RODIRECT:
8135 		un->un_node_type = DDI_NT_CD_CHAN;
8136 		un->un_ctype	 = CTYPE_CDROM;
8137 		break;
8138 	case DTYPE_OPTICAL:
8139 		un->un_node_type = DDI_NT_BLOCK_CHAN;
8140 		un->un_ctype	 = CTYPE_ROD;
8141 		break;
8142 	default:
8143 		un->un_node_type = DDI_NT_BLOCK_CHAN;
8144 		un->un_ctype	 = CTYPE_CCS;
8145 		break;
8146 	}
8147 
8148 	/*
8149 	 * Try to read the interconnect type from the HBA.
8150 	 *
8151 	 * Note: This driver is currently compiled as two binaries, a parallel
8152 	 * scsi version (sd) and a fibre channel version (ssd). All functional
8153 	 * differences are determined at compile time. In the future a single
8154 	 * binary will be provided and the inteconnect type will be used to
8155 	 * differentiate between fibre and parallel scsi behaviors. At that time
8156 	 * it will be necessary for all fibre channel HBAs to support this
8157 	 * property.
8158 	 *
8159 	 * set un_f_is_fiber to TRUE ( default fiber )
8160 	 */
8161 	un->un_f_is_fibre = TRUE;
8162 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
8163 	case INTERCONNECT_SSA:
8164 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
8165 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8166 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
8167 		break;
8168 	case INTERCONNECT_PARALLEL:
8169 		un->un_f_is_fibre = FALSE;
8170 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
8171 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8172 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
8173 		break;
8174 	case INTERCONNECT_SATA:
8175 		un->un_f_is_fibre = FALSE;
8176 		un->un_interconnect_type = SD_INTERCONNECT_SATA;
8177 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8178 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
8179 		break;
8180 	case INTERCONNECT_FIBRE:
8181 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
8182 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8183 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
8184 		break;
8185 	case INTERCONNECT_FABRIC:
8186 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
8187 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
8188 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8189 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
8190 		break;
8191 	default:
8192 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
8193 		/*
8194 		 * The HBA does not support the "interconnect-type" property
8195 		 * (or did not provide a recognized type).
8196 		 *
8197 		 * Note: This will be obsoleted when a single fibre channel
8198 		 * and parallel scsi driver is delivered. In the meantime the
8199 		 * interconnect type will be set to the platform default.If that
8200 		 * type is not parallel SCSI, it means that we should be
8201 		 * assuming "ssd" semantics. However, here this also means that
8202 		 * the FC HBA is not supporting the "interconnect-type" property
8203 		 * like we expect it to, so log this occurrence.
8204 		 */
8205 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
8206 		if (!SD_IS_PARALLEL_SCSI(un)) {
8207 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8208 			    "sd_unit_attach: un:0x%p Assuming "
8209 			    "INTERCONNECT_FIBRE\n", un);
8210 		} else {
8211 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8212 			    "sd_unit_attach: un:0x%p Assuming "
8213 			    "INTERCONNECT_PARALLEL\n", un);
8214 			un->un_f_is_fibre = FALSE;
8215 		}
8216 #else
8217 		/*
8218 		 * Note: This source will be implemented when a single fibre
8219 		 * channel and parallel scsi driver is delivered. The default
8220 		 * will be to assume that if a device does not support the
8221 		 * "interconnect-type" property it is a parallel SCSI HBA and
8222 		 * we will set the interconnect type for parallel scsi.
8223 		 */
8224 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
8225 		un->un_f_is_fibre = FALSE;
8226 #endif
8227 		break;
8228 	}
8229 
8230 	if (un->un_f_is_fibre == TRUE) {
8231 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
8232 			SCSI_VERSION_3) {
8233 			switch (un->un_interconnect_type) {
8234 			case SD_INTERCONNECT_FIBRE:
8235 			case SD_INTERCONNECT_SSA:
8236 				un->un_node_type = DDI_NT_BLOCK_WWN;
8237 				break;
8238 			default:
8239 				break;
8240 			}
8241 		}
8242 	}
8243 
8244 	/*
8245 	 * Initialize the Request Sense command for the target
8246 	 */
8247 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
8248 		goto alloc_rqs_failed;
8249 	}
8250 
8251 	/*
8252 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
8253 	 * with seperate binary for sd and ssd.
8254 	 *
8255 	 * x86 has 1 binary, un_retry_count is set base on connection type.
8256 	 * The hardcoded values will go away when Sparc uses 1 binary
8257 	 * for sd and ssd.  This hardcoded values need to match
8258 	 * SD_RETRY_COUNT in sddef.h
8259 	 * The value used is base on interconnect type.
8260 	 * fibre = 3, parallel = 5
8261 	 */
8262 #if defined(__i386) || defined(__amd64)
8263 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
8264 #else
8265 	un->un_retry_count = SD_RETRY_COUNT;
8266 #endif
8267 
8268 	/*
8269 	 * Set the per disk retry count to the default number of retries
8270 	 * for disks and CDROMs. This value can be overridden by the
8271 	 * disk property list or an entry in sd.conf.
8272 	 */
8273 	un->un_notready_retry_count =
8274 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
8275 			: DISK_NOT_READY_RETRY_COUNT(un);
8276 
8277 	/*
8278 	 * Set the busy retry count to the default value of un_retry_count.
8279 	 * This can be overridden by entries in sd.conf or the device
8280 	 * config table.
8281 	 */
8282 	un->un_busy_retry_count = un->un_retry_count;
8283 
8284 	/*
8285 	 * Init the reset threshold for retries.  This number determines
8286 	 * how many retries must be performed before a reset can be issued
8287 	 * (for certain error conditions). This can be overridden by entries
8288 	 * in sd.conf or the device config table.
8289 	 */
8290 	un->un_reset_retry_count = (un->un_retry_count / 2);
8291 
8292 	/*
8293 	 * Set the victim_retry_count to the default un_retry_count
8294 	 */
8295 	un->un_victim_retry_count = (2 * un->un_retry_count);
8296 
8297 	/*
8298 	 * Set the reservation release timeout to the default value of
8299 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
8300 	 * device config table.
8301 	 */
8302 	un->un_reserve_release_time = 5;
8303 
8304 	/*
8305 	 * Set up the default maximum transfer size. Note that this may
8306 	 * get updated later in the attach, when setting up default wide
8307 	 * operations for disks.
8308 	 */
8309 #if defined(__i386) || defined(__amd64)
8310 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
8311 #else
8312 	un->un_max_xfer_size = (uint_t)maxphys;
8313 #endif
8314 
8315 	/*
8316 	 * Get "allow bus device reset" property (defaults to "enabled" if
8317 	 * the property was not defined). This is to disable bus resets for
8318 	 * certain kinds of error recovery. Note: In the future when a run-time
8319 	 * fibre check is available the soft state flag should default to
8320 	 * enabled.
8321 	 */
8322 	if (un->un_f_is_fibre == TRUE) {
8323 		un->un_f_allow_bus_device_reset = TRUE;
8324 	} else {
8325 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8326 			"allow-bus-device-reset", 1) != 0) {
8327 			un->un_f_allow_bus_device_reset = TRUE;
8328 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8329 			"sd_unit_attach: un:0x%p Bus device reset enabled\n",
8330 				un);
8331 		} else {
8332 			un->un_f_allow_bus_device_reset = FALSE;
8333 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8334 			"sd_unit_attach: un:0x%p Bus device reset disabled\n",
8335 				un);
8336 		}
8337 	}
8338 
8339 	/*
8340 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
8341 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
8342 	 *
8343 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
8344 	 * property. The new "variant" property with a value of "atapi" has been
8345 	 * introduced so that future 'variants' of standard SCSI behavior (like
8346 	 * atapi) could be specified by the underlying HBA drivers by supplying
8347 	 * a new value for the "variant" property, instead of having to define a
8348 	 * new property.
8349 	 */
8350 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
8351 		un->un_f_cfg_is_atapi = TRUE;
8352 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8353 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
8354 	}
8355 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
8356 	    &variantp) == DDI_PROP_SUCCESS) {
8357 		if (strcmp(variantp, "atapi") == 0) {
8358 			un->un_f_cfg_is_atapi = TRUE;
8359 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8360 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
8361 		}
8362 		ddi_prop_free(variantp);
8363 	}
8364 
8365 	un->un_cmd_timeout	= SD_IO_TIME;
8366 
8367 	/* Info on current states, statuses, etc. (Updated frequently) */
8368 	un->un_state		= SD_STATE_NORMAL;
8369 	un->un_last_state	= SD_STATE_NORMAL;
8370 
8371 	/* Control & status info for command throttling */
8372 	un->un_throttle		= sd_max_throttle;
8373 	un->un_saved_throttle	= sd_max_throttle;
8374 	un->un_min_throttle	= sd_min_throttle;
8375 
8376 	if (un->un_f_is_fibre == TRUE) {
8377 		un->un_f_use_adaptive_throttle = TRUE;
8378 	} else {
8379 		un->un_f_use_adaptive_throttle = FALSE;
8380 	}
8381 
8382 	/* Removable media support. */
8383 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
8384 	un->un_mediastate		= DKIO_NONE;
8385 	un->un_specified_mediastate	= DKIO_NONE;
8386 
8387 	/* CVs for suspend/resume (PM or DR) */
8388 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
8389 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
8390 
8391 	/* Power management support. */
8392 	un->un_power_level = SD_SPINDLE_UNINIT;
8393 
8394 	cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
8395 	un->un_f_wcc_inprog = 0;
8396 
8397 	/*
8398 	 * The open/close semaphore is used to serialize threads executing
8399 	 * in the driver's open & close entry point routines for a given
8400 	 * instance.
8401 	 */
8402 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
8403 
8404 	/*
8405 	 * The conf file entry and softstate variable is a forceful override,
8406 	 * meaning a non-zero value must be entered to change the default.
8407 	 */
8408 	un->un_f_disksort_disabled = FALSE;
8409 
8410 	/*
8411 	 * Retrieve the properties from the static driver table or the driver
8412 	 * configuration file (.conf) for this unit and update the soft state
8413 	 * for the device as needed for the indicated properties.
8414 	 * Note: the property configuration needs to occur here as some of the
8415 	 * following routines may have dependancies on soft state flags set
8416 	 * as part of the driver property configuration.
8417 	 */
8418 	sd_read_unit_properties(un);
8419 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8420 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
8421 
8422 	/*
8423 	 * Only if a device has "hotpluggable" property, it is
8424 	 * treated as hotpluggable device. Otherwise, it is
8425 	 * regarded as non-hotpluggable one.
8426 	 */
8427 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
8428 	    -1) != -1) {
8429 		un->un_f_is_hotpluggable = TRUE;
8430 	}
8431 
8432 	/*
8433 	 * set unit's attributes(flags) according to "hotpluggable" and
8434 	 * RMB bit in INQUIRY data.
8435 	 */
8436 	sd_set_unit_attributes(un, devi);
8437 
8438 	/*
8439 	 * By default, we mark the capacity, lbasize, and geometry
8440 	 * as invalid. Only if we successfully read a valid capacity
8441 	 * will we update the un_blockcount and un_tgt_blocksize with the
8442 	 * valid values (the geometry will be validated later).
8443 	 */
8444 	un->un_f_blockcount_is_valid	= FALSE;
8445 	un->un_f_tgt_blocksize_is_valid	= FALSE;
8446 	un->un_f_geometry_is_valid	= FALSE;
8447 
8448 	/*
8449 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
8450 	 * otherwise.
8451 	 */
8452 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
8453 	un->un_blockcount = 0;
8454 
8455 	/*
8456 	 * Set up the per-instance info needed to determine the correct
8457 	 * CDBs and other info for issuing commands to the target.
8458 	 */
8459 	sd_init_cdb_limits(un);
8460 
8461 	/*
8462 	 * Set up the IO chains to use, based upon the target type.
8463 	 */
8464 	if (un->un_f_non_devbsize_supported) {
8465 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
8466 	} else {
8467 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
8468 	}
8469 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
8470 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
8471 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
8472 
8473 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
8474 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
8475 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
8476 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
8477 
8478 
8479 	if (ISCD(un)) {
8480 		un->un_additional_codes = sd_additional_codes;
8481 	} else {
8482 		un->un_additional_codes = NULL;
8483 	}
8484 
8485 	/*
8486 	 * Create the kstats here so they can be available for attach-time
8487 	 * routines that send commands to the unit (either polled or via
8488 	 * sd_send_scsi_cmd).
8489 	 *
8490 	 * Note: This is a critical sequence that needs to be maintained:
8491 	 *	1) Instantiate the kstats here, before any routines using the
8492 	 *	   iopath (i.e. sd_send_scsi_cmd).
8493 	 *	2) Instantiate and initialize the partition stats
8494 	 *	   (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(),
8495 	 *	   see detailed comments there.
8496 	 *	3) Initialize the error stats (sd_set_errstats), following
8497 	 *	   sd_validate_geometry(),sd_register_devid(),
8498 	 *	   and sd_cache_control().
8499 	 */
8500 
8501 	un->un_stats = kstat_create(sd_label, instance,
8502 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
8503 	if (un->un_stats != NULL) {
8504 		un->un_stats->ks_lock = SD_MUTEX(un);
8505 		kstat_install(un->un_stats);
8506 	}
8507 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8508 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
8509 
8510 	sd_create_errstats(un, instance);
8511 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8512 	    "sd_unit_attach: un:0x%p errstats created\n", un);
8513 
8514 	/*
8515 	 * The following if/else code was relocated here from below as part
8516 	 * of the fix for bug (4430280). However with the default setup added
8517 	 * on entry to this routine, it's no longer absolutely necessary for
8518 	 * this to be before the call to sd_spin_up_unit.
8519 	 */
8520 	if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
8521 		/*
8522 		 * If SCSI-2 tagged queueing is supported by the target
8523 		 * and by the host adapter then we will enable it.
8524 		 */
8525 		un->un_tagflags = 0;
8526 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) &&
8527 		    (devp->sd_inq->inq_cmdque) &&
8528 		    (un->un_f_arq_enabled == TRUE)) {
8529 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
8530 			    1, 1) == 1) {
8531 				un->un_tagflags = FLAG_STAG;
8532 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8533 				    "sd_unit_attach: un:0x%p tag queueing "
8534 				    "enabled\n", un);
8535 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
8536 			    "untagged-qing", 0) == 1) {
8537 				un->un_f_opt_queueing = TRUE;
8538 				un->un_saved_throttle = un->un_throttle =
8539 				    min(un->un_throttle, 3);
8540 			} else {
8541 				un->un_f_opt_queueing = FALSE;
8542 				un->un_saved_throttle = un->un_throttle = 1;
8543 			}
8544 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
8545 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
8546 			/* The Host Adapter supports internal queueing. */
8547 			un->un_f_opt_queueing = TRUE;
8548 			un->un_saved_throttle = un->un_throttle =
8549 			    min(un->un_throttle, 3);
8550 		} else {
8551 			un->un_f_opt_queueing = FALSE;
8552 			un->un_saved_throttle = un->un_throttle = 1;
8553 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8554 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
8555 		}
8556 
8557 		/*
8558 		 * Enable large transfers for SATA/SAS drives
8559 		 */
8560 		if (SD_IS_SERIAL(un)) {
8561 			un->un_max_xfer_size =
8562 			    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8563 			    sd_max_xfer_size, SD_MAX_XFER_SIZE);
8564 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8565 			    "sd_unit_attach: un:0x%p max transfer "
8566 			    "size=0x%x\n", un, un->un_max_xfer_size);
8567 
8568 		}
8569 
8570 		/* Setup or tear down default wide operations for disks */
8571 
8572 		/*
8573 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
8574 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
8575 		 * system and be set to different values. In the future this
8576 		 * code may need to be updated when the ssd module is
8577 		 * obsoleted and removed from the system. (4299588)
8578 		 */
8579 		if (SD_IS_PARALLEL_SCSI(un) &&
8580 		    (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
8581 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
8582 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
8583 			    1, 1) == 1) {
8584 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8585 				    "sd_unit_attach: un:0x%p Wide Transfer "
8586 				    "enabled\n", un);
8587 			}
8588 
8589 			/*
8590 			 * If tagged queuing has also been enabled, then
8591 			 * enable large xfers
8592 			 */
8593 			if (un->un_saved_throttle == sd_max_throttle) {
8594 				un->un_max_xfer_size =
8595 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8596 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
8597 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8598 				    "sd_unit_attach: un:0x%p max transfer "
8599 				    "size=0x%x\n", un, un->un_max_xfer_size);
8600 			}
8601 		} else {
8602 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
8603 			    0, 1) == 1) {
8604 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8605 				    "sd_unit_attach: un:0x%p "
8606 				    "Wide Transfer disabled\n", un);
8607 			}
8608 		}
8609 	} else {
8610 		un->un_tagflags = FLAG_STAG;
8611 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
8612 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
8613 	}
8614 
8615 	/*
8616 	 * If this target supports LUN reset, try to enable it.
8617 	 */
8618 	if (un->un_f_lun_reset_enabled) {
8619 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
8620 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
8621 			    "un:0x%p lun_reset capability set\n", un);
8622 		} else {
8623 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
8624 			    "un:0x%p lun-reset capability not set\n", un);
8625 		}
8626 	}
8627 
8628 	/*
8629 	 * At this point in the attach, we have enough info in the
8630 	 * soft state to be able to issue commands to the target.
8631 	 *
8632 	 * All command paths used below MUST issue their commands as
8633 	 * SD_PATH_DIRECT. This is important as intermediate layers
8634 	 * are not all initialized yet (such as PM).
8635 	 */
8636 
8637 	/*
8638 	 * Send a TEST UNIT READY command to the device. This should clear
8639 	 * any outstanding UNIT ATTENTION that may be present.
8640 	 *
8641 	 * Note: Don't check for success, just track if there is a reservation,
8642 	 * this is a throw away command to clear any unit attentions.
8643 	 *
8644 	 * Note: This MUST be the first command issued to the target during
8645 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
8646 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
8647 	 * with attempts at spinning up a device with no media.
8648 	 */
8649 	if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) {
8650 		reservation_flag = SD_TARGET_IS_RESERVED;
8651 	}
8652 
8653 	/*
8654 	 * If the device is NOT a removable media device, attempt to spin
8655 	 * it up (using the START_STOP_UNIT command) and read its capacity
8656 	 * (using the READ CAPACITY command).  Note, however, that either
8657 	 * of these could fail and in some cases we would continue with
8658 	 * the attach despite the failure (see below).
8659 	 */
8660 	if (un->un_f_descr_format_supported) {
8661 		switch (sd_spin_up_unit(un)) {
8662 		case 0:
8663 			/*
8664 			 * Spin-up was successful; now try to read the
8665 			 * capacity.  If successful then save the results
8666 			 * and mark the capacity & lbasize as valid.
8667 			 */
8668 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8669 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
8670 
8671 			switch (sd_send_scsi_READ_CAPACITY(un, &capacity,
8672 			    &lbasize, SD_PATH_DIRECT)) {
8673 			case 0: {
8674 				if (capacity > DK_MAX_BLOCKS) {
8675 #ifdef _LP64
8676 					if (capacity + 1 >
8677 					    SD_GROUP1_MAX_ADDRESS) {
8678 						/*
8679 						 * Enable descriptor format
8680 						 * sense data so that we can
8681 						 * get 64 bit sense data
8682 						 * fields.
8683 						 */
8684 						sd_enable_descr_sense(un);
8685 					}
8686 #else
8687 					/* 32-bit kernels can't handle this */
8688 					scsi_log(SD_DEVINFO(un),
8689 					    sd_label, CE_WARN,
8690 					    "disk has %llu blocks, which "
8691 					    "is too large for a 32-bit "
8692 					    "kernel", capacity);
8693 
8694 #if defined(__i386) || defined(__amd64)
8695 					/*
8696 					 * Refer to comments related to off-by-1
8697 					 * at the header of this file.
8698 					 * 1TB disk was treated as (1T - 512)B
8699 					 * in the past, so that it might has
8700 					 * valid VTOC and solaris partitions,
8701 					 * we have to allow it to continue to
8702 					 * work.
8703 					 */
8704 					if (capacity -1 > DK_MAX_BLOCKS)
8705 #endif
8706 					goto spinup_failed;
8707 #endif
8708 				}
8709 
8710 				/*
8711 				 * Here it's not necessary to check the case:
8712 				 * the capacity of the device is bigger than
8713 				 * what the max hba cdb can support. Because
8714 				 * sd_send_scsi_READ_CAPACITY will retrieve
8715 				 * the capacity by sending USCSI command, which
8716 				 * is constrained by the max hba cdb. Actually,
8717 				 * sd_send_scsi_READ_CAPACITY will return
8718 				 * EINVAL when using bigger cdb than required
8719 				 * cdb length. Will handle this case in
8720 				 * "case EINVAL".
8721 				 */
8722 
8723 				/*
8724 				 * The following relies on
8725 				 * sd_send_scsi_READ_CAPACITY never
8726 				 * returning 0 for capacity and/or lbasize.
8727 				 */
8728 				sd_update_block_info(un, lbasize, capacity);
8729 
8730 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8731 				    "sd_unit_attach: un:0x%p capacity = %ld "
8732 				    "blocks; lbasize= %ld.\n", un,
8733 				    un->un_blockcount, un->un_tgt_blocksize);
8734 
8735 				break;
8736 			}
8737 			case EINVAL:
8738 				/*
8739 				 * In the case where the max-cdb-length property
8740 				 * is smaller than the required CDB length for
8741 				 * a SCSI device, a target driver can fail to
8742 				 * attach to that device.
8743 				 */
8744 				scsi_log(SD_DEVINFO(un),
8745 				    sd_label, CE_WARN,
8746 				    "disk capacity is too large "
8747 				    "for current cdb length");
8748 				goto spinup_failed;
8749 			case EACCES:
8750 				/*
8751 				 * Should never get here if the spin-up
8752 				 * succeeded, but code it in anyway.
8753 				 * From here, just continue with the attach...
8754 				 */
8755 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8756 				    "sd_unit_attach: un:0x%p "
8757 				    "sd_send_scsi_READ_CAPACITY "
8758 				    "returned reservation conflict\n", un);
8759 				reservation_flag = SD_TARGET_IS_RESERVED;
8760 				break;
8761 			default:
8762 				/*
8763 				 * Likewise, should never get here if the
8764 				 * spin-up succeeded. Just continue with
8765 				 * the attach...
8766 				 */
8767 				break;
8768 			}
8769 			break;
8770 		case EACCES:
8771 			/*
8772 			 * Device is reserved by another host.  In this case
8773 			 * we could not spin it up or read the capacity, but
8774 			 * we continue with the attach anyway.
8775 			 */
8776 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8777 			    "sd_unit_attach: un:0x%p spin-up reservation "
8778 			    "conflict.\n", un);
8779 			reservation_flag = SD_TARGET_IS_RESERVED;
8780 			break;
8781 		default:
8782 			/* Fail the attach if the spin-up failed. */
8783 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8784 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8785 			goto spinup_failed;
8786 		}
8787 	}
8788 
8789 	/*
8790 	 * Check to see if this is a MMC drive
8791 	 */
8792 	if (ISCD(un)) {
8793 		sd_set_mmc_caps(un);
8794 	}
8795 
8796 	/*
8797 	 * Create the minor nodes for the device.
8798 	 * Note: If we want to support fdisk on both sparc and intel, this will
8799 	 * have to separate out the notion that VTOC8 is always sparc, and
8800 	 * VTOC16 is always intel (tho these can be the defaults).  The vtoc
8801 	 * type will have to be determined at run-time, and the fdisk
8802 	 * partitioning will have to have been read & set up before we
8803 	 * create the minor nodes. (any other inits (such as kstats) that
8804 	 * also ought to be done before creating the minor nodes?) (Doesn't
8805 	 * setting up the minor nodes kind of imply that we're ready to
8806 	 * handle an open from userland?)
8807 	 */
8808 	if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) {
8809 		goto create_minor_nodes_failed;
8810 	}
8811 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8812 	    "sd_unit_attach: un:0x%p minor nodes created\n", un);
8813 
8814 	/*
8815 	 * Add a zero-length attribute to tell the world we support
8816 	 * kernel ioctls (for layered drivers)
8817 	 */
8818 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8819 	    DDI_KERNEL_IOCTL, NULL, 0);
8820 
8821 	/*
8822 	 * Add a boolean property to tell the world we support
8823 	 * the B_FAILFAST flag (for layered drivers)
8824 	 */
8825 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8826 	    "ddi-failfast-supported", NULL, 0);
8827 
8828 	/*
8829 	 * Initialize power management
8830 	 */
8831 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8832 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8833 	sd_setup_pm(un, devi);
8834 	if (un->un_f_pm_is_enabled == FALSE) {
8835 		/*
8836 		 * For performance, point to a jump table that does
8837 		 * not include pm.
8838 		 * The direct and priority chains don't change with PM.
8839 		 *
8840 		 * Note: this is currently done based on individual device
8841 		 * capabilities. When an interface for determining system
8842 		 * power enabled state becomes available, or when additional
8843 		 * layers are added to the command chain, these values will
8844 		 * have to be re-evaluated for correctness.
8845 		 */
8846 		if (un->un_f_non_devbsize_supported) {
8847 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8848 		} else {
8849 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8850 		}
8851 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8852 	}
8853 
8854 	/*
8855 	 * This property is set to 0 by HA software to avoid retries
8856 	 * on a reserved disk. (The preferred property name is
8857 	 * "retry-on-reservation-conflict") (1189689)
8858 	 *
8859 	 * Note: The use of a global here can have unintended consequences. A
8860 	 * per instance variable is preferrable to match the capabilities of
8861 	 * different underlying hba's (4402600)
8862 	 */
8863 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8864 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8865 	    sd_retry_on_reservation_conflict);
8866 	if (sd_retry_on_reservation_conflict != 0) {
8867 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8868 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8869 		    sd_retry_on_reservation_conflict);
8870 	}
8871 
8872 	/* Set up options for QFULL handling. */
8873 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8874 	    "qfull-retries", -1)) != -1) {
8875 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8876 		    rval, 1);
8877 	}
8878 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8879 	    "qfull-retry-interval", -1)) != -1) {
8880 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8881 		    rval, 1);
8882 	}
8883 
8884 	/*
8885 	 * This just prints a message that announces the existence of the
8886 	 * device. The message is always printed in the system logfile, but
8887 	 * only appears on the console if the system is booted with the
8888 	 * -v (verbose) argument.
8889 	 */
8890 	ddi_report_dev(devi);
8891 
8892 	/*
8893 	 * The framework calls driver attach routines single-threaded
8894 	 * for a given instance.  However we still acquire SD_MUTEX here
8895 	 * because this required for calling the sd_validate_geometry()
8896 	 * and sd_register_devid() functions.
8897 	 */
8898 	mutex_enter(SD_MUTEX(un));
8899 	un->un_f_geometry_is_valid = FALSE;
8900 	un->un_mediastate = DKIO_NONE;
8901 	un->un_reserved = -1;
8902 
8903 	/*
8904 	 * Read and validate the device's geometry (ie, disk label)
8905 	 * A new unformatted drive will not have a valid geometry, but
8906 	 * the driver needs to successfully attach to this device so
8907 	 * the drive can be formatted via ioctls.
8908 	 */
8909 	if (((sd_validate_geometry(un, SD_PATH_DIRECT) ==
8910 	    ENOTSUP)) &&
8911 	    (un->un_blockcount < DK_MAX_BLOCKS)) {
8912 		/*
8913 		 * We found a small disk with an EFI label on it;
8914 		 * we need to fix up the minor nodes accordingly.
8915 		 */
8916 		ddi_remove_minor_node(devi, "h");
8917 		ddi_remove_minor_node(devi, "h,raw");
8918 		(void) ddi_create_minor_node(devi, "wd",
8919 		    S_IFBLK,
8920 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8921 		    un->un_node_type, NULL);
8922 		(void) ddi_create_minor_node(devi, "wd,raw",
8923 		    S_IFCHR,
8924 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8925 		    un->un_node_type, NULL);
8926 	}
8927 #if defined(__i386) || defined(__amd64)
8928 	else if (un->un_f_capacity_adjusted == 1) {
8929 		/*
8930 		 * Refer to comments related to off-by-1 at the
8931 		 * header of this file.
8932 		 * Adjust minor node for 1TB disk.
8933 		 */
8934 		ddi_remove_minor_node(devi, "wd");
8935 		ddi_remove_minor_node(devi, "wd,raw");
8936 		(void) ddi_create_minor_node(devi, "h",
8937 		    S_IFBLK,
8938 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8939 		    un->un_node_type, NULL);
8940 		(void) ddi_create_minor_node(devi, "h,raw",
8941 		    S_IFCHR,
8942 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8943 		    un->un_node_type, NULL);
8944 	}
8945 #endif
8946 	/*
8947 	 * Read and initialize the devid for the unit.
8948 	 */
8949 	ASSERT(un->un_errstats != NULL);
8950 	if (un->un_f_devid_supported) {
8951 		sd_register_devid(un, devi, reservation_flag);
8952 	}
8953 	mutex_exit(SD_MUTEX(un));
8954 
8955 #if (defined(__fibre))
8956 	/*
8957 	 * Register callbacks for fibre only.  You can't do this soley
8958 	 * on the basis of the devid_type because this is hba specific.
8959 	 * We need to query our hba capabilities to find out whether to
8960 	 * register or not.
8961 	 */
8962 	if (un->un_f_is_fibre) {
8963 	    if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8964 		sd_init_event_callbacks(un);
8965 		SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8966 		    "sd_unit_attach: un:0x%p event callbacks inserted", un);
8967 	    }
8968 	}
8969 #endif
8970 
8971 	if (un->un_f_opt_disable_cache == TRUE) {
8972 		/*
8973 		 * Disable both read cache and write cache.  This is
8974 		 * the historic behavior of the keywords in the config file.
8975 		 */
8976 		if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8977 		    0) {
8978 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8979 			    "sd_unit_attach: un:0x%p Could not disable "
8980 			    "caching", un);
8981 			goto devid_failed;
8982 		}
8983 	}
8984 
8985 	/*
8986 	 * Check the value of the WCE bit now and
8987 	 * set un_f_write_cache_enabled accordingly.
8988 	 */
8989 	(void) sd_get_write_cache_enabled(un, &wc_enabled);
8990 	mutex_enter(SD_MUTEX(un));
8991 	un->un_f_write_cache_enabled = (wc_enabled != 0);
8992 	mutex_exit(SD_MUTEX(un));
8993 
8994 	/*
8995 	 * Set the pstat and error stat values here, so data obtained during the
8996 	 * previous attach-time routines is available.
8997 	 *
8998 	 * Note: This is a critical sequence that needs to be maintained:
8999 	 *	1) Instantiate the kstats before any routines using the iopath
9000 	 *	   (i.e. sd_send_scsi_cmd).
9001 	 *	2) Instantiate and initialize the partition stats
9002 	 *	   (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(),
9003 	 *	   see detailed comments there.
9004 	 *	3) Initialize the error stats (sd_set_errstats), following
9005 	 *	   sd_validate_geometry(),sd_register_devid(),
9006 	 *	   and sd_cache_control().
9007 	 */
9008 	sd_set_errstats(un);
9009 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
9010 	    "sd_unit_attach: un:0x%p errstats set\n", un);
9011 
9012 	/*
9013 	 * Find out what type of reservation this disk supports.
9014 	 */
9015 	switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) {
9016 	case 0:
9017 		/*
9018 		 * SCSI-3 reservations are supported.
9019 		 */
9020 		un->un_reservation_type = SD_SCSI3_RESERVATION;
9021 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
9022 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
9023 		break;
9024 	case ENOTSUP:
9025 		/*
9026 		 * The PERSISTENT RESERVE IN command would not be recognized by
9027 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
9028 		 */
9029 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
9030 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
9031 		un->un_reservation_type = SD_SCSI2_RESERVATION;
9032 		break;
9033 	default:
9034 		/*
9035 		 * default to SCSI-3 reservations
9036 		 */
9037 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
9038 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
9039 		un->un_reservation_type = SD_SCSI3_RESERVATION;
9040 		break;
9041 	}
9042 
9043 	/*
9044 	 * After successfully attaching an instance, we record the information
9045 	 * of how many luns have been attached on the relative target and
9046 	 * controller for parallel SCSI. This information is used when sd tries
9047 	 * to set the tagged queuing capability in HBA.
9048 	 */
9049 	if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9050 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
9051 	}
9052 
9053 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
9054 	    "sd_unit_attach: un:0x%p exit success\n", un);
9055 
9056 	return (DDI_SUCCESS);
9057 
9058 	/*
9059 	 * An error occurred during the attach; clean up & return failure.
9060 	 */
9061 
9062 devid_failed:
9063 
9064 setup_pm_failed:
9065 	ddi_remove_minor_node(devi, NULL);
9066 
9067 create_minor_nodes_failed:
9068 	/*
9069 	 * Cleanup from the scsi_ifsetcap() calls (437868)
9070 	 */
9071 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
9072 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
9073 
9074 	/*
9075 	 * Refer to the comments of setting tagged-qing in the beginning of
9076 	 * sd_unit_attach. We can only disable tagged queuing when there is
9077 	 * no lun attached on the target.
9078 	 */
9079 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
9080 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
9081 	}
9082 
9083 	if (un->un_f_is_fibre == FALSE) {
9084 	    (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
9085 	}
9086 
9087 spinup_failed:
9088 
9089 	mutex_enter(SD_MUTEX(un));
9090 
9091 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
9092 	if (un->un_direct_priority_timeid != NULL) {
9093 		timeout_id_t temp_id = un->un_direct_priority_timeid;
9094 		un->un_direct_priority_timeid = NULL;
9095 		mutex_exit(SD_MUTEX(un));
9096 		(void) untimeout(temp_id);
9097 		mutex_enter(SD_MUTEX(un));
9098 	}
9099 
9100 	/* Cancel any pending start/stop timeouts */
9101 	if (un->un_startstop_timeid != NULL) {
9102 		timeout_id_t temp_id = un->un_startstop_timeid;
9103 		un->un_startstop_timeid = NULL;
9104 		mutex_exit(SD_MUTEX(un));
9105 		(void) untimeout(temp_id);
9106 		mutex_enter(SD_MUTEX(un));
9107 	}
9108 
9109 	/* Cancel any pending reset-throttle timeouts */
9110 	if (un->un_reset_throttle_timeid != NULL) {
9111 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
9112 		un->un_reset_throttle_timeid = NULL;
9113 		mutex_exit(SD_MUTEX(un));
9114 		(void) untimeout(temp_id);
9115 		mutex_enter(SD_MUTEX(un));
9116 	}
9117 
9118 	/* Cancel any pending retry timeouts */
9119 	if (un->un_retry_timeid != NULL) {
9120 		timeout_id_t temp_id = un->un_retry_timeid;
9121 		un->un_retry_timeid = NULL;
9122 		mutex_exit(SD_MUTEX(un));
9123 		(void) untimeout(temp_id);
9124 		mutex_enter(SD_MUTEX(un));
9125 	}
9126 
9127 	/* Cancel any pending delayed cv broadcast timeouts */
9128 	if (un->un_dcvb_timeid != NULL) {
9129 		timeout_id_t temp_id = un->un_dcvb_timeid;
9130 		un->un_dcvb_timeid = NULL;
9131 		mutex_exit(SD_MUTEX(un));
9132 		(void) untimeout(temp_id);
9133 		mutex_enter(SD_MUTEX(un));
9134 	}
9135 
9136 	mutex_exit(SD_MUTEX(un));
9137 
9138 	/* There should not be any in-progress I/O so ASSERT this check */
9139 	ASSERT(un->un_ncmds_in_transport == 0);
9140 	ASSERT(un->un_ncmds_in_driver == 0);
9141 
9142 	/* Do not free the softstate if the callback routine is active */
9143 	sd_sync_with_callback(un);
9144 
9145 	/*
9146 	 * Partition stats apparently are not used with removables. These would
9147 	 * not have been created during attach, so no need to clean them up...
9148 	 */
9149 	if (un->un_stats != NULL) {
9150 		kstat_delete(un->un_stats);
9151 		un->un_stats = NULL;
9152 	}
9153 	if (un->un_errstats != NULL) {
9154 		kstat_delete(un->un_errstats);
9155 		un->un_errstats = NULL;
9156 	}
9157 
9158 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
9159 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
9160 
9161 	ddi_prop_remove_all(devi);
9162 	sema_destroy(&un->un_semoclose);
9163 	cv_destroy(&un->un_state_cv);
9164 
9165 getrbuf_failed:
9166 
9167 	sd_free_rqs(un);
9168 
9169 alloc_rqs_failed:
9170 
9171 	devp->sd_private = NULL;
9172 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
9173 
9174 get_softstate_failed:
9175 	/*
9176 	 * Note: the man pages are unclear as to whether or not doing a
9177 	 * ddi_soft_state_free(sd_state, instance) is the right way to
9178 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
9179 	 * ddi_get_soft_state() fails.  The implication seems to be
9180 	 * that the get_soft_state cannot fail if the zalloc succeeds.
9181 	 */
9182 	ddi_soft_state_free(sd_state, instance);
9183 
9184 probe_failed:
9185 	scsi_unprobe(devp);
9186 #ifdef SDDEBUG
9187 	if ((sd_component_mask & SD_LOG_ATTACH_DETACH) &&
9188 	    (sd_level_mask & SD_LOGMASK_TRACE)) {
9189 		cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n",
9190 		    (void *)un);
9191 	}
9192 #endif
9193 	return (DDI_FAILURE);
9194 }
9195 
9196 
9197 /*
9198  *    Function: sd_unit_detach
9199  *
9200  * Description: Performs DDI_DETACH processing for sddetach().
9201  *
9202  * Return Code: DDI_SUCCESS
9203  *		DDI_FAILURE
9204  *
9205  *     Context: Kernel thread context
9206  */
9207 
9208 static int
9209 sd_unit_detach(dev_info_t *devi)
9210 {
9211 	struct scsi_device	*devp;
9212 	struct sd_lun		*un;
9213 	int			i;
9214 	int			tgt;
9215 	dev_t			dev;
9216 	dev_info_t		*pdip = ddi_get_parent(devi);
9217 	int			instance = ddi_get_instance(devi);
9218 
9219 	mutex_enter(&sd_detach_mutex);
9220 
9221 	/*
9222 	 * Fail the detach for any of the following:
9223 	 *  - Unable to get the sd_lun struct for the instance
9224 	 *  - A layered driver has an outstanding open on the instance
9225 	 *  - Another thread is already detaching this instance
9226 	 *  - Another thread is currently performing an open
9227 	 */
9228 	devp = ddi_get_driver_private(devi);
9229 	if ((devp == NULL) ||
9230 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
9231 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
9232 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
9233 		mutex_exit(&sd_detach_mutex);
9234 		return (DDI_FAILURE);
9235 	}
9236 
9237 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
9238 
9239 	/*
9240 	 * Mark this instance as currently in a detach, to inhibit any
9241 	 * opens from a layered driver.
9242 	 */
9243 	un->un_detach_count++;
9244 	mutex_exit(&sd_detach_mutex);
9245 
9246 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
9247 	    SCSI_ADDR_PROP_TARGET, -1);
9248 
9249 	dev = sd_make_device(SD_DEVINFO(un));
9250 
9251 #ifndef lint
9252 	_NOTE(COMPETING_THREADS_NOW);
9253 #endif
9254 
9255 	mutex_enter(SD_MUTEX(un));
9256 
9257 	/*
9258 	 * Fail the detach if there are any outstanding layered
9259 	 * opens on this device.
9260 	 */
9261 	for (i = 0; i < NDKMAP; i++) {
9262 		if (un->un_ocmap.lyropen[i] != 0) {
9263 			goto err_notclosed;
9264 		}
9265 	}
9266 
9267 	/*
9268 	 * Verify there are NO outstanding commands issued to this device.
9269 	 * ie, un_ncmds_in_transport == 0.
9270 	 * It's possible to have outstanding commands through the physio
9271 	 * code path, even though everything's closed.
9272 	 */
9273 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
9274 	    (un->un_direct_priority_timeid != NULL) ||
9275 	    (un->un_state == SD_STATE_RWAIT)) {
9276 		mutex_exit(SD_MUTEX(un));
9277 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9278 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
9279 		goto err_stillbusy;
9280 	}
9281 
9282 	/*
9283 	 * If we have the device reserved, release the reservation.
9284 	 */
9285 	if ((un->un_resvd_status & SD_RESERVE) &&
9286 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
9287 		mutex_exit(SD_MUTEX(un));
9288 		/*
9289 		 * Note: sd_reserve_release sends a command to the device
9290 		 * via the sd_ioctlcmd() path, and can sleep.
9291 		 */
9292 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
9293 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9294 			    "sd_dr_detach: Cannot release reservation \n");
9295 		}
9296 	} else {
9297 		mutex_exit(SD_MUTEX(un));
9298 	}
9299 
9300 	/*
9301 	 * Untimeout any reserve recover, throttle reset, restart unit
9302 	 * and delayed broadcast timeout threads. Protect the timeout pointer
9303 	 * from getting nulled by their callback functions.
9304 	 */
9305 	mutex_enter(SD_MUTEX(un));
9306 	if (un->un_resvd_timeid != NULL) {
9307 		timeout_id_t temp_id = un->un_resvd_timeid;
9308 		un->un_resvd_timeid = NULL;
9309 		mutex_exit(SD_MUTEX(un));
9310 		(void) untimeout(temp_id);
9311 		mutex_enter(SD_MUTEX(un));
9312 	}
9313 
9314 	if (un->un_reset_throttle_timeid != NULL) {
9315 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
9316 		un->un_reset_throttle_timeid = NULL;
9317 		mutex_exit(SD_MUTEX(un));
9318 		(void) untimeout(temp_id);
9319 		mutex_enter(SD_MUTEX(un));
9320 	}
9321 
9322 	if (un->un_startstop_timeid != NULL) {
9323 		timeout_id_t temp_id = un->un_startstop_timeid;
9324 		un->un_startstop_timeid = NULL;
9325 		mutex_exit(SD_MUTEX(un));
9326 		(void) untimeout(temp_id);
9327 		mutex_enter(SD_MUTEX(un));
9328 	}
9329 
9330 	if (un->un_dcvb_timeid != NULL) {
9331 		timeout_id_t temp_id = un->un_dcvb_timeid;
9332 		un->un_dcvb_timeid = NULL;
9333 		mutex_exit(SD_MUTEX(un));
9334 		(void) untimeout(temp_id);
9335 	} else {
9336 		mutex_exit(SD_MUTEX(un));
9337 	}
9338 
9339 	/* Remove any pending reservation reclaim requests for this device */
9340 	sd_rmv_resv_reclaim_req(dev);
9341 
9342 	mutex_enter(SD_MUTEX(un));
9343 
9344 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
9345 	if (un->un_direct_priority_timeid != NULL) {
9346 		timeout_id_t temp_id = un->un_direct_priority_timeid;
9347 		un->un_direct_priority_timeid = NULL;
9348 		mutex_exit(SD_MUTEX(un));
9349 		(void) untimeout(temp_id);
9350 		mutex_enter(SD_MUTEX(un));
9351 	}
9352 
9353 	/* Cancel any active multi-host disk watch thread requests */
9354 	if (un->un_mhd_token != NULL) {
9355 		mutex_exit(SD_MUTEX(un));
9356 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
9357 		if (scsi_watch_request_terminate(un->un_mhd_token,
9358 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
9359 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9360 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
9361 			/*
9362 			 * Note: We are returning here after having removed
9363 			 * some driver timeouts above. This is consistent with
9364 			 * the legacy implementation but perhaps the watch
9365 			 * terminate call should be made with the wait flag set.
9366 			 */
9367 			goto err_stillbusy;
9368 		}
9369 		mutex_enter(SD_MUTEX(un));
9370 		un->un_mhd_token = NULL;
9371 	}
9372 
9373 	if (un->un_swr_token != NULL) {
9374 		mutex_exit(SD_MUTEX(un));
9375 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
9376 		if (scsi_watch_request_terminate(un->un_swr_token,
9377 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
9378 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9379 			    "sd_dr_detach: Cannot cancel swr watch request\n");
9380 			/*
9381 			 * Note: We are returning here after having removed
9382 			 * some driver timeouts above. This is consistent with
9383 			 * the legacy implementation but perhaps the watch
9384 			 * terminate call should be made with the wait flag set.
9385 			 */
9386 			goto err_stillbusy;
9387 		}
9388 		mutex_enter(SD_MUTEX(un));
9389 		un->un_swr_token = NULL;
9390 	}
9391 
9392 	mutex_exit(SD_MUTEX(un));
9393 
9394 	/*
9395 	 * Clear any scsi_reset_notifies. We clear the reset notifies
9396 	 * if we have not registered one.
9397 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
9398 	 */
9399 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
9400 	    sd_mhd_reset_notify_cb, (caddr_t)un);
9401 
9402 	/*
9403 	 * protect the timeout pointers from getting nulled by
9404 	 * their callback functions during the cancellation process.
9405 	 * In such a scenario untimeout can be invoked with a null value.
9406 	 */
9407 	_NOTE(NO_COMPETING_THREADS_NOW);
9408 
9409 	mutex_enter(&un->un_pm_mutex);
9410 	if (un->un_pm_idle_timeid != NULL) {
9411 		timeout_id_t temp_id = un->un_pm_idle_timeid;
9412 		un->un_pm_idle_timeid = NULL;
9413 		mutex_exit(&un->un_pm_mutex);
9414 
9415 		/*
9416 		 * Timeout is active; cancel it.
9417 		 * Note that it'll never be active on a device
9418 		 * that does not support PM therefore we don't
9419 		 * have to check before calling pm_idle_component.
9420 		 */
9421 		(void) untimeout(temp_id);
9422 		(void) pm_idle_component(SD_DEVINFO(un), 0);
9423 		mutex_enter(&un->un_pm_mutex);
9424 	}
9425 
9426 	/*
9427 	 * Check whether there is already a timeout scheduled for power
9428 	 * management. If yes then don't lower the power here, that's.
9429 	 * the timeout handler's job.
9430 	 */
9431 	if (un->un_pm_timeid != NULL) {
9432 		timeout_id_t temp_id = un->un_pm_timeid;
9433 		un->un_pm_timeid = NULL;
9434 		mutex_exit(&un->un_pm_mutex);
9435 		/*
9436 		 * Timeout is active; cancel it.
9437 		 * Note that it'll never be active on a device
9438 		 * that does not support PM therefore we don't
9439 		 * have to check before calling pm_idle_component.
9440 		 */
9441 		(void) untimeout(temp_id);
9442 		(void) pm_idle_component(SD_DEVINFO(un), 0);
9443 
9444 	} else {
9445 		mutex_exit(&un->un_pm_mutex);
9446 		if ((un->un_f_pm_is_enabled == TRUE) &&
9447 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) !=
9448 		    DDI_SUCCESS)) {
9449 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9450 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
9451 			/*
9452 			 * Fix for bug: 4297749, item # 13
9453 			 * The above test now includes a check to see if PM is
9454 			 * supported by this device before call
9455 			 * pm_lower_power().
9456 			 * Note, the following is not dead code. The call to
9457 			 * pm_lower_power above will generate a call back into
9458 			 * our sdpower routine which might result in a timeout
9459 			 * handler getting activated. Therefore the following
9460 			 * code is valid and necessary.
9461 			 */
9462 			mutex_enter(&un->un_pm_mutex);
9463 			if (un->un_pm_timeid != NULL) {
9464 				timeout_id_t temp_id = un->un_pm_timeid;
9465 				un->un_pm_timeid = NULL;
9466 				mutex_exit(&un->un_pm_mutex);
9467 				(void) untimeout(temp_id);
9468 				(void) pm_idle_component(SD_DEVINFO(un), 0);
9469 			} else {
9470 				mutex_exit(&un->un_pm_mutex);
9471 			}
9472 		}
9473 	}
9474 
9475 	/*
9476 	 * Cleanup from the scsi_ifsetcap() calls (437868)
9477 	 * Relocated here from above to be after the call to
9478 	 * pm_lower_power, which was getting errors.
9479 	 */
9480 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
9481 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
9482 
9483 	/*
9484 	 * Currently, tagged queuing is supported per target based by HBA.
9485 	 * Setting this per lun instance actually sets the capability of this
9486 	 * target in HBA, which affects those luns already attached on the
9487 	 * same target. So during detach, we can only disable this capability
9488 	 * only when this is the only lun left on this target. By doing
9489 	 * this, we assume a target has the same tagged queuing capability
9490 	 * for every lun. The condition can be removed when HBA is changed to
9491 	 * support per lun based tagged queuing capability.
9492 	 */
9493 	if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
9494 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
9495 	}
9496 
9497 	if (un->un_f_is_fibre == FALSE) {
9498 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
9499 	}
9500 
9501 	/*
9502 	 * Remove any event callbacks, fibre only
9503 	 */
9504 	if (un->un_f_is_fibre == TRUE) {
9505 		if ((un->un_insert_event != NULL) &&
9506 			(ddi_remove_event_handler(un->un_insert_cb_id) !=
9507 				DDI_SUCCESS)) {
9508 			/*
9509 			 * Note: We are returning here after having done
9510 			 * substantial cleanup above. This is consistent
9511 			 * with the legacy implementation but this may not
9512 			 * be the right thing to do.
9513 			 */
9514 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9515 				"sd_dr_detach: Cannot cancel insert event\n");
9516 			goto err_remove_event;
9517 		}
9518 		un->un_insert_event = NULL;
9519 
9520 		if ((un->un_remove_event != NULL) &&
9521 			(ddi_remove_event_handler(un->un_remove_cb_id) !=
9522 				DDI_SUCCESS)) {
9523 			/*
9524 			 * Note: We are returning here after having done
9525 			 * substantial cleanup above. This is consistent
9526 			 * with the legacy implementation but this may not
9527 			 * be the right thing to do.
9528 			 */
9529 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9530 				"sd_dr_detach: Cannot cancel remove event\n");
9531 			goto err_remove_event;
9532 		}
9533 		un->un_remove_event = NULL;
9534 	}
9535 
9536 	/* Do not free the softstate if the callback routine is active */
9537 	sd_sync_with_callback(un);
9538 
9539 	/*
9540 	 * Hold the detach mutex here, to make sure that no other threads ever
9541 	 * can access a (partially) freed soft state structure.
9542 	 */
9543 	mutex_enter(&sd_detach_mutex);
9544 
9545 	/*
9546 	 * Clean up the soft state struct.
9547 	 * Cleanup is done in reverse order of allocs/inits.
9548 	 * At this point there should be no competing threads anymore.
9549 	 */
9550 
9551 	/* Unregister and free device id. */
9552 	ddi_devid_unregister(devi);
9553 	if (un->un_devid) {
9554 		ddi_devid_free(un->un_devid);
9555 		un->un_devid = NULL;
9556 	}
9557 
9558 	/*
9559 	 * Destroy wmap cache if it exists.
9560 	 */
9561 	if (un->un_wm_cache != NULL) {
9562 		kmem_cache_destroy(un->un_wm_cache);
9563 		un->un_wm_cache = NULL;
9564 	}
9565 
9566 	/* Remove minor nodes */
9567 	ddi_remove_minor_node(devi, NULL);
9568 
9569 	/*
9570 	 * kstat cleanup is done in detach for all device types (4363169).
9571 	 * We do not want to fail detach if the device kstats are not deleted
9572 	 * since there is a confusion about the devo_refcnt for the device.
9573 	 * We just delete the kstats and let detach complete successfully.
9574 	 */
9575 	if (un->un_stats != NULL) {
9576 		kstat_delete(un->un_stats);
9577 		un->un_stats = NULL;
9578 	}
9579 	if (un->un_errstats != NULL) {
9580 		kstat_delete(un->un_errstats);
9581 		un->un_errstats = NULL;
9582 	}
9583 
9584 	/* Remove partition stats */
9585 	if (un->un_f_pkstats_enabled) {
9586 		for (i = 0; i < NSDMAP; i++) {
9587 			if (un->un_pstats[i] != NULL) {
9588 				kstat_delete(un->un_pstats[i]);
9589 				un->un_pstats[i] = NULL;
9590 			}
9591 		}
9592 	}
9593 
9594 	/* Remove xbuf registration */
9595 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
9596 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
9597 
9598 	/* Remove driver properties */
9599 	ddi_prop_remove_all(devi);
9600 
9601 	mutex_destroy(&un->un_pm_mutex);
9602 	cv_destroy(&un->un_pm_busy_cv);
9603 
9604 	cv_destroy(&un->un_wcc_cv);
9605 
9606 	/* Open/close semaphore */
9607 	sema_destroy(&un->un_semoclose);
9608 
9609 	/* Removable media condvar. */
9610 	cv_destroy(&un->un_state_cv);
9611 
9612 	/* Suspend/resume condvar. */
9613 	cv_destroy(&un->un_suspend_cv);
9614 	cv_destroy(&un->un_disk_busy_cv);
9615 
9616 	sd_free_rqs(un);
9617 
9618 	/* Free up soft state */
9619 	devp->sd_private = NULL;
9620 	bzero(un, sizeof (struct sd_lun));
9621 	ddi_soft_state_free(sd_state, instance);
9622 
9623 	mutex_exit(&sd_detach_mutex);
9624 
9625 	/* This frees up the INQUIRY data associated with the device. */
9626 	scsi_unprobe(devp);
9627 
9628 	/*
9629 	 * After successfully detaching an instance, we update the information
9630 	 * of how many luns have been attached in the relative target and
9631 	 * controller for parallel SCSI. This information is used when sd tries
9632 	 * to set the tagged queuing capability in HBA.
9633 	 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
9634 	 * check if the device is parallel SCSI. However, we don't need to
9635 	 * check here because we've already checked during attach. No device
9636 	 * that is not parallel SCSI is in the chain.
9637 	 */
9638 	if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9639 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
9640 	}
9641 
9642 	return (DDI_SUCCESS);
9643 
9644 err_notclosed:
9645 	mutex_exit(SD_MUTEX(un));
9646 
9647 err_stillbusy:
9648 	_NOTE(NO_COMPETING_THREADS_NOW);
9649 
9650 err_remove_event:
9651 	mutex_enter(&sd_detach_mutex);
9652 	un->un_detach_count--;
9653 	mutex_exit(&sd_detach_mutex);
9654 
9655 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9656 	return (DDI_FAILURE);
9657 }
9658 
9659 
9660 /*
9661  * Driver minor node structure and data table
9662  */
9663 struct driver_minor_data {
9664 	char	*name;
9665 	minor_t	minor;
9666 	int	type;
9667 };
9668 
9669 static struct driver_minor_data sd_minor_data[] = {
9670 	{"a", 0, S_IFBLK},
9671 	{"b", 1, S_IFBLK},
9672 	{"c", 2, S_IFBLK},
9673 	{"d", 3, S_IFBLK},
9674 	{"e", 4, S_IFBLK},
9675 	{"f", 5, S_IFBLK},
9676 	{"g", 6, S_IFBLK},
9677 	{"h", 7, S_IFBLK},
9678 #if defined(_SUNOS_VTOC_16)
9679 	{"i", 8, S_IFBLK},
9680 	{"j", 9, S_IFBLK},
9681 	{"k", 10, S_IFBLK},
9682 	{"l", 11, S_IFBLK},
9683 	{"m", 12, S_IFBLK},
9684 	{"n", 13, S_IFBLK},
9685 	{"o", 14, S_IFBLK},
9686 	{"p", 15, S_IFBLK},
9687 #endif			/* defined(_SUNOS_VTOC_16) */
9688 #if defined(_FIRMWARE_NEEDS_FDISK)
9689 	{"q", 16, S_IFBLK},
9690 	{"r", 17, S_IFBLK},
9691 	{"s", 18, S_IFBLK},
9692 	{"t", 19, S_IFBLK},
9693 	{"u", 20, S_IFBLK},
9694 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9695 	{"a,raw", 0, S_IFCHR},
9696 	{"b,raw", 1, S_IFCHR},
9697 	{"c,raw", 2, S_IFCHR},
9698 	{"d,raw", 3, S_IFCHR},
9699 	{"e,raw", 4, S_IFCHR},
9700 	{"f,raw", 5, S_IFCHR},
9701 	{"g,raw", 6, S_IFCHR},
9702 	{"h,raw", 7, S_IFCHR},
9703 #if defined(_SUNOS_VTOC_16)
9704 	{"i,raw", 8, S_IFCHR},
9705 	{"j,raw", 9, S_IFCHR},
9706 	{"k,raw", 10, S_IFCHR},
9707 	{"l,raw", 11, S_IFCHR},
9708 	{"m,raw", 12, S_IFCHR},
9709 	{"n,raw", 13, S_IFCHR},
9710 	{"o,raw", 14, S_IFCHR},
9711 	{"p,raw", 15, S_IFCHR},
9712 #endif			/* defined(_SUNOS_VTOC_16) */
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 static struct driver_minor_data sd_minor_data_efi[] = {
9724 	{"a", 0, S_IFBLK},
9725 	{"b", 1, S_IFBLK},
9726 	{"c", 2, S_IFBLK},
9727 	{"d", 3, S_IFBLK},
9728 	{"e", 4, S_IFBLK},
9729 	{"f", 5, S_IFBLK},
9730 	{"g", 6, S_IFBLK},
9731 	{"wd", 7, S_IFBLK},
9732 #if defined(_FIRMWARE_NEEDS_FDISK)
9733 	{"q", 16, S_IFBLK},
9734 	{"r", 17, S_IFBLK},
9735 	{"s", 18, S_IFBLK},
9736 	{"t", 19, S_IFBLK},
9737 	{"u", 20, S_IFBLK},
9738 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9739 	{"a,raw", 0, S_IFCHR},
9740 	{"b,raw", 1, S_IFCHR},
9741 	{"c,raw", 2, S_IFCHR},
9742 	{"d,raw", 3, S_IFCHR},
9743 	{"e,raw", 4, S_IFCHR},
9744 	{"f,raw", 5, S_IFCHR},
9745 	{"g,raw", 6, S_IFCHR},
9746 	{"wd,raw", 7, S_IFCHR},
9747 #if defined(_FIRMWARE_NEEDS_FDISK)
9748 	{"q,raw", 16, S_IFCHR},
9749 	{"r,raw", 17, S_IFCHR},
9750 	{"s,raw", 18, S_IFCHR},
9751 	{"t,raw", 19, S_IFCHR},
9752 	{"u,raw", 20, S_IFCHR},
9753 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9754 	{0}
9755 };
9756 
9757 
9758 /*
9759  *    Function: sd_create_minor_nodes
9760  *
9761  * Description: Create the minor device nodes for the instance.
9762  *
9763  *   Arguments: un - driver soft state (unit) structure
9764  *		devi - pointer to device info structure
9765  *
9766  * Return Code: DDI_SUCCESS
9767  *		DDI_FAILURE
9768  *
9769  *     Context: Kernel thread context
9770  */
9771 
9772 static int
9773 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi)
9774 {
9775 	struct driver_minor_data	*dmdp;
9776 	struct scsi_device		*devp;
9777 	int				instance;
9778 	char				name[48];
9779 
9780 	ASSERT(un != NULL);
9781 	devp = ddi_get_driver_private(devi);
9782 	instance = ddi_get_instance(devp->sd_dev);
9783 
9784 	/*
9785 	 * Create all the minor nodes for this target.
9786 	 */
9787 	if (un->un_blockcount > DK_MAX_BLOCKS)
9788 		dmdp = sd_minor_data_efi;
9789 	else
9790 		dmdp = sd_minor_data;
9791 	while (dmdp->name != NULL) {
9792 
9793 		(void) sprintf(name, "%s", dmdp->name);
9794 
9795 		if (ddi_create_minor_node(devi, name, dmdp->type,
9796 		    (instance << SDUNIT_SHIFT) | dmdp->minor,
9797 		    un->un_node_type, NULL) == DDI_FAILURE) {
9798 			/*
9799 			 * Clean up any nodes that may have been created, in
9800 			 * case this fails in the middle of the loop.
9801 			 */
9802 			ddi_remove_minor_node(devi, NULL);
9803 			return (DDI_FAILURE);
9804 		}
9805 		dmdp++;
9806 	}
9807 
9808 	return (DDI_SUCCESS);
9809 }
9810 
9811 
9812 /*
9813  *    Function: sd_create_errstats
9814  *
9815  * Description: This routine instantiates the device error stats.
9816  *
9817  *		Note: During attach the stats are instantiated first so they are
9818  *		available for attach-time routines that utilize the driver
9819  *		iopath to send commands to the device. The stats are initialized
9820  *		separately so data obtained during some attach-time routines is
9821  *		available. (4362483)
9822  *
9823  *   Arguments: un - driver soft state (unit) structure
9824  *		instance - driver instance
9825  *
9826  *     Context: Kernel thread context
9827  */
9828 
9829 static void
9830 sd_create_errstats(struct sd_lun *un, int instance)
9831 {
9832 	struct	sd_errstats	*stp;
9833 	char	kstatmodule_err[KSTAT_STRLEN];
9834 	char	kstatname[KSTAT_STRLEN];
9835 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9836 
9837 	ASSERT(un != NULL);
9838 
9839 	if (un->un_errstats != NULL) {
9840 		return;
9841 	}
9842 
9843 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9844 	    "%serr", sd_label);
9845 	(void) snprintf(kstatname, sizeof (kstatname),
9846 	    "%s%d,err", sd_label, instance);
9847 
9848 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9849 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9850 
9851 	if (un->un_errstats == NULL) {
9852 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9853 		    "sd_create_errstats: Failed kstat_create\n");
9854 		return;
9855 	}
9856 
9857 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9858 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9859 	    KSTAT_DATA_UINT32);
9860 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9861 	    KSTAT_DATA_UINT32);
9862 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9863 	    KSTAT_DATA_UINT32);
9864 	kstat_named_init(&stp->sd_vid,		"Vendor",
9865 	    KSTAT_DATA_CHAR);
9866 	kstat_named_init(&stp->sd_pid,		"Product",
9867 	    KSTAT_DATA_CHAR);
9868 	kstat_named_init(&stp->sd_revision,	"Revision",
9869 	    KSTAT_DATA_CHAR);
9870 	kstat_named_init(&stp->sd_serial,	"Serial No",
9871 	    KSTAT_DATA_CHAR);
9872 	kstat_named_init(&stp->sd_capacity,	"Size",
9873 	    KSTAT_DATA_ULONGLONG);
9874 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9875 	    KSTAT_DATA_UINT32);
9876 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9877 	    KSTAT_DATA_UINT32);
9878 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9879 	    KSTAT_DATA_UINT32);
9880 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9881 	    KSTAT_DATA_UINT32);
9882 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9883 	    KSTAT_DATA_UINT32);
9884 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9885 	    KSTAT_DATA_UINT32);
9886 
9887 	un->un_errstats->ks_private = un;
9888 	un->un_errstats->ks_update  = nulldev;
9889 
9890 	kstat_install(un->un_errstats);
9891 }
9892 
9893 
9894 /*
9895  *    Function: sd_set_errstats
9896  *
9897  * Description: This routine sets the value of the vendor id, product id,
9898  *		revision, serial number, and capacity device error stats.
9899  *
9900  *		Note: During attach the stats are instantiated first so they are
9901  *		available for attach-time routines that utilize the driver
9902  *		iopath to send commands to the device. The stats are initialized
9903  *		separately so data obtained during some attach-time routines is
9904  *		available. (4362483)
9905  *
9906  *   Arguments: un - driver soft state (unit) structure
9907  *
9908  *     Context: Kernel thread context
9909  */
9910 
9911 static void
9912 sd_set_errstats(struct sd_lun *un)
9913 {
9914 	struct	sd_errstats	*stp;
9915 
9916 	ASSERT(un != NULL);
9917 	ASSERT(un->un_errstats != NULL);
9918 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9919 	ASSERT(stp != NULL);
9920 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9921 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9922 	(void) strncpy(stp->sd_revision.value.c,
9923 	    un->un_sd->sd_inq->inq_revision, 4);
9924 
9925 	/*
9926 	 * All the errstats are persistent across detach/attach,
9927 	 * so reset all the errstats here in case of the hot
9928 	 * replacement of disk drives, except for not changed
9929 	 * Sun qualified drives.
9930 	 */
9931 	if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9932 	    (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9933 	    sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9934 		stp->sd_softerrs.value.ui32 = 0;
9935 		stp->sd_harderrs.value.ui32 = 0;
9936 		stp->sd_transerrs.value.ui32 = 0;
9937 		stp->sd_rq_media_err.value.ui32 = 0;
9938 		stp->sd_rq_ntrdy_err.value.ui32 = 0;
9939 		stp->sd_rq_nodev_err.value.ui32 = 0;
9940 		stp->sd_rq_recov_err.value.ui32 = 0;
9941 		stp->sd_rq_illrq_err.value.ui32 = 0;
9942 		stp->sd_rq_pfa_err.value.ui32 = 0;
9943 	}
9944 
9945 	/*
9946 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9947 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9948 	 * (4376302))
9949 	 */
9950 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9951 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9952 		    sizeof (SD_INQUIRY(un)->inq_serial));
9953 	}
9954 
9955 	if (un->un_f_blockcount_is_valid != TRUE) {
9956 		/*
9957 		 * Set capacity error stat to 0 for no media. This ensures
9958 		 * a valid capacity is displayed in response to 'iostat -E'
9959 		 * when no media is present in the device.
9960 		 */
9961 		stp->sd_capacity.value.ui64 = 0;
9962 	} else {
9963 		/*
9964 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9965 		 * capacity.
9966 		 *
9967 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9968 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9969 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9970 		 */
9971 		stp->sd_capacity.value.ui64 = (uint64_t)
9972 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9973 	}
9974 }
9975 
9976 
9977 /*
9978  *    Function: sd_set_pstats
9979  *
9980  * Description: This routine instantiates and initializes the partition
9981  *              stats for each partition with more than zero blocks.
9982  *		(4363169)
9983  *
9984  *   Arguments: un - driver soft state (unit) structure
9985  *
9986  *     Context: Kernel thread context
9987  */
9988 
9989 static void
9990 sd_set_pstats(struct sd_lun *un)
9991 {
9992 	char	kstatname[KSTAT_STRLEN];
9993 	int	instance;
9994 	int	i;
9995 
9996 	ASSERT(un != NULL);
9997 
9998 	instance = ddi_get_instance(SD_DEVINFO(un));
9999 
10000 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
10001 	for (i = 0; i < NSDMAP; i++) {
10002 		if ((un->un_pstats[i] == NULL) &&
10003 		    (un->un_map[i].dkl_nblk != 0)) {
10004 			(void) snprintf(kstatname, sizeof (kstatname),
10005 			    "%s%d,%s", sd_label, instance,
10006 			    sd_minor_data[i].name);
10007 			un->un_pstats[i] = kstat_create(sd_label,
10008 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
10009 			    1, KSTAT_FLAG_PERSISTENT);
10010 			if (un->un_pstats[i] != NULL) {
10011 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
10012 				kstat_install(un->un_pstats[i]);
10013 			}
10014 		}
10015 	}
10016 }
10017 
10018 
10019 #if (defined(__fibre))
10020 /*
10021  *    Function: sd_init_event_callbacks
10022  *
10023  * Description: This routine initializes the insertion and removal event
10024  *		callbacks. (fibre only)
10025  *
10026  *   Arguments: un - driver soft state (unit) structure
10027  *
10028  *     Context: Kernel thread context
10029  */
10030 
10031 static void
10032 sd_init_event_callbacks(struct sd_lun *un)
10033 {
10034 	ASSERT(un != NULL);
10035 
10036 	if ((un->un_insert_event == NULL) &&
10037 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
10038 	    &un->un_insert_event) == DDI_SUCCESS)) {
10039 		/*
10040 		 * Add the callback for an insertion event
10041 		 */
10042 		(void) ddi_add_event_handler(SD_DEVINFO(un),
10043 		    un->un_insert_event, sd_event_callback, (void *)un,
10044 		    &(un->un_insert_cb_id));
10045 	}
10046 
10047 	if ((un->un_remove_event == NULL) &&
10048 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
10049 	    &un->un_remove_event) == DDI_SUCCESS)) {
10050 		/*
10051 		 * Add the callback for a removal event
10052 		 */
10053 		(void) ddi_add_event_handler(SD_DEVINFO(un),
10054 		    un->un_remove_event, sd_event_callback, (void *)un,
10055 		    &(un->un_remove_cb_id));
10056 	}
10057 }
10058 
10059 
10060 /*
10061  *    Function: sd_event_callback
10062  *
10063  * Description: This routine handles insert/remove events (photon). The
10064  *		state is changed to OFFLINE which can be used to supress
10065  *		error msgs. (fibre only)
10066  *
10067  *   Arguments: un - driver soft state (unit) structure
10068  *
10069  *     Context: Callout thread context
10070  */
10071 /* ARGSUSED */
10072 static void
10073 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
10074     void *bus_impldata)
10075 {
10076 	struct sd_lun *un = (struct sd_lun *)arg;
10077 
10078 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
10079 	if (event == un->un_insert_event) {
10080 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
10081 		mutex_enter(SD_MUTEX(un));
10082 		if (un->un_state == SD_STATE_OFFLINE) {
10083 			if (un->un_last_state != SD_STATE_SUSPENDED) {
10084 				un->un_state = un->un_last_state;
10085 			} else {
10086 				/*
10087 				 * We have gone through SUSPEND/RESUME while
10088 				 * we were offline. Restore the last state
10089 				 */
10090 				un->un_state = un->un_save_state;
10091 			}
10092 		}
10093 		mutex_exit(SD_MUTEX(un));
10094 
10095 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
10096 	} else if (event == un->un_remove_event) {
10097 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
10098 		mutex_enter(SD_MUTEX(un));
10099 		/*
10100 		 * We need to handle an event callback that occurs during
10101 		 * the suspend operation, since we don't prevent it.
10102 		 */
10103 		if (un->un_state != SD_STATE_OFFLINE) {
10104 			if (un->un_state != SD_STATE_SUSPENDED) {
10105 				New_state(un, SD_STATE_OFFLINE);
10106 			} else {
10107 				un->un_last_state = SD_STATE_OFFLINE;
10108 			}
10109 		}
10110 		mutex_exit(SD_MUTEX(un));
10111 	} else {
10112 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
10113 		    "!Unknown event\n");
10114 	}
10115 
10116 }
10117 #endif
10118 
10119 /*
10120  *    Function: sd_cache_control()
10121  *
10122  * Description: This routine is the driver entry point for setting
10123  *		read and write caching by modifying the WCE (write cache
10124  *		enable) and RCD (read cache disable) bits of mode
10125  *		page 8 (MODEPAGE_CACHING).
10126  *
10127  *   Arguments: un - driver soft state (unit) structure
10128  *		rcd_flag - flag for controlling the read cache
10129  *		wce_flag - flag for controlling the write cache
10130  *
10131  * Return Code: EIO
10132  *		code returned by sd_send_scsi_MODE_SENSE and
10133  *		sd_send_scsi_MODE_SELECT
10134  *
10135  *     Context: Kernel Thread
10136  */
10137 
10138 static int
10139 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag)
10140 {
10141 	struct mode_caching	*mode_caching_page;
10142 	uchar_t			*header;
10143 	size_t			buflen;
10144 	int			hdrlen;
10145 	int			bd_len;
10146 	int			rval = 0;
10147 	struct mode_header_grp2	*mhp;
10148 
10149 	ASSERT(un != NULL);
10150 
10151 	/*
10152 	 * Do a test unit ready, otherwise a mode sense may not work if this
10153 	 * is the first command sent to the device after boot.
10154 	 */
10155 	(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
10156 
10157 	if (un->un_f_cfg_is_atapi == TRUE) {
10158 		hdrlen = MODE_HEADER_LENGTH_GRP2;
10159 	} else {
10160 		hdrlen = MODE_HEADER_LENGTH;
10161 	}
10162 
10163 	/*
10164 	 * Allocate memory for the retrieved mode page and its headers.  Set
10165 	 * a pointer to the page itself.  Use mode_cache_scsi3 to insure
10166 	 * we get all of the mode sense data otherwise, the mode select
10167 	 * will fail.  mode_cache_scsi3 is a superset of mode_caching.
10168 	 */
10169 	buflen = hdrlen + MODE_BLK_DESC_LENGTH +
10170 		sizeof (struct mode_cache_scsi3);
10171 
10172 	header = kmem_zalloc(buflen, KM_SLEEP);
10173 
10174 	/* Get the information from the device. */
10175 	if (un->un_f_cfg_is_atapi == TRUE) {
10176 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen,
10177 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
10178 	} else {
10179 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
10180 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
10181 	}
10182 	if (rval != 0) {
10183 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
10184 		    "sd_cache_control: Mode Sense Failed\n");
10185 		kmem_free(header, buflen);
10186 		return (rval);
10187 	}
10188 
10189 	/*
10190 	 * Determine size of Block Descriptors in order to locate
10191 	 * the mode page data. ATAPI devices return 0, SCSI devices
10192 	 * should return MODE_BLK_DESC_LENGTH.
10193 	 */
10194 	if (un->un_f_cfg_is_atapi == TRUE) {
10195 		mhp	= (struct mode_header_grp2 *)header;
10196 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
10197 	} else {
10198 		bd_len  = ((struct mode_header *)header)->bdesc_length;
10199 	}
10200 
10201 	if (bd_len > MODE_BLK_DESC_LENGTH) {
10202 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10203 		    "sd_cache_control: Mode Sense returned invalid "
10204 		    "block descriptor length\n");
10205 		kmem_free(header, buflen);
10206 		return (EIO);
10207 	}
10208 
10209 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
10210 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
10211 		SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense"
10212 		    " caching page code mismatch %d\n",
10213 		    mode_caching_page->mode_page.code);
10214 		kmem_free(header, buflen);
10215 		return (EIO);
10216 	}
10217 
10218 	/* Check the relevant bits on successful mode sense. */
10219 	if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
10220 	    (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
10221 	    (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
10222 	    (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
10223 
10224 		size_t sbuflen;
10225 		uchar_t save_pg;
10226 
10227 		/*
10228 		 * Construct select buffer length based on the
10229 		 * length of the sense data returned.
10230 		 */
10231 		sbuflen =  hdrlen + MODE_BLK_DESC_LENGTH +
10232 				sizeof (struct mode_page) +
10233 				(int)mode_caching_page->mode_page.length;
10234 
10235 		/*
10236 		 * Set the caching bits as requested.
10237 		 */
10238 		if (rcd_flag == SD_CACHE_ENABLE)
10239 			mode_caching_page->rcd = 0;
10240 		else if (rcd_flag == SD_CACHE_DISABLE)
10241 			mode_caching_page->rcd = 1;
10242 
10243 		if (wce_flag == SD_CACHE_ENABLE)
10244 			mode_caching_page->wce = 1;
10245 		else if (wce_flag == SD_CACHE_DISABLE)
10246 			mode_caching_page->wce = 0;
10247 
10248 		/*
10249 		 * Save the page if the mode sense says the
10250 		 * drive supports it.
10251 		 */
10252 		save_pg = mode_caching_page->mode_page.ps ?
10253 				SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
10254 
10255 		/* Clear reserved bits before mode select. */
10256 		mode_caching_page->mode_page.ps = 0;
10257 
10258 		/*
10259 		 * Clear out mode header for mode select.
10260 		 * The rest of the retrieved page will be reused.
10261 		 */
10262 		bzero(header, hdrlen);
10263 
10264 		if (un->un_f_cfg_is_atapi == TRUE) {
10265 			mhp = (struct mode_header_grp2 *)header;
10266 			mhp->bdesc_length_hi = bd_len >> 8;
10267 			mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff;
10268 		} else {
10269 			((struct mode_header *)header)->bdesc_length = bd_len;
10270 		}
10271 
10272 		/* Issue mode select to change the cache settings */
10273 		if (un->un_f_cfg_is_atapi == TRUE) {
10274 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header,
10275 			    sbuflen, save_pg, SD_PATH_DIRECT);
10276 		} else {
10277 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
10278 			    sbuflen, save_pg, SD_PATH_DIRECT);
10279 		}
10280 	}
10281 
10282 	kmem_free(header, buflen);
10283 	return (rval);
10284 }
10285 
10286 
10287 /*
10288  *    Function: sd_get_write_cache_enabled()
10289  *
10290  * Description: This routine is the driver entry point for determining if
10291  *		write caching is enabled.  It examines the WCE (write cache
10292  *		enable) bits of mode page 8 (MODEPAGE_CACHING).
10293  *
10294  *   Arguments: un - driver soft state (unit) structure
10295  *   		is_enabled - pointer to int where write cache enabled state
10296  *   			is returned (non-zero -> write cache enabled)
10297  *
10298  *
10299  * Return Code: EIO
10300  *		code returned by sd_send_scsi_MODE_SENSE
10301  *
10302  *     Context: Kernel Thread
10303  *
10304  * NOTE: If ioctl is added to disable write cache, this sequence should
10305  * be followed so that no locking is required for accesses to
10306  * un->un_f_write_cache_enabled:
10307  * 	do mode select to clear wce
10308  * 	do synchronize cache to flush cache
10309  * 	set un->un_f_write_cache_enabled = FALSE
10310  *
10311  * Conversely, an ioctl to enable the write cache should be done
10312  * in this order:
10313  * 	set un->un_f_write_cache_enabled = TRUE
10314  * 	do mode select to set wce
10315  */
10316 
10317 static int
10318 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled)
10319 {
10320 	struct mode_caching	*mode_caching_page;
10321 	uchar_t			*header;
10322 	size_t			buflen;
10323 	int			hdrlen;
10324 	int			bd_len;
10325 	int			rval = 0;
10326 
10327 	ASSERT(un != NULL);
10328 	ASSERT(is_enabled != NULL);
10329 
10330 	/* in case of error, flag as enabled */
10331 	*is_enabled = TRUE;
10332 
10333 	/*
10334 	 * Do a test unit ready, otherwise a mode sense may not work if this
10335 	 * is the first command sent to the device after boot.
10336 	 */
10337 	(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
10338 
10339 	if (un->un_f_cfg_is_atapi == TRUE) {
10340 		hdrlen = MODE_HEADER_LENGTH_GRP2;
10341 	} else {
10342 		hdrlen = MODE_HEADER_LENGTH;
10343 	}
10344 
10345 	/*
10346 	 * Allocate memory for the retrieved mode page and its headers.  Set
10347 	 * a pointer to the page itself.
10348 	 */
10349 	buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
10350 	header = kmem_zalloc(buflen, KM_SLEEP);
10351 
10352 	/* Get the information from the device. */
10353 	if (un->un_f_cfg_is_atapi == TRUE) {
10354 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen,
10355 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
10356 	} else {
10357 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
10358 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
10359 	}
10360 	if (rval != 0) {
10361 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
10362 		    "sd_get_write_cache_enabled: Mode Sense Failed\n");
10363 		kmem_free(header, buflen);
10364 		return (rval);
10365 	}
10366 
10367 	/*
10368 	 * Determine size of Block Descriptors in order to locate
10369 	 * the mode page data. ATAPI devices return 0, SCSI devices
10370 	 * should return MODE_BLK_DESC_LENGTH.
10371 	 */
10372 	if (un->un_f_cfg_is_atapi == TRUE) {
10373 		struct mode_header_grp2	*mhp;
10374 		mhp	= (struct mode_header_grp2 *)header;
10375 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
10376 	} else {
10377 		bd_len  = ((struct mode_header *)header)->bdesc_length;
10378 	}
10379 
10380 	if (bd_len > MODE_BLK_DESC_LENGTH) {
10381 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10382 		    "sd_get_write_cache_enabled: Mode Sense returned invalid "
10383 		    "block descriptor length\n");
10384 		kmem_free(header, buflen);
10385 		return (EIO);
10386 	}
10387 
10388 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
10389 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
10390 		SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense"
10391 		    " caching page code mismatch %d\n",
10392 		    mode_caching_page->mode_page.code);
10393 		kmem_free(header, buflen);
10394 		return (EIO);
10395 	}
10396 	*is_enabled = mode_caching_page->wce;
10397 
10398 	kmem_free(header, buflen);
10399 	return (0);
10400 }
10401 
10402 
10403 /*
10404  *    Function: sd_make_device
10405  *
10406  * Description: Utility routine to return the Solaris device number from
10407  *		the data in the device's dev_info structure.
10408  *
10409  * Return Code: The Solaris device number
10410  *
10411  *     Context: Any
10412  */
10413 
10414 static dev_t
10415 sd_make_device(dev_info_t *devi)
10416 {
10417 	return (makedevice(ddi_name_to_major(ddi_get_name(devi)),
10418 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
10419 }
10420 
10421 
10422 /*
10423  *    Function: sd_pm_entry
10424  *
10425  * Description: Called at the start of a new command to manage power
10426  *		and busy status of a device. This includes determining whether
10427  *		the current power state of the device is sufficient for
10428  *		performing the command or whether it must be changed.
10429  *		The PM framework is notified appropriately.
10430  *		Only with a return status of DDI_SUCCESS will the
10431  *		component be busy to the framework.
10432  *
10433  *		All callers of sd_pm_entry must check the return status
10434  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
10435  *		of DDI_FAILURE indicates the device failed to power up.
10436  *		In this case un_pm_count has been adjusted so the result
10437  *		on exit is still powered down, ie. count is less than 0.
10438  *		Calling sd_pm_exit with this count value hits an ASSERT.
10439  *
10440  * Return Code: DDI_SUCCESS or DDI_FAILURE
10441  *
10442  *     Context: Kernel thread context.
10443  */
10444 
10445 static int
10446 sd_pm_entry(struct sd_lun *un)
10447 {
10448 	int return_status = DDI_SUCCESS;
10449 
10450 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10451 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10452 
10453 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
10454 
10455 	if (un->un_f_pm_is_enabled == FALSE) {
10456 		SD_TRACE(SD_LOG_IO_PM, un,
10457 		    "sd_pm_entry: exiting, PM not enabled\n");
10458 		return (return_status);
10459 	}
10460 
10461 	/*
10462 	 * Just increment a counter if PM is enabled. On the transition from
10463 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
10464 	 * the count with each IO and mark the device as idle when the count
10465 	 * hits 0.
10466 	 *
10467 	 * If the count is less than 0 the device is powered down. If a powered
10468 	 * down device is successfully powered up then the count must be
10469 	 * incremented to reflect the power up. Note that it'll get incremented
10470 	 * a second time to become busy.
10471 	 *
10472 	 * Because the following has the potential to change the device state
10473 	 * and must release the un_pm_mutex to do so, only one thread can be
10474 	 * allowed through at a time.
10475 	 */
10476 
10477 	mutex_enter(&un->un_pm_mutex);
10478 	while (un->un_pm_busy == TRUE) {
10479 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
10480 	}
10481 	un->un_pm_busy = TRUE;
10482 
10483 	if (un->un_pm_count < 1) {
10484 
10485 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
10486 
10487 		/*
10488 		 * Indicate we are now busy so the framework won't attempt to
10489 		 * power down the device. This call will only fail if either
10490 		 * we passed a bad component number or the device has no
10491 		 * components. Neither of these should ever happen.
10492 		 */
10493 		mutex_exit(&un->un_pm_mutex);
10494 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
10495 		ASSERT(return_status == DDI_SUCCESS);
10496 
10497 		mutex_enter(&un->un_pm_mutex);
10498 
10499 		if (un->un_pm_count < 0) {
10500 			mutex_exit(&un->un_pm_mutex);
10501 
10502 			SD_TRACE(SD_LOG_IO_PM, un,
10503 			    "sd_pm_entry: power up component\n");
10504 
10505 			/*
10506 			 * pm_raise_power will cause sdpower to be called
10507 			 * which brings the device power level to the
10508 			 * desired state, ON in this case. If successful,
10509 			 * un_pm_count and un_power_level will be updated
10510 			 * appropriately.
10511 			 */
10512 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
10513 			    SD_SPINDLE_ON);
10514 
10515 			mutex_enter(&un->un_pm_mutex);
10516 
10517 			if (return_status != DDI_SUCCESS) {
10518 				/*
10519 				 * Power up failed.
10520 				 * Idle the device and adjust the count
10521 				 * so the result on exit is that we're
10522 				 * still powered down, ie. count is less than 0.
10523 				 */
10524 				SD_TRACE(SD_LOG_IO_PM, un,
10525 				    "sd_pm_entry: power up failed,"
10526 				    " idle the component\n");
10527 
10528 				(void) pm_idle_component(SD_DEVINFO(un), 0);
10529 				un->un_pm_count--;
10530 			} else {
10531 				/*
10532 				 * Device is powered up, verify the
10533 				 * count is non-negative.
10534 				 * This is debug only.
10535 				 */
10536 				ASSERT(un->un_pm_count == 0);
10537 			}
10538 		}
10539 
10540 		if (return_status == DDI_SUCCESS) {
10541 			/*
10542 			 * For performance, now that the device has been tagged
10543 			 * as busy, and it's known to be powered up, update the
10544 			 * chain types to use jump tables that do not include
10545 			 * pm. This significantly lowers the overhead and
10546 			 * therefore improves performance.
10547 			 */
10548 
10549 			mutex_exit(&un->un_pm_mutex);
10550 			mutex_enter(SD_MUTEX(un));
10551 			SD_TRACE(SD_LOG_IO_PM, un,
10552 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
10553 			    un->un_uscsi_chain_type);
10554 
10555 			if (un->un_f_non_devbsize_supported) {
10556 				un->un_buf_chain_type =
10557 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
10558 			} else {
10559 				un->un_buf_chain_type =
10560 				    SD_CHAIN_INFO_DISK_NO_PM;
10561 			}
10562 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
10563 
10564 			SD_TRACE(SD_LOG_IO_PM, un,
10565 			    "             changed  uscsi_chain_type to   %d\n",
10566 			    un->un_uscsi_chain_type);
10567 			mutex_exit(SD_MUTEX(un));
10568 			mutex_enter(&un->un_pm_mutex);
10569 
10570 			if (un->un_pm_idle_timeid == NULL) {
10571 				/* 300 ms. */
10572 				un->un_pm_idle_timeid =
10573 				    timeout(sd_pm_idletimeout_handler, un,
10574 				    (drv_usectohz((clock_t)300000)));
10575 				/*
10576 				 * Include an extra call to busy which keeps the
10577 				 * device busy with-respect-to the PM layer
10578 				 * until the timer fires, at which time it'll
10579 				 * get the extra idle call.
10580 				 */
10581 				(void) pm_busy_component(SD_DEVINFO(un), 0);
10582 			}
10583 		}
10584 	}
10585 	un->un_pm_busy = FALSE;
10586 	/* Next... */
10587 	cv_signal(&un->un_pm_busy_cv);
10588 
10589 	un->un_pm_count++;
10590 
10591 	SD_TRACE(SD_LOG_IO_PM, un,
10592 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
10593 
10594 	mutex_exit(&un->un_pm_mutex);
10595 
10596 	return (return_status);
10597 }
10598 
10599 
10600 /*
10601  *    Function: sd_pm_exit
10602  *
10603  * Description: Called at the completion of a command to manage busy
10604  *		status for the device. If the device becomes idle the
10605  *		PM framework is notified.
10606  *
10607  *     Context: Kernel thread context
10608  */
10609 
10610 static void
10611 sd_pm_exit(struct sd_lun *un)
10612 {
10613 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10614 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10615 
10616 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10617 
10618 	/*
10619 	 * After attach the following flag is only read, so don't
10620 	 * take the penalty of acquiring a mutex for it.
10621 	 */
10622 	if (un->un_f_pm_is_enabled == TRUE) {
10623 
10624 		mutex_enter(&un->un_pm_mutex);
10625 		un->un_pm_count--;
10626 
10627 		SD_TRACE(SD_LOG_IO_PM, un,
10628 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10629 
10630 		ASSERT(un->un_pm_count >= 0);
10631 		if (un->un_pm_count == 0) {
10632 			mutex_exit(&un->un_pm_mutex);
10633 
10634 			SD_TRACE(SD_LOG_IO_PM, un,
10635 			    "sd_pm_exit: idle component\n");
10636 
10637 			(void) pm_idle_component(SD_DEVINFO(un), 0);
10638 
10639 		} else {
10640 			mutex_exit(&un->un_pm_mutex);
10641 		}
10642 	}
10643 
10644 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10645 }
10646 
10647 
10648 /*
10649  *    Function: sdopen
10650  *
10651  * Description: Driver's open(9e) entry point function.
10652  *
10653  *   Arguments: dev_i   - pointer to device number
10654  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10655  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10656  *		cred_p  - user credential pointer
10657  *
10658  * Return Code: EINVAL
10659  *		ENXIO
10660  *		EIO
10661  *		EROFS
10662  *		EBUSY
10663  *
10664  *     Context: Kernel thread context
10665  */
10666 /* ARGSUSED */
10667 static int
10668 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10669 {
10670 	struct sd_lun	*un;
10671 	int		nodelay;
10672 	int		part;
10673 	uint64_t	partmask;
10674 	int		instance;
10675 	dev_t		dev;
10676 	int		rval = EIO;
10677 
10678 	/* Validate the open type */
10679 	if (otyp >= OTYPCNT) {
10680 		return (EINVAL);
10681 	}
10682 
10683 	dev = *dev_p;
10684 	instance = SDUNIT(dev);
10685 	mutex_enter(&sd_detach_mutex);
10686 
10687 	/*
10688 	 * Fail the open if there is no softstate for the instance, or
10689 	 * if another thread somewhere is trying to detach the instance.
10690 	 */
10691 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10692 	    (un->un_detach_count != 0)) {
10693 		mutex_exit(&sd_detach_mutex);
10694 		/*
10695 		 * The probe cache only needs to be cleared when open (9e) fails
10696 		 * with ENXIO (4238046).
10697 		 */
10698 		/*
10699 		 * un-conditionally clearing probe cache is ok with
10700 		 * separate sd/ssd binaries
10701 		 * x86 platform can be an issue with both parallel
10702 		 * and fibre in 1 binary
10703 		 */
10704 		sd_scsi_clear_probe_cache();
10705 		return (ENXIO);
10706 	}
10707 
10708 	/*
10709 	 * The un_layer_count is to prevent another thread in specfs from
10710 	 * trying to detach the instance, which can happen when we are
10711 	 * called from a higher-layer driver instead of thru specfs.
10712 	 * This will not be needed when DDI provides a layered driver
10713 	 * interface that allows specfs to know that an instance is in
10714 	 * use by a layered driver & should not be detached.
10715 	 *
10716 	 * Note: the semantics for layered driver opens are exactly one
10717 	 * close for every open.
10718 	 */
10719 	if (otyp == OTYP_LYR) {
10720 		un->un_layer_count++;
10721 	}
10722 
10723 	/*
10724 	 * Keep a count of the current # of opens in progress. This is because
10725 	 * some layered drivers try to call us as a regular open. This can
10726 	 * cause problems that we cannot prevent, however by keeping this count
10727 	 * we can at least keep our open and detach routines from racing against
10728 	 * each other under such conditions.
10729 	 */
10730 	un->un_opens_in_progress++;
10731 	mutex_exit(&sd_detach_mutex);
10732 
10733 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
10734 	part	 = SDPART(dev);
10735 	partmask = 1 << part;
10736 
10737 	/*
10738 	 * We use a semaphore here in order to serialize
10739 	 * open and close requests on the device.
10740 	 */
10741 	sema_p(&un->un_semoclose);
10742 
10743 	mutex_enter(SD_MUTEX(un));
10744 
10745 	/*
10746 	 * All device accesses go thru sdstrategy() where we check
10747 	 * on suspend status but there could be a scsi_poll command,
10748 	 * which bypasses sdstrategy(), so we need to check pm
10749 	 * status.
10750 	 */
10751 
10752 	if (!nodelay) {
10753 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10754 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10755 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10756 		}
10757 
10758 		mutex_exit(SD_MUTEX(un));
10759 		if (sd_pm_entry(un) != DDI_SUCCESS) {
10760 			rval = EIO;
10761 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10762 			    "sdopen: sd_pm_entry failed\n");
10763 			goto open_failed_with_pm;
10764 		}
10765 		mutex_enter(SD_MUTEX(un));
10766 	}
10767 
10768 	/* check for previous exclusive open */
10769 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10770 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10771 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10772 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10773 
10774 	if (un->un_exclopen & (partmask)) {
10775 		goto excl_open_fail;
10776 	}
10777 
10778 	if (flag & FEXCL) {
10779 		int i;
10780 		if (un->un_ocmap.lyropen[part]) {
10781 			goto excl_open_fail;
10782 		}
10783 		for (i = 0; i < (OTYPCNT - 1); i++) {
10784 			if (un->un_ocmap.regopen[i] & (partmask)) {
10785 				goto excl_open_fail;
10786 			}
10787 		}
10788 	}
10789 
10790 	/*
10791 	 * Check the write permission if this is a removable media device,
10792 	 * NDELAY has not been set, and writable permission is requested.
10793 	 *
10794 	 * Note: If NDELAY was set and this is write-protected media the WRITE
10795 	 * attempt will fail with EIO as part of the I/O processing. This is a
10796 	 * more permissive implementation that allows the open to succeed and
10797 	 * WRITE attempts to fail when appropriate.
10798 	 */
10799 	if (un->un_f_chk_wp_open) {
10800 		if ((flag & FWRITE) && (!nodelay)) {
10801 			mutex_exit(SD_MUTEX(un));
10802 			/*
10803 			 * Defer the check for write permission on writable
10804 			 * DVD drive till sdstrategy and will not fail open even
10805 			 * if FWRITE is set as the device can be writable
10806 			 * depending upon the media and the media can change
10807 			 * after the call to open().
10808 			 */
10809 			if (un->un_f_dvdram_writable_device == FALSE) {
10810 				if (ISCD(un) || sr_check_wp(dev)) {
10811 				rval = EROFS;
10812 				mutex_enter(SD_MUTEX(un));
10813 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10814 				    "write to cd or write protected media\n");
10815 				goto open_fail;
10816 				}
10817 			}
10818 			mutex_enter(SD_MUTEX(un));
10819 		}
10820 	}
10821 
10822 	/*
10823 	 * If opening in NDELAY/NONBLOCK mode, just return.
10824 	 * Check if disk is ready and has a valid geometry later.
10825 	 */
10826 	if (!nodelay) {
10827 		mutex_exit(SD_MUTEX(un));
10828 		rval = sd_ready_and_valid(un);
10829 		mutex_enter(SD_MUTEX(un));
10830 		/*
10831 		 * Fail if device is not ready or if the number of disk
10832 		 * blocks is zero or negative for non CD devices.
10833 		 */
10834 		if ((rval != SD_READY_VALID) ||
10835 		    (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) {
10836 			rval = un->un_f_has_removable_media ? ENXIO : EIO;
10837 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10838 			    "device not ready or invalid disk block value\n");
10839 			goto open_fail;
10840 		}
10841 #if defined(__i386) || defined(__amd64)
10842 	} else {
10843 		uchar_t *cp;
10844 		/*
10845 		 * x86 requires special nodelay handling, so that p0 is
10846 		 * always defined and accessible.
10847 		 * Invalidate geometry only if device is not already open.
10848 		 */
10849 		cp = &un->un_ocmap.chkd[0];
10850 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10851 			if (*cp != (uchar_t)0) {
10852 			    break;
10853 			}
10854 			cp++;
10855 		}
10856 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10857 			un->un_f_geometry_is_valid = FALSE;
10858 		}
10859 
10860 #endif
10861 	}
10862 
10863 	if (otyp == OTYP_LYR) {
10864 		un->un_ocmap.lyropen[part]++;
10865 	} else {
10866 		un->un_ocmap.regopen[otyp] |= partmask;
10867 	}
10868 
10869 	/* Set up open and exclusive open flags */
10870 	if (flag & FEXCL) {
10871 		un->un_exclopen |= (partmask);
10872 	}
10873 
10874 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10875 	    "open of part %d type %d\n", part, otyp);
10876 
10877 	mutex_exit(SD_MUTEX(un));
10878 	if (!nodelay) {
10879 		sd_pm_exit(un);
10880 	}
10881 
10882 	sema_v(&un->un_semoclose);
10883 
10884 	mutex_enter(&sd_detach_mutex);
10885 	un->un_opens_in_progress--;
10886 	mutex_exit(&sd_detach_mutex);
10887 
10888 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10889 	return (DDI_SUCCESS);
10890 
10891 excl_open_fail:
10892 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10893 	rval = EBUSY;
10894 
10895 open_fail:
10896 	mutex_exit(SD_MUTEX(un));
10897 
10898 	/*
10899 	 * On a failed open we must exit the pm management.
10900 	 */
10901 	if (!nodelay) {
10902 		sd_pm_exit(un);
10903 	}
10904 open_failed_with_pm:
10905 	sema_v(&un->un_semoclose);
10906 
10907 	mutex_enter(&sd_detach_mutex);
10908 	un->un_opens_in_progress--;
10909 	if (otyp == OTYP_LYR) {
10910 		un->un_layer_count--;
10911 	}
10912 	mutex_exit(&sd_detach_mutex);
10913 
10914 	return (rval);
10915 }
10916 
10917 
10918 /*
10919  *    Function: sdclose
10920  *
10921  * Description: Driver's close(9e) entry point function.
10922  *
10923  *   Arguments: dev    - device number
10924  *		flag   - file status flag, informational only
10925  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10926  *		cred_p - user credential pointer
10927  *
10928  * Return Code: ENXIO
10929  *
10930  *     Context: Kernel thread context
10931  */
10932 /* ARGSUSED */
10933 static int
10934 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10935 {
10936 	struct sd_lun	*un;
10937 	uchar_t		*cp;
10938 	int		part;
10939 	int		nodelay;
10940 	int		rval = 0;
10941 
10942 	/* Validate the open type */
10943 	if (otyp >= OTYPCNT) {
10944 		return (ENXIO);
10945 	}
10946 
10947 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10948 		return (ENXIO);
10949 	}
10950 
10951 	part = SDPART(dev);
10952 	nodelay = flag & (FNDELAY | FNONBLOCK);
10953 
10954 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10955 	    "sdclose: close of part %d type %d\n", part, otyp);
10956 
10957 	/*
10958 	 * We use a semaphore here in order to serialize
10959 	 * open and close requests on the device.
10960 	 */
10961 	sema_p(&un->un_semoclose);
10962 
10963 	mutex_enter(SD_MUTEX(un));
10964 
10965 	/* Don't proceed if power is being changed. */
10966 	while (un->un_state == SD_STATE_PM_CHANGING) {
10967 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10968 	}
10969 
10970 	if (un->un_exclopen & (1 << part)) {
10971 		un->un_exclopen &= ~(1 << part);
10972 	}
10973 
10974 	/* Update the open partition map */
10975 	if (otyp == OTYP_LYR) {
10976 		un->un_ocmap.lyropen[part] -= 1;
10977 	} else {
10978 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10979 	}
10980 
10981 	cp = &un->un_ocmap.chkd[0];
10982 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10983 		if (*cp != NULL) {
10984 			break;
10985 		}
10986 		cp++;
10987 	}
10988 
10989 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10990 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10991 
10992 		/*
10993 		 * We avoid persistance upon the last close, and set
10994 		 * the throttle back to the maximum.
10995 		 */
10996 		un->un_throttle = un->un_saved_throttle;
10997 
10998 		if (un->un_state == SD_STATE_OFFLINE) {
10999 			if (un->un_f_is_fibre == FALSE) {
11000 				scsi_log(SD_DEVINFO(un), sd_label,
11001 					CE_WARN, "offline\n");
11002 			}
11003 			un->un_f_geometry_is_valid = FALSE;
11004 
11005 		} else {
11006 			/*
11007 			 * Flush any outstanding writes in NVRAM cache.
11008 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
11009 			 * cmd, it may not work for non-Pluto devices.
11010 			 * SYNCHRONIZE CACHE is not required for removables,
11011 			 * except DVD-RAM drives.
11012 			 *
11013 			 * Also note: because SYNCHRONIZE CACHE is currently
11014 			 * the only command issued here that requires the
11015 			 * drive be powered up, only do the power up before
11016 			 * sending the Sync Cache command. If additional
11017 			 * commands are added which require a powered up
11018 			 * drive, the following sequence may have to change.
11019 			 *
11020 			 * And finally, note that parallel SCSI on SPARC
11021 			 * only issues a Sync Cache to DVD-RAM, a newly
11022 			 * supported device.
11023 			 */
11024 #if defined(__i386) || defined(__amd64)
11025 			if (un->un_f_sync_cache_supported ||
11026 			    un->un_f_dvdram_writable_device == TRUE) {
11027 #else
11028 			if (un->un_f_dvdram_writable_device == TRUE) {
11029 #endif
11030 				mutex_exit(SD_MUTEX(un));
11031 				if (sd_pm_entry(un) == DDI_SUCCESS) {
11032 					rval =
11033 					    sd_send_scsi_SYNCHRONIZE_CACHE(un,
11034 					    NULL);
11035 					/* ignore error if not supported */
11036 					if (rval == ENOTSUP) {
11037 						rval = 0;
11038 					} else if (rval != 0) {
11039 						rval = EIO;
11040 					}
11041 					sd_pm_exit(un);
11042 				} else {
11043 					rval = EIO;
11044 				}
11045 				mutex_enter(SD_MUTEX(un));
11046 			}
11047 
11048 			/*
11049 			 * For devices which supports DOOR_LOCK, send an ALLOW
11050 			 * MEDIA REMOVAL command, but don't get upset if it
11051 			 * fails. We need to raise the power of the drive before
11052 			 * we can call sd_send_scsi_DOORLOCK()
11053 			 */
11054 			if (un->un_f_doorlock_supported) {
11055 				mutex_exit(SD_MUTEX(un));
11056 				if (sd_pm_entry(un) == DDI_SUCCESS) {
11057 					rval = sd_send_scsi_DOORLOCK(un,
11058 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
11059 
11060 					sd_pm_exit(un);
11061 					if (ISCD(un) && (rval != 0) &&
11062 					    (nodelay != 0)) {
11063 						rval = ENXIO;
11064 					}
11065 				} else {
11066 					rval = EIO;
11067 				}
11068 				mutex_enter(SD_MUTEX(un));
11069 			}
11070 
11071 			/*
11072 			 * If a device has removable media, invalidate all
11073 			 * parameters related to media, such as geometry,
11074 			 * blocksize, and blockcount.
11075 			 */
11076 			if (un->un_f_has_removable_media) {
11077 				sr_ejected(un);
11078 			}
11079 
11080 			/*
11081 			 * Destroy the cache (if it exists) which was
11082 			 * allocated for the write maps since this is
11083 			 * the last close for this media.
11084 			 */
11085 			if (un->un_wm_cache) {
11086 				/*
11087 				 * Check if there are pending commands.
11088 				 * and if there are give a warning and
11089 				 * do not destroy the cache.
11090 				 */
11091 				if (un->un_ncmds_in_driver > 0) {
11092 					scsi_log(SD_DEVINFO(un),
11093 					    sd_label, CE_WARN,
11094 					    "Unable to clean up memory "
11095 					    "because of pending I/O\n");
11096 				} else {
11097 					kmem_cache_destroy(
11098 					    un->un_wm_cache);
11099 					un->un_wm_cache = NULL;
11100 				}
11101 			}
11102 		}
11103 	}
11104 
11105 	mutex_exit(SD_MUTEX(un));
11106 	sema_v(&un->un_semoclose);
11107 
11108 	if (otyp == OTYP_LYR) {
11109 		mutex_enter(&sd_detach_mutex);
11110 		/*
11111 		 * The detach routine may run when the layer count
11112 		 * drops to zero.
11113 		 */
11114 		un->un_layer_count--;
11115 		mutex_exit(&sd_detach_mutex);
11116 	}
11117 
11118 	return (rval);
11119 }
11120 
11121 
11122 /*
11123  *    Function: sd_ready_and_valid
11124  *
11125  * Description: Test if device is ready and has a valid geometry.
11126  *
11127  *   Arguments: dev - device number
11128  *		un  - driver soft state (unit) structure
11129  *
11130  * Return Code: SD_READY_VALID		ready and valid label
11131  *		SD_READY_NOT_VALID	ready, geom ops never applicable
11132  *		SD_NOT_READY_VALID	not ready, no label
11133  *		SD_RESERVED_BY_OTHERS	reservation conflict
11134  *
11135  *     Context: Never called at interrupt context.
11136  */
11137 
11138 static int
11139 sd_ready_and_valid(struct sd_lun *un)
11140 {
11141 	struct sd_errstats	*stp;
11142 	uint64_t		capacity;
11143 	uint_t			lbasize;
11144 	int			rval = SD_READY_VALID;
11145 	char			name_str[48];
11146 
11147 	ASSERT(un != NULL);
11148 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11149 
11150 	mutex_enter(SD_MUTEX(un));
11151 	/*
11152 	 * If a device has removable media, we must check if media is
11153 	 * ready when checking if this device is ready and valid.
11154 	 */
11155 	if (un->un_f_has_removable_media) {
11156 		mutex_exit(SD_MUTEX(un));
11157 		if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) {
11158 			rval = SD_NOT_READY_VALID;
11159 			mutex_enter(SD_MUTEX(un));
11160 			goto done;
11161 		}
11162 
11163 		mutex_enter(SD_MUTEX(un));
11164 		if ((un->un_f_geometry_is_valid == FALSE) ||
11165 		    (un->un_f_blockcount_is_valid == FALSE) ||
11166 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
11167 
11168 			/* capacity has to be read every open. */
11169 			mutex_exit(SD_MUTEX(un));
11170 			if (sd_send_scsi_READ_CAPACITY(un, &capacity,
11171 			    &lbasize, SD_PATH_DIRECT) != 0) {
11172 				mutex_enter(SD_MUTEX(un));
11173 				un->un_f_geometry_is_valid = FALSE;
11174 				rval = SD_NOT_READY_VALID;
11175 				goto done;
11176 			} else {
11177 				mutex_enter(SD_MUTEX(un));
11178 				sd_update_block_info(un, lbasize, capacity);
11179 			}
11180 		}
11181 
11182 		/*
11183 		 * Check if the media in the device is writable or not.
11184 		 */
11185 		if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) {
11186 			sd_check_for_writable_cd(un);
11187 		}
11188 
11189 	} else {
11190 		/*
11191 		 * Do a test unit ready to clear any unit attention from non-cd
11192 		 * devices.
11193 		 */
11194 		mutex_exit(SD_MUTEX(un));
11195 		(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
11196 		mutex_enter(SD_MUTEX(un));
11197 	}
11198 
11199 
11200 	/*
11201 	 * If this is a non 512 block device, allocate space for
11202 	 * the wmap cache. This is being done here since every time
11203 	 * a media is changed this routine will be called and the
11204 	 * block size is a function of media rather than device.
11205 	 */
11206 	if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) {
11207 		if (!(un->un_wm_cache)) {
11208 			(void) snprintf(name_str, sizeof (name_str),
11209 			    "%s%d_cache",
11210 			    ddi_driver_name(SD_DEVINFO(un)),
11211 			    ddi_get_instance(SD_DEVINFO(un)));
11212 			un->un_wm_cache = kmem_cache_create(
11213 			    name_str, sizeof (struct sd_w_map),
11214 			    8, sd_wm_cache_constructor,
11215 			    sd_wm_cache_destructor, NULL,
11216 			    (void *)un, NULL, 0);
11217 			if (!(un->un_wm_cache)) {
11218 					rval = ENOMEM;
11219 					goto done;
11220 			}
11221 		}
11222 	}
11223 
11224 	if (un->un_state == SD_STATE_NORMAL) {
11225 		/*
11226 		 * If the target is not yet ready here (defined by a TUR
11227 		 * failure), invalidate the geometry and print an 'offline'
11228 		 * message. This is a legacy message, as the state of the
11229 		 * target is not actually changed to SD_STATE_OFFLINE.
11230 		 *
11231 		 * If the TUR fails for EACCES (Reservation Conflict),
11232 		 * SD_RESERVED_BY_OTHERS will be returned to indicate
11233 		 * reservation conflict. If the TUR fails for other
11234 		 * reasons, SD_NOT_READY_VALID will be returned.
11235 		 */
11236 		int err;
11237 
11238 		mutex_exit(SD_MUTEX(un));
11239 		err = sd_send_scsi_TEST_UNIT_READY(un, 0);
11240 		mutex_enter(SD_MUTEX(un));
11241 
11242 		if (err != 0) {
11243 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
11244 			    "offline or reservation conflict\n");
11245 			un->un_f_geometry_is_valid = FALSE;
11246 			if (err == EACCES) {
11247 				rval = SD_RESERVED_BY_OTHERS;
11248 			} else {
11249 				rval = SD_NOT_READY_VALID;
11250 			}
11251 			goto done;
11252 		}
11253 	}
11254 
11255 	if (un->un_f_format_in_progress == FALSE) {
11256 		/*
11257 		 * Note: sd_validate_geometry may return TRUE, but that does
11258 		 * not necessarily mean un_f_geometry_is_valid == TRUE!
11259 		 */
11260 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
11261 		if (rval == ENOTSUP) {
11262 			if (un->un_f_geometry_is_valid == TRUE)
11263 				rval = 0;
11264 			else {
11265 				rval = SD_READY_NOT_VALID;
11266 				goto done;
11267 			}
11268 		}
11269 		if (rval != 0) {
11270 			/*
11271 			 * We don't check the validity of geometry for
11272 			 * CDROMs. Also we assume we have a good label
11273 			 * even if sd_validate_geometry returned ENOMEM.
11274 			 */
11275 			if (!ISCD(un) && rval != ENOMEM) {
11276 				rval = SD_NOT_READY_VALID;
11277 				goto done;
11278 			}
11279 		}
11280 	}
11281 
11282 	/*
11283 	 * If this device supports DOOR_LOCK command, try and send
11284 	 * this command to PREVENT MEDIA REMOVAL, but don't get upset
11285 	 * if it fails. For a CD, however, it is an error
11286 	 */
11287 	if (un->un_f_doorlock_supported) {
11288 		mutex_exit(SD_MUTEX(un));
11289 		if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
11290 		    SD_PATH_DIRECT) != 0) && ISCD(un)) {
11291 			rval = SD_NOT_READY_VALID;
11292 			mutex_enter(SD_MUTEX(un));
11293 			goto done;
11294 		}
11295 		mutex_enter(SD_MUTEX(un));
11296 	}
11297 
11298 	/* The state has changed, inform the media watch routines */
11299 	un->un_mediastate = DKIO_INSERTED;
11300 	cv_broadcast(&un->un_state_cv);
11301 	rval = SD_READY_VALID;
11302 
11303 done:
11304 
11305 	/*
11306 	 * Initialize the capacity kstat value, if no media previously
11307 	 * (capacity kstat is 0) and a media has been inserted
11308 	 * (un_blockcount > 0).
11309 	 */
11310 	if (un->un_errstats != NULL) {
11311 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
11312 		if ((stp->sd_capacity.value.ui64 == 0) &&
11313 		    (un->un_f_blockcount_is_valid == TRUE)) {
11314 			stp->sd_capacity.value.ui64 =
11315 			    (uint64_t)((uint64_t)un->un_blockcount *
11316 			    un->un_sys_blocksize);
11317 		}
11318 	}
11319 
11320 	mutex_exit(SD_MUTEX(un));
11321 	return (rval);
11322 }
11323 
11324 
11325 /*
11326  *    Function: sdmin
11327  *
11328  * Description: Routine to limit the size of a data transfer. Used in
11329  *		conjunction with physio(9F).
11330  *
11331  *   Arguments: bp - pointer to the indicated buf(9S) struct.
11332  *
11333  *     Context: Kernel thread context.
11334  */
11335 
11336 static void
11337 sdmin(struct buf *bp)
11338 {
11339 	struct sd_lun	*un;
11340 	int		instance;
11341 
11342 	instance = SDUNIT(bp->b_edev);
11343 
11344 	un = ddi_get_soft_state(sd_state, instance);
11345 	ASSERT(un != NULL);
11346 
11347 	if (bp->b_bcount > un->un_max_xfer_size) {
11348 		bp->b_bcount = un->un_max_xfer_size;
11349 	}
11350 }
11351 
11352 
11353 /*
11354  *    Function: sdread
11355  *
11356  * Description: Driver's read(9e) entry point function.
11357  *
11358  *   Arguments: dev   - device number
11359  *		uio   - structure pointer describing where data is to be stored
11360  *			in user's space
11361  *		cred_p  - user credential pointer
11362  *
11363  * Return Code: ENXIO
11364  *		EIO
11365  *		EINVAL
11366  *		value returned by physio
11367  *
11368  *     Context: Kernel thread context.
11369  */
11370 /* ARGSUSED */
11371 static int
11372 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
11373 {
11374 	struct sd_lun	*un = NULL;
11375 	int		secmask;
11376 	int		err;
11377 
11378 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11379 		return (ENXIO);
11380 	}
11381 
11382 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11383 
11384 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11385 		mutex_enter(SD_MUTEX(un));
11386 		/*
11387 		 * Because the call to sd_ready_and_valid will issue I/O we
11388 		 * must wait here if either the device is suspended or
11389 		 * if it's power level is changing.
11390 		 */
11391 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11392 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11393 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11394 		}
11395 		un->un_ncmds_in_driver++;
11396 		mutex_exit(SD_MUTEX(un));
11397 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11398 			mutex_enter(SD_MUTEX(un));
11399 			un->un_ncmds_in_driver--;
11400 			ASSERT(un->un_ncmds_in_driver >= 0);
11401 			mutex_exit(SD_MUTEX(un));
11402 			return (EIO);
11403 		}
11404 		mutex_enter(SD_MUTEX(un));
11405 		un->un_ncmds_in_driver--;
11406 		ASSERT(un->un_ncmds_in_driver >= 0);
11407 		mutex_exit(SD_MUTEX(un));
11408 	}
11409 
11410 	/*
11411 	 * Read requests are restricted to multiples of the system block size.
11412 	 */
11413 	secmask = un->un_sys_blocksize - 1;
11414 
11415 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11416 		SD_ERROR(SD_LOG_READ_WRITE, un,
11417 		    "sdread: file offset not modulo %d\n",
11418 		    un->un_sys_blocksize);
11419 		err = EINVAL;
11420 	} else if (uio->uio_iov->iov_len & (secmask)) {
11421 		SD_ERROR(SD_LOG_READ_WRITE, un,
11422 		    "sdread: transfer length not modulo %d\n",
11423 		    un->un_sys_blocksize);
11424 		err = EINVAL;
11425 	} else {
11426 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
11427 	}
11428 	return (err);
11429 }
11430 
11431 
11432 /*
11433  *    Function: sdwrite
11434  *
11435  * Description: Driver's write(9e) entry point function.
11436  *
11437  *   Arguments: dev   - device number
11438  *		uio   - structure pointer describing where data is stored in
11439  *			user's space
11440  *		cred_p  - user credential pointer
11441  *
11442  * Return Code: ENXIO
11443  *		EIO
11444  *		EINVAL
11445  *		value returned by physio
11446  *
11447  *     Context: Kernel thread context.
11448  */
11449 /* ARGSUSED */
11450 static int
11451 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
11452 {
11453 	struct sd_lun	*un = NULL;
11454 	int		secmask;
11455 	int		err;
11456 
11457 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11458 		return (ENXIO);
11459 	}
11460 
11461 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11462 
11463 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11464 		mutex_enter(SD_MUTEX(un));
11465 		/*
11466 		 * Because the call to sd_ready_and_valid will issue I/O we
11467 		 * must wait here if either the device is suspended or
11468 		 * if it's power level is changing.
11469 		 */
11470 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11471 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11472 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11473 		}
11474 		un->un_ncmds_in_driver++;
11475 		mutex_exit(SD_MUTEX(un));
11476 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11477 			mutex_enter(SD_MUTEX(un));
11478 			un->un_ncmds_in_driver--;
11479 			ASSERT(un->un_ncmds_in_driver >= 0);
11480 			mutex_exit(SD_MUTEX(un));
11481 			return (EIO);
11482 		}
11483 		mutex_enter(SD_MUTEX(un));
11484 		un->un_ncmds_in_driver--;
11485 		ASSERT(un->un_ncmds_in_driver >= 0);
11486 		mutex_exit(SD_MUTEX(un));
11487 	}
11488 
11489 	/*
11490 	 * Write requests are restricted to multiples of the system block size.
11491 	 */
11492 	secmask = un->un_sys_blocksize - 1;
11493 
11494 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11495 		SD_ERROR(SD_LOG_READ_WRITE, un,
11496 		    "sdwrite: file offset not modulo %d\n",
11497 		    un->un_sys_blocksize);
11498 		err = EINVAL;
11499 	} else if (uio->uio_iov->iov_len & (secmask)) {
11500 		SD_ERROR(SD_LOG_READ_WRITE, un,
11501 		    "sdwrite: transfer length not modulo %d\n",
11502 		    un->un_sys_blocksize);
11503 		err = EINVAL;
11504 	} else {
11505 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11506 	}
11507 	return (err);
11508 }
11509 
11510 
11511 /*
11512  *    Function: sdaread
11513  *
11514  * Description: Driver's aread(9e) entry point function.
11515  *
11516  *   Arguments: dev   - device number
11517  *		aio   - structure pointer describing where data is to be stored
11518  *		cred_p  - user credential pointer
11519  *
11520  * Return Code: ENXIO
11521  *		EIO
11522  *		EINVAL
11523  *		value returned by aphysio
11524  *
11525  *     Context: Kernel thread context.
11526  */
11527 /* ARGSUSED */
11528 static int
11529 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11530 {
11531 	struct sd_lun	*un = NULL;
11532 	struct uio	*uio = aio->aio_uio;
11533 	int		secmask;
11534 	int		err;
11535 
11536 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11537 		return (ENXIO);
11538 	}
11539 
11540 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11541 
11542 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11543 		mutex_enter(SD_MUTEX(un));
11544 		/*
11545 		 * Because the call to sd_ready_and_valid will issue I/O we
11546 		 * must wait here if either the device is suspended or
11547 		 * if it's power level is changing.
11548 		 */
11549 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11550 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11551 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11552 		}
11553 		un->un_ncmds_in_driver++;
11554 		mutex_exit(SD_MUTEX(un));
11555 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11556 			mutex_enter(SD_MUTEX(un));
11557 			un->un_ncmds_in_driver--;
11558 			ASSERT(un->un_ncmds_in_driver >= 0);
11559 			mutex_exit(SD_MUTEX(un));
11560 			return (EIO);
11561 		}
11562 		mutex_enter(SD_MUTEX(un));
11563 		un->un_ncmds_in_driver--;
11564 		ASSERT(un->un_ncmds_in_driver >= 0);
11565 		mutex_exit(SD_MUTEX(un));
11566 	}
11567 
11568 	/*
11569 	 * Read requests are restricted to multiples of the system block size.
11570 	 */
11571 	secmask = un->un_sys_blocksize - 1;
11572 
11573 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11574 		SD_ERROR(SD_LOG_READ_WRITE, un,
11575 		    "sdaread: file offset not modulo %d\n",
11576 		    un->un_sys_blocksize);
11577 		err = EINVAL;
11578 	} else if (uio->uio_iov->iov_len & (secmask)) {
11579 		SD_ERROR(SD_LOG_READ_WRITE, un,
11580 		    "sdaread: transfer length not modulo %d\n",
11581 		    un->un_sys_blocksize);
11582 		err = EINVAL;
11583 	} else {
11584 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11585 	}
11586 	return (err);
11587 }
11588 
11589 
11590 /*
11591  *    Function: sdawrite
11592  *
11593  * Description: Driver's awrite(9e) entry point function.
11594  *
11595  *   Arguments: dev   - device number
11596  *		aio   - structure pointer describing where data is stored
11597  *		cred_p  - user credential pointer
11598  *
11599  * Return Code: ENXIO
11600  *		EIO
11601  *		EINVAL
11602  *		value returned by aphysio
11603  *
11604  *     Context: Kernel thread context.
11605  */
11606 /* ARGSUSED */
11607 static int
11608 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11609 {
11610 	struct sd_lun	*un = NULL;
11611 	struct uio	*uio = aio->aio_uio;
11612 	int		secmask;
11613 	int		err;
11614 
11615 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11616 		return (ENXIO);
11617 	}
11618 
11619 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11620 
11621 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11622 		mutex_enter(SD_MUTEX(un));
11623 		/*
11624 		 * Because the call to sd_ready_and_valid will issue I/O we
11625 		 * must wait here if either the device is suspended or
11626 		 * if it's power level is changing.
11627 		 */
11628 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11629 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11630 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11631 		}
11632 		un->un_ncmds_in_driver++;
11633 		mutex_exit(SD_MUTEX(un));
11634 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11635 			mutex_enter(SD_MUTEX(un));
11636 			un->un_ncmds_in_driver--;
11637 			ASSERT(un->un_ncmds_in_driver >= 0);
11638 			mutex_exit(SD_MUTEX(un));
11639 			return (EIO);
11640 		}
11641 		mutex_enter(SD_MUTEX(un));
11642 		un->un_ncmds_in_driver--;
11643 		ASSERT(un->un_ncmds_in_driver >= 0);
11644 		mutex_exit(SD_MUTEX(un));
11645 	}
11646 
11647 	/*
11648 	 * Write requests are restricted to multiples of the system block size.
11649 	 */
11650 	secmask = un->un_sys_blocksize - 1;
11651 
11652 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11653 		SD_ERROR(SD_LOG_READ_WRITE, un,
11654 		    "sdawrite: file offset not modulo %d\n",
11655 		    un->un_sys_blocksize);
11656 		err = EINVAL;
11657 	} else if (uio->uio_iov->iov_len & (secmask)) {
11658 		SD_ERROR(SD_LOG_READ_WRITE, un,
11659 		    "sdawrite: transfer length not modulo %d\n",
11660 		    un->un_sys_blocksize);
11661 		err = EINVAL;
11662 	} else {
11663 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11664 	}
11665 	return (err);
11666 }
11667 
11668 
11669 
11670 
11671 
11672 /*
11673  * Driver IO processing follows the following sequence:
11674  *
11675  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11676  *         |                |                     ^
11677  *         v                v                     |
11678  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11679  *         |                |                     |                   |
11680  *         v                |                     |                   |
11681  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11682  *         |                |                     ^                   ^
11683  *         v                v                     |                   |
11684  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11685  *         |                |                     |                   |
11686  *     +---+                |                     +------------+      +-------+
11687  *     |                    |                                  |              |
11688  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11689  *     |                    v                                  |              |
11690  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11691  *     |                    |                                  ^              |
11692  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11693  *     |                    v                                  |              |
11694  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11695  *     |                    |                                  ^              |
11696  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11697  *     |                    v                                  |              |
11698  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11699  *     |                    |                                  ^              |
11700  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11701  *     |                    v                                  |              |
11702  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11703  *     |                    |                                  ^              |
11704  *     |                    |                                  |              |
11705  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11706  *                          |                           ^
11707  *                          v                           |
11708  *                   sd_core_iostart()                  |
11709  *                          |                           |
11710  *                          |                           +------>(*destroypkt)()
11711  *                          +-> sd_start_cmds() <-+     |           |
11712  *                          |                     |     |           v
11713  *                          |                     |     |  scsi_destroy_pkt(9F)
11714  *                          |                     |     |
11715  *                          +->(*initpkt)()       +- sdintr()
11716  *                          |  |                        |  |
11717  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11718  *                          |  +-> scsi_setup_cdb(9F)   |
11719  *                          |                           |
11720  *                          +--> scsi_transport(9F)     |
11721  *                                     |                |
11722  *                                     +----> SCSA ---->+
11723  *
11724  *
11725  * This code is based upon the following presumtions:
11726  *
11727  *   - iostart and iodone functions operate on buf(9S) structures. These
11728  *     functions perform the necessary operations on the buf(9S) and pass
11729  *     them along to the next function in the chain by using the macros
11730  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11731  *     (for iodone side functions).
11732  *
11733  *   - The iostart side functions may sleep. The iodone side functions
11734  *     are called under interrupt context and may NOT sleep. Therefore
11735  *     iodone side functions also may not call iostart side functions.
11736  *     (NOTE: iostart side functions should NOT sleep for memory, as
11737  *     this could result in deadlock.)
11738  *
11739  *   - An iostart side function may call its corresponding iodone side
11740  *     function directly (if necessary).
11741  *
11742  *   - In the event of an error, an iostart side function can return a buf(9S)
11743  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11744  *     b_error in the usual way of course).
11745  *
11746  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11747  *     requests to the iostart side functions.  The iostart side functions in
11748  *     this case would be called under the context of a taskq thread, so it's
11749  *     OK for them to block/sleep/spin in this case.
11750  *
11751  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11752  *     pass them along to the next function in the chain.  The corresponding
11753  *     iodone side functions must coalesce the "shadow" bufs and return
11754  *     the "original" buf to the next higher layer.
11755  *
11756  *   - The b_private field of the buf(9S) struct holds a pointer to
11757  *     an sd_xbuf struct, which contains information needed to
11758  *     construct the scsi_pkt for the command.
11759  *
11760  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11761  *     layer must acquire & release the SD_MUTEX(un) as needed.
11762  */
11763 
11764 
11765 /*
11766  * Create taskq for all targets in the system. This is created at
11767  * _init(9E) and destroyed at _fini(9E).
11768  *
11769  * Note: here we set the minalloc to a reasonably high number to ensure that
11770  * we will have an adequate supply of task entries available at interrupt time.
11771  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11772  * sd_create_taskq().  Since we do not want to sleep for allocations at
11773  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11774  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11775  * requests any one instant in time.
11776  */
11777 #define	SD_TASKQ_NUMTHREADS	8
11778 #define	SD_TASKQ_MINALLOC	256
11779 #define	SD_TASKQ_MAXALLOC	256
11780 
11781 static taskq_t	*sd_tq = NULL;
11782 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11783 
11784 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11785 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11786 
11787 /*
11788  * The following task queue is being created for the write part of
11789  * read-modify-write of non-512 block size devices.
11790  * Limit the number of threads to 1 for now. This number has been choosen
11791  * considering the fact that it applies only to dvd ram drives/MO drives
11792  * currently. Performance for which is not main criteria at this stage.
11793  * Note: It needs to be explored if we can use a single taskq in future
11794  */
11795 #define	SD_WMR_TASKQ_NUMTHREADS	1
11796 static taskq_t	*sd_wmr_tq = NULL;
11797 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11798 
11799 /*
11800  *    Function: sd_taskq_create
11801  *
11802  * Description: Create taskq thread(s) and preallocate task entries
11803  *
11804  * Return Code: Returns a pointer to the allocated taskq_t.
11805  *
11806  *     Context: Can sleep. Requires blockable context.
11807  *
11808  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11809  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
11810  *		- taskq_create() will block for memory, also it will panic
11811  *		  if it cannot create the requested number of threads.
11812  *		- Currently taskq_create() creates threads that cannot be
11813  *		  swapped.
11814  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
11815  *		  supply of taskq entries at interrupt time (ie, so that we
11816  *		  do not have to sleep for memory)
11817  */
11818 
11819 static void
11820 sd_taskq_create(void)
11821 {
11822 	char	taskq_name[TASKQ_NAMELEN];
11823 
11824 	ASSERT(sd_tq == NULL);
11825 	ASSERT(sd_wmr_tq == NULL);
11826 
11827 	(void) snprintf(taskq_name, sizeof (taskq_name),
11828 	    "%s_drv_taskq", sd_label);
11829 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11830 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11831 	    TASKQ_PREPOPULATE));
11832 
11833 	(void) snprintf(taskq_name, sizeof (taskq_name),
11834 	    "%s_rmw_taskq", sd_label);
11835 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11836 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11837 	    TASKQ_PREPOPULATE));
11838 }
11839 
11840 
11841 /*
11842  *    Function: sd_taskq_delete
11843  *
11844  * Description: Complementary cleanup routine for sd_taskq_create().
11845  *
11846  *     Context: Kernel thread context.
11847  */
11848 
11849 static void
11850 sd_taskq_delete(void)
11851 {
11852 	ASSERT(sd_tq != NULL);
11853 	ASSERT(sd_wmr_tq != NULL);
11854 	taskq_destroy(sd_tq);
11855 	taskq_destroy(sd_wmr_tq);
11856 	sd_tq = NULL;
11857 	sd_wmr_tq = NULL;
11858 }
11859 
11860 
11861 /*
11862  *    Function: sdstrategy
11863  *
11864  * Description: Driver's strategy (9E) entry point function.
11865  *
11866  *   Arguments: bp - pointer to buf(9S)
11867  *
11868  * Return Code: Always returns zero
11869  *
11870  *     Context: Kernel thread context.
11871  */
11872 
11873 static int
11874 sdstrategy(struct buf *bp)
11875 {
11876 	struct sd_lun *un;
11877 
11878 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11879 	if (un == NULL) {
11880 		bioerror(bp, EIO);
11881 		bp->b_resid = bp->b_bcount;
11882 		biodone(bp);
11883 		return (0);
11884 	}
11885 	/* As was done in the past, fail new cmds. if state is dumping. */
11886 	if (un->un_state == SD_STATE_DUMPING) {
11887 		bioerror(bp, ENXIO);
11888 		bp->b_resid = bp->b_bcount;
11889 		biodone(bp);
11890 		return (0);
11891 	}
11892 
11893 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11894 
11895 	/*
11896 	 * Commands may sneak in while we released the mutex in
11897 	 * DDI_SUSPEND, we should block new commands. However, old
11898 	 * commands that are still in the driver at this point should
11899 	 * still be allowed to drain.
11900 	 */
11901 	mutex_enter(SD_MUTEX(un));
11902 	/*
11903 	 * Must wait here if either the device is suspended or
11904 	 * if it's power level is changing.
11905 	 */
11906 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11907 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11908 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11909 	}
11910 
11911 	un->un_ncmds_in_driver++;
11912 
11913 	/*
11914 	 * atapi: Since we are running the CD for now in PIO mode we need to
11915 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11916 	 * the HBA's init_pkt routine.
11917 	 */
11918 	if (un->un_f_cfg_is_atapi == TRUE) {
11919 		mutex_exit(SD_MUTEX(un));
11920 		bp_mapin(bp);
11921 		mutex_enter(SD_MUTEX(un));
11922 	}
11923 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11924 	    un->un_ncmds_in_driver);
11925 
11926 	mutex_exit(SD_MUTEX(un));
11927 
11928 	/*
11929 	 * This will (eventually) allocate the sd_xbuf area and
11930 	 * call sd_xbuf_strategy().  We just want to return the
11931 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11932 	 * imized tail call which saves us a stack frame.
11933 	 */
11934 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11935 }
11936 
11937 
11938 /*
11939  *    Function: sd_xbuf_strategy
11940  *
11941  * Description: Function for initiating IO operations via the
11942  *		ddi_xbuf_qstrategy() mechanism.
11943  *
11944  *     Context: Kernel thread context.
11945  */
11946 
11947 static void
11948 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11949 {
11950 	struct sd_lun *un = arg;
11951 
11952 	ASSERT(bp != NULL);
11953 	ASSERT(xp != NULL);
11954 	ASSERT(un != NULL);
11955 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11956 
11957 	/*
11958 	 * Initialize the fields in the xbuf and save a pointer to the
11959 	 * xbuf in bp->b_private.
11960 	 */
11961 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11962 
11963 	/* Send the buf down the iostart chain */
11964 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11965 }
11966 
11967 
11968 /*
11969  *    Function: sd_xbuf_init
11970  *
11971  * Description: Prepare the given sd_xbuf struct for use.
11972  *
11973  *   Arguments: un - ptr to softstate
11974  *		bp - ptr to associated buf(9S)
11975  *		xp - ptr to associated sd_xbuf
11976  *		chain_type - IO chain type to use:
11977  *			SD_CHAIN_NULL
11978  *			SD_CHAIN_BUFIO
11979  *			SD_CHAIN_USCSI
11980  *			SD_CHAIN_DIRECT
11981  *			SD_CHAIN_DIRECT_PRIORITY
11982  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11983  *			initialization; may be NULL if none.
11984  *
11985  *     Context: Kernel thread context
11986  */
11987 
11988 static void
11989 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11990 	uchar_t chain_type, void *pktinfop)
11991 {
11992 	int index;
11993 
11994 	ASSERT(un != NULL);
11995 	ASSERT(bp != NULL);
11996 	ASSERT(xp != NULL);
11997 
11998 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11999 	    bp, chain_type);
12000 
12001 	xp->xb_un	= un;
12002 	xp->xb_pktp	= NULL;
12003 	xp->xb_pktinfo	= pktinfop;
12004 	xp->xb_private	= bp->b_private;
12005 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
12006 
12007 	/*
12008 	 * Set up the iostart and iodone chain indexes in the xbuf, based
12009 	 * upon the specified chain type to use.
12010 	 */
12011 	switch (chain_type) {
12012 	case SD_CHAIN_NULL:
12013 		/*
12014 		 * Fall thru to just use the values for the buf type, even
12015 		 * tho for the NULL chain these values will never be used.
12016 		 */
12017 		/* FALLTHRU */
12018 	case SD_CHAIN_BUFIO:
12019 		index = un->un_buf_chain_type;
12020 		break;
12021 	case SD_CHAIN_USCSI:
12022 		index = un->un_uscsi_chain_type;
12023 		break;
12024 	case SD_CHAIN_DIRECT:
12025 		index = un->un_direct_chain_type;
12026 		break;
12027 	case SD_CHAIN_DIRECT_PRIORITY:
12028 		index = un->un_priority_chain_type;
12029 		break;
12030 	default:
12031 		/* We're really broken if we ever get here... */
12032 		panic("sd_xbuf_init: illegal chain type!");
12033 		/*NOTREACHED*/
12034 	}
12035 
12036 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
12037 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
12038 
12039 	/*
12040 	 * It might be a bit easier to simply bzero the entire xbuf above,
12041 	 * but it turns out that since we init a fair number of members anyway,
12042 	 * we save a fair number cycles by doing explicit assignment of zero.
12043 	 */
12044 	xp->xb_pkt_flags	= 0;
12045 	xp->xb_dma_resid	= 0;
12046 	xp->xb_retry_count	= 0;
12047 	xp->xb_victim_retry_count = 0;
12048 	xp->xb_ua_retry_count	= 0;
12049 	xp->xb_sense_bp		= NULL;
12050 	xp->xb_sense_status	= 0;
12051 	xp->xb_sense_state	= 0;
12052 	xp->xb_sense_resid	= 0;
12053 
12054 	bp->b_private	= xp;
12055 	bp->b_flags	&= ~(B_DONE | B_ERROR);
12056 	bp->b_resid	= 0;
12057 	bp->av_forw	= NULL;
12058 	bp->av_back	= NULL;
12059 	bioerror(bp, 0);
12060 
12061 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
12062 }
12063 
12064 
12065 /*
12066  *    Function: sd_uscsi_strategy
12067  *
12068  * Description: Wrapper for calling into the USCSI chain via physio(9F)
12069  *
12070  *   Arguments: bp - buf struct ptr
12071  *
12072  * Return Code: Always returns 0
12073  *
12074  *     Context: Kernel thread context
12075  */
12076 
12077 static int
12078 sd_uscsi_strategy(struct buf *bp)
12079 {
12080 	struct sd_lun		*un;
12081 	struct sd_uscsi_info	*uip;
12082 	struct sd_xbuf		*xp;
12083 	uchar_t			chain_type;
12084 
12085 	ASSERT(bp != NULL);
12086 
12087 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
12088 	if (un == NULL) {
12089 		bioerror(bp, EIO);
12090 		bp->b_resid = bp->b_bcount;
12091 		biodone(bp);
12092 		return (0);
12093 	}
12094 
12095 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12096 
12097 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
12098 
12099 	mutex_enter(SD_MUTEX(un));
12100 	/*
12101 	 * atapi: Since we are running the CD for now in PIO mode we need to
12102 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
12103 	 * the HBA's init_pkt routine.
12104 	 */
12105 	if (un->un_f_cfg_is_atapi == TRUE) {
12106 		mutex_exit(SD_MUTEX(un));
12107 		bp_mapin(bp);
12108 		mutex_enter(SD_MUTEX(un));
12109 	}
12110 	un->un_ncmds_in_driver++;
12111 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
12112 	    un->un_ncmds_in_driver);
12113 	mutex_exit(SD_MUTEX(un));
12114 
12115 	/*
12116 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
12117 	 */
12118 	ASSERT(bp->b_private != NULL);
12119 	uip = (struct sd_uscsi_info *)bp->b_private;
12120 
12121 	switch (uip->ui_flags) {
12122 	case SD_PATH_DIRECT:
12123 		chain_type = SD_CHAIN_DIRECT;
12124 		break;
12125 	case SD_PATH_DIRECT_PRIORITY:
12126 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
12127 		break;
12128 	default:
12129 		chain_type = SD_CHAIN_USCSI;
12130 		break;
12131 	}
12132 
12133 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
12134 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
12135 
12136 	/* Use the index obtained within xbuf_init */
12137 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
12138 
12139 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
12140 
12141 	return (0);
12142 }
12143 
12144 /*
12145  *    Function: sd_send_scsi_cmd
12146  *
12147  * Description: Runs a USCSI command for user (when called thru sdioctl),
12148  *		or for the driver
12149  *
12150  *   Arguments: dev - the dev_t for the device
12151  *		incmd - ptr to a valid uscsi_cmd struct
12152  *		flag - bit flag, indicating open settings, 32/64 bit type
12153  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
12154  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
12155  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
12156  *			to use the USCSI "direct" chain and bypass the normal
12157  *			command waitq.
12158  *
12159  * Return Code: 0 -  successful completion of the given command
12160  *		EIO - scsi_uscsi_handle_command() failed
12161  *		ENXIO  - soft state not found for specified dev
12162  *		EINVAL
12163  *		EFAULT - copyin/copyout error
12164  *		return code of scsi_uscsi_handle_command():
12165  *			EIO
12166  *			ENXIO
12167  *			EACCES
12168  *
12169  *     Context: Waits for command to complete. Can sleep.
12170  */
12171 
12172 static int
12173 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
12174 	enum uio_seg dataspace, int path_flag)
12175 {
12176 	struct sd_uscsi_info	*uip;
12177 	struct uscsi_cmd	*uscmd;
12178 	struct sd_lun	*un;
12179 	int	format = 0;
12180 	int	rval;
12181 
12182 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
12183 	if (un == NULL) {
12184 		return (ENXIO);
12185 	}
12186 
12187 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12188 
12189 #ifdef SDDEBUG
12190 	switch (dataspace) {
12191 	case UIO_USERSPACE:
12192 		SD_TRACE(SD_LOG_IO, un,
12193 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un);
12194 		break;
12195 	case UIO_SYSSPACE:
12196 		SD_TRACE(SD_LOG_IO, un,
12197 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un);
12198 		break;
12199 	default:
12200 		SD_TRACE(SD_LOG_IO, un,
12201 		    "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un);
12202 		break;
12203 	}
12204 #endif
12205 
12206 	rval = scsi_uscsi_alloc_and_copyin((intptr_t)incmd, flag,
12207 	    SD_ADDRESS(un), &uscmd);
12208 	if (rval != 0) {
12209 		SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
12210 		    "scsi_uscsi_alloc_and_copyin failed\n", un);
12211 		return (rval);
12212 	}
12213 
12214 	if ((uscmd->uscsi_cdb != NULL) &&
12215 	    (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
12216 		mutex_enter(SD_MUTEX(un));
12217 		un->un_f_format_in_progress = TRUE;
12218 		mutex_exit(SD_MUTEX(un));
12219 		format = 1;
12220 	}
12221 
12222 	/*
12223 	 * Allocate an sd_uscsi_info struct and fill it with the info
12224 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
12225 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
12226 	 * since we allocate the buf here in this function, we do not
12227 	 * need to preserve the prior contents of b_private.
12228 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12229 	 */
12230 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
12231 	uip->ui_flags = path_flag;
12232 	uip->ui_cmdp = uscmd;
12233 
12234 	/*
12235 	 * Commands sent with priority are intended for error recovery
12236 	 * situations, and do not have retries performed.
12237 	 */
12238 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12239 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12240 	}
12241 	uscmd->uscsi_flags &= ~USCSI_NOINTR;
12242 
12243 	rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12244 	    sd_uscsi_strategy, NULL, uip);
12245 
12246 #ifdef SDDEBUG
12247 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12248 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
12249 	    uscmd->uscsi_status, uscmd->uscsi_resid);
12250 	if (uscmd->uscsi_bufaddr != NULL) {
12251 		SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
12252 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
12253 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12254 		if (dataspace == UIO_SYSSPACE) {
12255 			SD_DUMP_MEMORY(un, SD_LOG_IO,
12256 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
12257 			    uscmd->uscsi_buflen, SD_LOG_HEX);
12258 		}
12259 	}
12260 #endif
12261 
12262 	if (format == 1) {
12263 		mutex_enter(SD_MUTEX(un));
12264 		un->un_f_format_in_progress = FALSE;
12265 		mutex_exit(SD_MUTEX(un));
12266 	}
12267 
12268 	(void) scsi_uscsi_copyout_and_free((intptr_t)incmd, uscmd);
12269 	kmem_free(uip, sizeof (struct sd_uscsi_info));
12270 
12271 	return (rval);
12272 }
12273 
12274 
12275 /*
12276  *    Function: sd_buf_iodone
12277  *
12278  * Description: Frees the sd_xbuf & returns the buf to its originator.
12279  *
12280  *     Context: May be called from interrupt context.
12281  */
12282 /* ARGSUSED */
12283 static void
12284 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12285 {
12286 	struct sd_xbuf *xp;
12287 
12288 	ASSERT(un != NULL);
12289 	ASSERT(bp != NULL);
12290 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12291 
12292 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12293 
12294 	xp = SD_GET_XBUF(bp);
12295 	ASSERT(xp != NULL);
12296 
12297 	mutex_enter(SD_MUTEX(un));
12298 
12299 	/*
12300 	 * Grab time when the cmd completed.
12301 	 * This is used for determining if the system has been
12302 	 * idle long enough to make it idle to the PM framework.
12303 	 * This is for lowering the overhead, and therefore improving
12304 	 * performance per I/O operation.
12305 	 */
12306 	un->un_pm_idle_time = ddi_get_time();
12307 
12308 	un->un_ncmds_in_driver--;
12309 	ASSERT(un->un_ncmds_in_driver >= 0);
12310 	SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12311 	    un->un_ncmds_in_driver);
12312 
12313 	mutex_exit(SD_MUTEX(un));
12314 
12315 	ddi_xbuf_done(bp, un->un_xbuf_attr);	/* xbuf is gone after this */
12316 	biodone(bp);				/* bp is gone after this */
12317 
12318 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12319 }
12320 
12321 
12322 /*
12323  *    Function: sd_uscsi_iodone
12324  *
12325  * Description: Frees the sd_xbuf & returns the buf to its originator.
12326  *
12327  *     Context: May be called from interrupt context.
12328  */
12329 /* ARGSUSED */
12330 static void
12331 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12332 {
12333 	struct sd_xbuf *xp;
12334 
12335 	ASSERT(un != NULL);
12336 	ASSERT(bp != NULL);
12337 
12338 	xp = SD_GET_XBUF(bp);
12339 	ASSERT(xp != NULL);
12340 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12341 
12342 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12343 
12344 	bp->b_private = xp->xb_private;
12345 
12346 	mutex_enter(SD_MUTEX(un));
12347 
12348 	/*
12349 	 * Grab time when the cmd completed.
12350 	 * This is used for determining if the system has been
12351 	 * idle long enough to make it idle to the PM framework.
12352 	 * This is for lowering the overhead, and therefore improving
12353 	 * performance per I/O operation.
12354 	 */
12355 	un->un_pm_idle_time = ddi_get_time();
12356 
12357 	un->un_ncmds_in_driver--;
12358 	ASSERT(un->un_ncmds_in_driver >= 0);
12359 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12360 	    un->un_ncmds_in_driver);
12361 
12362 	mutex_exit(SD_MUTEX(un));
12363 
12364 	kmem_free(xp, sizeof (struct sd_xbuf));
12365 	biodone(bp);
12366 
12367 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12368 }
12369 
12370 
12371 /*
12372  *    Function: sd_mapblockaddr_iostart
12373  *
12374  * Description: Verify request lies withing the partition limits for
12375  *		the indicated minor device.  Issue "overrun" buf if
12376  *		request would exceed partition range.  Converts
12377  *		partition-relative block address to absolute.
12378  *
12379  *     Context: Can sleep
12380  *
12381  *      Issues: This follows what the old code did, in terms of accessing
12382  *		some of the partition info in the unit struct without holding
12383  *		the mutext.  This is a general issue, if the partition info
12384  *		can be altered while IO is in progress... as soon as we send
12385  *		a buf, its partitioning can be invalid before it gets to the
12386  *		device.  Probably the right fix is to move partitioning out
12387  *		of the driver entirely.
12388  */
12389 
12390 static void
12391 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12392 {
12393 	daddr_t	nblocks;	/* #blocks in the given partition */
12394 	daddr_t	blocknum;	/* Block number specified by the buf */
12395 	size_t	requested_nblocks;
12396 	size_t	available_nblocks;
12397 	int	partition;
12398 	diskaddr_t	partition_offset;
12399 	struct sd_xbuf *xp;
12400 
12401 
12402 	ASSERT(un != NULL);
12403 	ASSERT(bp != NULL);
12404 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12405 
12406 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12407 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12408 
12409 	xp = SD_GET_XBUF(bp);
12410 	ASSERT(xp != NULL);
12411 
12412 	/*
12413 	 * If the geometry is not indicated as valid, attempt to access
12414 	 * the unit & verify the geometry/label. This can be the case for
12415 	 * removable-media devices, of if the device was opened in
12416 	 * NDELAY/NONBLOCK mode.
12417 	 */
12418 	if ((un->un_f_geometry_is_valid != TRUE) &&
12419 	    (sd_ready_and_valid(un) != SD_READY_VALID)) {
12420 		/*
12421 		 * For removable devices it is possible to start an I/O
12422 		 * without a media by opening the device in nodelay mode.
12423 		 * Also for writable CDs there can be many scenarios where
12424 		 * there is no geometry yet but volume manager is trying to
12425 		 * issue a read() just because it can see TOC on the CD. So
12426 		 * do not print a message for removables.
12427 		 */
12428 		if (!un->un_f_has_removable_media) {
12429 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12430 			    "i/o to invalid geometry\n");
12431 		}
12432 		bioerror(bp, EIO);
12433 		bp->b_resid = bp->b_bcount;
12434 		SD_BEGIN_IODONE(index, un, bp);
12435 		return;
12436 	}
12437 
12438 	partition = SDPART(bp->b_edev);
12439 
12440 	/* #blocks in partition */
12441 	nblocks = un->un_map[partition].dkl_nblk;    /* #blocks in partition */
12442 
12443 	/* Use of a local variable potentially improves performance slightly */
12444 	partition_offset = un->un_offset[partition];
12445 
12446 	/*
12447 	 * blocknum is the starting block number of the request. At this
12448 	 * point it is still relative to the start of the minor device.
12449 	 */
12450 	blocknum = xp->xb_blkno;
12451 
12452 	/*
12453 	 * Legacy: If the starting block number is one past the last block
12454 	 * in the partition, do not set B_ERROR in the buf.
12455 	 */
12456 	if (blocknum == nblocks)  {
12457 		goto error_exit;
12458 	}
12459 
12460 	/*
12461 	 * Confirm that the first block of the request lies within the
12462 	 * partition limits. Also the requested number of bytes must be
12463 	 * a multiple of the system block size.
12464 	 */
12465 	if ((blocknum < 0) || (blocknum >= nblocks) ||
12466 	    ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) {
12467 		bp->b_flags |= B_ERROR;
12468 		goto error_exit;
12469 	}
12470 
12471 	/*
12472 	 * If the requsted # blocks exceeds the available # blocks, that
12473 	 * is an overrun of the partition.
12474 	 */
12475 	requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount);
12476 	available_nblocks = (size_t)(nblocks - blocknum);
12477 	ASSERT(nblocks >= blocknum);
12478 
12479 	if (requested_nblocks > available_nblocks) {
12480 		/*
12481 		 * Allocate an "overrun" buf to allow the request to proceed
12482 		 * for the amount of space available in the partition. The
12483 		 * amount not transferred will be added into the b_resid
12484 		 * when the operation is complete. The overrun buf
12485 		 * replaces the original buf here, and the original buf
12486 		 * is saved inside the overrun buf, for later use.
12487 		 */
12488 		size_t resid = SD_SYSBLOCKS2BYTES(un,
12489 		    (offset_t)(requested_nblocks - available_nblocks));
12490 		size_t count = bp->b_bcount - resid;
12491 		/*
12492 		 * Note: count is an unsigned entity thus it'll NEVER
12493 		 * be less than 0 so ASSERT the original values are
12494 		 * correct.
12495 		 */
12496 		ASSERT(bp->b_bcount >= resid);
12497 
12498 		bp = sd_bioclone_alloc(bp, count, blocknum,
12499 			(int (*)(struct buf *)) sd_mapblockaddr_iodone);
12500 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12501 		ASSERT(xp != NULL);
12502 	}
12503 
12504 	/* At this point there should be no residual for this buf. */
12505 	ASSERT(bp->b_resid == 0);
12506 
12507 	/* Convert the block number to an absolute address. */
12508 	xp->xb_blkno += partition_offset;
12509 
12510 	SD_NEXT_IOSTART(index, un, bp);
12511 
12512 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12513 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12514 
12515 	return;
12516 
12517 error_exit:
12518 	bp->b_resid = bp->b_bcount;
12519 	SD_BEGIN_IODONE(index, un, bp);
12520 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12521 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12522 }
12523 
12524 
12525 /*
12526  *    Function: sd_mapblockaddr_iodone
12527  *
12528  * Description: Completion-side processing for partition management.
12529  *
12530  *     Context: May be called under interrupt context
12531  */
12532 
12533 static void
12534 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12535 {
12536 	/* int	partition; */	/* Not used, see below. */
12537 	ASSERT(un != NULL);
12538 	ASSERT(bp != NULL);
12539 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12540 
12541 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12542 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12543 
12544 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12545 		/*
12546 		 * We have an "overrun" buf to deal with...
12547 		 */
12548 		struct sd_xbuf	*xp;
12549 		struct buf	*obp;	/* ptr to the original buf */
12550 
12551 		xp = SD_GET_XBUF(bp);
12552 		ASSERT(xp != NULL);
12553 
12554 		/* Retrieve the pointer to the original buf */
12555 		obp = (struct buf *)xp->xb_private;
12556 		ASSERT(obp != NULL);
12557 
12558 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12559 		bioerror(obp, bp->b_error);
12560 
12561 		sd_bioclone_free(bp);
12562 
12563 		/*
12564 		 * Get back the original buf.
12565 		 * Note that since the restoration of xb_blkno below
12566 		 * was removed, the sd_xbuf is not needed.
12567 		 */
12568 		bp = obp;
12569 		/*
12570 		 * xp = SD_GET_XBUF(bp);
12571 		 * ASSERT(xp != NULL);
12572 		 */
12573 	}
12574 
12575 	/*
12576 	 * Convert sd->xb_blkno back to a minor-device relative value.
12577 	 * Note: this has been commented out, as it is not needed in the
12578 	 * current implementation of the driver (ie, since this function
12579 	 * is at the top of the layering chains, so the info will be
12580 	 * discarded) and it is in the "hot" IO path.
12581 	 *
12582 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
12583 	 * xp->xb_blkno -= un->un_offset[partition];
12584 	 */
12585 
12586 	SD_NEXT_IODONE(index, un, bp);
12587 
12588 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12589 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12590 }
12591 
12592 
12593 /*
12594  *    Function: sd_mapblocksize_iostart
12595  *
12596  * Description: Convert between system block size (un->un_sys_blocksize)
12597  *		and target block size (un->un_tgt_blocksize).
12598  *
12599  *     Context: Can sleep to allocate resources.
12600  *
12601  * Assumptions: A higher layer has already performed any partition validation,
12602  *		and converted the xp->xb_blkno to an absolute value relative
12603  *		to the start of the device.
12604  *
12605  *		It is also assumed that the higher layer has implemented
12606  *		an "overrun" mechanism for the case where the request would
12607  *		read/write beyond the end of a partition.  In this case we
12608  *		assume (and ASSERT) that bp->b_resid == 0.
12609  *
12610  *		Note: The implementation for this routine assumes the target
12611  *		block size remains constant between allocation and transport.
12612  */
12613 
12614 static void
12615 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12616 {
12617 	struct sd_mapblocksize_info	*bsp;
12618 	struct sd_xbuf			*xp;
12619 	offset_t first_byte;
12620 	daddr_t	start_block, end_block;
12621 	daddr_t	request_bytes;
12622 	ushort_t is_aligned = FALSE;
12623 
12624 	ASSERT(un != NULL);
12625 	ASSERT(bp != NULL);
12626 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12627 	ASSERT(bp->b_resid == 0);
12628 
12629 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12630 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12631 
12632 	/*
12633 	 * For a non-writable CD, a write request is an error
12634 	 */
12635 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12636 	    (un->un_f_mmc_writable_media == FALSE)) {
12637 		bioerror(bp, EIO);
12638 		bp->b_resid = bp->b_bcount;
12639 		SD_BEGIN_IODONE(index, un, bp);
12640 		return;
12641 	}
12642 
12643 	/*
12644 	 * We do not need a shadow buf if the device is using
12645 	 * un->un_sys_blocksize as its block size or if bcount == 0.
12646 	 * In this case there is no layer-private data block allocated.
12647 	 */
12648 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
12649 	    (bp->b_bcount == 0)) {
12650 		goto done;
12651 	}
12652 
12653 #if defined(__i386) || defined(__amd64)
12654 	/* We do not support non-block-aligned transfers for ROD devices */
12655 	ASSERT(!ISROD(un));
12656 #endif
12657 
12658 	xp = SD_GET_XBUF(bp);
12659 	ASSERT(xp != NULL);
12660 
12661 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12662 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12663 	    un->un_tgt_blocksize, un->un_sys_blocksize);
12664 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12665 	    "request start block:0x%x\n", xp->xb_blkno);
12666 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12667 	    "request len:0x%x\n", bp->b_bcount);
12668 
12669 	/*
12670 	 * Allocate the layer-private data area for the mapblocksize layer.
12671 	 * Layers are allowed to use the xp_private member of the sd_xbuf
12672 	 * struct to store the pointer to their layer-private data block, but
12673 	 * each layer also has the responsibility of restoring the prior
12674 	 * contents of xb_private before returning the buf/xbuf to the
12675 	 * higher layer that sent it.
12676 	 *
12677 	 * Here we save the prior contents of xp->xb_private into the
12678 	 * bsp->mbs_oprivate field of our layer-private data area. This value
12679 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12680 	 * the layer-private area and returning the buf/xbuf to the layer
12681 	 * that sent it.
12682 	 *
12683 	 * Note that here we use kmem_zalloc for the allocation as there are
12684 	 * parts of the mapblocksize code that expect certain fields to be
12685 	 * zero unless explicitly set to a required value.
12686 	 */
12687 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12688 	bsp->mbs_oprivate = xp->xb_private;
12689 	xp->xb_private = bsp;
12690 
12691 	/*
12692 	 * This treats the data on the disk (target) as an array of bytes.
12693 	 * first_byte is the byte offset, from the beginning of the device,
12694 	 * to the location of the request. This is converted from a
12695 	 * un->un_sys_blocksize block address to a byte offset, and then back
12696 	 * to a block address based upon a un->un_tgt_blocksize block size.
12697 	 *
12698 	 * xp->xb_blkno should be absolute upon entry into this function,
12699 	 * but, but it is based upon partitions that use the "system"
12700 	 * block size. It must be adjusted to reflect the block size of
12701 	 * the target.
12702 	 *
12703 	 * Note that end_block is actually the block that follows the last
12704 	 * block of the request, but that's what is needed for the computation.
12705 	 */
12706 	first_byte  = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12707 	start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12708 	end_block   = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) /
12709 	    un->un_tgt_blocksize;
12710 
12711 	/* request_bytes is rounded up to a multiple of the target block size */
12712 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12713 
12714 	/*
12715 	 * See if the starting address of the request and the request
12716 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12717 	 * then we do not need to allocate a shadow buf to handle the request.
12718 	 */
12719 	if (((first_byte   % un->un_tgt_blocksize) == 0) &&
12720 	    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12721 		is_aligned = TRUE;
12722 	}
12723 
12724 	if ((bp->b_flags & B_READ) == 0) {
12725 		/*
12726 		 * Lock the range for a write operation. An aligned request is
12727 		 * considered a simple write; otherwise the request must be a
12728 		 * read-modify-write.
12729 		 */
12730 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12731 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12732 	}
12733 
12734 	/*
12735 	 * Alloc a shadow buf if the request is not aligned. Also, this is
12736 	 * where the READ command is generated for a read-modify-write. (The
12737 	 * write phase is deferred until after the read completes.)
12738 	 */
12739 	if (is_aligned == FALSE) {
12740 
12741 		struct sd_mapblocksize_info	*shadow_bsp;
12742 		struct sd_xbuf	*shadow_xp;
12743 		struct buf	*shadow_bp;
12744 
12745 		/*
12746 		 * Allocate the shadow buf and it associated xbuf. Note that
12747 		 * after this call the xb_blkno value in both the original
12748 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12749 		 * same: absolute relative to the start of the device, and
12750 		 * adjusted for the target block size. The b_blkno in the
12751 		 * shadow buf will also be set to this value. We should never
12752 		 * change b_blkno in the original bp however.
12753 		 *
12754 		 * Note also that the shadow buf will always need to be a
12755 		 * READ command, regardless of whether the incoming command
12756 		 * is a READ or a WRITE.
12757 		 */
12758 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
12759 		    xp->xb_blkno,
12760 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
12761 
12762 		shadow_xp = SD_GET_XBUF(shadow_bp);
12763 
12764 		/*
12765 		 * Allocate the layer-private data for the shadow buf.
12766 		 * (No need to preserve xb_private in the shadow xbuf.)
12767 		 */
12768 		shadow_xp->xb_private = shadow_bsp =
12769 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12770 
12771 		/*
12772 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
12773 		 * to figure out where the start of the user data is (based upon
12774 		 * the system block size) in the data returned by the READ
12775 		 * command (which will be based upon the target blocksize). Note
12776 		 * that this is only really used if the request is unaligned.
12777 		 */
12778 		bsp->mbs_copy_offset = (ssize_t)(first_byte -
12779 		    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
12780 		ASSERT((bsp->mbs_copy_offset >= 0) &&
12781 		    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
12782 
12783 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
12784 
12785 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
12786 
12787 		/* Transfer the wmap (if any) to the shadow buf */
12788 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
12789 		bsp->mbs_wmp = NULL;
12790 
12791 		/*
12792 		 * The shadow buf goes on from here in place of the
12793 		 * original buf.
12794 		 */
12795 		shadow_bsp->mbs_orig_bp = bp;
12796 		bp = shadow_bp;
12797 	}
12798 
12799 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12800 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
12801 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12802 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
12803 	    request_bytes);
12804 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12805 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
12806 
12807 done:
12808 	SD_NEXT_IOSTART(index, un, bp);
12809 
12810 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12811 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
12812 }
12813 
12814 
12815 /*
12816  *    Function: sd_mapblocksize_iodone
12817  *
12818  * Description: Completion side processing for block-size mapping.
12819  *
12820  *     Context: May be called under interrupt context
12821  */
12822 
12823 static void
12824 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
12825 {
12826 	struct sd_mapblocksize_info	*bsp;
12827 	struct sd_xbuf	*xp;
12828 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
12829 	struct buf	*orig_bp;	/* ptr to the original buf */
12830 	offset_t	shadow_end;
12831 	offset_t	request_end;
12832 	offset_t	shadow_start;
12833 	ssize_t		copy_offset;
12834 	size_t		copy_length;
12835 	size_t		shortfall;
12836 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
12837 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
12838 
12839 	ASSERT(un != NULL);
12840 	ASSERT(bp != NULL);
12841 
12842 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12843 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
12844 
12845 	/*
12846 	 * There is no shadow buf or layer-private data if the target is
12847 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
12848 	 */
12849 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
12850 	    (bp->b_bcount == 0)) {
12851 		goto exit;
12852 	}
12853 
12854 	xp = SD_GET_XBUF(bp);
12855 	ASSERT(xp != NULL);
12856 
12857 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
12858 	bsp = xp->xb_private;
12859 
12860 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
12861 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
12862 
12863 	if (is_write) {
12864 		/*
12865 		 * For a WRITE request we must free up the block range that
12866 		 * we have locked up.  This holds regardless of whether this is
12867 		 * an aligned write request or a read-modify-write request.
12868 		 */
12869 		sd_range_unlock(un, bsp->mbs_wmp);
12870 		bsp->mbs_wmp = NULL;
12871 	}
12872 
12873 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
12874 		/*
12875 		 * An aligned read or write command will have no shadow buf;
12876 		 * there is not much else to do with it.
12877 		 */
12878 		goto done;
12879 	}
12880 
12881 	orig_bp = bsp->mbs_orig_bp;
12882 	ASSERT(orig_bp != NULL);
12883 	orig_xp = SD_GET_XBUF(orig_bp);
12884 	ASSERT(orig_xp != NULL);
12885 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12886 
12887 	if (!is_write && has_wmap) {
12888 		/*
12889 		 * A READ with a wmap means this is the READ phase of a
12890 		 * read-modify-write. If an error occurred on the READ then
12891 		 * we do not proceed with the WRITE phase or copy any data.
12892 		 * Just release the write maps and return with an error.
12893 		 */
12894 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
12895 			orig_bp->b_resid = orig_bp->b_bcount;
12896 			bioerror(orig_bp, bp->b_error);
12897 			sd_range_unlock(un, bsp->mbs_wmp);
12898 			goto freebuf_done;
12899 		}
12900 	}
12901 
12902 	/*
12903 	 * Here is where we set up to copy the data from the shadow buf
12904 	 * into the space associated with the original buf.
12905 	 *
12906 	 * To deal with the conversion between block sizes, these
12907 	 * computations treat the data as an array of bytes, with the
12908 	 * first byte (byte 0) corresponding to the first byte in the
12909 	 * first block on the disk.
12910 	 */
12911 
12912 	/*
12913 	 * shadow_start and shadow_len indicate the location and size of
12914 	 * the data returned with the shadow IO request.
12915 	 */
12916 	shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12917 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
12918 
12919 	/*
12920 	 * copy_offset gives the offset (in bytes) from the start of the first
12921 	 * block of the READ request to the beginning of the data.  We retrieve
12922 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
12923 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
12924 	 * data to be copied (in bytes).
12925 	 */
12926 	copy_offset  = bsp->mbs_copy_offset;
12927 	ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize));
12928 	copy_length  = orig_bp->b_bcount;
12929 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
12930 
12931 	/*
12932 	 * Set up the resid and error fields of orig_bp as appropriate.
12933 	 */
12934 	if (shadow_end >= request_end) {
12935 		/* We got all the requested data; set resid to zero */
12936 		orig_bp->b_resid = 0;
12937 	} else {
12938 		/*
12939 		 * We failed to get enough data to fully satisfy the original
12940 		 * request. Just copy back whatever data we got and set
12941 		 * up the residual and error code as required.
12942 		 *
12943 		 * 'shortfall' is the amount by which the data received with the
12944 		 * shadow buf has "fallen short" of the requested amount.
12945 		 */
12946 		shortfall = (size_t)(request_end - shadow_end);
12947 
12948 		if (shortfall > orig_bp->b_bcount) {
12949 			/*
12950 			 * We did not get enough data to even partially
12951 			 * fulfill the original request.  The residual is
12952 			 * equal to the amount requested.
12953 			 */
12954 			orig_bp->b_resid = orig_bp->b_bcount;
12955 		} else {
12956 			/*
12957 			 * We did not get all the data that we requested
12958 			 * from the device, but we will try to return what
12959 			 * portion we did get.
12960 			 */
12961 			orig_bp->b_resid = shortfall;
12962 		}
12963 		ASSERT(copy_length >= orig_bp->b_resid);
12964 		copy_length  -= orig_bp->b_resid;
12965 	}
12966 
12967 	/* Propagate the error code from the shadow buf to the original buf */
12968 	bioerror(orig_bp, bp->b_error);
12969 
12970 	if (is_write) {
12971 		goto freebuf_done;	/* No data copying for a WRITE */
12972 	}
12973 
12974 	if (has_wmap) {
12975 		/*
12976 		 * This is a READ command from the READ phase of a
12977 		 * read-modify-write request. We have to copy the data given
12978 		 * by the user OVER the data returned by the READ command,
12979 		 * then convert the command from a READ to a WRITE and send
12980 		 * it back to the target.
12981 		 */
12982 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
12983 		    copy_length);
12984 
12985 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
12986 
12987 		/*
12988 		 * Dispatch the WRITE command to the taskq thread, which
12989 		 * will in turn send the command to the target. When the
12990 		 * WRITE command completes, we (sd_mapblocksize_iodone())
12991 		 * will get called again as part of the iodone chain
12992 		 * processing for it. Note that we will still be dealing
12993 		 * with the shadow buf at that point.
12994 		 */
12995 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
12996 		    KM_NOSLEEP) != 0) {
12997 			/*
12998 			 * Dispatch was successful so we are done. Return
12999 			 * without going any higher up the iodone chain. Do
13000 			 * not free up any layer-private data until after the
13001 			 * WRITE completes.
13002 			 */
13003 			return;
13004 		}
13005 
13006 		/*
13007 		 * Dispatch of the WRITE command failed; set up the error
13008 		 * condition and send this IO back up the iodone chain.
13009 		 */
13010 		bioerror(orig_bp, EIO);
13011 		orig_bp->b_resid = orig_bp->b_bcount;
13012 
13013 	} else {
13014 		/*
13015 		 * This is a regular READ request (ie, not a RMW). Copy the
13016 		 * data from the shadow buf into the original buf. The
13017 		 * copy_offset compensates for any "misalignment" between the
13018 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
13019 		 * original buf (with its un->un_sys_blocksize blocks).
13020 		 */
13021 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13022 		    copy_length);
13023 	}
13024 
13025 freebuf_done:
13026 
13027 	/*
13028 	 * At this point we still have both the shadow buf AND the original
13029 	 * buf to deal with, as well as the layer-private data area in each.
13030 	 * Local variables are as follows:
13031 	 *
13032 	 * bp -- points to shadow buf
13033 	 * xp -- points to xbuf of shadow buf
13034 	 * bsp -- points to layer-private data area of shadow buf
13035 	 * orig_bp -- points to original buf
13036 	 *
13037 	 * First free the shadow buf and its associated xbuf, then free the
13038 	 * layer-private data area from the shadow buf. There is no need to
13039 	 * restore xb_private in the shadow xbuf.
13040 	 */
13041 	sd_shadow_buf_free(bp);
13042 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13043 
13044 	/*
13045 	 * Now update the local variables to point to the original buf, xbuf,
13046 	 * and layer-private area.
13047 	 */
13048 	bp = orig_bp;
13049 	xp = SD_GET_XBUF(bp);
13050 	ASSERT(xp != NULL);
13051 	ASSERT(xp == orig_xp);
13052 	bsp = xp->xb_private;
13053 	ASSERT(bsp != NULL);
13054 
13055 done:
13056 	/*
13057 	 * Restore xb_private to whatever it was set to by the next higher
13058 	 * layer in the chain, then free the layer-private data area.
13059 	 */
13060 	xp->xb_private = bsp->mbs_oprivate;
13061 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13062 
13063 exit:
13064 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13065 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13066 
13067 	SD_NEXT_IODONE(index, un, bp);
13068 }
13069 
13070 
13071 /*
13072  *    Function: sd_checksum_iostart
13073  *
13074  * Description: A stub function for a layer that's currently not used.
13075  *		For now just a placeholder.
13076  *
13077  *     Context: Kernel thread context
13078  */
13079 
13080 static void
13081 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13082 {
13083 	ASSERT(un != NULL);
13084 	ASSERT(bp != NULL);
13085 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13086 	SD_NEXT_IOSTART(index, un, bp);
13087 }
13088 
13089 
13090 /*
13091  *    Function: sd_checksum_iodone
13092  *
13093  * Description: A stub function for a layer that's currently not used.
13094  *		For now just a placeholder.
13095  *
13096  *     Context: May be called under interrupt context
13097  */
13098 
13099 static void
13100 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13101 {
13102 	ASSERT(un != NULL);
13103 	ASSERT(bp != NULL);
13104 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13105 	SD_NEXT_IODONE(index, un, bp);
13106 }
13107 
13108 
13109 /*
13110  *    Function: sd_checksum_uscsi_iostart
13111  *
13112  * Description: A stub function for a layer that's currently not used.
13113  *		For now just a placeholder.
13114  *
13115  *     Context: Kernel thread context
13116  */
13117 
13118 static void
13119 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13120 {
13121 	ASSERT(un != NULL);
13122 	ASSERT(bp != NULL);
13123 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13124 	SD_NEXT_IOSTART(index, un, bp);
13125 }
13126 
13127 
13128 /*
13129  *    Function: sd_checksum_uscsi_iodone
13130  *
13131  * Description: A stub function for a layer that's currently not used.
13132  *		For now just a placeholder.
13133  *
13134  *     Context: May be called under interrupt context
13135  */
13136 
13137 static void
13138 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13139 {
13140 	ASSERT(un != NULL);
13141 	ASSERT(bp != NULL);
13142 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13143 	SD_NEXT_IODONE(index, un, bp);
13144 }
13145 
13146 
13147 /*
13148  *    Function: sd_pm_iostart
13149  *
13150  * Description: iostart-side routine for Power mangement.
13151  *
13152  *     Context: Kernel thread context
13153  */
13154 
13155 static void
13156 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13157 {
13158 	ASSERT(un != NULL);
13159 	ASSERT(bp != NULL);
13160 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13161 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13162 
13163 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13164 
13165 	if (sd_pm_entry(un) != DDI_SUCCESS) {
13166 		/*
13167 		 * Set up to return the failed buf back up the 'iodone'
13168 		 * side of the calling chain.
13169 		 */
13170 		bioerror(bp, EIO);
13171 		bp->b_resid = bp->b_bcount;
13172 
13173 		SD_BEGIN_IODONE(index, un, bp);
13174 
13175 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13176 		return;
13177 	}
13178 
13179 	SD_NEXT_IOSTART(index, un, bp);
13180 
13181 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13182 }
13183 
13184 
13185 /*
13186  *    Function: sd_pm_iodone
13187  *
13188  * Description: iodone-side routine for power mangement.
13189  *
13190  *     Context: may be called from interrupt context
13191  */
13192 
13193 static void
13194 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13195 {
13196 	ASSERT(un != NULL);
13197 	ASSERT(bp != NULL);
13198 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13199 
13200 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13201 
13202 	/*
13203 	 * After attach the following flag is only read, so don't
13204 	 * take the penalty of acquiring a mutex for it.
13205 	 */
13206 	if (un->un_f_pm_is_enabled == TRUE) {
13207 		sd_pm_exit(un);
13208 	}
13209 
13210 	SD_NEXT_IODONE(index, un, bp);
13211 
13212 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13213 }
13214 
13215 
13216 /*
13217  *    Function: sd_core_iostart
13218  *
13219  * Description: Primary driver function for enqueuing buf(9S) structs from
13220  *		the system and initiating IO to the target device
13221  *
13222  *     Context: Kernel thread context. Can sleep.
13223  *
13224  * Assumptions:  - The given xp->xb_blkno is absolute
13225  *		   (ie, relative to the start of the device).
13226  *		 - The IO is to be done using the native blocksize of
13227  *		   the device, as specified in un->un_tgt_blocksize.
13228  */
13229 /* ARGSUSED */
13230 static void
13231 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13232 {
13233 	struct sd_xbuf *xp;
13234 
13235 	ASSERT(un != NULL);
13236 	ASSERT(bp != NULL);
13237 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13238 	ASSERT(bp->b_resid == 0);
13239 
13240 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13241 
13242 	xp = SD_GET_XBUF(bp);
13243 	ASSERT(xp != NULL);
13244 
13245 	mutex_enter(SD_MUTEX(un));
13246 
13247 	/*
13248 	 * If we are currently in the failfast state, fail any new IO
13249 	 * that has B_FAILFAST set, then return.
13250 	 */
13251 	if ((bp->b_flags & B_FAILFAST) &&
13252 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13253 		mutex_exit(SD_MUTEX(un));
13254 		bioerror(bp, EIO);
13255 		bp->b_resid = bp->b_bcount;
13256 		SD_BEGIN_IODONE(index, un, bp);
13257 		return;
13258 	}
13259 
13260 	if (SD_IS_DIRECT_PRIORITY(xp)) {
13261 		/*
13262 		 * Priority command -- transport it immediately.
13263 		 *
13264 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13265 		 * because all direct priority commands should be associated
13266 		 * with error recovery actions which we don't want to retry.
13267 		 */
13268 		sd_start_cmds(un, bp);
13269 	} else {
13270 		/*
13271 		 * Normal command -- add it to the wait queue, then start
13272 		 * transporting commands from the wait queue.
13273 		 */
13274 		sd_add_buf_to_waitq(un, bp);
13275 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13276 		sd_start_cmds(un, NULL);
13277 	}
13278 
13279 	mutex_exit(SD_MUTEX(un));
13280 
13281 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13282 }
13283 
13284 
13285 /*
13286  *    Function: sd_init_cdb_limits
13287  *
13288  * Description: This is to handle scsi_pkt initialization differences
13289  *		between the driver platforms.
13290  *
13291  *		Legacy behaviors:
13292  *
13293  *		If the block number or the sector count exceeds the
13294  *		capabilities of a Group 0 command, shift over to a
13295  *		Group 1 command. We don't blindly use Group 1
13296  *		commands because a) some drives (CDC Wren IVs) get a
13297  *		bit confused, and b) there is probably a fair amount
13298  *		of speed difference for a target to receive and decode
13299  *		a 10 byte command instead of a 6 byte command.
13300  *
13301  *		The xfer time difference of 6 vs 10 byte CDBs is
13302  *		still significant so this code is still worthwhile.
13303  *		10 byte CDBs are very inefficient with the fas HBA driver
13304  *		and older disks. Each CDB byte took 1 usec with some
13305  *		popular disks.
13306  *
13307  *     Context: Must be called at attach time
13308  */
13309 
13310 static void
13311 sd_init_cdb_limits(struct sd_lun *un)
13312 {
13313 	int hba_cdb_limit;
13314 
13315 	/*
13316 	 * Use CDB_GROUP1 commands for most devices except for
13317 	 * parallel SCSI fixed drives in which case we get better
13318 	 * performance using CDB_GROUP0 commands (where applicable).
13319 	 */
13320 	un->un_mincdb = SD_CDB_GROUP1;
13321 #if !defined(__fibre)
13322 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13323 	    !un->un_f_has_removable_media) {
13324 		un->un_mincdb = SD_CDB_GROUP0;
13325 	}
13326 #endif
13327 
13328 	/*
13329 	 * Try to read the max-cdb-length supported by HBA.
13330 	 */
13331 	un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13332 	if (0 >= un->un_max_hba_cdb) {
13333 		un->un_max_hba_cdb = CDB_GROUP4;
13334 		hba_cdb_limit = SD_CDB_GROUP4;
13335 	} else if (0 < un->un_max_hba_cdb &&
13336 	    un->un_max_hba_cdb < CDB_GROUP1) {
13337 		hba_cdb_limit = SD_CDB_GROUP0;
13338 	} else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13339 	    un->un_max_hba_cdb < CDB_GROUP5) {
13340 		hba_cdb_limit = SD_CDB_GROUP1;
13341 	} else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13342 	    un->un_max_hba_cdb < CDB_GROUP4) {
13343 		hba_cdb_limit = SD_CDB_GROUP5;
13344 	} else {
13345 		hba_cdb_limit = SD_CDB_GROUP4;
13346 	}
13347 
13348 	/*
13349 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13350 	 * commands for fixed disks unless we are building for a 32 bit
13351 	 * kernel.
13352 	 */
13353 #ifdef _LP64
13354 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13355 	    min(hba_cdb_limit, SD_CDB_GROUP4);
13356 #else
13357 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13358 	    min(hba_cdb_limit, SD_CDB_GROUP1);
13359 #endif
13360 
13361 	/*
13362 	 * x86 systems require the PKT_DMA_PARTIAL flag
13363 	 */
13364 #if defined(__x86)
13365 	un->un_pkt_flags = PKT_DMA_PARTIAL;
13366 #else
13367 	un->un_pkt_flags = 0;
13368 #endif
13369 
13370 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13371 	    ? sizeof (struct scsi_arq_status) : 1);
13372 	un->un_cmd_timeout = (ushort_t)sd_io_time;
13373 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13374 }
13375 
13376 
13377 /*
13378  *    Function: sd_initpkt_for_buf
13379  *
13380  * Description: Allocate and initialize for transport a scsi_pkt struct,
13381  *		based upon the info specified in the given buf struct.
13382  *
13383  *		Assumes the xb_blkno in the request is absolute (ie,
13384  *		relative to the start of the device (NOT partition!).
13385  *		Also assumes that the request is using the native block
13386  *		size of the device (as returned by the READ CAPACITY
13387  *		command).
13388  *
13389  * Return Code: SD_PKT_ALLOC_SUCCESS
13390  *		SD_PKT_ALLOC_FAILURE
13391  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13392  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13393  *
13394  *     Context: Kernel thread and may be called from software interrupt context
13395  *		as part of a sdrunout callback. This function may not block or
13396  *		call routines that block
13397  */
13398 
13399 static int
13400 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13401 {
13402 	struct sd_xbuf	*xp;
13403 	struct scsi_pkt *pktp = NULL;
13404 	struct sd_lun	*un;
13405 	size_t		blockcount;
13406 	daddr_t		startblock;
13407 	int		rval;
13408 	int		cmd_flags;
13409 
13410 	ASSERT(bp != NULL);
13411 	ASSERT(pktpp != NULL);
13412 	xp = SD_GET_XBUF(bp);
13413 	ASSERT(xp != NULL);
13414 	un = SD_GET_UN(bp);
13415 	ASSERT(un != NULL);
13416 	ASSERT(mutex_owned(SD_MUTEX(un)));
13417 	ASSERT(bp->b_resid == 0);
13418 
13419 	SD_TRACE(SD_LOG_IO_CORE, un,
13420 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13421 
13422 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13423 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13424 		/*
13425 		 * Already have a scsi_pkt -- just need DMA resources.
13426 		 * We must recompute the CDB in case the mapping returns
13427 		 * a nonzero pkt_resid.
13428 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13429 		 * that is being retried, the unmap/remap of the DMA resouces
13430 		 * will result in the entire transfer starting over again
13431 		 * from the very first block.
13432 		 */
13433 		ASSERT(xp->xb_pktp != NULL);
13434 		pktp = xp->xb_pktp;
13435 	} else {
13436 		pktp = NULL;
13437 	}
13438 #endif /* __i386 || __amd64 */
13439 
13440 	startblock = xp->xb_blkno;	/* Absolute block num. */
13441 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13442 
13443 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13444 
13445 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13446 
13447 #else
13448 
13449 	cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags;
13450 
13451 #endif
13452 
13453 	/*
13454 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13455 	 * call scsi_init_pkt, and build the CDB.
13456 	 */
13457 	rval = sd_setup_rw_pkt(un, &pktp, bp,
13458 	    cmd_flags, sdrunout, (caddr_t)un,
13459 	    startblock, blockcount);
13460 
13461 	if (rval == 0) {
13462 		/*
13463 		 * Success.
13464 		 *
13465 		 * If partial DMA is being used and required for this transfer.
13466 		 * set it up here.
13467 		 */
13468 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13469 		    (pktp->pkt_resid != 0)) {
13470 
13471 			/*
13472 			 * Save the CDB length and pkt_resid for the
13473 			 * next xfer
13474 			 */
13475 			xp->xb_dma_resid = pktp->pkt_resid;
13476 
13477 			/* rezero resid */
13478 			pktp->pkt_resid = 0;
13479 
13480 		} else {
13481 			xp->xb_dma_resid = 0;
13482 		}
13483 
13484 		pktp->pkt_flags = un->un_tagflags;
13485 		pktp->pkt_time  = un->un_cmd_timeout;
13486 		pktp->pkt_comp  = sdintr;
13487 
13488 		pktp->pkt_private = bp;
13489 		*pktpp = pktp;
13490 
13491 		SD_TRACE(SD_LOG_IO_CORE, un,
13492 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13493 
13494 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13495 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13496 #endif
13497 
13498 		return (SD_PKT_ALLOC_SUCCESS);
13499 
13500 	}
13501 
13502 	/*
13503 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13504 	 * from sd_setup_rw_pkt.
13505 	 */
13506 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13507 
13508 	if (rval == SD_PKT_ALLOC_FAILURE) {
13509 		*pktpp = NULL;
13510 		/*
13511 		 * Set the driver state to RWAIT to indicate the driver
13512 		 * is waiting on resource allocations. The driver will not
13513 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13514 		 */
13515 		New_state(un, SD_STATE_RWAIT);
13516 
13517 		SD_ERROR(SD_LOG_IO_CORE, un,
13518 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13519 
13520 		if ((bp->b_flags & B_ERROR) != 0) {
13521 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13522 		}
13523 		return (SD_PKT_ALLOC_FAILURE);
13524 	} else {
13525 		/*
13526 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13527 		 *
13528 		 * This should never happen.  Maybe someone messed with the
13529 		 * kernel's minphys?
13530 		 */
13531 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13532 		    "Request rejected: too large for CDB: "
13533 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13534 		SD_ERROR(SD_LOG_IO_CORE, un,
13535 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13536 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13537 
13538 	}
13539 }
13540 
13541 
13542 /*
13543  *    Function: sd_destroypkt_for_buf
13544  *
13545  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13546  *
13547  *     Context: Kernel thread or interrupt context
13548  */
13549 
13550 static void
13551 sd_destroypkt_for_buf(struct buf *bp)
13552 {
13553 	ASSERT(bp != NULL);
13554 	ASSERT(SD_GET_UN(bp) != NULL);
13555 
13556 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13557 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13558 
13559 	ASSERT(SD_GET_PKTP(bp) != NULL);
13560 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13561 
13562 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13563 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13564 }
13565 
13566 /*
13567  *    Function: sd_setup_rw_pkt
13568  *
13569  * Description: Determines appropriate CDB group for the requested LBA
13570  *		and transfer length, calls scsi_init_pkt, and builds
13571  *		the CDB.  Do not use for partial DMA transfers except
13572  *		for the initial transfer since the CDB size must
13573  *		remain constant.
13574  *
13575  *     Context: Kernel thread and may be called from software interrupt
13576  *		context as part of a sdrunout callback. This function may not
13577  *		block or call routines that block
13578  */
13579 
13580 
13581 int
13582 sd_setup_rw_pkt(struct sd_lun *un,
13583     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13584     int (*callback)(caddr_t), caddr_t callback_arg,
13585     diskaddr_t lba, uint32_t blockcount)
13586 {
13587 	struct scsi_pkt *return_pktp;
13588 	union scsi_cdb *cdbp;
13589 	struct sd_cdbinfo *cp = NULL;
13590 	int i;
13591 
13592 	/*
13593 	 * See which size CDB to use, based upon the request.
13594 	 */
13595 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13596 
13597 		/*
13598 		 * Check lba and block count against sd_cdbtab limits.
13599 		 * In the partial DMA case, we have to use the same size
13600 		 * CDB for all the transfers.  Check lba + blockcount
13601 		 * against the max LBA so we know that segment of the
13602 		 * transfer can use the CDB we select.
13603 		 */
13604 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13605 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13606 
13607 			/*
13608 			 * The command will fit into the CDB type
13609 			 * specified by sd_cdbtab[i].
13610 			 */
13611 			cp = sd_cdbtab + i;
13612 
13613 			/*
13614 			 * Call scsi_init_pkt so we can fill in the
13615 			 * CDB.
13616 			 */
13617 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13618 			    bp, cp->sc_grpcode, un->un_status_len, 0,
13619 			    flags, callback, callback_arg);
13620 
13621 			if (return_pktp != NULL) {
13622 
13623 				/*
13624 				 * Return new value of pkt
13625 				 */
13626 				*pktpp = return_pktp;
13627 
13628 				/*
13629 				 * To be safe, zero the CDB insuring there is
13630 				 * no leftover data from a previous command.
13631 				 */
13632 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13633 
13634 				/*
13635 				 * Handle partial DMA mapping
13636 				 */
13637 				if (return_pktp->pkt_resid != 0) {
13638 
13639 					/*
13640 					 * Not going to xfer as many blocks as
13641 					 * originally expected
13642 					 */
13643 					blockcount -=
13644 					    SD_BYTES2TGTBLOCKS(un,
13645 						return_pktp->pkt_resid);
13646 				}
13647 
13648 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13649 
13650 				/*
13651 				 * Set command byte based on the CDB
13652 				 * type we matched.
13653 				 */
13654 				cdbp->scc_cmd = cp->sc_grpmask |
13655 				    ((bp->b_flags & B_READ) ?
13656 					SCMD_READ : SCMD_WRITE);
13657 
13658 				SD_FILL_SCSI1_LUN(un, return_pktp);
13659 
13660 				/*
13661 				 * Fill in LBA and length
13662 				 */
13663 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13664 				    (cp->sc_grpcode == CDB_GROUP4) ||
13665 				    (cp->sc_grpcode == CDB_GROUP0) ||
13666 				    (cp->sc_grpcode == CDB_GROUP5));
13667 
13668 				if (cp->sc_grpcode == CDB_GROUP1) {
13669 					FORMG1ADDR(cdbp, lba);
13670 					FORMG1COUNT(cdbp, blockcount);
13671 					return (0);
13672 				} else if (cp->sc_grpcode == CDB_GROUP4) {
13673 					FORMG4LONGADDR(cdbp, lba);
13674 					FORMG4COUNT(cdbp, blockcount);
13675 					return (0);
13676 				} else if (cp->sc_grpcode == CDB_GROUP0) {
13677 					FORMG0ADDR(cdbp, lba);
13678 					FORMG0COUNT(cdbp, blockcount);
13679 					return (0);
13680 				} else if (cp->sc_grpcode == CDB_GROUP5) {
13681 					FORMG5ADDR(cdbp, lba);
13682 					FORMG5COUNT(cdbp, blockcount);
13683 					return (0);
13684 				}
13685 
13686 				/*
13687 				 * It should be impossible to not match one
13688 				 * of the CDB types above, so we should never
13689 				 * reach this point.  Set the CDB command byte
13690 				 * to test-unit-ready to avoid writing
13691 				 * to somewhere we don't intend.
13692 				 */
13693 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13694 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13695 			} else {
13696 				/*
13697 				 * Couldn't get scsi_pkt
13698 				 */
13699 				return (SD_PKT_ALLOC_FAILURE);
13700 			}
13701 		}
13702 	}
13703 
13704 	/*
13705 	 * None of the available CDB types were suitable.  This really
13706 	 * should never happen:  on a 64 bit system we support
13707 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
13708 	 * and on a 32 bit system we will refuse to bind to a device
13709 	 * larger than 2TB so addresses will never be larger than 32 bits.
13710 	 */
13711 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13712 }
13713 
13714 #if defined(__i386) || defined(__amd64)
13715 /*
13716  *    Function: sd_setup_next_rw_pkt
13717  *
13718  * Description: Setup packet for partial DMA transfers, except for the
13719  * 		initial transfer.  sd_setup_rw_pkt should be used for
13720  *		the initial transfer.
13721  *
13722  *     Context: Kernel thread and may be called from interrupt context.
13723  */
13724 
13725 int
13726 sd_setup_next_rw_pkt(struct sd_lun *un,
13727     struct scsi_pkt *pktp, struct buf *bp,
13728     diskaddr_t lba, uint32_t blockcount)
13729 {
13730 	uchar_t com;
13731 	union scsi_cdb *cdbp;
13732 	uchar_t cdb_group_id;
13733 
13734 	ASSERT(pktp != NULL);
13735 	ASSERT(pktp->pkt_cdbp != NULL);
13736 
13737 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13738 	com = cdbp->scc_cmd;
13739 	cdb_group_id = CDB_GROUPID(com);
13740 
13741 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13742 	    (cdb_group_id == CDB_GROUPID_1) ||
13743 	    (cdb_group_id == CDB_GROUPID_4) ||
13744 	    (cdb_group_id == CDB_GROUPID_5));
13745 
13746 	/*
13747 	 * Move pkt to the next portion of the xfer.
13748 	 * func is NULL_FUNC so we do not have to release
13749 	 * the disk mutex here.
13750 	 */
13751 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13752 	    NULL_FUNC, NULL) == pktp) {
13753 		/* Success.  Handle partial DMA */
13754 		if (pktp->pkt_resid != 0) {
13755 			blockcount -=
13756 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
13757 		}
13758 
13759 		cdbp->scc_cmd = com;
13760 		SD_FILL_SCSI1_LUN(un, pktp);
13761 		if (cdb_group_id == CDB_GROUPID_1) {
13762 			FORMG1ADDR(cdbp, lba);
13763 			FORMG1COUNT(cdbp, blockcount);
13764 			return (0);
13765 		} else if (cdb_group_id == CDB_GROUPID_4) {
13766 			FORMG4LONGADDR(cdbp, lba);
13767 			FORMG4COUNT(cdbp, blockcount);
13768 			return (0);
13769 		} else if (cdb_group_id == CDB_GROUPID_0) {
13770 			FORMG0ADDR(cdbp, lba);
13771 			FORMG0COUNT(cdbp, blockcount);
13772 			return (0);
13773 		} else if (cdb_group_id == CDB_GROUPID_5) {
13774 			FORMG5ADDR(cdbp, lba);
13775 			FORMG5COUNT(cdbp, blockcount);
13776 			return (0);
13777 		}
13778 
13779 		/* Unreachable */
13780 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13781 	}
13782 
13783 	/*
13784 	 * Error setting up next portion of cmd transfer.
13785 	 * Something is definitely very wrong and this
13786 	 * should not happen.
13787 	 */
13788 	return (SD_PKT_ALLOC_FAILURE);
13789 }
13790 #endif /* defined(__i386) || defined(__amd64) */
13791 
13792 /*
13793  *    Function: sd_initpkt_for_uscsi
13794  *
13795  * Description: Allocate and initialize for transport a scsi_pkt struct,
13796  *		based upon the info specified in the given uscsi_cmd struct.
13797  *
13798  * Return Code: SD_PKT_ALLOC_SUCCESS
13799  *		SD_PKT_ALLOC_FAILURE
13800  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13801  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13802  *
13803  *     Context: Kernel thread and may be called from software interrupt context
13804  *		as part of a sdrunout callback. This function may not block or
13805  *		call routines that block
13806  */
13807 
13808 static int
13809 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
13810 {
13811 	struct uscsi_cmd *uscmd;
13812 	struct sd_xbuf	*xp;
13813 	struct scsi_pkt	*pktp;
13814 	struct sd_lun	*un;
13815 	uint32_t	flags = 0;
13816 
13817 	ASSERT(bp != NULL);
13818 	ASSERT(pktpp != NULL);
13819 	xp = SD_GET_XBUF(bp);
13820 	ASSERT(xp != NULL);
13821 	un = SD_GET_UN(bp);
13822 	ASSERT(un != NULL);
13823 	ASSERT(mutex_owned(SD_MUTEX(un)));
13824 
13825 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13826 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13827 	ASSERT(uscmd != NULL);
13828 
13829 	SD_TRACE(SD_LOG_IO_CORE, un,
13830 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
13831 
13832 	/*
13833 	 * Allocate the scsi_pkt for the command.
13834 	 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
13835 	 *	 during scsi_init_pkt time and will continue to use the
13836 	 *	 same path as long as the same scsi_pkt is used without
13837 	 *	 intervening scsi_dma_free(). Since uscsi command does
13838 	 *	 not call scsi_dmafree() before retry failed command, it
13839 	 *	 is necessary to make sure PKT_DMA_PARTIAL flag is NOT
13840 	 *	 set such that scsi_vhci can use other available path for
13841 	 *	 retry. Besides, ucsci command does not allow DMA breakup,
13842 	 *	 so there is no need to set PKT_DMA_PARTIAL flag.
13843 	 */
13844 	pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
13845 	    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
13846 	    sizeof (struct scsi_arq_status), 0,
13847 	    (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
13848 	    sdrunout, (caddr_t)un);
13849 
13850 	if (pktp == NULL) {
13851 		*pktpp = NULL;
13852 		/*
13853 		 * Set the driver state to RWAIT to indicate the driver
13854 		 * is waiting on resource allocations. The driver will not
13855 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13856 		 */
13857 		New_state(un, SD_STATE_RWAIT);
13858 
13859 		SD_ERROR(SD_LOG_IO_CORE, un,
13860 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
13861 
13862 		if ((bp->b_flags & B_ERROR) != 0) {
13863 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13864 		}
13865 		return (SD_PKT_ALLOC_FAILURE);
13866 	}
13867 
13868 	/*
13869 	 * We do not do DMA breakup for USCSI commands, so return failure
13870 	 * here if all the needed DMA resources were not allocated.
13871 	 */
13872 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
13873 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
13874 		scsi_destroy_pkt(pktp);
13875 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
13876 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
13877 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
13878 	}
13879 
13880 	/* Init the cdb from the given uscsi struct */
13881 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
13882 	    uscmd->uscsi_cdb[0], 0, 0, 0);
13883 
13884 	SD_FILL_SCSI1_LUN(un, pktp);
13885 
13886 	/*
13887 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
13888 	 * for listing of the supported flags.
13889 	 */
13890 
13891 	if (uscmd->uscsi_flags & USCSI_SILENT) {
13892 		flags |= FLAG_SILENT;
13893 	}
13894 
13895 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
13896 		flags |= FLAG_DIAGNOSE;
13897 	}
13898 
13899 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
13900 		flags |= FLAG_ISOLATE;
13901 	}
13902 
13903 	if (un->un_f_is_fibre == FALSE) {
13904 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
13905 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
13906 		}
13907 	}
13908 
13909 	/*
13910 	 * Set the pkt flags here so we save time later.
13911 	 * Note: These flags are NOT in the uscsi man page!!!
13912 	 */
13913 	if (uscmd->uscsi_flags & USCSI_HEAD) {
13914 		flags |= FLAG_HEAD;
13915 	}
13916 
13917 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
13918 		flags |= FLAG_NOINTR;
13919 	}
13920 
13921 	/*
13922 	 * For tagged queueing, things get a bit complicated.
13923 	 * Check first for head of queue and last for ordered queue.
13924 	 * If neither head nor order, use the default driver tag flags.
13925 	 */
13926 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
13927 		if (uscmd->uscsi_flags & USCSI_HTAG) {
13928 			flags |= FLAG_HTAG;
13929 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
13930 			flags |= FLAG_OTAG;
13931 		} else {
13932 			flags |= un->un_tagflags & FLAG_TAGMASK;
13933 		}
13934 	}
13935 
13936 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
13937 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
13938 	}
13939 
13940 	pktp->pkt_flags = flags;
13941 
13942 	/* Copy the caller's CDB into the pkt... */
13943 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
13944 
13945 	if (uscmd->uscsi_timeout == 0) {
13946 		pktp->pkt_time = un->un_uscsi_timeout;
13947 	} else {
13948 		pktp->pkt_time = uscmd->uscsi_timeout;
13949 	}
13950 
13951 	/* need it later to identify USCSI request in sdintr */
13952 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
13953 
13954 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
13955 
13956 	pktp->pkt_private = bp;
13957 	pktp->pkt_comp = sdintr;
13958 	*pktpp = pktp;
13959 
13960 	SD_TRACE(SD_LOG_IO_CORE, un,
13961 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
13962 
13963 	return (SD_PKT_ALLOC_SUCCESS);
13964 }
13965 
13966 
13967 /*
13968  *    Function: sd_destroypkt_for_uscsi
13969  *
13970  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
13971  *		IOs.. Also saves relevant info into the associated uscsi_cmd
13972  *		struct.
13973  *
13974  *     Context: May be called under interrupt context
13975  */
13976 
13977 static void
13978 sd_destroypkt_for_uscsi(struct buf *bp)
13979 {
13980 	struct uscsi_cmd *uscmd;
13981 	struct sd_xbuf	*xp;
13982 	struct scsi_pkt	*pktp;
13983 	struct sd_lun	*un;
13984 
13985 	ASSERT(bp != NULL);
13986 	xp = SD_GET_XBUF(bp);
13987 	ASSERT(xp != NULL);
13988 	un = SD_GET_UN(bp);
13989 	ASSERT(un != NULL);
13990 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13991 	pktp = SD_GET_PKTP(bp);
13992 	ASSERT(pktp != NULL);
13993 
13994 	SD_TRACE(SD_LOG_IO_CORE, un,
13995 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
13996 
13997 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13998 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13999 	ASSERT(uscmd != NULL);
14000 
14001 	/* Save the status and the residual into the uscsi_cmd struct */
14002 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14003 	uscmd->uscsi_resid  = bp->b_resid;
14004 
14005 	/*
14006 	 * If enabled, copy any saved sense data into the area specified
14007 	 * by the uscsi command.
14008 	 */
14009 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14010 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14011 		/*
14012 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
14013 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14014 		 */
14015 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
14016 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
14017 		bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH);
14018 	}
14019 
14020 	/* We are done with the scsi_pkt; free it now */
14021 	ASSERT(SD_GET_PKTP(bp) != NULL);
14022 	scsi_destroy_pkt(SD_GET_PKTP(bp));
14023 
14024 	SD_TRACE(SD_LOG_IO_CORE, un,
14025 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14026 }
14027 
14028 
14029 /*
14030  *    Function: sd_bioclone_alloc
14031  *
14032  * Description: Allocate a buf(9S) and init it as per the given buf
14033  *		and the various arguments.  The associated sd_xbuf
14034  *		struct is (nearly) duplicated.  The struct buf *bp
14035  *		argument is saved in new_xp->xb_private.
14036  *
14037  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14038  *		datalen - size of data area for the shadow bp
14039  *		blkno - starting LBA
14040  *		func - function pointer for b_iodone in the shadow buf. (May
14041  *			be NULL if none.)
14042  *
14043  * Return Code: Pointer to allocates buf(9S) struct
14044  *
14045  *     Context: Can sleep.
14046  */
14047 
14048 static struct buf *
14049 sd_bioclone_alloc(struct buf *bp, size_t datalen,
14050 	daddr_t blkno, int (*func)(struct buf *))
14051 {
14052 	struct	sd_lun	*un;
14053 	struct	sd_xbuf	*xp;
14054 	struct	sd_xbuf	*new_xp;
14055 	struct	buf	*new_bp;
14056 
14057 	ASSERT(bp != NULL);
14058 	xp = SD_GET_XBUF(bp);
14059 	ASSERT(xp != NULL);
14060 	un = SD_GET_UN(bp);
14061 	ASSERT(un != NULL);
14062 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14063 
14064 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14065 	    NULL, KM_SLEEP);
14066 
14067 	new_bp->b_lblkno	= blkno;
14068 
14069 	/*
14070 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14071 	 * original xbuf into it.
14072 	 */
14073 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14074 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14075 
14076 	/*
14077 	 * The given bp is automatically saved in the xb_private member
14078 	 * of the new xbuf.  Callers are allowed to depend on this.
14079 	 */
14080 	new_xp->xb_private = bp;
14081 
14082 	new_bp->b_private  = new_xp;
14083 
14084 	return (new_bp);
14085 }
14086 
14087 /*
14088  *    Function: sd_shadow_buf_alloc
14089  *
14090  * Description: Allocate a buf(9S) and init it as per the given buf
14091  *		and the various arguments.  The associated sd_xbuf
14092  *		struct is (nearly) duplicated.  The struct buf *bp
14093  *		argument is saved in new_xp->xb_private.
14094  *
14095  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14096  *		datalen - size of data area for the shadow bp
14097  *		bflags - B_READ or B_WRITE (pseudo flag)
14098  *		blkno - starting LBA
14099  *		func - function pointer for b_iodone in the shadow buf. (May
14100  *			be NULL if none.)
14101  *
14102  * Return Code: Pointer to allocates buf(9S) struct
14103  *
14104  *     Context: Can sleep.
14105  */
14106 
14107 static struct buf *
14108 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14109 	daddr_t blkno, int (*func)(struct buf *))
14110 {
14111 	struct	sd_lun	*un;
14112 	struct	sd_xbuf	*xp;
14113 	struct	sd_xbuf	*new_xp;
14114 	struct	buf	*new_bp;
14115 
14116 	ASSERT(bp != NULL);
14117 	xp = SD_GET_XBUF(bp);
14118 	ASSERT(xp != NULL);
14119 	un = SD_GET_UN(bp);
14120 	ASSERT(un != NULL);
14121 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14122 
14123 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14124 		bp_mapin(bp);
14125 	}
14126 
14127 	bflags &= (B_READ | B_WRITE);
14128 #if defined(__i386) || defined(__amd64)
14129 	new_bp = getrbuf(KM_SLEEP);
14130 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14131 	new_bp->b_bcount = datalen;
14132 	new_bp->b_flags = bflags |
14133 	    (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14134 #else
14135 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14136 	    datalen, bflags, SLEEP_FUNC, NULL);
14137 #endif
14138 	new_bp->av_forw	= NULL;
14139 	new_bp->av_back	= NULL;
14140 	new_bp->b_dev	= bp->b_dev;
14141 	new_bp->b_blkno	= blkno;
14142 	new_bp->b_iodone = func;
14143 	new_bp->b_edev	= bp->b_edev;
14144 	new_bp->b_resid	= 0;
14145 
14146 	/* We need to preserve the B_FAILFAST flag */
14147 	if (bp->b_flags & B_FAILFAST) {
14148 		new_bp->b_flags |= B_FAILFAST;
14149 	}
14150 
14151 	/*
14152 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14153 	 * original xbuf into it.
14154 	 */
14155 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14156 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14157 
14158 	/* Need later to copy data between the shadow buf & original buf! */
14159 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14160 
14161 	/*
14162 	 * The given bp is automatically saved in the xb_private member
14163 	 * of the new xbuf.  Callers are allowed to depend on this.
14164 	 */
14165 	new_xp->xb_private = bp;
14166 
14167 	new_bp->b_private  = new_xp;
14168 
14169 	return (new_bp);
14170 }
14171 
14172 /*
14173  *    Function: sd_bioclone_free
14174  *
14175  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14176  *		in the larger than partition operation.
14177  *
14178  *     Context: May be called under interrupt context
14179  */
14180 
14181 static void
14182 sd_bioclone_free(struct buf *bp)
14183 {
14184 	struct sd_xbuf	*xp;
14185 
14186 	ASSERT(bp != NULL);
14187 	xp = SD_GET_XBUF(bp);
14188 	ASSERT(xp != NULL);
14189 
14190 	/*
14191 	 * Call bp_mapout() before freeing the buf,  in case a lower
14192 	 * layer or HBA  had done a bp_mapin().  we must do this here
14193 	 * as we are the "originator" of the shadow buf.
14194 	 */
14195 	bp_mapout(bp);
14196 
14197 	/*
14198 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14199 	 * never gets confused by a stale value in this field. (Just a little
14200 	 * extra defensiveness here.)
14201 	 */
14202 	bp->b_iodone = NULL;
14203 
14204 	freerbuf(bp);
14205 
14206 	kmem_free(xp, sizeof (struct sd_xbuf));
14207 }
14208 
14209 /*
14210  *    Function: sd_shadow_buf_free
14211  *
14212  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14213  *
14214  *     Context: May be called under interrupt context
14215  */
14216 
14217 static void
14218 sd_shadow_buf_free(struct buf *bp)
14219 {
14220 	struct sd_xbuf	*xp;
14221 
14222 	ASSERT(bp != NULL);
14223 	xp = SD_GET_XBUF(bp);
14224 	ASSERT(xp != NULL);
14225 
14226 #if defined(__sparc)
14227 	/*
14228 	 * Call bp_mapout() before freeing the buf,  in case a lower
14229 	 * layer or HBA  had done a bp_mapin().  we must do this here
14230 	 * as we are the "originator" of the shadow buf.
14231 	 */
14232 	bp_mapout(bp);
14233 #endif
14234 
14235 	/*
14236 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14237 	 * never gets confused by a stale value in this field. (Just a little
14238 	 * extra defensiveness here.)
14239 	 */
14240 	bp->b_iodone = NULL;
14241 
14242 #if defined(__i386) || defined(__amd64)
14243 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
14244 	freerbuf(bp);
14245 #else
14246 	scsi_free_consistent_buf(bp);
14247 #endif
14248 
14249 	kmem_free(xp, sizeof (struct sd_xbuf));
14250 }
14251 
14252 
14253 /*
14254  *    Function: sd_print_transport_rejected_message
14255  *
14256  * Description: This implements the ludicrously complex rules for printing
14257  *		a "transport rejected" message.  This is to address the
14258  *		specific problem of having a flood of this error message
14259  *		produced when a failover occurs.
14260  *
14261  *     Context: Any.
14262  */
14263 
14264 static void
14265 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14266 	int code)
14267 {
14268 	ASSERT(un != NULL);
14269 	ASSERT(mutex_owned(SD_MUTEX(un)));
14270 	ASSERT(xp != NULL);
14271 
14272 	/*
14273 	 * Print the "transport rejected" message under the following
14274 	 * conditions:
14275 	 *
14276 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14277 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14278 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14279 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14280 	 *   scsi_transport(9F) (which indicates that the target might have
14281 	 *   gone off-line).  This uses the un->un_tran_fatal_count
14282 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14283 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14284 	 *   from scsi_transport().
14285 	 *
14286 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14287 	 * the preceeding cases in order for the message to be printed.
14288 	 */
14289 	if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) {
14290 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14291 		    (code != TRAN_FATAL_ERROR) ||
14292 		    (un->un_tran_fatal_count == 1)) {
14293 			switch (code) {
14294 			case TRAN_BADPKT:
14295 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14296 				    "transport rejected bad packet\n");
14297 				break;
14298 			case TRAN_FATAL_ERROR:
14299 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14300 				    "transport rejected fatal error\n");
14301 				break;
14302 			default:
14303 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14304 				    "transport rejected (%d)\n", code);
14305 				break;
14306 			}
14307 		}
14308 	}
14309 }
14310 
14311 
14312 /*
14313  *    Function: sd_add_buf_to_waitq
14314  *
14315  * Description: Add the given buf(9S) struct to the wait queue for the
14316  *		instance.  If sorting is enabled, then the buf is added
14317  *		to the queue via an elevator sort algorithm (a la
14318  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14319  *		If sorting is not enabled, then the buf is just added
14320  *		to the end of the wait queue.
14321  *
14322  * Return Code: void
14323  *
14324  *     Context: Does not sleep/block, therefore technically can be called
14325  *		from any context.  However if sorting is enabled then the
14326  *		execution time is indeterminate, and may take long if
14327  *		the wait queue grows large.
14328  */
14329 
14330 static void
14331 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14332 {
14333 	struct buf *ap;
14334 
14335 	ASSERT(bp != NULL);
14336 	ASSERT(un != NULL);
14337 	ASSERT(mutex_owned(SD_MUTEX(un)));
14338 
14339 	/* If the queue is empty, add the buf as the only entry & return. */
14340 	if (un->un_waitq_headp == NULL) {
14341 		ASSERT(un->un_waitq_tailp == NULL);
14342 		un->un_waitq_headp = un->un_waitq_tailp = bp;
14343 		bp->av_forw = NULL;
14344 		return;
14345 	}
14346 
14347 	ASSERT(un->un_waitq_tailp != NULL);
14348 
14349 	/*
14350 	 * If sorting is disabled, just add the buf to the tail end of
14351 	 * the wait queue and return.
14352 	 */
14353 	if (un->un_f_disksort_disabled) {
14354 		un->un_waitq_tailp->av_forw = bp;
14355 		un->un_waitq_tailp = bp;
14356 		bp->av_forw = NULL;
14357 		return;
14358 	}
14359 
14360 	/*
14361 	 * Sort thru the list of requests currently on the wait queue
14362 	 * and add the new buf request at the appropriate position.
14363 	 *
14364 	 * The un->un_waitq_headp is an activity chain pointer on which
14365 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14366 	 * first queue holds those requests which are positioned after
14367 	 * the current SD_GET_BLKNO() (in the first request); the second holds
14368 	 * requests which came in after their SD_GET_BLKNO() number was passed.
14369 	 * Thus we implement a one way scan, retracting after reaching
14370 	 * the end of the drive to the first request on the second
14371 	 * queue, at which time it becomes the first queue.
14372 	 * A one-way scan is natural because of the way UNIX read-ahead
14373 	 * blocks are allocated.
14374 	 *
14375 	 * If we lie after the first request, then we must locate the
14376 	 * second request list and add ourselves to it.
14377 	 */
14378 	ap = un->un_waitq_headp;
14379 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14380 		while (ap->av_forw != NULL) {
14381 			/*
14382 			 * Look for an "inversion" in the (normally
14383 			 * ascending) block numbers. This indicates
14384 			 * the start of the second request list.
14385 			 */
14386 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14387 				/*
14388 				 * Search the second request list for the
14389 				 * first request at a larger block number.
14390 				 * We go before that; however if there is
14391 				 * no such request, we go at the end.
14392 				 */
14393 				do {
14394 					if (SD_GET_BLKNO(bp) <
14395 					    SD_GET_BLKNO(ap->av_forw)) {
14396 						goto insert;
14397 					}
14398 					ap = ap->av_forw;
14399 				} while (ap->av_forw != NULL);
14400 				goto insert;		/* after last */
14401 			}
14402 			ap = ap->av_forw;
14403 		}
14404 
14405 		/*
14406 		 * No inversions... we will go after the last, and
14407 		 * be the first request in the second request list.
14408 		 */
14409 		goto insert;
14410 	}
14411 
14412 	/*
14413 	 * Request is at/after the current request...
14414 	 * sort in the first request list.
14415 	 */
14416 	while (ap->av_forw != NULL) {
14417 		/*
14418 		 * We want to go after the current request (1) if
14419 		 * there is an inversion after it (i.e. it is the end
14420 		 * of the first request list), or (2) if the next
14421 		 * request is a larger block no. than our request.
14422 		 */
14423 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14424 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14425 			goto insert;
14426 		}
14427 		ap = ap->av_forw;
14428 	}
14429 
14430 	/*
14431 	 * Neither a second list nor a larger request, therefore
14432 	 * we go at the end of the first list (which is the same
14433 	 * as the end of the whole schebang).
14434 	 */
14435 insert:
14436 	bp->av_forw = ap->av_forw;
14437 	ap->av_forw = bp;
14438 
14439 	/*
14440 	 * If we inserted onto the tail end of the waitq, make sure the
14441 	 * tail pointer is updated.
14442 	 */
14443 	if (ap == un->un_waitq_tailp) {
14444 		un->un_waitq_tailp = bp;
14445 	}
14446 }
14447 
14448 
14449 /*
14450  *    Function: sd_start_cmds
14451  *
14452  * Description: Remove and transport cmds from the driver queues.
14453  *
14454  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14455  *
14456  *		immed_bp - ptr to a buf to be transported immediately. Only
14457  *		the immed_bp is transported; bufs on the waitq are not
14458  *		processed and the un_retry_bp is not checked.  If immed_bp is
14459  *		NULL, then normal queue processing is performed.
14460  *
14461  *     Context: May be called from kernel thread context, interrupt context,
14462  *		or runout callback context. This function may not block or
14463  *		call routines that block.
14464  */
14465 
14466 static void
14467 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14468 {
14469 	struct	sd_xbuf	*xp;
14470 	struct	buf	*bp;
14471 	void	(*statp)(kstat_io_t *);
14472 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14473 	void	(*saved_statp)(kstat_io_t *);
14474 #endif
14475 	int	rval;
14476 
14477 	ASSERT(un != NULL);
14478 	ASSERT(mutex_owned(SD_MUTEX(un)));
14479 	ASSERT(un->un_ncmds_in_transport >= 0);
14480 	ASSERT(un->un_throttle >= 0);
14481 
14482 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14483 
14484 	do {
14485 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14486 		saved_statp = NULL;
14487 #endif
14488 
14489 		/*
14490 		 * If we are syncing or dumping, fail the command to
14491 		 * avoid recursively calling back into scsi_transport().
14492 		 * The dump I/O itself uses a separate code path so this
14493 		 * only prevents non-dump I/O from being sent while dumping.
14494 		 * File system sync takes place before dumping begins.
14495 		 * During panic, filesystem I/O is allowed provided
14496 		 * un_in_callback is <= 1.  This is to prevent recursion
14497 		 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14498 		 * sd_start_cmds and so on.  See panic.c for more information
14499 		 * about the states the system can be in during panic.
14500 		 */
14501 		if ((un->un_state == SD_STATE_DUMPING) ||
14502 		    (ddi_in_panic() && (un->un_in_callback > 1))) {
14503 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14504 			    "sd_start_cmds: panicking\n");
14505 			goto exit;
14506 		}
14507 
14508 		if ((bp = immed_bp) != NULL) {
14509 			/*
14510 			 * We have a bp that must be transported immediately.
14511 			 * It's OK to transport the immed_bp here without doing
14512 			 * the throttle limit check because the immed_bp is
14513 			 * always used in a retry/recovery case. This means
14514 			 * that we know we are not at the throttle limit by
14515 			 * virtue of the fact that to get here we must have
14516 			 * already gotten a command back via sdintr(). This also
14517 			 * relies on (1) the command on un_retry_bp preventing
14518 			 * further commands from the waitq from being issued;
14519 			 * and (2) the code in sd_retry_command checking the
14520 			 * throttle limit before issuing a delayed or immediate
14521 			 * retry. This holds even if the throttle limit is
14522 			 * currently ratcheted down from its maximum value.
14523 			 */
14524 			statp = kstat_runq_enter;
14525 			if (bp == un->un_retry_bp) {
14526 				ASSERT((un->un_retry_statp == NULL) ||
14527 				    (un->un_retry_statp == kstat_waitq_enter) ||
14528 				    (un->un_retry_statp ==
14529 				    kstat_runq_back_to_waitq));
14530 				/*
14531 				 * If the waitq kstat was incremented when
14532 				 * sd_set_retry_bp() queued this bp for a retry,
14533 				 * then we must set up statp so that the waitq
14534 				 * count will get decremented correctly below.
14535 				 * Also we must clear un->un_retry_statp to
14536 				 * ensure that we do not act on a stale value
14537 				 * in this field.
14538 				 */
14539 				if ((un->un_retry_statp == kstat_waitq_enter) ||
14540 				    (un->un_retry_statp ==
14541 				    kstat_runq_back_to_waitq)) {
14542 					statp = kstat_waitq_to_runq;
14543 				}
14544 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14545 				saved_statp = un->un_retry_statp;
14546 #endif
14547 				un->un_retry_statp = NULL;
14548 
14549 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14550 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14551 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
14552 				    un, un->un_retry_bp, un->un_throttle,
14553 				    un->un_ncmds_in_transport);
14554 			} else {
14555 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14556 				    "processing priority bp:0x%p\n", bp);
14557 			}
14558 
14559 		} else if ((bp = un->un_waitq_headp) != NULL) {
14560 			/*
14561 			 * A command on the waitq is ready to go, but do not
14562 			 * send it if:
14563 			 *
14564 			 * (1) the throttle limit has been reached, or
14565 			 * (2) a retry is pending, or
14566 			 * (3) a START_STOP_UNIT callback pending, or
14567 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14568 			 *	command is pending.
14569 			 *
14570 			 * For all of these conditions, IO processing will
14571 			 * restart after the condition is cleared.
14572 			 */
14573 			if (un->un_ncmds_in_transport >= un->un_throttle) {
14574 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14575 				    "sd_start_cmds: exiting, "
14576 				    "throttle limit reached!\n");
14577 				goto exit;
14578 			}
14579 			if (un->un_retry_bp != NULL) {
14580 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14581 				    "sd_start_cmds: exiting, retry pending!\n");
14582 				goto exit;
14583 			}
14584 			if (un->un_startstop_timeid != NULL) {
14585 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14586 				    "sd_start_cmds: exiting, "
14587 				    "START_STOP pending!\n");
14588 				goto exit;
14589 			}
14590 			if (un->un_direct_priority_timeid != NULL) {
14591 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14592 				    "sd_start_cmds: exiting, "
14593 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14594 				goto exit;
14595 			}
14596 
14597 			/* Dequeue the command */
14598 			un->un_waitq_headp = bp->av_forw;
14599 			if (un->un_waitq_headp == NULL) {
14600 				un->un_waitq_tailp = NULL;
14601 			}
14602 			bp->av_forw = NULL;
14603 			statp = kstat_waitq_to_runq;
14604 			SD_TRACE(SD_LOG_IO_CORE, un,
14605 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14606 
14607 		} else {
14608 			/* No work to do so bail out now */
14609 			SD_TRACE(SD_LOG_IO_CORE, un,
14610 			    "sd_start_cmds: no more work, exiting!\n");
14611 			goto exit;
14612 		}
14613 
14614 		/*
14615 		 * Reset the state to normal. This is the mechanism by which
14616 		 * the state transitions from either SD_STATE_RWAIT or
14617 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14618 		 * If state is SD_STATE_PM_CHANGING then this command is
14619 		 * part of the device power control and the state must
14620 		 * not be put back to normal. Doing so would would
14621 		 * allow new commands to proceed when they shouldn't,
14622 		 * the device may be going off.
14623 		 */
14624 		if ((un->un_state != SD_STATE_SUSPENDED) &&
14625 		    (un->un_state != SD_STATE_PM_CHANGING)) {
14626 			New_state(un, SD_STATE_NORMAL);
14627 		    }
14628 
14629 		xp = SD_GET_XBUF(bp);
14630 		ASSERT(xp != NULL);
14631 
14632 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14633 		/*
14634 		 * Allocate the scsi_pkt if we need one, or attach DMA
14635 		 * resources if we have a scsi_pkt that needs them. The
14636 		 * latter should only occur for commands that are being
14637 		 * retried.
14638 		 */
14639 		if ((xp->xb_pktp == NULL) ||
14640 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14641 #else
14642 		if (xp->xb_pktp == NULL) {
14643 #endif
14644 			/*
14645 			 * There is no scsi_pkt allocated for this buf. Call
14646 			 * the initpkt function to allocate & init one.
14647 			 *
14648 			 * The scsi_init_pkt runout callback functionality is
14649 			 * implemented as follows:
14650 			 *
14651 			 * 1) The initpkt function always calls
14652 			 *    scsi_init_pkt(9F) with sdrunout specified as the
14653 			 *    callback routine.
14654 			 * 2) A successful packet allocation is initialized and
14655 			 *    the I/O is transported.
14656 			 * 3) The I/O associated with an allocation resource
14657 			 *    failure is left on its queue to be retried via
14658 			 *    runout or the next I/O.
14659 			 * 4) The I/O associated with a DMA error is removed
14660 			 *    from the queue and failed with EIO. Processing of
14661 			 *    the transport queues is also halted to be
14662 			 *    restarted via runout or the next I/O.
14663 			 * 5) The I/O associated with a CDB size or packet
14664 			 *    size error is removed from the queue and failed
14665 			 *    with EIO. Processing of the transport queues is
14666 			 *    continued.
14667 			 *
14668 			 * Note: there is no interface for canceling a runout
14669 			 * callback. To prevent the driver from detaching or
14670 			 * suspending while a runout is pending the driver
14671 			 * state is set to SD_STATE_RWAIT
14672 			 *
14673 			 * Note: using the scsi_init_pkt callback facility can
14674 			 * result in an I/O request persisting at the head of
14675 			 * the list which cannot be satisfied even after
14676 			 * multiple retries. In the future the driver may
14677 			 * implement some kind of maximum runout count before
14678 			 * failing an I/O.
14679 			 *
14680 			 * Note: the use of funcp below may seem superfluous,
14681 			 * but it helps warlock figure out the correct
14682 			 * initpkt function calls (see [s]sd.wlcmd).
14683 			 */
14684 			struct scsi_pkt	*pktp;
14685 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14686 
14687 			ASSERT(bp != un->un_rqs_bp);
14688 
14689 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
14690 			switch ((*funcp)(bp, &pktp)) {
14691 			case  SD_PKT_ALLOC_SUCCESS:
14692 				xp->xb_pktp = pktp;
14693 				SD_TRACE(SD_LOG_IO_CORE, un,
14694 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14695 				    pktp);
14696 				goto got_pkt;
14697 
14698 			case SD_PKT_ALLOC_FAILURE:
14699 				/*
14700 				 * Temporary (hopefully) resource depletion.
14701 				 * Since retries and RQS commands always have a
14702 				 * scsi_pkt allocated, these cases should never
14703 				 * get here. So the only cases this needs to
14704 				 * handle is a bp from the waitq (which we put
14705 				 * back onto the waitq for sdrunout), or a bp
14706 				 * sent as an immed_bp (which we just fail).
14707 				 */
14708 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14709 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14710 
14711 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14712 
14713 				if (bp == immed_bp) {
14714 					/*
14715 					 * If SD_XB_DMA_FREED is clear, then
14716 					 * this is a failure to allocate a
14717 					 * scsi_pkt, and we must fail the
14718 					 * command.
14719 					 */
14720 					if ((xp->xb_pkt_flags &
14721 					    SD_XB_DMA_FREED) == 0) {
14722 						break;
14723 					}
14724 
14725 					/*
14726 					 * If this immediate command is NOT our
14727 					 * un_retry_bp, then we must fail it.
14728 					 */
14729 					if (bp != un->un_retry_bp) {
14730 						break;
14731 					}
14732 
14733 					/*
14734 					 * We get here if this cmd is our
14735 					 * un_retry_bp that was DMAFREED, but
14736 					 * scsi_init_pkt() failed to reallocate
14737 					 * DMA resources when we attempted to
14738 					 * retry it. This can happen when an
14739 					 * mpxio failover is in progress, but
14740 					 * we don't want to just fail the
14741 					 * command in this case.
14742 					 *
14743 					 * Use timeout(9F) to restart it after
14744 					 * a 100ms delay.  We don't want to
14745 					 * let sdrunout() restart it, because
14746 					 * sdrunout() is just supposed to start
14747 					 * commands that are sitting on the
14748 					 * wait queue.  The un_retry_bp stays
14749 					 * set until the command completes, but
14750 					 * sdrunout can be called many times
14751 					 * before that happens.  Since sdrunout
14752 					 * cannot tell if the un_retry_bp is
14753 					 * already in the transport, it could
14754 					 * end up calling scsi_transport() for
14755 					 * the un_retry_bp multiple times.
14756 					 *
14757 					 * Also: don't schedule the callback
14758 					 * if some other callback is already
14759 					 * pending.
14760 					 */
14761 					if (un->un_retry_statp == NULL) {
14762 						/*
14763 						 * restore the kstat pointer to
14764 						 * keep kstat counts coherent
14765 						 * when we do retry the command.
14766 						 */
14767 						un->un_retry_statp =
14768 						    saved_statp;
14769 					}
14770 
14771 					if ((un->un_startstop_timeid == NULL) &&
14772 					    (un->un_retry_timeid == NULL) &&
14773 					    (un->un_direct_priority_timeid ==
14774 					    NULL)) {
14775 
14776 						un->un_retry_timeid =
14777 						    timeout(
14778 						    sd_start_retry_command,
14779 						    un, SD_RESTART_TIMEOUT);
14780 					}
14781 					goto exit;
14782 				}
14783 
14784 #else
14785 				if (bp == immed_bp) {
14786 					break;	/* Just fail the command */
14787 				}
14788 #endif
14789 
14790 				/* Add the buf back to the head of the waitq */
14791 				bp->av_forw = un->un_waitq_headp;
14792 				un->un_waitq_headp = bp;
14793 				if (un->un_waitq_tailp == NULL) {
14794 					un->un_waitq_tailp = bp;
14795 				}
14796 				goto exit;
14797 
14798 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
14799 				/*
14800 				 * HBA DMA resource failure. Fail the command
14801 				 * and continue processing of the queues.
14802 				 */
14803 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14804 				    "sd_start_cmds: "
14805 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
14806 				break;
14807 
14808 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
14809 				/*
14810 				 * Note:x86: Partial DMA mapping not supported
14811 				 * for USCSI commands, and all the needed DMA
14812 				 * resources were not allocated.
14813 				 */
14814 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14815 				    "sd_start_cmds: "
14816 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
14817 				break;
14818 
14819 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
14820 				/*
14821 				 * Note:x86: Request cannot fit into CDB based
14822 				 * on lba and len.
14823 				 */
14824 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14825 				    "sd_start_cmds: "
14826 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
14827 				break;
14828 
14829 			default:
14830 				/* Should NEVER get here! */
14831 				panic("scsi_initpkt error");
14832 				/*NOTREACHED*/
14833 			}
14834 
14835 			/*
14836 			 * Fatal error in allocating a scsi_pkt for this buf.
14837 			 * Update kstats & return the buf with an error code.
14838 			 * We must use sd_return_failed_command_no_restart() to
14839 			 * avoid a recursive call back into sd_start_cmds().
14840 			 * However this also means that we must keep processing
14841 			 * the waitq here in order to avoid stalling.
14842 			 */
14843 			if (statp == kstat_waitq_to_runq) {
14844 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
14845 			}
14846 			sd_return_failed_command_no_restart(un, bp, EIO);
14847 			if (bp == immed_bp) {
14848 				/* immed_bp is gone by now, so clear this */
14849 				immed_bp = NULL;
14850 			}
14851 			continue;
14852 		}
14853 got_pkt:
14854 		if (bp == immed_bp) {
14855 			/* goto the head of the class.... */
14856 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
14857 		}
14858 
14859 		un->un_ncmds_in_transport++;
14860 		SD_UPDATE_KSTATS(un, statp, bp);
14861 
14862 		/*
14863 		 * Call scsi_transport() to send the command to the target.
14864 		 * According to SCSA architecture, we must drop the mutex here
14865 		 * before calling scsi_transport() in order to avoid deadlock.
14866 		 * Note that the scsi_pkt's completion routine can be executed
14867 		 * (from interrupt context) even before the call to
14868 		 * scsi_transport() returns.
14869 		 */
14870 		SD_TRACE(SD_LOG_IO_CORE, un,
14871 		    "sd_start_cmds: calling scsi_transport()\n");
14872 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
14873 
14874 		mutex_exit(SD_MUTEX(un));
14875 		rval = scsi_transport(xp->xb_pktp);
14876 		mutex_enter(SD_MUTEX(un));
14877 
14878 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14879 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
14880 
14881 		switch (rval) {
14882 		case TRAN_ACCEPT:
14883 			/* Clear this with every pkt accepted by the HBA */
14884 			un->un_tran_fatal_count = 0;
14885 			break;	/* Success; try the next cmd (if any) */
14886 
14887 		case TRAN_BUSY:
14888 			un->un_ncmds_in_transport--;
14889 			ASSERT(un->un_ncmds_in_transport >= 0);
14890 
14891 			/*
14892 			 * Don't retry request sense, the sense data
14893 			 * is lost when another request is sent.
14894 			 * Free up the rqs buf and retry
14895 			 * the original failed cmd.  Update kstat.
14896 			 */
14897 			if (bp == un->un_rqs_bp) {
14898 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14899 				bp = sd_mark_rqs_idle(un, xp);
14900 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
14901 					NULL, NULL, EIO, SD_BSY_TIMEOUT / 500,
14902 					kstat_waitq_enter);
14903 				goto exit;
14904 			}
14905 
14906 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14907 			/*
14908 			 * Free the DMA resources for the  scsi_pkt. This will
14909 			 * allow mpxio to select another path the next time
14910 			 * we call scsi_transport() with this scsi_pkt.
14911 			 * See sdintr() for the rationalization behind this.
14912 			 */
14913 			if ((un->un_f_is_fibre == TRUE) &&
14914 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
14915 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
14916 				scsi_dmafree(xp->xb_pktp);
14917 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
14918 			}
14919 #endif
14920 
14921 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
14922 				/*
14923 				 * Commands that are SD_PATH_DIRECT_PRIORITY
14924 				 * are for error recovery situations. These do
14925 				 * not use the normal command waitq, so if they
14926 				 * get a TRAN_BUSY we cannot put them back onto
14927 				 * the waitq for later retry. One possible
14928 				 * problem is that there could already be some
14929 				 * other command on un_retry_bp that is waiting
14930 				 * for this one to complete, so we would be
14931 				 * deadlocked if we put this command back onto
14932 				 * the waitq for later retry (since un_retry_bp
14933 				 * must complete before the driver gets back to
14934 				 * commands on the waitq).
14935 				 *
14936 				 * To avoid deadlock we must schedule a callback
14937 				 * that will restart this command after a set
14938 				 * interval.  This should keep retrying for as
14939 				 * long as the underlying transport keeps
14940 				 * returning TRAN_BUSY (just like for other
14941 				 * commands).  Use the same timeout interval as
14942 				 * for the ordinary TRAN_BUSY retry.
14943 				 */
14944 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14945 				    "sd_start_cmds: scsi_transport() returned "
14946 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
14947 
14948 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14949 				un->un_direct_priority_timeid =
14950 				    timeout(sd_start_direct_priority_command,
14951 				    bp, SD_BSY_TIMEOUT / 500);
14952 
14953 				goto exit;
14954 			}
14955 
14956 			/*
14957 			 * For TRAN_BUSY, we want to reduce the throttle value,
14958 			 * unless we are retrying a command.
14959 			 */
14960 			if (bp != un->un_retry_bp) {
14961 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
14962 			}
14963 
14964 			/*
14965 			 * Set up the bp to be tried again 10 ms later.
14966 			 * Note:x86: Is there a timeout value in the sd_lun
14967 			 * for this condition?
14968 			 */
14969 			sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500,
14970 				kstat_runq_back_to_waitq);
14971 			goto exit;
14972 
14973 		case TRAN_FATAL_ERROR:
14974 			un->un_tran_fatal_count++;
14975 			/* FALLTHRU */
14976 
14977 		case TRAN_BADPKT:
14978 		default:
14979 			un->un_ncmds_in_transport--;
14980 			ASSERT(un->un_ncmds_in_transport >= 0);
14981 
14982 			/*
14983 			 * If this is our REQUEST SENSE command with a
14984 			 * transport error, we must get back the pointers
14985 			 * to the original buf, and mark the REQUEST
14986 			 * SENSE command as "available".
14987 			 */
14988 			if (bp == un->un_rqs_bp) {
14989 				bp = sd_mark_rqs_idle(un, xp);
14990 				xp = SD_GET_XBUF(bp);
14991 			} else {
14992 				/*
14993 				 * Legacy behavior: do not update transport
14994 				 * error count for request sense commands.
14995 				 */
14996 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
14997 			}
14998 
14999 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15000 			sd_print_transport_rejected_message(un, xp, rval);
15001 
15002 			/*
15003 			 * We must use sd_return_failed_command_no_restart() to
15004 			 * avoid a recursive call back into sd_start_cmds().
15005 			 * However this also means that we must keep processing
15006 			 * the waitq here in order to avoid stalling.
15007 			 */
15008 			sd_return_failed_command_no_restart(un, bp, EIO);
15009 
15010 			/*
15011 			 * Notify any threads waiting in sd_ddi_suspend() that
15012 			 * a command completion has occurred.
15013 			 */
15014 			if (un->un_state == SD_STATE_SUSPENDED) {
15015 				cv_broadcast(&un->un_disk_busy_cv);
15016 			}
15017 
15018 			if (bp == immed_bp) {
15019 				/* immed_bp is gone by now, so clear this */
15020 				immed_bp = NULL;
15021 			}
15022 			break;
15023 		}
15024 
15025 	} while (immed_bp == NULL);
15026 
15027 exit:
15028 	ASSERT(mutex_owned(SD_MUTEX(un)));
15029 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15030 }
15031 
15032 
15033 /*
15034  *    Function: sd_return_command
15035  *
15036  * Description: Returns a command to its originator (with or without an
15037  *		error).  Also starts commands waiting to be transported
15038  *		to the target.
15039  *
15040  *     Context: May be called from interrupt, kernel, or timeout context
15041  */
15042 
15043 static void
15044 sd_return_command(struct sd_lun *un, struct buf *bp)
15045 {
15046 	struct sd_xbuf *xp;
15047 #if defined(__i386) || defined(__amd64)
15048 	struct scsi_pkt *pktp;
15049 #endif
15050 
15051 	ASSERT(bp != NULL);
15052 	ASSERT(un != NULL);
15053 	ASSERT(mutex_owned(SD_MUTEX(un)));
15054 	ASSERT(bp != un->un_rqs_bp);
15055 	xp = SD_GET_XBUF(bp);
15056 	ASSERT(xp != NULL);
15057 
15058 #if defined(__i386) || defined(__amd64)
15059 	pktp = SD_GET_PKTP(bp);
15060 #endif
15061 
15062 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15063 
15064 #if defined(__i386) || defined(__amd64)
15065 	/*
15066 	 * Note:x86: check for the "sdrestart failed" case.
15067 	 */
15068 	if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15069 		(geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15070 		(xp->xb_pktp->pkt_resid == 0)) {
15071 
15072 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15073 			/*
15074 			 * Successfully set up next portion of cmd
15075 			 * transfer, try sending it
15076 			 */
15077 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15078 			    NULL, NULL, 0, (clock_t)0, NULL);
15079 			sd_start_cmds(un, NULL);
15080 			return;	/* Note:x86: need a return here? */
15081 		}
15082 	}
15083 #endif
15084 
15085 	/*
15086 	 * If this is the failfast bp, clear it from un_failfast_bp. This
15087 	 * can happen if upon being re-tried the failfast bp either
15088 	 * succeeded or encountered another error (possibly even a different
15089 	 * error than the one that precipitated the failfast state, but in
15090 	 * that case it would have had to exhaust retries as well). Regardless,
15091 	 * this should not occur whenever the instance is in the active
15092 	 * failfast state.
15093 	 */
15094 	if (bp == un->un_failfast_bp) {
15095 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15096 		un->un_failfast_bp = NULL;
15097 	}
15098 
15099 	/*
15100 	 * Clear the failfast state upon successful completion of ANY cmd.
15101 	 */
15102 	if (bp->b_error == 0) {
15103 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15104 	}
15105 
15106 	/*
15107 	 * This is used if the command was retried one or more times. Show that
15108 	 * we are done with it, and allow processing of the waitq to resume.
15109 	 */
15110 	if (bp == un->un_retry_bp) {
15111 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15112 		    "sd_return_command: un:0x%p: "
15113 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15114 		un->un_retry_bp = NULL;
15115 		un->un_retry_statp = NULL;
15116 	}
15117 
15118 	SD_UPDATE_RDWR_STATS(un, bp);
15119 	SD_UPDATE_PARTITION_STATS(un, bp);
15120 
15121 	switch (un->un_state) {
15122 	case SD_STATE_SUSPENDED:
15123 		/*
15124 		 * Notify any threads waiting in sd_ddi_suspend() that
15125 		 * a command completion has occurred.
15126 		 */
15127 		cv_broadcast(&un->un_disk_busy_cv);
15128 		break;
15129 	default:
15130 		sd_start_cmds(un, NULL);
15131 		break;
15132 	}
15133 
15134 	/* Return this command up the iodone chain to its originator. */
15135 	mutex_exit(SD_MUTEX(un));
15136 
15137 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15138 	xp->xb_pktp = NULL;
15139 
15140 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15141 
15142 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15143 	mutex_enter(SD_MUTEX(un));
15144 
15145 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15146 }
15147 
15148 
15149 /*
15150  *    Function: sd_return_failed_command
15151  *
15152  * Description: Command completion when an error occurred.
15153  *
15154  *     Context: May be called from interrupt context
15155  */
15156 
15157 static void
15158 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15159 {
15160 	ASSERT(bp != NULL);
15161 	ASSERT(un != NULL);
15162 	ASSERT(mutex_owned(SD_MUTEX(un)));
15163 
15164 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15165 	    "sd_return_failed_command: entry\n");
15166 
15167 	/*
15168 	 * b_resid could already be nonzero due to a partial data
15169 	 * transfer, so do not change it here.
15170 	 */
15171 	SD_BIOERROR(bp, errcode);
15172 
15173 	sd_return_command(un, bp);
15174 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15175 	    "sd_return_failed_command: exit\n");
15176 }
15177 
15178 
15179 /*
15180  *    Function: sd_return_failed_command_no_restart
15181  *
15182  * Description: Same as sd_return_failed_command, but ensures that no
15183  *		call back into sd_start_cmds will be issued.
15184  *
15185  *     Context: May be called from interrupt context
15186  */
15187 
15188 static void
15189 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15190 	int errcode)
15191 {
15192 	struct sd_xbuf *xp;
15193 
15194 	ASSERT(bp != NULL);
15195 	ASSERT(un != NULL);
15196 	ASSERT(mutex_owned(SD_MUTEX(un)));
15197 	xp = SD_GET_XBUF(bp);
15198 	ASSERT(xp != NULL);
15199 	ASSERT(errcode != 0);
15200 
15201 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15202 	    "sd_return_failed_command_no_restart: entry\n");
15203 
15204 	/*
15205 	 * b_resid could already be nonzero due to a partial data
15206 	 * transfer, so do not change it here.
15207 	 */
15208 	SD_BIOERROR(bp, errcode);
15209 
15210 	/*
15211 	 * If this is the failfast bp, clear it. This can happen if the
15212 	 * failfast bp encounterd a fatal error when we attempted to
15213 	 * re-try it (such as a scsi_transport(9F) failure).  However
15214 	 * we should NOT be in an active failfast state if the failfast
15215 	 * bp is not NULL.
15216 	 */
15217 	if (bp == un->un_failfast_bp) {
15218 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15219 		un->un_failfast_bp = NULL;
15220 	}
15221 
15222 	if (bp == un->un_retry_bp) {
15223 		/*
15224 		 * This command was retried one or more times. Show that we are
15225 		 * done with it, and allow processing of the waitq to resume.
15226 		 */
15227 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15228 		    "sd_return_failed_command_no_restart: "
15229 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15230 		un->un_retry_bp = NULL;
15231 		un->un_retry_statp = NULL;
15232 	}
15233 
15234 	SD_UPDATE_RDWR_STATS(un, bp);
15235 	SD_UPDATE_PARTITION_STATS(un, bp);
15236 
15237 	mutex_exit(SD_MUTEX(un));
15238 
15239 	if (xp->xb_pktp != NULL) {
15240 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15241 		xp->xb_pktp = NULL;
15242 	}
15243 
15244 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15245 
15246 	mutex_enter(SD_MUTEX(un));
15247 
15248 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15249 	    "sd_return_failed_command_no_restart: exit\n");
15250 }
15251 
15252 
15253 /*
15254  *    Function: sd_retry_command
15255  *
15256  * Description: queue up a command for retry, or (optionally) fail it
15257  *		if retry counts are exhausted.
15258  *
15259  *   Arguments: un - Pointer to the sd_lun struct for the target.
15260  *
15261  *		bp - Pointer to the buf for the command to be retried.
15262  *
15263  *		retry_check_flag - Flag to see which (if any) of the retry
15264  *		   counts should be decremented/checked. If the indicated
15265  *		   retry count is exhausted, then the command will not be
15266  *		   retried; it will be failed instead. This should use a
15267  *		   value equal to one of the following:
15268  *
15269  *			SD_RETRIES_NOCHECK
15270  *			SD_RESD_RETRIES_STANDARD
15271  *			SD_RETRIES_VICTIM
15272  *
15273  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15274  *		   if the check should be made to see of FLAG_ISOLATE is set
15275  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
15276  *		   not retried, it is simply failed.
15277  *
15278  *		user_funcp - Ptr to function to call before dispatching the
15279  *		   command. May be NULL if no action needs to be performed.
15280  *		   (Primarily intended for printing messages.)
15281  *
15282  *		user_arg - Optional argument to be passed along to
15283  *		   the user_funcp call.
15284  *
15285  *		failure_code - errno return code to set in the bp if the
15286  *		   command is going to be failed.
15287  *
15288  *		retry_delay - Retry delay interval in (clock_t) units. May
15289  *		   be zero which indicates that the retry should be retried
15290  *		   immediately (ie, without an intervening delay).
15291  *
15292  *		statp - Ptr to kstat function to be updated if the command
15293  *		   is queued for a delayed retry. May be NULL if no kstat
15294  *		   update is desired.
15295  *
15296  *     Context: May be called from interupt context.
15297  */
15298 
15299 static void
15300 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15301 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
15302 	code), void *user_arg, int failure_code,  clock_t retry_delay,
15303 	void (*statp)(kstat_io_t *))
15304 {
15305 	struct sd_xbuf	*xp;
15306 	struct scsi_pkt	*pktp;
15307 
15308 	ASSERT(un != NULL);
15309 	ASSERT(mutex_owned(SD_MUTEX(un)));
15310 	ASSERT(bp != NULL);
15311 	xp = SD_GET_XBUF(bp);
15312 	ASSERT(xp != NULL);
15313 	pktp = SD_GET_PKTP(bp);
15314 	ASSERT(pktp != NULL);
15315 
15316 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15317 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15318 
15319 	/*
15320 	 * If we are syncing or dumping, fail the command to avoid
15321 	 * recursively calling back into scsi_transport().
15322 	 */
15323 	if (ddi_in_panic()) {
15324 		goto fail_command_no_log;
15325 	}
15326 
15327 	/*
15328 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15329 	 * log an error and fail the command.
15330 	 */
15331 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15332 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15333 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
15334 		sd_dump_memory(un, SD_LOG_IO, "CDB",
15335 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15336 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15337 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15338 		goto fail_command;
15339 	}
15340 
15341 	/*
15342 	 * If we are suspended, then put the command onto head of the
15343 	 * wait queue since we don't want to start more commands.
15344 	 */
15345 	switch (un->un_state) {
15346 	case SD_STATE_SUSPENDED:
15347 	case SD_STATE_DUMPING:
15348 		bp->av_forw = un->un_waitq_headp;
15349 		un->un_waitq_headp = bp;
15350 		if (un->un_waitq_tailp == NULL) {
15351 			un->un_waitq_tailp = bp;
15352 		}
15353 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15354 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15355 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15356 		return;
15357 	default:
15358 		break;
15359 	}
15360 
15361 	/*
15362 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
15363 	 * is set; if it is then we do not want to retry the command.
15364 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15365 	 */
15366 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15367 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15368 			goto fail_command;
15369 		}
15370 	}
15371 
15372 
15373 	/*
15374 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15375 	 * command timeout or a selection timeout has occurred. This means
15376 	 * that we were unable to establish an kind of communication with
15377 	 * the target, and subsequent retries and/or commands are likely
15378 	 * to encounter similar results and take a long time to complete.
15379 	 *
15380 	 * If this is a failfast error condition, we need to update the
15381 	 * failfast state, even if this bp does not have B_FAILFAST set.
15382 	 */
15383 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
15384 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15385 			ASSERT(un->un_failfast_bp == NULL);
15386 			/*
15387 			 * If we are already in the active failfast state, and
15388 			 * another failfast error condition has been detected,
15389 			 * then fail this command if it has B_FAILFAST set.
15390 			 * If B_FAILFAST is clear, then maintain the legacy
15391 			 * behavior of retrying heroically, even tho this will
15392 			 * take a lot more time to fail the command.
15393 			 */
15394 			if (bp->b_flags & B_FAILFAST) {
15395 				goto fail_command;
15396 			}
15397 		} else {
15398 			/*
15399 			 * We're not in the active failfast state, but we
15400 			 * have a failfast error condition, so we must begin
15401 			 * transition to the next state. We do this regardless
15402 			 * of whether or not this bp has B_FAILFAST set.
15403 			 */
15404 			if (un->un_failfast_bp == NULL) {
15405 				/*
15406 				 * This is the first bp to meet a failfast
15407 				 * condition so save it on un_failfast_bp &
15408 				 * do normal retry processing. Do not enter
15409 				 * active failfast state yet. This marks
15410 				 * entry into the "failfast pending" state.
15411 				 */
15412 				un->un_failfast_bp = bp;
15413 
15414 			} else if (un->un_failfast_bp == bp) {
15415 				/*
15416 				 * This is the second time *this* bp has
15417 				 * encountered a failfast error condition,
15418 				 * so enter active failfast state & flush
15419 				 * queues as appropriate.
15420 				 */
15421 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
15422 				un->un_failfast_bp = NULL;
15423 				sd_failfast_flushq(un);
15424 
15425 				/*
15426 				 * Fail this bp now if B_FAILFAST set;
15427 				 * otherwise continue with retries. (It would
15428 				 * be pretty ironic if this bp succeeded on a
15429 				 * subsequent retry after we just flushed all
15430 				 * the queues).
15431 				 */
15432 				if (bp->b_flags & B_FAILFAST) {
15433 					goto fail_command;
15434 				}
15435 
15436 #if !defined(lint) && !defined(__lint)
15437 			} else {
15438 				/*
15439 				 * If neither of the preceeding conditionals
15440 				 * was true, it means that there is some
15441 				 * *other* bp that has met an inital failfast
15442 				 * condition and is currently either being
15443 				 * retried or is waiting to be retried. In
15444 				 * that case we should perform normal retry
15445 				 * processing on *this* bp, since there is a
15446 				 * chance that the current failfast condition
15447 				 * is transient and recoverable. If that does
15448 				 * not turn out to be the case, then retries
15449 				 * will be cleared when the wait queue is
15450 				 * flushed anyway.
15451 				 */
15452 #endif
15453 			}
15454 		}
15455 	} else {
15456 		/*
15457 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
15458 		 * likely were able to at least establish some level of
15459 		 * communication with the target and subsequent commands
15460 		 * and/or retries are likely to get through to the target,
15461 		 * In this case we want to be aggressive about clearing
15462 		 * the failfast state. Note that this does not affect
15463 		 * the "failfast pending" condition.
15464 		 */
15465 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15466 	}
15467 
15468 
15469 	/*
15470 	 * Check the specified retry count to see if we can still do
15471 	 * any retries with this pkt before we should fail it.
15472 	 */
15473 	switch (retry_check_flag & SD_RETRIES_MASK) {
15474 	case SD_RETRIES_VICTIM:
15475 		/*
15476 		 * Check the victim retry count. If exhausted, then fall
15477 		 * thru & check against the standard retry count.
15478 		 */
15479 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15480 			/* Increment count & proceed with the retry */
15481 			xp->xb_victim_retry_count++;
15482 			break;
15483 		}
15484 		/* Victim retries exhausted, fall back to std. retries... */
15485 		/* FALLTHRU */
15486 
15487 	case SD_RETRIES_STANDARD:
15488 		if (xp->xb_retry_count >= un->un_retry_count) {
15489 			/* Retries exhausted, fail the command */
15490 			SD_TRACE(SD_LOG_IO_CORE, un,
15491 			    "sd_retry_command: retries exhausted!\n");
15492 			/*
15493 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
15494 			 * commands with nonzero pkt_resid.
15495 			 */
15496 			if ((pktp->pkt_reason == CMD_CMPLT) &&
15497 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15498 			    (pktp->pkt_resid != 0)) {
15499 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15500 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15501 					SD_UPDATE_B_RESID(bp, pktp);
15502 				}
15503 			}
15504 			goto fail_command;
15505 		}
15506 		xp->xb_retry_count++;
15507 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15508 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15509 		break;
15510 
15511 	case SD_RETRIES_UA:
15512 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15513 			/* Retries exhausted, fail the command */
15514 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15515 			    "Unit Attention retries exhausted. "
15516 			    "Check the target.\n");
15517 			goto fail_command;
15518 		}
15519 		xp->xb_ua_retry_count++;
15520 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15521 		    "sd_retry_command: retry count:%d\n",
15522 			xp->xb_ua_retry_count);
15523 		break;
15524 
15525 	case SD_RETRIES_BUSY:
15526 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
15527 			/* Retries exhausted, fail the command */
15528 			SD_TRACE(SD_LOG_IO_CORE, un,
15529 			    "sd_retry_command: retries exhausted!\n");
15530 			goto fail_command;
15531 		}
15532 		xp->xb_retry_count++;
15533 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15534 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15535 		break;
15536 
15537 	case SD_RETRIES_NOCHECK:
15538 	default:
15539 		/* No retry count to check. Just proceed with the retry */
15540 		break;
15541 	}
15542 
15543 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15544 
15545 	/*
15546 	 * If we were given a zero timeout, we must attempt to retry the
15547 	 * command immediately (ie, without a delay).
15548 	 */
15549 	if (retry_delay == 0) {
15550 		/*
15551 		 * Check some limiting conditions to see if we can actually
15552 		 * do the immediate retry.  If we cannot, then we must
15553 		 * fall back to queueing up a delayed retry.
15554 		 */
15555 		if (un->un_ncmds_in_transport >= un->un_throttle) {
15556 			/*
15557 			 * We are at the throttle limit for the target,
15558 			 * fall back to delayed retry.
15559 			 */
15560 			retry_delay = SD_BSY_TIMEOUT;
15561 			statp = kstat_waitq_enter;
15562 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15563 			    "sd_retry_command: immed. retry hit "
15564 			    "throttle!\n");
15565 		} else {
15566 			/*
15567 			 * We're clear to proceed with the immediate retry.
15568 			 * First call the user-provided function (if any)
15569 			 */
15570 			if (user_funcp != NULL) {
15571 				(*user_funcp)(un, bp, user_arg,
15572 				    SD_IMMEDIATE_RETRY_ISSUED);
15573 #ifdef __lock_lint
15574 				sd_print_incomplete_msg(un, bp, user_arg,
15575 				    SD_IMMEDIATE_RETRY_ISSUED);
15576 				sd_print_cmd_incomplete_msg(un, bp, user_arg,
15577 				    SD_IMMEDIATE_RETRY_ISSUED);
15578 				sd_print_sense_failed_msg(un, bp, user_arg,
15579 				    SD_IMMEDIATE_RETRY_ISSUED);
15580 #endif
15581 			}
15582 
15583 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15584 			    "sd_retry_command: issuing immediate retry\n");
15585 
15586 			/*
15587 			 * Call sd_start_cmds() to transport the command to
15588 			 * the target.
15589 			 */
15590 			sd_start_cmds(un, bp);
15591 
15592 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15593 			    "sd_retry_command exit\n");
15594 			return;
15595 		}
15596 	}
15597 
15598 	/*
15599 	 * Set up to retry the command after a delay.
15600 	 * First call the user-provided function (if any)
15601 	 */
15602 	if (user_funcp != NULL) {
15603 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15604 	}
15605 
15606 	sd_set_retry_bp(un, bp, retry_delay, statp);
15607 
15608 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15609 	return;
15610 
15611 fail_command:
15612 
15613 	if (user_funcp != NULL) {
15614 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15615 	}
15616 
15617 fail_command_no_log:
15618 
15619 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15620 	    "sd_retry_command: returning failed command\n");
15621 
15622 	sd_return_failed_command(un, bp, failure_code);
15623 
15624 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15625 }
15626 
15627 
15628 /*
15629  *    Function: sd_set_retry_bp
15630  *
15631  * Description: Set up the given bp for retry.
15632  *
15633  *   Arguments: un - ptr to associated softstate
15634  *		bp - ptr to buf(9S) for the command
15635  *		retry_delay - time interval before issuing retry (may be 0)
15636  *		statp - optional pointer to kstat function
15637  *
15638  *     Context: May be called under interrupt context
15639  */
15640 
15641 static void
15642 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15643 	void (*statp)(kstat_io_t *))
15644 {
15645 	ASSERT(un != NULL);
15646 	ASSERT(mutex_owned(SD_MUTEX(un)));
15647 	ASSERT(bp != NULL);
15648 
15649 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15650 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15651 
15652 	/*
15653 	 * Indicate that the command is being retried. This will not allow any
15654 	 * other commands on the wait queue to be transported to the target
15655 	 * until this command has been completed (success or failure). The
15656 	 * "retry command" is not transported to the target until the given
15657 	 * time delay expires, unless the user specified a 0 retry_delay.
15658 	 *
15659 	 * Note: the timeout(9F) callback routine is what actually calls
15660 	 * sd_start_cmds() to transport the command, with the exception of a
15661 	 * zero retry_delay. The only current implementor of a zero retry delay
15662 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
15663 	 */
15664 	if (un->un_retry_bp == NULL) {
15665 		ASSERT(un->un_retry_statp == NULL);
15666 		un->un_retry_bp = bp;
15667 
15668 		/*
15669 		 * If the user has not specified a delay the command should
15670 		 * be queued and no timeout should be scheduled.
15671 		 */
15672 		if (retry_delay == 0) {
15673 			/*
15674 			 * Save the kstat pointer that will be used in the
15675 			 * call to SD_UPDATE_KSTATS() below, so that
15676 			 * sd_start_cmds() can correctly decrement the waitq
15677 			 * count when it is time to transport this command.
15678 			 */
15679 			un->un_retry_statp = statp;
15680 			goto done;
15681 		}
15682 	}
15683 
15684 	if (un->un_retry_bp == bp) {
15685 		/*
15686 		 * Save the kstat pointer that will be used in the call to
15687 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
15688 		 * correctly decrement the waitq count when it is time to
15689 		 * transport this command.
15690 		 */
15691 		un->un_retry_statp = statp;
15692 
15693 		/*
15694 		 * Schedule a timeout if:
15695 		 *   1) The user has specified a delay.
15696 		 *   2) There is not a START_STOP_UNIT callback pending.
15697 		 *
15698 		 * If no delay has been specified, then it is up to the caller
15699 		 * to ensure that IO processing continues without stalling.
15700 		 * Effectively, this means that the caller will issue the
15701 		 * required call to sd_start_cmds(). The START_STOP_UNIT
15702 		 * callback does this after the START STOP UNIT command has
15703 		 * completed. In either of these cases we should not schedule
15704 		 * a timeout callback here.  Also don't schedule the timeout if
15705 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
15706 		 */
15707 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
15708 		    (un->un_direct_priority_timeid == NULL)) {
15709 			un->un_retry_timeid =
15710 			    timeout(sd_start_retry_command, un, retry_delay);
15711 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15712 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
15713 			    " bp:0x%p un_retry_timeid:0x%p\n",
15714 			    un, bp, un->un_retry_timeid);
15715 		}
15716 	} else {
15717 		/*
15718 		 * We only get in here if there is already another command
15719 		 * waiting to be retried.  In this case, we just put the
15720 		 * given command onto the wait queue, so it can be transported
15721 		 * after the current retry command has completed.
15722 		 *
15723 		 * Also we have to make sure that if the command at the head
15724 		 * of the wait queue is the un_failfast_bp, that we do not
15725 		 * put ahead of it any other commands that are to be retried.
15726 		 */
15727 		if ((un->un_failfast_bp != NULL) &&
15728 		    (un->un_failfast_bp == un->un_waitq_headp)) {
15729 			/*
15730 			 * Enqueue this command AFTER the first command on
15731 			 * the wait queue (which is also un_failfast_bp).
15732 			 */
15733 			bp->av_forw = un->un_waitq_headp->av_forw;
15734 			un->un_waitq_headp->av_forw = bp;
15735 			if (un->un_waitq_headp == un->un_waitq_tailp) {
15736 				un->un_waitq_tailp = bp;
15737 			}
15738 		} else {
15739 			/* Enqueue this command at the head of the waitq. */
15740 			bp->av_forw = un->un_waitq_headp;
15741 			un->un_waitq_headp = bp;
15742 			if (un->un_waitq_tailp == NULL) {
15743 				un->un_waitq_tailp = bp;
15744 			}
15745 		}
15746 
15747 		if (statp == NULL) {
15748 			statp = kstat_waitq_enter;
15749 		}
15750 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15751 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
15752 	}
15753 
15754 done:
15755 	if (statp != NULL) {
15756 		SD_UPDATE_KSTATS(un, statp, bp);
15757 	}
15758 
15759 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15760 	    "sd_set_retry_bp: exit un:0x%p\n", un);
15761 }
15762 
15763 
15764 /*
15765  *    Function: sd_start_retry_command
15766  *
15767  * Description: Start the command that has been waiting on the target's
15768  *		retry queue.  Called from timeout(9F) context after the
15769  *		retry delay interval has expired.
15770  *
15771  *   Arguments: arg - pointer to associated softstate for the device.
15772  *
15773  *     Context: timeout(9F) thread context.  May not sleep.
15774  */
15775 
15776 static void
15777 sd_start_retry_command(void *arg)
15778 {
15779 	struct sd_lun *un = arg;
15780 
15781 	ASSERT(un != NULL);
15782 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15783 
15784 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15785 	    "sd_start_retry_command: entry\n");
15786 
15787 	mutex_enter(SD_MUTEX(un));
15788 
15789 	un->un_retry_timeid = NULL;
15790 
15791 	if (un->un_retry_bp != NULL) {
15792 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15793 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
15794 		    un, un->un_retry_bp);
15795 		sd_start_cmds(un, un->un_retry_bp);
15796 	}
15797 
15798 	mutex_exit(SD_MUTEX(un));
15799 
15800 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15801 	    "sd_start_retry_command: exit\n");
15802 }
15803 
15804 
15805 /*
15806  *    Function: sd_start_direct_priority_command
15807  *
15808  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
15809  *		received TRAN_BUSY when we called scsi_transport() to send it
15810  *		to the underlying HBA. This function is called from timeout(9F)
15811  *		context after the delay interval has expired.
15812  *
15813  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
15814  *
15815  *     Context: timeout(9F) thread context.  May not sleep.
15816  */
15817 
15818 static void
15819 sd_start_direct_priority_command(void *arg)
15820 {
15821 	struct buf	*priority_bp = arg;
15822 	struct sd_lun	*un;
15823 
15824 	ASSERT(priority_bp != NULL);
15825 	un = SD_GET_UN(priority_bp);
15826 	ASSERT(un != NULL);
15827 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15828 
15829 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15830 	    "sd_start_direct_priority_command: entry\n");
15831 
15832 	mutex_enter(SD_MUTEX(un));
15833 	un->un_direct_priority_timeid = NULL;
15834 	sd_start_cmds(un, priority_bp);
15835 	mutex_exit(SD_MUTEX(un));
15836 
15837 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15838 	    "sd_start_direct_priority_command: exit\n");
15839 }
15840 
15841 
15842 /*
15843  *    Function: sd_send_request_sense_command
15844  *
15845  * Description: Sends a REQUEST SENSE command to the target
15846  *
15847  *     Context: May be called from interrupt context.
15848  */
15849 
15850 static void
15851 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
15852 	struct scsi_pkt *pktp)
15853 {
15854 	ASSERT(bp != NULL);
15855 	ASSERT(un != NULL);
15856 	ASSERT(mutex_owned(SD_MUTEX(un)));
15857 
15858 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
15859 	    "entry: buf:0x%p\n", bp);
15860 
15861 	/*
15862 	 * If we are syncing or dumping, then fail the command to avoid a
15863 	 * recursive callback into scsi_transport(). Also fail the command
15864 	 * if we are suspended (legacy behavior).
15865 	 */
15866 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
15867 	    (un->un_state == SD_STATE_DUMPING)) {
15868 		sd_return_failed_command(un, bp, EIO);
15869 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15870 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
15871 		return;
15872 	}
15873 
15874 	/*
15875 	 * Retry the failed command and don't issue the request sense if:
15876 	 *    1) the sense buf is busy
15877 	 *    2) we have 1 or more outstanding commands on the target
15878 	 *    (the sense data will be cleared or invalidated any way)
15879 	 *
15880 	 * Note: There could be an issue with not checking a retry limit here,
15881 	 * the problem is determining which retry limit to check.
15882 	 */
15883 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
15884 		/* Don't retry if the command is flagged as non-retryable */
15885 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
15886 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15887 			    NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter);
15888 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15889 			    "sd_send_request_sense_command: "
15890 			    "at full throttle, retrying exit\n");
15891 		} else {
15892 			sd_return_failed_command(un, bp, EIO);
15893 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15894 			    "sd_send_request_sense_command: "
15895 			    "at full throttle, non-retryable exit\n");
15896 		}
15897 		return;
15898 	}
15899 
15900 	sd_mark_rqs_busy(un, bp);
15901 	sd_start_cmds(un, un->un_rqs_bp);
15902 
15903 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15904 	    "sd_send_request_sense_command: exit\n");
15905 }
15906 
15907 
15908 /*
15909  *    Function: sd_mark_rqs_busy
15910  *
15911  * Description: Indicate that the request sense bp for this instance is
15912  *		in use.
15913  *
15914  *     Context: May be called under interrupt context
15915  */
15916 
15917 static void
15918 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
15919 {
15920 	struct sd_xbuf	*sense_xp;
15921 
15922 	ASSERT(un != NULL);
15923 	ASSERT(bp != NULL);
15924 	ASSERT(mutex_owned(SD_MUTEX(un)));
15925 	ASSERT(un->un_sense_isbusy == 0);
15926 
15927 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
15928 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
15929 
15930 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
15931 	ASSERT(sense_xp != NULL);
15932 
15933 	SD_INFO(SD_LOG_IO, un,
15934 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
15935 
15936 	ASSERT(sense_xp->xb_pktp != NULL);
15937 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
15938 	    == (FLAG_SENSING | FLAG_HEAD));
15939 
15940 	un->un_sense_isbusy = 1;
15941 	un->un_rqs_bp->b_resid = 0;
15942 	sense_xp->xb_pktp->pkt_resid  = 0;
15943 	sense_xp->xb_pktp->pkt_reason = 0;
15944 
15945 	/* So we can get back the bp at interrupt time! */
15946 	sense_xp->xb_sense_bp = bp;
15947 
15948 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
15949 
15950 	/*
15951 	 * Mark this buf as awaiting sense data. (This is already set in
15952 	 * the pkt_flags for the RQS packet.)
15953 	 */
15954 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
15955 
15956 	sense_xp->xb_retry_count	= 0;
15957 	sense_xp->xb_victim_retry_count = 0;
15958 	sense_xp->xb_ua_retry_count	= 0;
15959 	sense_xp->xb_dma_resid  = 0;
15960 
15961 	/* Clean up the fields for auto-request sense */
15962 	sense_xp->xb_sense_status = 0;
15963 	sense_xp->xb_sense_state  = 0;
15964 	sense_xp->xb_sense_resid  = 0;
15965 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
15966 
15967 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
15968 }
15969 
15970 
15971 /*
15972  *    Function: sd_mark_rqs_idle
15973  *
15974  * Description: SD_MUTEX must be held continuously through this routine
15975  *		to prevent reuse of the rqs struct before the caller can
15976  *		complete it's processing.
15977  *
15978  * Return Code: Pointer to the RQS buf
15979  *
15980  *     Context: May be called under interrupt context
15981  */
15982 
15983 static struct buf *
15984 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
15985 {
15986 	struct buf *bp;
15987 	ASSERT(un != NULL);
15988 	ASSERT(sense_xp != NULL);
15989 	ASSERT(mutex_owned(SD_MUTEX(un)));
15990 	ASSERT(un->un_sense_isbusy != 0);
15991 
15992 	un->un_sense_isbusy = 0;
15993 	bp = sense_xp->xb_sense_bp;
15994 	sense_xp->xb_sense_bp = NULL;
15995 
15996 	/* This pkt is no longer interested in getting sense data */
15997 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
15998 
15999 	return (bp);
16000 }
16001 
16002 
16003 
16004 /*
16005  *    Function: sd_alloc_rqs
16006  *
16007  * Description: Set up the unit to receive auto request sense data
16008  *
16009  * Return Code: DDI_SUCCESS or DDI_FAILURE
16010  *
16011  *     Context: Called under attach(9E) context
16012  */
16013 
16014 static int
16015 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16016 {
16017 	struct sd_xbuf *xp;
16018 
16019 	ASSERT(un != NULL);
16020 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16021 	ASSERT(un->un_rqs_bp == NULL);
16022 	ASSERT(un->un_rqs_pktp == NULL);
16023 
16024 	/*
16025 	 * First allocate the required buf and scsi_pkt structs, then set up
16026 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
16027 	 */
16028 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16029 	    SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16030 	if (un->un_rqs_bp == NULL) {
16031 		return (DDI_FAILURE);
16032 	}
16033 
16034 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16035 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16036 
16037 	if (un->un_rqs_pktp == NULL) {
16038 		sd_free_rqs(un);
16039 		return (DDI_FAILURE);
16040 	}
16041 
16042 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16043 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16044 	    SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
16045 
16046 	SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16047 
16048 	/* Set up the other needed members in the ARQ scsi_pkt. */
16049 	un->un_rqs_pktp->pkt_comp   = sdintr;
16050 	un->un_rqs_pktp->pkt_time   = sd_io_time;
16051 	un->un_rqs_pktp->pkt_flags |=
16052 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
16053 
16054 	/*
16055 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16056 	 * provide any intpkt, destroypkt routines as we take care of
16057 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16058 	 */
16059 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16060 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16061 	xp->xb_pktp = un->un_rqs_pktp;
16062 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
16063 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16064 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16065 
16066 	/*
16067 	 * Save the pointer to the request sense private bp so it can
16068 	 * be retrieved in sdintr.
16069 	 */
16070 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16071 	ASSERT(un->un_rqs_bp->b_private == xp);
16072 
16073 	/*
16074 	 * See if the HBA supports auto-request sense for the specified
16075 	 * target/lun. If it does, then try to enable it (if not already
16076 	 * enabled).
16077 	 *
16078 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16079 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16080 	 * return success.  However, in both of these cases ARQ is always
16081 	 * enabled and scsi_ifgetcap will always return true. The best approach
16082 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16083 	 *
16084 	 * The 3rd case is the HBA (adp) always return enabled on
16085 	 * scsi_ifgetgetcap even when it's not enable, the best approach
16086 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16087 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16088 	 */
16089 
16090 	if (un->un_f_is_fibre == TRUE) {
16091 		un->un_f_arq_enabled = TRUE;
16092 	} else {
16093 #if defined(__i386) || defined(__amd64)
16094 		/*
16095 		 * Circumvent the Adaptec bug, remove this code when
16096 		 * the bug is fixed
16097 		 */
16098 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16099 #endif
16100 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16101 		case 0:
16102 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16103 				"sd_alloc_rqs: HBA supports ARQ\n");
16104 			/*
16105 			 * ARQ is supported by this HBA but currently is not
16106 			 * enabled. Attempt to enable it and if successful then
16107 			 * mark this instance as ARQ enabled.
16108 			 */
16109 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16110 				== 1) {
16111 				/* Successfully enabled ARQ in the HBA */
16112 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16113 					"sd_alloc_rqs: ARQ enabled\n");
16114 				un->un_f_arq_enabled = TRUE;
16115 			} else {
16116 				/* Could not enable ARQ in the HBA */
16117 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16118 				"sd_alloc_rqs: failed ARQ enable\n");
16119 				un->un_f_arq_enabled = FALSE;
16120 			}
16121 			break;
16122 		case 1:
16123 			/*
16124 			 * ARQ is supported by this HBA and is already enabled.
16125 			 * Just mark ARQ as enabled for this instance.
16126 			 */
16127 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16128 				"sd_alloc_rqs: ARQ already enabled\n");
16129 			un->un_f_arq_enabled = TRUE;
16130 			break;
16131 		default:
16132 			/*
16133 			 * ARQ is not supported by this HBA; disable it for this
16134 			 * instance.
16135 			 */
16136 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16137 				"sd_alloc_rqs: HBA does not support ARQ\n");
16138 			un->un_f_arq_enabled = FALSE;
16139 			break;
16140 		}
16141 	}
16142 
16143 	return (DDI_SUCCESS);
16144 }
16145 
16146 
16147 /*
16148  *    Function: sd_free_rqs
16149  *
16150  * Description: Cleanup for the pre-instance RQS command.
16151  *
16152  *     Context: Kernel thread context
16153  */
16154 
16155 static void
16156 sd_free_rqs(struct sd_lun *un)
16157 {
16158 	ASSERT(un != NULL);
16159 
16160 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16161 
16162 	/*
16163 	 * If consistent memory is bound to a scsi_pkt, the pkt
16164 	 * has to be destroyed *before* freeing the consistent memory.
16165 	 * Don't change the sequence of this operations.
16166 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
16167 	 * after it was freed in scsi_free_consistent_buf().
16168 	 */
16169 	if (un->un_rqs_pktp != NULL) {
16170 		scsi_destroy_pkt(un->un_rqs_pktp);
16171 		un->un_rqs_pktp = NULL;
16172 	}
16173 
16174 	if (un->un_rqs_bp != NULL) {
16175 		kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf));
16176 		scsi_free_consistent_buf(un->un_rqs_bp);
16177 		un->un_rqs_bp = NULL;
16178 	}
16179 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16180 }
16181 
16182 
16183 
16184 /*
16185  *    Function: sd_reduce_throttle
16186  *
16187  * Description: Reduces the maximun # of outstanding commands on a
16188  *		target to the current number of outstanding commands.
16189  *		Queues a tiemout(9F) callback to restore the limit
16190  *		after a specified interval has elapsed.
16191  *		Typically used when we get a TRAN_BUSY return code
16192  *		back from scsi_transport().
16193  *
16194  *   Arguments: un - ptr to the sd_lun softstate struct
16195  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16196  *
16197  *     Context: May be called from interrupt context
16198  */
16199 
16200 static void
16201 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16202 {
16203 	ASSERT(un != NULL);
16204 	ASSERT(mutex_owned(SD_MUTEX(un)));
16205 	ASSERT(un->un_ncmds_in_transport >= 0);
16206 
16207 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16208 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16209 	    un, un->un_throttle, un->un_ncmds_in_transport);
16210 
16211 	if (un->un_throttle > 1) {
16212 		if (un->un_f_use_adaptive_throttle == TRUE) {
16213 			switch (throttle_type) {
16214 			case SD_THROTTLE_TRAN_BUSY:
16215 				if (un->un_busy_throttle == 0) {
16216 					un->un_busy_throttle = un->un_throttle;
16217 				}
16218 				break;
16219 			case SD_THROTTLE_QFULL:
16220 				un->un_busy_throttle = 0;
16221 				break;
16222 			default:
16223 				ASSERT(FALSE);
16224 			}
16225 
16226 			if (un->un_ncmds_in_transport > 0) {
16227 			    un->un_throttle = un->un_ncmds_in_transport;
16228 			}
16229 
16230 		} else {
16231 			if (un->un_ncmds_in_transport == 0) {
16232 				un->un_throttle = 1;
16233 			} else {
16234 				un->un_throttle = un->un_ncmds_in_transport;
16235 			}
16236 		}
16237 	}
16238 
16239 	/* Reschedule the timeout if none is currently active */
16240 	if (un->un_reset_throttle_timeid == NULL) {
16241 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16242 		    un, SD_THROTTLE_RESET_INTERVAL);
16243 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16244 		    "sd_reduce_throttle: timeout scheduled!\n");
16245 	}
16246 
16247 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16248 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16249 }
16250 
16251 
16252 
16253 /*
16254  *    Function: sd_restore_throttle
16255  *
16256  * Description: Callback function for timeout(9F).  Resets the current
16257  *		value of un->un_throttle to its default.
16258  *
16259  *   Arguments: arg - pointer to associated softstate for the device.
16260  *
16261  *     Context: May be called from interrupt context
16262  */
16263 
16264 static void
16265 sd_restore_throttle(void *arg)
16266 {
16267 	struct sd_lun	*un = arg;
16268 
16269 	ASSERT(un != NULL);
16270 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16271 
16272 	mutex_enter(SD_MUTEX(un));
16273 
16274 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16275 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16276 
16277 	un->un_reset_throttle_timeid = NULL;
16278 
16279 	if (un->un_f_use_adaptive_throttle == TRUE) {
16280 		/*
16281 		 * If un_busy_throttle is nonzero, then it contains the
16282 		 * value that un_throttle was when we got a TRAN_BUSY back
16283 		 * from scsi_transport(). We want to revert back to this
16284 		 * value.
16285 		 *
16286 		 * In the QFULL case, the throttle limit will incrementally
16287 		 * increase until it reaches max throttle.
16288 		 */
16289 		if (un->un_busy_throttle > 0) {
16290 			un->un_throttle = un->un_busy_throttle;
16291 			un->un_busy_throttle = 0;
16292 		} else {
16293 			/*
16294 			 * increase throttle by 10% open gate slowly, schedule
16295 			 * another restore if saved throttle has not been
16296 			 * reached
16297 			 */
16298 			short throttle;
16299 			if (sd_qfull_throttle_enable) {
16300 				throttle = un->un_throttle +
16301 				    max((un->un_throttle / 10), 1);
16302 				un->un_throttle =
16303 				    (throttle < un->un_saved_throttle) ?
16304 				    throttle : un->un_saved_throttle;
16305 				if (un->un_throttle < un->un_saved_throttle) {
16306 				    un->un_reset_throttle_timeid =
16307 					timeout(sd_restore_throttle,
16308 					un, SD_QFULL_THROTTLE_RESET_INTERVAL);
16309 				}
16310 			}
16311 		}
16312 
16313 		/*
16314 		 * If un_throttle has fallen below the low-water mark, we
16315 		 * restore the maximum value here (and allow it to ratchet
16316 		 * down again if necessary).
16317 		 */
16318 		if (un->un_throttle < un->un_min_throttle) {
16319 			un->un_throttle = un->un_saved_throttle;
16320 		}
16321 	} else {
16322 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16323 		    "restoring limit from 0x%x to 0x%x\n",
16324 		    un->un_throttle, un->un_saved_throttle);
16325 		un->un_throttle = un->un_saved_throttle;
16326 	}
16327 
16328 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16329 	    "sd_restore_throttle: calling sd_start_cmds!\n");
16330 
16331 	sd_start_cmds(un, NULL);
16332 
16333 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16334 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16335 	    un, un->un_throttle);
16336 
16337 	mutex_exit(SD_MUTEX(un));
16338 
16339 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16340 }
16341 
16342 /*
16343  *    Function: sdrunout
16344  *
16345  * Description: Callback routine for scsi_init_pkt when a resource allocation
16346  *		fails.
16347  *
16348  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16349  *		soft state instance.
16350  *
16351  * Return Code: The scsi_init_pkt routine allows for the callback function to
16352  *		return a 0 indicating the callback should be rescheduled or a 1
16353  *		indicating not to reschedule. This routine always returns 1
16354  *		because the driver always provides a callback function to
16355  *		scsi_init_pkt. This results in a callback always being scheduled
16356  *		(via the scsi_init_pkt callback implementation) if a resource
16357  *		failure occurs.
16358  *
16359  *     Context: This callback function may not block or call routines that block
16360  *
16361  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16362  *		request persisting at the head of the list which cannot be
16363  *		satisfied even after multiple retries. In the future the driver
16364  *		may implement some time of maximum runout count before failing
16365  *		an I/O.
16366  */
16367 
16368 static int
16369 sdrunout(caddr_t arg)
16370 {
16371 	struct sd_lun	*un = (struct sd_lun *)arg;
16372 
16373 	ASSERT(un != NULL);
16374 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16375 
16376 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16377 
16378 	mutex_enter(SD_MUTEX(un));
16379 	sd_start_cmds(un, NULL);
16380 	mutex_exit(SD_MUTEX(un));
16381 	/*
16382 	 * This callback routine always returns 1 (i.e. do not reschedule)
16383 	 * because we always specify sdrunout as the callback handler for
16384 	 * scsi_init_pkt inside the call to sd_start_cmds.
16385 	 */
16386 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16387 	return (1);
16388 }
16389 
16390 
16391 /*
16392  *    Function: sdintr
16393  *
16394  * Description: Completion callback routine for scsi_pkt(9S) structs
16395  *		sent to the HBA driver via scsi_transport(9F).
16396  *
16397  *     Context: Interrupt context
16398  */
16399 
16400 static void
16401 sdintr(struct scsi_pkt *pktp)
16402 {
16403 	struct buf	*bp;
16404 	struct sd_xbuf	*xp;
16405 	struct sd_lun	*un;
16406 
16407 	ASSERT(pktp != NULL);
16408 	bp = (struct buf *)pktp->pkt_private;
16409 	ASSERT(bp != NULL);
16410 	xp = SD_GET_XBUF(bp);
16411 	ASSERT(xp != NULL);
16412 	ASSERT(xp->xb_pktp != NULL);
16413 	un = SD_GET_UN(bp);
16414 	ASSERT(un != NULL);
16415 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16416 
16417 #ifdef SD_FAULT_INJECTION
16418 
16419 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16420 	/* SD FaultInjection */
16421 	sd_faultinjection(pktp);
16422 
16423 #endif /* SD_FAULT_INJECTION */
16424 
16425 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16426 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
16427 
16428 	mutex_enter(SD_MUTEX(un));
16429 
16430 	/* Reduce the count of the #commands currently in transport */
16431 	un->un_ncmds_in_transport--;
16432 	ASSERT(un->un_ncmds_in_transport >= 0);
16433 
16434 	/* Increment counter to indicate that the callback routine is active */
16435 	un->un_in_callback++;
16436 
16437 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16438 
16439 #ifdef	SDDEBUG
16440 	if (bp == un->un_retry_bp) {
16441 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16442 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16443 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
16444 	}
16445 #endif
16446 
16447 	/*
16448 	 * If pkt_reason is CMD_DEV_GONE, just fail the command
16449 	 */
16450 	if (pktp->pkt_reason == CMD_DEV_GONE) {
16451 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16452 			    "Device is gone\n");
16453 		sd_return_failed_command(un, bp, EIO);
16454 		goto exit;
16455 	}
16456 
16457 	/*
16458 	 * First see if the pkt has auto-request sense data with it....
16459 	 * Look at the packet state first so we don't take a performance
16460 	 * hit looking at the arq enabled flag unless absolutely necessary.
16461 	 */
16462 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16463 	    (un->un_f_arq_enabled == TRUE)) {
16464 		/*
16465 		 * The HBA did an auto request sense for this command so check
16466 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16467 		 * driver command that should not be retried.
16468 		 */
16469 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16470 			/*
16471 			 * Save the relevant sense info into the xp for the
16472 			 * original cmd.
16473 			 */
16474 			struct scsi_arq_status *asp;
16475 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16476 			xp->xb_sense_status =
16477 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
16478 			xp->xb_sense_state  = asp->sts_rqpkt_state;
16479 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16480 			bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16481 			    min(sizeof (struct scsi_extended_sense),
16482 			    SENSE_LENGTH));
16483 
16484 			/* fail the command */
16485 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16486 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
16487 			sd_return_failed_command(un, bp, EIO);
16488 			goto exit;
16489 		}
16490 
16491 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16492 		/*
16493 		 * We want to either retry or fail this command, so free
16494 		 * the DMA resources here.  If we retry the command then
16495 		 * the DMA resources will be reallocated in sd_start_cmds().
16496 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16497 		 * causes the *entire* transfer to start over again from the
16498 		 * beginning of the request, even for PARTIAL chunks that
16499 		 * have already transferred successfully.
16500 		 */
16501 		if ((un->un_f_is_fibre == TRUE) &&
16502 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16503 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16504 			scsi_dmafree(pktp);
16505 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16506 		}
16507 #endif
16508 
16509 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16510 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
16511 
16512 		sd_handle_auto_request_sense(un, bp, xp, pktp);
16513 		goto exit;
16514 	}
16515 
16516 	/* Next see if this is the REQUEST SENSE pkt for the instance */
16517 	if (pktp->pkt_flags & FLAG_SENSING)  {
16518 		/* This pktp is from the unit's REQUEST_SENSE command */
16519 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16520 		    "sdintr: sd_handle_request_sense\n");
16521 		sd_handle_request_sense(un, bp, xp, pktp);
16522 		goto exit;
16523 	}
16524 
16525 	/*
16526 	 * Check to see if the command successfully completed as requested;
16527 	 * this is the most common case (and also the hot performance path).
16528 	 *
16529 	 * Requirements for successful completion are:
16530 	 * pkt_reason is CMD_CMPLT and packet status is status good.
16531 	 * In addition:
16532 	 * - A residual of zero indicates successful completion no matter what
16533 	 *   the command is.
16534 	 * - If the residual is not zero and the command is not a read or
16535 	 *   write, then it's still defined as successful completion. In other
16536 	 *   words, if the command is a read or write the residual must be
16537 	 *   zero for successful completion.
16538 	 * - If the residual is not zero and the command is a read or
16539 	 *   write, and it's a USCSICMD, then it's still defined as
16540 	 *   successful completion.
16541 	 */
16542 	if ((pktp->pkt_reason == CMD_CMPLT) &&
16543 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16544 
16545 		/*
16546 		 * Since this command is returned with a good status, we
16547 		 * can reset the count for Sonoma failover.
16548 		 */
16549 		un->un_sonoma_failure_count = 0;
16550 
16551 		/*
16552 		 * Return all USCSI commands on good status
16553 		 */
16554 		if (pktp->pkt_resid == 0) {
16555 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16556 			    "sdintr: returning command for resid == 0\n");
16557 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
16558 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
16559 			SD_UPDATE_B_RESID(bp, pktp);
16560 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16561 			    "sdintr: returning command for resid != 0\n");
16562 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16563 			SD_UPDATE_B_RESID(bp, pktp);
16564 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16565 				"sdintr: returning uscsi command\n");
16566 		} else {
16567 			goto not_successful;
16568 		}
16569 		sd_return_command(un, bp);
16570 
16571 		/*
16572 		 * Decrement counter to indicate that the callback routine
16573 		 * is done.
16574 		 */
16575 		un->un_in_callback--;
16576 		ASSERT(un->un_in_callback >= 0);
16577 		mutex_exit(SD_MUTEX(un));
16578 
16579 		return;
16580 	}
16581 
16582 not_successful:
16583 
16584 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16585 	/*
16586 	 * The following is based upon knowledge of the underlying transport
16587 	 * and its use of DMA resources.  This code should be removed when
16588 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
16589 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
16590 	 * and sd_start_cmds().
16591 	 *
16592 	 * Free any DMA resources associated with this command if there
16593 	 * is a chance it could be retried or enqueued for later retry.
16594 	 * If we keep the DMA binding then mpxio cannot reissue the
16595 	 * command on another path whenever a path failure occurs.
16596 	 *
16597 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
16598 	 * causes the *entire* transfer to start over again from the
16599 	 * beginning of the request, even for PARTIAL chunks that
16600 	 * have already transferred successfully.
16601 	 *
16602 	 * This is only done for non-uscsi commands (and also skipped for the
16603 	 * driver's internal RQS command). Also just do this for Fibre Channel
16604 	 * devices as these are the only ones that support mpxio.
16605 	 */
16606 	if ((un->un_f_is_fibre == TRUE) &&
16607 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16608 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16609 		scsi_dmafree(pktp);
16610 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16611 	}
16612 #endif
16613 
16614 	/*
16615 	 * The command did not successfully complete as requested so check
16616 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16617 	 * driver command that should not be retried so just return. If
16618 	 * FLAG_DIAGNOSE is not set the error will be processed below.
16619 	 */
16620 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16621 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16622 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
16623 		/*
16624 		 * Issue a request sense if a check condition caused the error
16625 		 * (we handle the auto request sense case above), otherwise
16626 		 * just fail the command.
16627 		 */
16628 		if ((pktp->pkt_reason == CMD_CMPLT) &&
16629 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
16630 			sd_send_request_sense_command(un, bp, pktp);
16631 		} else {
16632 			sd_return_failed_command(un, bp, EIO);
16633 		}
16634 		goto exit;
16635 	}
16636 
16637 	/*
16638 	 * The command did not successfully complete as requested so process
16639 	 * the error, retry, and/or attempt recovery.
16640 	 */
16641 	switch (pktp->pkt_reason) {
16642 	case CMD_CMPLT:
16643 		switch (SD_GET_PKT_STATUS(pktp)) {
16644 		case STATUS_GOOD:
16645 			/*
16646 			 * The command completed successfully with a non-zero
16647 			 * residual
16648 			 */
16649 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16650 			    "sdintr: STATUS_GOOD \n");
16651 			sd_pkt_status_good(un, bp, xp, pktp);
16652 			break;
16653 
16654 		case STATUS_CHECK:
16655 		case STATUS_TERMINATED:
16656 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16657 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
16658 			sd_pkt_status_check_condition(un, bp, xp, pktp);
16659 			break;
16660 
16661 		case STATUS_BUSY:
16662 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16663 			    "sdintr: STATUS_BUSY\n");
16664 			sd_pkt_status_busy(un, bp, xp, pktp);
16665 			break;
16666 
16667 		case STATUS_RESERVATION_CONFLICT:
16668 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16669 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
16670 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
16671 			break;
16672 
16673 		case STATUS_QFULL:
16674 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16675 			    "sdintr: STATUS_QFULL\n");
16676 			sd_pkt_status_qfull(un, bp, xp, pktp);
16677 			break;
16678 
16679 		case STATUS_MET:
16680 		case STATUS_INTERMEDIATE:
16681 		case STATUS_SCSI2:
16682 		case STATUS_INTERMEDIATE_MET:
16683 		case STATUS_ACA_ACTIVE:
16684 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16685 			    "Unexpected SCSI status received: 0x%x\n",
16686 			    SD_GET_PKT_STATUS(pktp));
16687 			sd_return_failed_command(un, bp, EIO);
16688 			break;
16689 
16690 		default:
16691 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16692 			    "Invalid SCSI status received: 0x%x\n",
16693 			    SD_GET_PKT_STATUS(pktp));
16694 			sd_return_failed_command(un, bp, EIO);
16695 			break;
16696 
16697 		}
16698 		break;
16699 
16700 	case CMD_INCOMPLETE:
16701 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16702 		    "sdintr:  CMD_INCOMPLETE\n");
16703 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
16704 		break;
16705 	case CMD_TRAN_ERR:
16706 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16707 		    "sdintr: CMD_TRAN_ERR\n");
16708 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
16709 		break;
16710 	case CMD_RESET:
16711 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16712 		    "sdintr: CMD_RESET \n");
16713 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
16714 		break;
16715 	case CMD_ABORTED:
16716 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16717 		    "sdintr: CMD_ABORTED \n");
16718 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
16719 		break;
16720 	case CMD_TIMEOUT:
16721 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16722 		    "sdintr: CMD_TIMEOUT\n");
16723 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
16724 		break;
16725 	case CMD_UNX_BUS_FREE:
16726 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16727 		    "sdintr: CMD_UNX_BUS_FREE \n");
16728 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
16729 		break;
16730 	case CMD_TAG_REJECT:
16731 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16732 		    "sdintr: CMD_TAG_REJECT\n");
16733 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
16734 		break;
16735 	default:
16736 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16737 		    "sdintr: default\n");
16738 		sd_pkt_reason_default(un, bp, xp, pktp);
16739 		break;
16740 	}
16741 
16742 exit:
16743 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
16744 
16745 	/* Decrement counter to indicate that the callback routine is done. */
16746 	un->un_in_callback--;
16747 	ASSERT(un->un_in_callback >= 0);
16748 
16749 	/*
16750 	 * At this point, the pkt has been dispatched, ie, it is either
16751 	 * being re-tried or has been returned to its caller and should
16752 	 * not be referenced.
16753 	 */
16754 
16755 	mutex_exit(SD_MUTEX(un));
16756 }
16757 
16758 
16759 /*
16760  *    Function: sd_print_incomplete_msg
16761  *
16762  * Description: Prints the error message for a CMD_INCOMPLETE error.
16763  *
16764  *   Arguments: un - ptr to associated softstate for the device.
16765  *		bp - ptr to the buf(9S) for the command.
16766  *		arg - message string ptr
16767  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
16768  *			or SD_NO_RETRY_ISSUED.
16769  *
16770  *     Context: May be called under interrupt context
16771  */
16772 
16773 static void
16774 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
16775 {
16776 	struct scsi_pkt	*pktp;
16777 	char	*msgp;
16778 	char	*cmdp = arg;
16779 
16780 	ASSERT(un != NULL);
16781 	ASSERT(mutex_owned(SD_MUTEX(un)));
16782 	ASSERT(bp != NULL);
16783 	ASSERT(arg != NULL);
16784 	pktp = SD_GET_PKTP(bp);
16785 	ASSERT(pktp != NULL);
16786 
16787 	switch (code) {
16788 	case SD_DELAYED_RETRY_ISSUED:
16789 	case SD_IMMEDIATE_RETRY_ISSUED:
16790 		msgp = "retrying";
16791 		break;
16792 	case SD_NO_RETRY_ISSUED:
16793 	default:
16794 		msgp = "giving up";
16795 		break;
16796 	}
16797 
16798 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
16799 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16800 		    "incomplete %s- %s\n", cmdp, msgp);
16801 	}
16802 }
16803 
16804 
16805 
16806 /*
16807  *    Function: sd_pkt_status_good
16808  *
16809  * Description: Processing for a STATUS_GOOD code in pkt_status.
16810  *
16811  *     Context: May be called under interrupt context
16812  */
16813 
16814 static void
16815 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
16816 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
16817 {
16818 	char	*cmdp;
16819 
16820 	ASSERT(un != NULL);
16821 	ASSERT(mutex_owned(SD_MUTEX(un)));
16822 	ASSERT(bp != NULL);
16823 	ASSERT(xp != NULL);
16824 	ASSERT(pktp != NULL);
16825 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
16826 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
16827 	ASSERT(pktp->pkt_resid != 0);
16828 
16829 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
16830 
16831 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
16832 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
16833 	case SCMD_READ:
16834 		cmdp = "read";
16835 		break;
16836 	case SCMD_WRITE:
16837 		cmdp = "write";
16838 		break;
16839 	default:
16840 		SD_UPDATE_B_RESID(bp, pktp);
16841 		sd_return_command(un, bp);
16842 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16843 		return;
16844 	}
16845 
16846 	/*
16847 	 * See if we can retry the read/write, preferrably immediately.
16848 	 * If retries are exhaused, then sd_retry_command() will update
16849 	 * the b_resid count.
16850 	 */
16851 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
16852 	    cmdp, EIO, (clock_t)0, NULL);
16853 
16854 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16855 }
16856 
16857 
16858 
16859 
16860 
16861 /*
16862  *    Function: sd_handle_request_sense
16863  *
16864  * Description: Processing for non-auto Request Sense command.
16865  *
16866  *   Arguments: un - ptr to associated softstate
16867  *		sense_bp - ptr to buf(9S) for the RQS command
16868  *		sense_xp - ptr to the sd_xbuf for the RQS command
16869  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
16870  *
16871  *     Context: May be called under interrupt context
16872  */
16873 
16874 static void
16875 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
16876 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
16877 {
16878 	struct buf	*cmd_bp;	/* buf for the original command */
16879 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
16880 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
16881 
16882 	ASSERT(un != NULL);
16883 	ASSERT(mutex_owned(SD_MUTEX(un)));
16884 	ASSERT(sense_bp != NULL);
16885 	ASSERT(sense_xp != NULL);
16886 	ASSERT(sense_pktp != NULL);
16887 
16888 	/*
16889 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
16890 	 * RQS command and not the original command.
16891 	 */
16892 	ASSERT(sense_pktp == un->un_rqs_pktp);
16893 	ASSERT(sense_bp   == un->un_rqs_bp);
16894 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
16895 	    (FLAG_SENSING | FLAG_HEAD));
16896 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
16897 	    FLAG_SENSING) == FLAG_SENSING);
16898 
16899 	/* These are the bp, xp, and pktp for the original command */
16900 	cmd_bp = sense_xp->xb_sense_bp;
16901 	cmd_xp = SD_GET_XBUF(cmd_bp);
16902 	cmd_pktp = SD_GET_PKTP(cmd_bp);
16903 
16904 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
16905 		/*
16906 		 * The REQUEST SENSE command failed.  Release the REQUEST
16907 		 * SENSE command for re-use, get back the bp for the original
16908 		 * command, and attempt to re-try the original command if
16909 		 * FLAG_DIAGNOSE is not set in the original packet.
16910 		 */
16911 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
16912 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16913 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
16914 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
16915 			    NULL, NULL, EIO, (clock_t)0, NULL);
16916 			return;
16917 		}
16918 	}
16919 
16920 	/*
16921 	 * Save the relevant sense info into the xp for the original cmd.
16922 	 *
16923 	 * Note: if the request sense failed the state info will be zero
16924 	 * as set in sd_mark_rqs_busy()
16925 	 */
16926 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
16927 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
16928 	cmd_xp->xb_sense_resid  = sense_pktp->pkt_resid;
16929 	bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH);
16930 
16931 	/*
16932 	 *  Free up the RQS command....
16933 	 *  NOTE:
16934 	 *	Must do this BEFORE calling sd_validate_sense_data!
16935 	 *	sd_validate_sense_data may return the original command in
16936 	 *	which case the pkt will be freed and the flags can no
16937 	 *	longer be touched.
16938 	 *	SD_MUTEX is held through this process until the command
16939 	 *	is dispatched based upon the sense data, so there are
16940 	 *	no race conditions.
16941 	 */
16942 	(void) sd_mark_rqs_idle(un, sense_xp);
16943 
16944 	/*
16945 	 * For a retryable command see if we have valid sense data, if so then
16946 	 * turn it over to sd_decode_sense() to figure out the right course of
16947 	 * action. Just fail a non-retryable command.
16948 	 */
16949 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16950 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp) ==
16951 		    SD_SENSE_DATA_IS_VALID) {
16952 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
16953 		}
16954 	} else {
16955 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
16956 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
16957 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
16958 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
16959 		sd_return_failed_command(un, cmd_bp, EIO);
16960 	}
16961 }
16962 
16963 
16964 
16965 
16966 /*
16967  *    Function: sd_handle_auto_request_sense
16968  *
16969  * Description: Processing for auto-request sense information.
16970  *
16971  *   Arguments: un - ptr to associated softstate
16972  *		bp - ptr to buf(9S) for the command
16973  *		xp - ptr to the sd_xbuf for the command
16974  *		pktp - ptr to the scsi_pkt(9S) for the command
16975  *
16976  *     Context: May be called under interrupt context
16977  */
16978 
16979 static void
16980 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
16981 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
16982 {
16983 	struct scsi_arq_status *asp;
16984 
16985 	ASSERT(un != NULL);
16986 	ASSERT(mutex_owned(SD_MUTEX(un)));
16987 	ASSERT(bp != NULL);
16988 	ASSERT(xp != NULL);
16989 	ASSERT(pktp != NULL);
16990 	ASSERT(pktp != un->un_rqs_pktp);
16991 	ASSERT(bp   != un->un_rqs_bp);
16992 
16993 	/*
16994 	 * For auto-request sense, we get a scsi_arq_status back from
16995 	 * the HBA, with the sense data in the sts_sensedata member.
16996 	 * The pkt_scbp of the packet points to this scsi_arq_status.
16997 	 */
16998 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16999 
17000 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17001 		/*
17002 		 * The auto REQUEST SENSE failed; see if we can re-try
17003 		 * the original command.
17004 		 */
17005 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17006 		    "auto request sense failed (reason=%s)\n",
17007 		    scsi_rname(asp->sts_rqpkt_reason));
17008 
17009 		sd_reset_target(un, pktp);
17010 
17011 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17012 		    NULL, NULL, EIO, (clock_t)0, NULL);
17013 		return;
17014 	}
17015 
17016 	/* Save the relevant sense info into the xp for the original cmd. */
17017 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17018 	xp->xb_sense_state  = asp->sts_rqpkt_state;
17019 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17020 	bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17021 	    min(sizeof (struct scsi_extended_sense), SENSE_LENGTH));
17022 
17023 	/*
17024 	 * See if we have valid sense data, if so then turn it over to
17025 	 * sd_decode_sense() to figure out the right course of action.
17026 	 */
17027 	if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) {
17028 		sd_decode_sense(un, bp, xp, pktp);
17029 	}
17030 }
17031 
17032 
17033 /*
17034  *    Function: sd_print_sense_failed_msg
17035  *
17036  * Description: Print log message when RQS has failed.
17037  *
17038  *   Arguments: un - ptr to associated softstate
17039  *		bp - ptr to buf(9S) for the command
17040  *		arg - generic message string ptr
17041  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17042  *			or SD_NO_RETRY_ISSUED
17043  *
17044  *     Context: May be called from interrupt context
17045  */
17046 
17047 static void
17048 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17049 	int code)
17050 {
17051 	char	*msgp = arg;
17052 
17053 	ASSERT(un != NULL);
17054 	ASSERT(mutex_owned(SD_MUTEX(un)));
17055 	ASSERT(bp != NULL);
17056 
17057 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17058 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17059 	}
17060 }
17061 
17062 
17063 /*
17064  *    Function: sd_validate_sense_data
17065  *
17066  * Description: Check the given sense data for validity.
17067  *		If the sense data is not valid, the command will
17068  *		be either failed or retried!
17069  *
17070  * Return Code: SD_SENSE_DATA_IS_INVALID
17071  *		SD_SENSE_DATA_IS_VALID
17072  *
17073  *     Context: May be called from interrupt context
17074  */
17075 
17076 static int
17077 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp)
17078 {
17079 	struct scsi_extended_sense *esp;
17080 	struct	scsi_pkt *pktp;
17081 	size_t	actual_len;
17082 	char	*msgp = NULL;
17083 
17084 	ASSERT(un != NULL);
17085 	ASSERT(mutex_owned(SD_MUTEX(un)));
17086 	ASSERT(bp != NULL);
17087 	ASSERT(bp != un->un_rqs_bp);
17088 	ASSERT(xp != NULL);
17089 
17090 	pktp = SD_GET_PKTP(bp);
17091 	ASSERT(pktp != NULL);
17092 
17093 	/*
17094 	 * Check the status of the RQS command (auto or manual).
17095 	 */
17096 	switch (xp->xb_sense_status & STATUS_MASK) {
17097 	case STATUS_GOOD:
17098 		break;
17099 
17100 	case STATUS_RESERVATION_CONFLICT:
17101 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17102 		return (SD_SENSE_DATA_IS_INVALID);
17103 
17104 	case STATUS_BUSY:
17105 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17106 		    "Busy Status on REQUEST SENSE\n");
17107 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17108 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
17109 		return (SD_SENSE_DATA_IS_INVALID);
17110 
17111 	case STATUS_QFULL:
17112 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17113 		    "QFULL Status on REQUEST SENSE\n");
17114 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17115 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
17116 		return (SD_SENSE_DATA_IS_INVALID);
17117 
17118 	case STATUS_CHECK:
17119 	case STATUS_TERMINATED:
17120 		msgp = "Check Condition on REQUEST SENSE\n";
17121 		goto sense_failed;
17122 
17123 	default:
17124 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17125 		goto sense_failed;
17126 	}
17127 
17128 	/*
17129 	 * See if we got the minimum required amount of sense data.
17130 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17131 	 * or less.
17132 	 */
17133 	actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid);
17134 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17135 	    (actual_len == 0)) {
17136 		msgp = "Request Sense couldn't get sense data\n";
17137 		goto sense_failed;
17138 	}
17139 
17140 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
17141 		msgp = "Not enough sense information\n";
17142 		goto sense_failed;
17143 	}
17144 
17145 	/*
17146 	 * We require the extended sense data
17147 	 */
17148 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17149 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
17150 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17151 			static char tmp[8];
17152 			static char buf[148];
17153 			char *p = (char *)(xp->xb_sense_data);
17154 			int i;
17155 
17156 			mutex_enter(&sd_sense_mutex);
17157 			(void) strcpy(buf, "undecodable sense information:");
17158 			for (i = 0; i < actual_len; i++) {
17159 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17160 				(void) strcpy(&buf[strlen(buf)], tmp);
17161 			}
17162 			i = strlen(buf);
17163 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
17164 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf);
17165 			mutex_exit(&sd_sense_mutex);
17166 		}
17167 		/* Note: Legacy behavior, fail the command with no retry */
17168 		sd_return_failed_command(un, bp, EIO);
17169 		return (SD_SENSE_DATA_IS_INVALID);
17170 	}
17171 
17172 	/*
17173 	 * Check that es_code is valid (es_class concatenated with es_code
17174 	 * make up the "response code" field.  es_class will always be 7, so
17175 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17176 	 * format.
17177 	 */
17178 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17179 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17180 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17181 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17182 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17183 		goto sense_failed;
17184 	}
17185 
17186 	return (SD_SENSE_DATA_IS_VALID);
17187 
17188 sense_failed:
17189 	/*
17190 	 * If the request sense failed (for whatever reason), attempt
17191 	 * to retry the original command.
17192 	 */
17193 #if defined(__i386) || defined(__amd64)
17194 	/*
17195 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17196 	 * sddef.h for Sparc platform, and x86 uses 1 binary
17197 	 * for both SCSI/FC.
17198 	 * The SD_RETRY_DELAY value need to be adjusted here
17199 	 * when SD_RETRY_DELAY change in sddef.h
17200 	 */
17201 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17202 	    sd_print_sense_failed_msg, msgp, EIO,
17203 		un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17204 #else
17205 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17206 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17207 #endif
17208 
17209 	return (SD_SENSE_DATA_IS_INVALID);
17210 }
17211 
17212 
17213 
17214 /*
17215  *    Function: sd_decode_sense
17216  *
17217  * Description: Take recovery action(s) when SCSI Sense Data is received.
17218  *
17219  *     Context: Interrupt context.
17220  */
17221 
17222 static void
17223 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17224 	struct scsi_pkt *pktp)
17225 {
17226 	uint8_t sense_key;
17227 
17228 	ASSERT(un != NULL);
17229 	ASSERT(mutex_owned(SD_MUTEX(un)));
17230 	ASSERT(bp != NULL);
17231 	ASSERT(bp != un->un_rqs_bp);
17232 	ASSERT(xp != NULL);
17233 	ASSERT(pktp != NULL);
17234 
17235 	sense_key = scsi_sense_key(xp->xb_sense_data);
17236 
17237 	switch (sense_key) {
17238 	case KEY_NO_SENSE:
17239 		sd_sense_key_no_sense(un, bp, xp, pktp);
17240 		break;
17241 	case KEY_RECOVERABLE_ERROR:
17242 		sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17243 		    bp, xp, pktp);
17244 		break;
17245 	case KEY_NOT_READY:
17246 		sd_sense_key_not_ready(un, xp->xb_sense_data,
17247 		    bp, xp, pktp);
17248 		break;
17249 	case KEY_MEDIUM_ERROR:
17250 	case KEY_HARDWARE_ERROR:
17251 		sd_sense_key_medium_or_hardware_error(un,
17252 		    xp->xb_sense_data, bp, xp, pktp);
17253 		break;
17254 	case KEY_ILLEGAL_REQUEST:
17255 		sd_sense_key_illegal_request(un, bp, xp, pktp);
17256 		break;
17257 	case KEY_UNIT_ATTENTION:
17258 		sd_sense_key_unit_attention(un, xp->xb_sense_data,
17259 		    bp, xp, pktp);
17260 		break;
17261 	case KEY_WRITE_PROTECT:
17262 	case KEY_VOLUME_OVERFLOW:
17263 	case KEY_MISCOMPARE:
17264 		sd_sense_key_fail_command(un, bp, xp, pktp);
17265 		break;
17266 	case KEY_BLANK_CHECK:
17267 		sd_sense_key_blank_check(un, bp, xp, pktp);
17268 		break;
17269 	case KEY_ABORTED_COMMAND:
17270 		sd_sense_key_aborted_command(un, bp, xp, pktp);
17271 		break;
17272 	case KEY_VENDOR_UNIQUE:
17273 	case KEY_COPY_ABORTED:
17274 	case KEY_EQUAL:
17275 	case KEY_RESERVED:
17276 	default:
17277 		sd_sense_key_default(un, xp->xb_sense_data,
17278 		    bp, xp, pktp);
17279 		break;
17280 	}
17281 }
17282 
17283 
17284 /*
17285  *    Function: sd_dump_memory
17286  *
17287  * Description: Debug logging routine to print the contents of a user provided
17288  *		buffer. The output of the buffer is broken up into 256 byte
17289  *		segments due to a size constraint of the scsi_log.
17290  *		implementation.
17291  *
17292  *   Arguments: un - ptr to softstate
17293  *		comp - component mask
17294  *		title - "title" string to preceed data when printed
17295  *		data - ptr to data block to be printed
17296  *		len - size of data block to be printed
17297  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17298  *
17299  *     Context: May be called from interrupt context
17300  */
17301 
17302 #define	SD_DUMP_MEMORY_BUF_SIZE	256
17303 
17304 static char *sd_dump_format_string[] = {
17305 		" 0x%02x",
17306 		" %c"
17307 };
17308 
17309 static void
17310 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17311     int len, int fmt)
17312 {
17313 	int	i, j;
17314 	int	avail_count;
17315 	int	start_offset;
17316 	int	end_offset;
17317 	size_t	entry_len;
17318 	char	*bufp;
17319 	char	*local_buf;
17320 	char	*format_string;
17321 
17322 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17323 
17324 	/*
17325 	 * In the debug version of the driver, this function is called from a
17326 	 * number of places which are NOPs in the release driver.
17327 	 * The debug driver therefore has additional methods of filtering
17328 	 * debug output.
17329 	 */
17330 #ifdef SDDEBUG
17331 	/*
17332 	 * In the debug version of the driver we can reduce the amount of debug
17333 	 * messages by setting sd_error_level to something other than
17334 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17335 	 * sd_component_mask.
17336 	 */
17337 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17338 	    (sd_error_level != SCSI_ERR_ALL)) {
17339 		return;
17340 	}
17341 	if (((sd_component_mask & comp) == 0) ||
17342 	    (sd_error_level != SCSI_ERR_ALL)) {
17343 		return;
17344 	}
17345 #else
17346 	if (sd_error_level != SCSI_ERR_ALL) {
17347 		return;
17348 	}
17349 #endif
17350 
17351 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17352 	bufp = local_buf;
17353 	/*
17354 	 * Available length is the length of local_buf[], minus the
17355 	 * length of the title string, minus one for the ":", minus
17356 	 * one for the newline, minus one for the NULL terminator.
17357 	 * This gives the #bytes available for holding the printed
17358 	 * values from the given data buffer.
17359 	 */
17360 	if (fmt == SD_LOG_HEX) {
17361 		format_string = sd_dump_format_string[0];
17362 	} else /* SD_LOG_CHAR */ {
17363 		format_string = sd_dump_format_string[1];
17364 	}
17365 	/*
17366 	 * Available count is the number of elements from the given
17367 	 * data buffer that we can fit into the available length.
17368 	 * This is based upon the size of the format string used.
17369 	 * Make one entry and find it's size.
17370 	 */
17371 	(void) sprintf(bufp, format_string, data[0]);
17372 	entry_len = strlen(bufp);
17373 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17374 
17375 	j = 0;
17376 	while (j < len) {
17377 		bufp = local_buf;
17378 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17379 		start_offset = j;
17380 
17381 		end_offset = start_offset + avail_count;
17382 
17383 		(void) sprintf(bufp, "%s:", title);
17384 		bufp += strlen(bufp);
17385 		for (i = start_offset; ((i < end_offset) && (j < len));
17386 		    i++, j++) {
17387 			(void) sprintf(bufp, format_string, data[i]);
17388 			bufp += entry_len;
17389 		}
17390 		(void) sprintf(bufp, "\n");
17391 
17392 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17393 	}
17394 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17395 }
17396 
17397 /*
17398  *    Function: sd_print_sense_msg
17399  *
17400  * Description: Log a message based upon the given sense data.
17401  *
17402  *   Arguments: un - ptr to associated softstate
17403  *		bp - ptr to buf(9S) for the command
17404  *		arg - ptr to associate sd_sense_info struct
17405  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17406  *			or SD_NO_RETRY_ISSUED
17407  *
17408  *     Context: May be called from interrupt context
17409  */
17410 
17411 static void
17412 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17413 {
17414 	struct sd_xbuf	*xp;
17415 	struct scsi_pkt	*pktp;
17416 	uint8_t *sensep;
17417 	daddr_t request_blkno;
17418 	diskaddr_t err_blkno;
17419 	int severity;
17420 	int pfa_flag;
17421 	extern struct scsi_key_strings scsi_cmds[];
17422 
17423 	ASSERT(un != NULL);
17424 	ASSERT(mutex_owned(SD_MUTEX(un)));
17425 	ASSERT(bp != NULL);
17426 	xp = SD_GET_XBUF(bp);
17427 	ASSERT(xp != NULL);
17428 	pktp = SD_GET_PKTP(bp);
17429 	ASSERT(pktp != NULL);
17430 	ASSERT(arg != NULL);
17431 
17432 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17433 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17434 
17435 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
17436 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17437 		severity = SCSI_ERR_RETRYABLE;
17438 	}
17439 
17440 	/* Use absolute block number for the request block number */
17441 	request_blkno = xp->xb_blkno;
17442 
17443 	/*
17444 	 * Now try to get the error block number from the sense data
17445 	 */
17446 	sensep = xp->xb_sense_data;
17447 
17448 	if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
17449 		(uint64_t *)&err_blkno)) {
17450 		/*
17451 		 * We retrieved the error block number from the information
17452 		 * portion of the sense data.
17453 		 *
17454 		 * For USCSI commands we are better off using the error
17455 		 * block no. as the requested block no. (This is the best
17456 		 * we can estimate.)
17457 		 */
17458 		if ((SD_IS_BUFIO(xp) == FALSE) &&
17459 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17460 			request_blkno = err_blkno;
17461 		}
17462 	} else {
17463 		/*
17464 		 * Without the es_valid bit set (for fixed format) or an
17465 		 * information descriptor (for descriptor format) we cannot
17466 		 * be certain of the error blkno, so just use the
17467 		 * request_blkno.
17468 		 */
17469 		err_blkno = (diskaddr_t)request_blkno;
17470 	}
17471 
17472 	/*
17473 	 * The following will log the buffer contents for the release driver
17474 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17475 	 * level is set to verbose.
17476 	 */
17477 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17478 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17479 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
17480 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
17481 
17482 	if (pfa_flag == FALSE) {
17483 		/* This is normally only set for USCSI */
17484 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
17485 			return;
17486 		}
17487 
17488 		if ((SD_IS_BUFIO(xp) == TRUE) &&
17489 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
17490 		    (severity < sd_error_level))) {
17491 			return;
17492 		}
17493 	}
17494 
17495 	/*
17496 	 * Check for Sonoma Failover and keep a count of how many failed I/O's
17497 	 */
17498 	if ((SD_IS_LSI(un)) &&
17499 	    (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
17500 	    (scsi_sense_asc(sensep) == 0x94) &&
17501 	    (scsi_sense_ascq(sensep) == 0x01)) {
17502 		un->un_sonoma_failure_count++;
17503 		if (un->un_sonoma_failure_count > 1) {
17504 			return;
17505 		}
17506 	}
17507 
17508 	scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
17509 	    request_blkno, err_blkno, scsi_cmds,
17510 	    (struct scsi_extended_sense *)sensep,
17511 	    un->un_additional_codes, NULL);
17512 }
17513 
17514 /*
17515  *    Function: sd_sense_key_no_sense
17516  *
17517  * Description: Recovery action when sense data was not received.
17518  *
17519  *     Context: May be called from interrupt context
17520  */
17521 
17522 static void
17523 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
17524 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17525 {
17526 	struct sd_sense_info	si;
17527 
17528 	ASSERT(un != NULL);
17529 	ASSERT(mutex_owned(SD_MUTEX(un)));
17530 	ASSERT(bp != NULL);
17531 	ASSERT(xp != NULL);
17532 	ASSERT(pktp != NULL);
17533 
17534 	si.ssi_severity = SCSI_ERR_FATAL;
17535 	si.ssi_pfa_flag = FALSE;
17536 
17537 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17538 
17539 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17540 		&si, EIO, (clock_t)0, NULL);
17541 }
17542 
17543 
17544 /*
17545  *    Function: sd_sense_key_recoverable_error
17546  *
17547  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
17548  *
17549  *     Context: May be called from interrupt context
17550  */
17551 
17552 static void
17553 sd_sense_key_recoverable_error(struct sd_lun *un,
17554 	uint8_t *sense_datap,
17555 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17556 {
17557 	struct sd_sense_info	si;
17558 	uint8_t asc = scsi_sense_asc(sense_datap);
17559 
17560 	ASSERT(un != NULL);
17561 	ASSERT(mutex_owned(SD_MUTEX(un)));
17562 	ASSERT(bp != NULL);
17563 	ASSERT(xp != NULL);
17564 	ASSERT(pktp != NULL);
17565 
17566 	/*
17567 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
17568 	 */
17569 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
17570 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17571 		si.ssi_severity = SCSI_ERR_INFO;
17572 		si.ssi_pfa_flag = TRUE;
17573 	} else {
17574 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
17575 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
17576 		si.ssi_severity = SCSI_ERR_RECOVERED;
17577 		si.ssi_pfa_flag = FALSE;
17578 	}
17579 
17580 	if (pktp->pkt_resid == 0) {
17581 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17582 		sd_return_command(un, bp);
17583 		return;
17584 	}
17585 
17586 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17587 	    &si, EIO, (clock_t)0, NULL);
17588 }
17589 
17590 
17591 
17592 
17593 /*
17594  *    Function: sd_sense_key_not_ready
17595  *
17596  * Description: Recovery actions for a SCSI "Not Ready" sense key.
17597  *
17598  *     Context: May be called from interrupt context
17599  */
17600 
17601 static void
17602 sd_sense_key_not_ready(struct sd_lun *un,
17603 	uint8_t *sense_datap,
17604 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17605 {
17606 	struct sd_sense_info	si;
17607 	uint8_t asc = scsi_sense_asc(sense_datap);
17608 	uint8_t ascq = scsi_sense_ascq(sense_datap);
17609 
17610 	ASSERT(un != NULL);
17611 	ASSERT(mutex_owned(SD_MUTEX(un)));
17612 	ASSERT(bp != NULL);
17613 	ASSERT(xp != NULL);
17614 	ASSERT(pktp != NULL);
17615 
17616 	si.ssi_severity = SCSI_ERR_FATAL;
17617 	si.ssi_pfa_flag = FALSE;
17618 
17619 	/*
17620 	 * Update error stats after first NOT READY error. Disks may have
17621 	 * been powered down and may need to be restarted.  For CDROMs,
17622 	 * report NOT READY errors only if media is present.
17623 	 */
17624 	if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) ||
17625 	    (xp->xb_retry_count > 0)) {
17626 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17627 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
17628 	}
17629 
17630 	/*
17631 	 * Just fail if the "not ready" retry limit has been reached.
17632 	 */
17633 	if (xp->xb_retry_count >= un->un_notready_retry_count) {
17634 		/* Special check for error message printing for removables. */
17635 		if (un->un_f_has_removable_media && (asc == 0x04) &&
17636 		    (ascq >= 0x04)) {
17637 			si.ssi_severity = SCSI_ERR_ALL;
17638 		}
17639 		goto fail_command;
17640 	}
17641 
17642 	/*
17643 	 * Check the ASC and ASCQ in the sense data as needed, to determine
17644 	 * what to do.
17645 	 */
17646 	switch (asc) {
17647 	case 0x04:	/* LOGICAL UNIT NOT READY */
17648 		/*
17649 		 * disk drives that don't spin up result in a very long delay
17650 		 * in format without warning messages. We will log a message
17651 		 * if the error level is set to verbose.
17652 		 */
17653 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17654 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17655 			    "logical unit not ready, resetting disk\n");
17656 		}
17657 
17658 		/*
17659 		 * There are different requirements for CDROMs and disks for
17660 		 * the number of retries.  If a CD-ROM is giving this, it is
17661 		 * probably reading TOC and is in the process of getting
17662 		 * ready, so we should keep on trying for a long time to make
17663 		 * sure that all types of media are taken in account (for
17664 		 * some media the drive takes a long time to read TOC).  For
17665 		 * disks we do not want to retry this too many times as this
17666 		 * can cause a long hang in format when the drive refuses to
17667 		 * spin up (a very common failure).
17668 		 */
17669 		switch (ascq) {
17670 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
17671 			/*
17672 			 * Disk drives frequently refuse to spin up which
17673 			 * results in a very long hang in format without
17674 			 * warning messages.
17675 			 *
17676 			 * Note: This code preserves the legacy behavior of
17677 			 * comparing xb_retry_count against zero for fibre
17678 			 * channel targets instead of comparing against the
17679 			 * un_reset_retry_count value.  The reason for this
17680 			 * discrepancy has been so utterly lost beneath the
17681 			 * Sands of Time that even Indiana Jones could not
17682 			 * find it.
17683 			 */
17684 			if (un->un_f_is_fibre == TRUE) {
17685 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17686 					(xp->xb_retry_count > 0)) &&
17687 					(un->un_startstop_timeid == NULL)) {
17688 					scsi_log(SD_DEVINFO(un), sd_label,
17689 					CE_WARN, "logical unit not ready, "
17690 					"resetting disk\n");
17691 					sd_reset_target(un, pktp);
17692 				}
17693 			} else {
17694 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17695 					(xp->xb_retry_count >
17696 					un->un_reset_retry_count)) &&
17697 					(un->un_startstop_timeid == NULL)) {
17698 					scsi_log(SD_DEVINFO(un), sd_label,
17699 					CE_WARN, "logical unit not ready, "
17700 					"resetting disk\n");
17701 					sd_reset_target(un, pktp);
17702 				}
17703 			}
17704 			break;
17705 
17706 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
17707 			/*
17708 			 * If the target is in the process of becoming
17709 			 * ready, just proceed with the retry. This can
17710 			 * happen with CD-ROMs that take a long time to
17711 			 * read TOC after a power cycle or reset.
17712 			 */
17713 			goto do_retry;
17714 
17715 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
17716 			break;
17717 
17718 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
17719 			/*
17720 			 * Retries cannot help here so just fail right away.
17721 			 */
17722 			goto fail_command;
17723 
17724 		case 0x88:
17725 			/*
17726 			 * Vendor-unique code for T3/T4: it indicates a
17727 			 * path problem in a mutipathed config, but as far as
17728 			 * the target driver is concerned it equates to a fatal
17729 			 * error, so we should just fail the command right away
17730 			 * (without printing anything to the console). If this
17731 			 * is not a T3/T4, fall thru to the default recovery
17732 			 * action.
17733 			 * T3/T4 is FC only, don't need to check is_fibre
17734 			 */
17735 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
17736 				sd_return_failed_command(un, bp, EIO);
17737 				return;
17738 			}
17739 			/* FALLTHRU */
17740 
17741 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
17742 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
17743 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
17744 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
17745 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
17746 		default:    /* Possible future codes in SCSI spec? */
17747 			/*
17748 			 * For removable-media devices, do not retry if
17749 			 * ASCQ > 2 as these result mostly from USCSI commands
17750 			 * on MMC devices issued to check status of an
17751 			 * operation initiated in immediate mode.  Also for
17752 			 * ASCQ >= 4 do not print console messages as these
17753 			 * mainly represent a user-initiated operation
17754 			 * instead of a system failure.
17755 			 */
17756 			if (un->un_f_has_removable_media) {
17757 				si.ssi_severity = SCSI_ERR_ALL;
17758 				goto fail_command;
17759 			}
17760 			break;
17761 		}
17762 
17763 		/*
17764 		 * As part of our recovery attempt for the NOT READY
17765 		 * condition, we issue a START STOP UNIT command. However
17766 		 * we want to wait for a short delay before attempting this
17767 		 * as there may still be more commands coming back from the
17768 		 * target with the check condition. To do this we use
17769 		 * timeout(9F) to call sd_start_stop_unit_callback() after
17770 		 * the delay interval expires. (sd_start_stop_unit_callback()
17771 		 * dispatches sd_start_stop_unit_task(), which will issue
17772 		 * the actual START STOP UNIT command. The delay interval
17773 		 * is one-half of the delay that we will use to retry the
17774 		 * command that generated the NOT READY condition.
17775 		 *
17776 		 * Note that we could just dispatch sd_start_stop_unit_task()
17777 		 * from here and allow it to sleep for the delay interval,
17778 		 * but then we would be tying up the taskq thread
17779 		 * uncesessarily for the duration of the delay.
17780 		 *
17781 		 * Do not issue the START STOP UNIT if the current command
17782 		 * is already a START STOP UNIT.
17783 		 */
17784 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
17785 			break;
17786 		}
17787 
17788 		/*
17789 		 * Do not schedule the timeout if one is already pending.
17790 		 */
17791 		if (un->un_startstop_timeid != NULL) {
17792 			SD_INFO(SD_LOG_ERROR, un,
17793 			    "sd_sense_key_not_ready: restart already issued to"
17794 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
17795 			    ddi_get_instance(SD_DEVINFO(un)));
17796 			break;
17797 		}
17798 
17799 		/*
17800 		 * Schedule the START STOP UNIT command, then queue the command
17801 		 * for a retry.
17802 		 *
17803 		 * Note: A timeout is not scheduled for this retry because we
17804 		 * want the retry to be serial with the START_STOP_UNIT. The
17805 		 * retry will be started when the START_STOP_UNIT is completed
17806 		 * in sd_start_stop_unit_task.
17807 		 */
17808 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
17809 		    un, SD_BSY_TIMEOUT / 2);
17810 		xp->xb_retry_count++;
17811 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
17812 		return;
17813 
17814 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
17815 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17816 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17817 			    "unit does not respond to selection\n");
17818 		}
17819 		break;
17820 
17821 	case 0x3A:	/* MEDIUM NOT PRESENT */
17822 		if (sd_error_level >= SCSI_ERR_FATAL) {
17823 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17824 			    "Caddy not inserted in drive\n");
17825 		}
17826 
17827 		sr_ejected(un);
17828 		un->un_mediastate = DKIO_EJECTED;
17829 		/* The state has changed, inform the media watch routines */
17830 		cv_broadcast(&un->un_state_cv);
17831 		/* Just fail if no media is present in the drive. */
17832 		goto fail_command;
17833 
17834 	default:
17835 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17836 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
17837 			    "Unit not Ready. Additional sense code 0x%x\n",
17838 			    asc);
17839 		}
17840 		break;
17841 	}
17842 
17843 do_retry:
17844 
17845 	/*
17846 	 * Retry the command, as some targets may report NOT READY for
17847 	 * several seconds after being reset.
17848 	 */
17849 	xp->xb_retry_count++;
17850 	si.ssi_severity = SCSI_ERR_RETRYABLE;
17851 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
17852 	    &si, EIO, SD_BSY_TIMEOUT, NULL);
17853 
17854 	return;
17855 
17856 fail_command:
17857 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17858 	sd_return_failed_command(un, bp, EIO);
17859 }
17860 
17861 
17862 
17863 /*
17864  *    Function: sd_sense_key_medium_or_hardware_error
17865  *
17866  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
17867  *		sense key.
17868  *
17869  *     Context: May be called from interrupt context
17870  */
17871 
17872 static void
17873 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
17874 	uint8_t *sense_datap,
17875 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17876 {
17877 	struct sd_sense_info	si;
17878 	uint8_t sense_key = scsi_sense_key(sense_datap);
17879 	uint8_t asc = scsi_sense_asc(sense_datap);
17880 
17881 	ASSERT(un != NULL);
17882 	ASSERT(mutex_owned(SD_MUTEX(un)));
17883 	ASSERT(bp != NULL);
17884 	ASSERT(xp != NULL);
17885 	ASSERT(pktp != NULL);
17886 
17887 	si.ssi_severity = SCSI_ERR_FATAL;
17888 	si.ssi_pfa_flag = FALSE;
17889 
17890 	if (sense_key == KEY_MEDIUM_ERROR) {
17891 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
17892 	}
17893 
17894 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17895 
17896 	if ((un->un_reset_retry_count != 0) &&
17897 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
17898 		mutex_exit(SD_MUTEX(un));
17899 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
17900 		if (un->un_f_allow_bus_device_reset == TRUE) {
17901 
17902 			boolean_t try_resetting_target = B_TRUE;
17903 
17904 			/*
17905 			 * We need to be able to handle specific ASC when we are
17906 			 * handling a KEY_HARDWARE_ERROR. In particular
17907 			 * taking the default action of resetting the target may
17908 			 * not be the appropriate way to attempt recovery.
17909 			 * Resetting a target because of a single LUN failure
17910 			 * victimizes all LUNs on that target.
17911 			 *
17912 			 * This is true for the LSI arrays, if an LSI
17913 			 * array controller returns an ASC of 0x84 (LUN Dead) we
17914 			 * should trust it.
17915 			 */
17916 
17917 			if (sense_key == KEY_HARDWARE_ERROR) {
17918 				switch (asc) {
17919 				case 0x84:
17920 					if (SD_IS_LSI(un)) {
17921 						try_resetting_target = B_FALSE;
17922 					}
17923 					break;
17924 				default:
17925 					break;
17926 				}
17927 			}
17928 
17929 			if (try_resetting_target == B_TRUE) {
17930 				int reset_retval = 0;
17931 				if (un->un_f_lun_reset_enabled == TRUE) {
17932 					SD_TRACE(SD_LOG_IO_CORE, un,
17933 					    "sd_sense_key_medium_or_hardware_"
17934 					    "error: issuing RESET_LUN\n");
17935 					reset_retval =
17936 					    scsi_reset(SD_ADDRESS(un),
17937 					    RESET_LUN);
17938 				}
17939 				if (reset_retval == 0) {
17940 					SD_TRACE(SD_LOG_IO_CORE, un,
17941 					    "sd_sense_key_medium_or_hardware_"
17942 					    "error: issuing RESET_TARGET\n");
17943 					(void) scsi_reset(SD_ADDRESS(un),
17944 					    RESET_TARGET);
17945 				}
17946 			}
17947 		}
17948 		mutex_enter(SD_MUTEX(un));
17949 	}
17950 
17951 	/*
17952 	 * This really ought to be a fatal error, but we will retry anyway
17953 	 * as some drives report this as a spurious error.
17954 	 */
17955 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17956 	    &si, EIO, (clock_t)0, NULL);
17957 }
17958 
17959 
17960 
17961 /*
17962  *    Function: sd_sense_key_illegal_request
17963  *
17964  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
17965  *
17966  *     Context: May be called from interrupt context
17967  */
17968 
17969 static void
17970 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
17971 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17972 {
17973 	struct sd_sense_info	si;
17974 
17975 	ASSERT(un != NULL);
17976 	ASSERT(mutex_owned(SD_MUTEX(un)));
17977 	ASSERT(bp != NULL);
17978 	ASSERT(xp != NULL);
17979 	ASSERT(pktp != NULL);
17980 
17981 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17982 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
17983 
17984 	si.ssi_severity = SCSI_ERR_INFO;
17985 	si.ssi_pfa_flag = FALSE;
17986 
17987 	/* Pointless to retry if the target thinks it's an illegal request */
17988 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17989 	sd_return_failed_command(un, bp, EIO);
17990 }
17991 
17992 
17993 
17994 
17995 /*
17996  *    Function: sd_sense_key_unit_attention
17997  *
17998  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
17999  *
18000  *     Context: May be called from interrupt context
18001  */
18002 
18003 static void
18004 sd_sense_key_unit_attention(struct sd_lun *un,
18005 	uint8_t *sense_datap,
18006 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18007 {
18008 	/*
18009 	 * For UNIT ATTENTION we allow retries for one minute. Devices
18010 	 * like Sonoma can return UNIT ATTENTION close to a minute
18011 	 * under certain conditions.
18012 	 */
18013 	int	retry_check_flag = SD_RETRIES_UA;
18014 	boolean_t	kstat_updated = B_FALSE;
18015 	struct	sd_sense_info		si;
18016 	uint8_t asc = scsi_sense_asc(sense_datap);
18017 
18018 	ASSERT(un != NULL);
18019 	ASSERT(mutex_owned(SD_MUTEX(un)));
18020 	ASSERT(bp != NULL);
18021 	ASSERT(xp != NULL);
18022 	ASSERT(pktp != NULL);
18023 
18024 	si.ssi_severity = SCSI_ERR_INFO;
18025 	si.ssi_pfa_flag = FALSE;
18026 
18027 
18028 	switch (asc) {
18029 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18030 		if (sd_report_pfa != 0) {
18031 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18032 			si.ssi_pfa_flag = TRUE;
18033 			retry_check_flag = SD_RETRIES_STANDARD;
18034 			goto do_retry;
18035 		}
18036 
18037 		break;
18038 
18039 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18040 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18041 			un->un_resvd_status |=
18042 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
18043 		}
18044 #ifdef _LP64
18045 		if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18046 			if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18047 			    un, KM_NOSLEEP) == 0) {
18048 				/*
18049 				 * If we can't dispatch the task we'll just
18050 				 * live without descriptor sense.  We can
18051 				 * try again on the next "unit attention"
18052 				 */
18053 				SD_ERROR(SD_LOG_ERROR, un,
18054 				    "sd_sense_key_unit_attention: "
18055 				    "Could not dispatch "
18056 				    "sd_reenable_dsense_task\n");
18057 			}
18058 		}
18059 #endif /* _LP64 */
18060 		/* FALLTHRU */
18061 
18062 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18063 		if (!un->un_f_has_removable_media) {
18064 			break;
18065 		}
18066 
18067 		/*
18068 		 * When we get a unit attention from a removable-media device,
18069 		 * it may be in a state that will take a long time to recover
18070 		 * (e.g., from a reset).  Since we are executing in interrupt
18071 		 * context here, we cannot wait around for the device to come
18072 		 * back. So hand this command off to sd_media_change_task()
18073 		 * for deferred processing under taskq thread context. (Note
18074 		 * that the command still may be failed if a problem is
18075 		 * encountered at a later time.)
18076 		 */
18077 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18078 		    KM_NOSLEEP) == 0) {
18079 			/*
18080 			 * Cannot dispatch the request so fail the command.
18081 			 */
18082 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
18083 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18084 			si.ssi_severity = SCSI_ERR_FATAL;
18085 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18086 			sd_return_failed_command(un, bp, EIO);
18087 		}
18088 
18089 		/*
18090 		 * If failed to dispatch sd_media_change_task(), we already
18091 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
18092 		 * we should update kstat later if it encounters an error. So,
18093 		 * we update kstat_updated flag here.
18094 		 */
18095 		kstat_updated = B_TRUE;
18096 
18097 		/*
18098 		 * Either the command has been successfully dispatched to a
18099 		 * task Q for retrying, or the dispatch failed. In either case
18100 		 * do NOT retry again by calling sd_retry_command. This sets up
18101 		 * two retries of the same command and when one completes and
18102 		 * frees the resources the other will access freed memory,
18103 		 * a bad thing.
18104 		 */
18105 		return;
18106 
18107 	default:
18108 		break;
18109 	}
18110 
18111 	/*
18112 	 * Update kstat if we haven't done that.
18113 	 */
18114 	if (!kstat_updated) {
18115 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18116 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18117 	}
18118 
18119 do_retry:
18120 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18121 	    EIO, SD_UA_RETRY_DELAY, NULL);
18122 }
18123 
18124 
18125 
18126 /*
18127  *    Function: sd_sense_key_fail_command
18128  *
18129  * Description: Use to fail a command when we don't like the sense key that
18130  *		was returned.
18131  *
18132  *     Context: May be called from interrupt context
18133  */
18134 
18135 static void
18136 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
18137 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18138 {
18139 	struct sd_sense_info	si;
18140 
18141 	ASSERT(un != NULL);
18142 	ASSERT(mutex_owned(SD_MUTEX(un)));
18143 	ASSERT(bp != NULL);
18144 	ASSERT(xp != NULL);
18145 	ASSERT(pktp != NULL);
18146 
18147 	si.ssi_severity = SCSI_ERR_FATAL;
18148 	si.ssi_pfa_flag = FALSE;
18149 
18150 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18151 	sd_return_failed_command(un, bp, EIO);
18152 }
18153 
18154 
18155 
18156 /*
18157  *    Function: sd_sense_key_blank_check
18158  *
18159  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18160  *		Has no monetary connotation.
18161  *
18162  *     Context: May be called from interrupt context
18163  */
18164 
18165 static void
18166 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
18167 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18168 {
18169 	struct sd_sense_info	si;
18170 
18171 	ASSERT(un != NULL);
18172 	ASSERT(mutex_owned(SD_MUTEX(un)));
18173 	ASSERT(bp != NULL);
18174 	ASSERT(xp != NULL);
18175 	ASSERT(pktp != NULL);
18176 
18177 	/*
18178 	 * Blank check is not fatal for removable devices, therefore
18179 	 * it does not require a console message.
18180 	 */
18181 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18182 	    SCSI_ERR_FATAL;
18183 	si.ssi_pfa_flag = FALSE;
18184 
18185 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18186 	sd_return_failed_command(un, bp, EIO);
18187 }
18188 
18189 
18190 
18191 
18192 /*
18193  *    Function: sd_sense_key_aborted_command
18194  *
18195  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18196  *
18197  *     Context: May be called from interrupt context
18198  */
18199 
18200 static void
18201 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18202 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18203 {
18204 	struct sd_sense_info	si;
18205 
18206 	ASSERT(un != NULL);
18207 	ASSERT(mutex_owned(SD_MUTEX(un)));
18208 	ASSERT(bp != NULL);
18209 	ASSERT(xp != NULL);
18210 	ASSERT(pktp != NULL);
18211 
18212 	si.ssi_severity = SCSI_ERR_FATAL;
18213 	si.ssi_pfa_flag = FALSE;
18214 
18215 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18216 
18217 	/*
18218 	 * This really ought to be a fatal error, but we will retry anyway
18219 	 * as some drives report this as a spurious error.
18220 	 */
18221 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18222 	    &si, EIO, (clock_t)0, NULL);
18223 }
18224 
18225 
18226 
18227 /*
18228  *    Function: sd_sense_key_default
18229  *
18230  * Description: Default recovery action for several SCSI sense keys (basically
18231  *		attempts a retry).
18232  *
18233  *     Context: May be called from interrupt context
18234  */
18235 
18236 static void
18237 sd_sense_key_default(struct sd_lun *un,
18238 	uint8_t *sense_datap,
18239 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18240 {
18241 	struct sd_sense_info	si;
18242 	uint8_t sense_key = scsi_sense_key(sense_datap);
18243 
18244 	ASSERT(un != NULL);
18245 	ASSERT(mutex_owned(SD_MUTEX(un)));
18246 	ASSERT(bp != NULL);
18247 	ASSERT(xp != NULL);
18248 	ASSERT(pktp != NULL);
18249 
18250 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18251 
18252 	/*
18253 	 * Undecoded sense key.	Attempt retries and hope that will fix
18254 	 * the problem.  Otherwise, we're dead.
18255 	 */
18256 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18257 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18258 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18259 	}
18260 
18261 	si.ssi_severity = SCSI_ERR_FATAL;
18262 	si.ssi_pfa_flag = FALSE;
18263 
18264 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18265 	    &si, EIO, (clock_t)0, NULL);
18266 }
18267 
18268 
18269 
18270 /*
18271  *    Function: sd_print_retry_msg
18272  *
18273  * Description: Print a message indicating the retry action being taken.
18274  *
18275  *   Arguments: un - ptr to associated softstate
18276  *		bp - ptr to buf(9S) for the command
18277  *		arg - not used.
18278  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18279  *			or SD_NO_RETRY_ISSUED
18280  *
18281  *     Context: May be called from interrupt context
18282  */
18283 /* ARGSUSED */
18284 static void
18285 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18286 {
18287 	struct sd_xbuf	*xp;
18288 	struct scsi_pkt *pktp;
18289 	char *reasonp;
18290 	char *msgp;
18291 
18292 	ASSERT(un != NULL);
18293 	ASSERT(mutex_owned(SD_MUTEX(un)));
18294 	ASSERT(bp != NULL);
18295 	pktp = SD_GET_PKTP(bp);
18296 	ASSERT(pktp != NULL);
18297 	xp = SD_GET_XBUF(bp);
18298 	ASSERT(xp != NULL);
18299 
18300 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18301 	mutex_enter(&un->un_pm_mutex);
18302 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18303 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18304 	    (pktp->pkt_flags & FLAG_SILENT)) {
18305 		mutex_exit(&un->un_pm_mutex);
18306 		goto update_pkt_reason;
18307 	}
18308 	mutex_exit(&un->un_pm_mutex);
18309 
18310 	/*
18311 	 * Suppress messages if they are all the same pkt_reason; with
18312 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18313 	 * If we are in panic, then suppress the retry messages.
18314 	 */
18315 	switch (flag) {
18316 	case SD_NO_RETRY_ISSUED:
18317 		msgp = "giving up";
18318 		break;
18319 	case SD_IMMEDIATE_RETRY_ISSUED:
18320 	case SD_DELAYED_RETRY_ISSUED:
18321 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18322 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18323 		    (sd_error_level != SCSI_ERR_ALL))) {
18324 			return;
18325 		}
18326 		msgp = "retrying command";
18327 		break;
18328 	default:
18329 		goto update_pkt_reason;
18330 	}
18331 
18332 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18333 	    scsi_rname(pktp->pkt_reason));
18334 
18335 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18336 	    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18337 
18338 update_pkt_reason:
18339 	/*
18340 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18341 	 * This is to prevent multiple console messages for the same failure
18342 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18343 	 * when the command is retried successfully because there still may be
18344 	 * more commands coming back with the same value of pktp->pkt_reason.
18345 	 */
18346 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18347 		un->un_last_pkt_reason = pktp->pkt_reason;
18348 	}
18349 }
18350 
18351 
18352 /*
18353  *    Function: sd_print_cmd_incomplete_msg
18354  *
18355  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18356  *
18357  *   Arguments: un - ptr to associated softstate
18358  *		bp - ptr to buf(9S) for the command
18359  *		arg - passed to sd_print_retry_msg()
18360  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18361  *			or SD_NO_RETRY_ISSUED
18362  *
18363  *     Context: May be called from interrupt context
18364  */
18365 
18366 static void
18367 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18368 	int code)
18369 {
18370 	dev_info_t	*dip;
18371 
18372 	ASSERT(un != NULL);
18373 	ASSERT(mutex_owned(SD_MUTEX(un)));
18374 	ASSERT(bp != NULL);
18375 
18376 	switch (code) {
18377 	case SD_NO_RETRY_ISSUED:
18378 		/* Command was failed. Someone turned off this target? */
18379 		if (un->un_state != SD_STATE_OFFLINE) {
18380 			/*
18381 			 * Suppress message if we are detaching and
18382 			 * device has been disconnected
18383 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18384 			 * private interface and not part of the DDI
18385 			 */
18386 			dip = un->un_sd->sd_dev;
18387 			if (!(DEVI_IS_DETACHING(dip) &&
18388 			    DEVI_IS_DEVICE_REMOVED(dip))) {
18389 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18390 				"disk not responding to selection\n");
18391 			}
18392 			New_state(un, SD_STATE_OFFLINE);
18393 		}
18394 		break;
18395 
18396 	case SD_DELAYED_RETRY_ISSUED:
18397 	case SD_IMMEDIATE_RETRY_ISSUED:
18398 	default:
18399 		/* Command was successfully queued for retry */
18400 		sd_print_retry_msg(un, bp, arg, code);
18401 		break;
18402 	}
18403 }
18404 
18405 
18406 /*
18407  *    Function: sd_pkt_reason_cmd_incomplete
18408  *
18409  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18410  *
18411  *     Context: May be called from interrupt context
18412  */
18413 
18414 static void
18415 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18416 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18417 {
18418 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18419 
18420 	ASSERT(un != NULL);
18421 	ASSERT(mutex_owned(SD_MUTEX(un)));
18422 	ASSERT(bp != NULL);
18423 	ASSERT(xp != NULL);
18424 	ASSERT(pktp != NULL);
18425 
18426 	/* Do not do a reset if selection did not complete */
18427 	/* Note: Should this not just check the bit? */
18428 	if (pktp->pkt_state != STATE_GOT_BUS) {
18429 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18430 		sd_reset_target(un, pktp);
18431 	}
18432 
18433 	/*
18434 	 * If the target was not successfully selected, then set
18435 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
18436 	 * with the target, and further retries and/or commands are
18437 	 * likely to take a long time.
18438 	 */
18439 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18440 		flag |= SD_RETRIES_FAILFAST;
18441 	}
18442 
18443 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18444 
18445 	sd_retry_command(un, bp, flag,
18446 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18447 }
18448 
18449 
18450 
18451 /*
18452  *    Function: sd_pkt_reason_cmd_tran_err
18453  *
18454  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
18455  *
18456  *     Context: May be called from interrupt context
18457  */
18458 
18459 static void
18460 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
18461 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18462 {
18463 	ASSERT(un != NULL);
18464 	ASSERT(mutex_owned(SD_MUTEX(un)));
18465 	ASSERT(bp != NULL);
18466 	ASSERT(xp != NULL);
18467 	ASSERT(pktp != NULL);
18468 
18469 	/*
18470 	 * Do not reset if we got a parity error, or if
18471 	 * selection did not complete.
18472 	 */
18473 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18474 	/* Note: Should this not just check the bit for pkt_state? */
18475 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
18476 	    (pktp->pkt_state != STATE_GOT_BUS)) {
18477 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18478 		sd_reset_target(un, pktp);
18479 	}
18480 
18481 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18482 
18483 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18484 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18485 }
18486 
18487 
18488 
18489 /*
18490  *    Function: sd_pkt_reason_cmd_reset
18491  *
18492  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
18493  *
18494  *     Context: May be called from interrupt context
18495  */
18496 
18497 static void
18498 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
18499 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18500 {
18501 	ASSERT(un != NULL);
18502 	ASSERT(mutex_owned(SD_MUTEX(un)));
18503 	ASSERT(bp != NULL);
18504 	ASSERT(xp != NULL);
18505 	ASSERT(pktp != NULL);
18506 
18507 	/* The target may still be running the command, so try to reset. */
18508 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18509 	sd_reset_target(un, pktp);
18510 
18511 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18512 
18513 	/*
18514 	 * If pkt_reason is CMD_RESET chances are that this pkt got
18515 	 * reset because another target on this bus caused it. The target
18516 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18517 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18518 	 */
18519 
18520 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18521 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18522 }
18523 
18524 
18525 
18526 
18527 /*
18528  *    Function: sd_pkt_reason_cmd_aborted
18529  *
18530  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
18531  *
18532  *     Context: May be called from interrupt context
18533  */
18534 
18535 static void
18536 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
18537 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18538 {
18539 	ASSERT(un != NULL);
18540 	ASSERT(mutex_owned(SD_MUTEX(un)));
18541 	ASSERT(bp != NULL);
18542 	ASSERT(xp != NULL);
18543 	ASSERT(pktp != NULL);
18544 
18545 	/* The target may still be running the command, so try to reset. */
18546 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18547 	sd_reset_target(un, pktp);
18548 
18549 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18550 
18551 	/*
18552 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
18553 	 * aborted because another target on this bus caused it. The target
18554 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18555 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18556 	 */
18557 
18558 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18559 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18560 }
18561 
18562 
18563 
18564 /*
18565  *    Function: sd_pkt_reason_cmd_timeout
18566  *
18567  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
18568  *
18569  *     Context: May be called from interrupt context
18570  */
18571 
18572 static void
18573 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
18574 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18575 {
18576 	ASSERT(un != NULL);
18577 	ASSERT(mutex_owned(SD_MUTEX(un)));
18578 	ASSERT(bp != NULL);
18579 	ASSERT(xp != NULL);
18580 	ASSERT(pktp != NULL);
18581 
18582 
18583 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18584 	sd_reset_target(un, pktp);
18585 
18586 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18587 
18588 	/*
18589 	 * A command timeout indicates that we could not establish
18590 	 * communication with the target, so set SD_RETRIES_FAILFAST
18591 	 * as further retries/commands are likely to take a long time.
18592 	 */
18593 	sd_retry_command(un, bp,
18594 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
18595 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18596 }
18597 
18598 
18599 
18600 /*
18601  *    Function: sd_pkt_reason_cmd_unx_bus_free
18602  *
18603  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
18604  *
18605  *     Context: May be called from interrupt context
18606  */
18607 
18608 static void
18609 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
18610 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18611 {
18612 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
18613 
18614 	ASSERT(un != NULL);
18615 	ASSERT(mutex_owned(SD_MUTEX(un)));
18616 	ASSERT(bp != NULL);
18617 	ASSERT(xp != NULL);
18618 	ASSERT(pktp != NULL);
18619 
18620 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18621 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18622 
18623 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
18624 	    sd_print_retry_msg : NULL;
18625 
18626 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18627 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18628 }
18629 
18630 
18631 /*
18632  *    Function: sd_pkt_reason_cmd_tag_reject
18633  *
18634  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
18635  *
18636  *     Context: May be called from interrupt context
18637  */
18638 
18639 static void
18640 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
18641 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18642 {
18643 	ASSERT(un != NULL);
18644 	ASSERT(mutex_owned(SD_MUTEX(un)));
18645 	ASSERT(bp != NULL);
18646 	ASSERT(xp != NULL);
18647 	ASSERT(pktp != NULL);
18648 
18649 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18650 	pktp->pkt_flags = 0;
18651 	un->un_tagflags = 0;
18652 	if (un->un_f_opt_queueing == TRUE) {
18653 		un->un_throttle = min(un->un_throttle, 3);
18654 	} else {
18655 		un->un_throttle = 1;
18656 	}
18657 	mutex_exit(SD_MUTEX(un));
18658 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
18659 	mutex_enter(SD_MUTEX(un));
18660 
18661 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18662 
18663 	/* Legacy behavior not to check retry counts here. */
18664 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
18665 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18666 }
18667 
18668 
18669 /*
18670  *    Function: sd_pkt_reason_default
18671  *
18672  * Description: Default recovery actions for SCSA pkt_reason values that
18673  *		do not have more explicit recovery actions.
18674  *
18675  *     Context: May be called from interrupt context
18676  */
18677 
18678 static void
18679 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
18680 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18681 {
18682 	ASSERT(un != NULL);
18683 	ASSERT(mutex_owned(SD_MUTEX(un)));
18684 	ASSERT(bp != NULL);
18685 	ASSERT(xp != NULL);
18686 	ASSERT(pktp != NULL);
18687 
18688 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18689 	sd_reset_target(un, pktp);
18690 
18691 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18692 
18693 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18694 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18695 }
18696 
18697 
18698 
18699 /*
18700  *    Function: sd_pkt_status_check_condition
18701  *
18702  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
18703  *
18704  *     Context: May be called from interrupt context
18705  */
18706 
18707 static void
18708 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
18709 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18710 {
18711 	ASSERT(un != NULL);
18712 	ASSERT(mutex_owned(SD_MUTEX(un)));
18713 	ASSERT(bp != NULL);
18714 	ASSERT(xp != NULL);
18715 	ASSERT(pktp != NULL);
18716 
18717 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
18718 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
18719 
18720 	/*
18721 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
18722 	 * command will be retried after the request sense). Otherwise, retry
18723 	 * the command. Note: we are issuing the request sense even though the
18724 	 * retry limit may have been reached for the failed command.
18725 	 */
18726 	if (un->un_f_arq_enabled == FALSE) {
18727 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18728 		    "no ARQ, sending request sense command\n");
18729 		sd_send_request_sense_command(un, bp, pktp);
18730 	} else {
18731 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18732 		    "ARQ,retrying request sense command\n");
18733 #if defined(__i386) || defined(__amd64)
18734 		/*
18735 		 * The SD_RETRY_DELAY value need to be adjusted here
18736 		 * when SD_RETRY_DELAY change in sddef.h
18737 		 */
18738 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
18739 			un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
18740 			NULL);
18741 #else
18742 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
18743 		    EIO, SD_RETRY_DELAY, NULL);
18744 #endif
18745 	}
18746 
18747 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
18748 }
18749 
18750 
18751 /*
18752  *    Function: sd_pkt_status_busy
18753  *
18754  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
18755  *
18756  *     Context: May be called from interrupt context
18757  */
18758 
18759 static void
18760 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18761 	struct scsi_pkt *pktp)
18762 {
18763 	ASSERT(un != NULL);
18764 	ASSERT(mutex_owned(SD_MUTEX(un)));
18765 	ASSERT(bp != NULL);
18766 	ASSERT(xp != NULL);
18767 	ASSERT(pktp != NULL);
18768 
18769 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18770 	    "sd_pkt_status_busy: entry\n");
18771 
18772 	/* If retries are exhausted, just fail the command. */
18773 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
18774 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18775 		    "device busy too long\n");
18776 		sd_return_failed_command(un, bp, EIO);
18777 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18778 		    "sd_pkt_status_busy: exit\n");
18779 		return;
18780 	}
18781 	xp->xb_retry_count++;
18782 
18783 	/*
18784 	 * Try to reset the target. However, we do not want to perform
18785 	 * more than one reset if the device continues to fail. The reset
18786 	 * will be performed when the retry count reaches the reset
18787 	 * threshold.  This threshold should be set such that at least
18788 	 * one retry is issued before the reset is performed.
18789 	 */
18790 	if (xp->xb_retry_count ==
18791 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
18792 		int rval = 0;
18793 		mutex_exit(SD_MUTEX(un));
18794 		if (un->un_f_allow_bus_device_reset == TRUE) {
18795 			/*
18796 			 * First try to reset the LUN; if we cannot then
18797 			 * try to reset the target.
18798 			 */
18799 			if (un->un_f_lun_reset_enabled == TRUE) {
18800 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18801 				    "sd_pkt_status_busy: RESET_LUN\n");
18802 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
18803 			}
18804 			if (rval == 0) {
18805 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18806 				    "sd_pkt_status_busy: RESET_TARGET\n");
18807 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
18808 			}
18809 		}
18810 		if (rval == 0) {
18811 			/*
18812 			 * If the RESET_LUN and/or RESET_TARGET failed,
18813 			 * try RESET_ALL
18814 			 */
18815 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18816 			    "sd_pkt_status_busy: RESET_ALL\n");
18817 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
18818 		}
18819 		mutex_enter(SD_MUTEX(un));
18820 		if (rval == 0) {
18821 			/*
18822 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
18823 			 * At this point we give up & fail the command.
18824 			 */
18825 			sd_return_failed_command(un, bp, EIO);
18826 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18827 			    "sd_pkt_status_busy: exit (failed cmd)\n");
18828 			return;
18829 		}
18830 	}
18831 
18832 	/*
18833 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
18834 	 * we have already checked the retry counts above.
18835 	 */
18836 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
18837 	    EIO, SD_BSY_TIMEOUT, NULL);
18838 
18839 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18840 	    "sd_pkt_status_busy: exit\n");
18841 }
18842 
18843 
18844 /*
18845  *    Function: sd_pkt_status_reservation_conflict
18846  *
18847  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
18848  *		command status.
18849  *
18850  *     Context: May be called from interrupt context
18851  */
18852 
18853 static void
18854 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
18855 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18856 {
18857 	ASSERT(un != NULL);
18858 	ASSERT(mutex_owned(SD_MUTEX(un)));
18859 	ASSERT(bp != NULL);
18860 	ASSERT(xp != NULL);
18861 	ASSERT(pktp != NULL);
18862 
18863 	/*
18864 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
18865 	 * conflict could be due to various reasons like incorrect keys, not
18866 	 * registered or not reserved etc. So, we return EACCES to the caller.
18867 	 */
18868 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
18869 		int cmd = SD_GET_PKT_OPCODE(pktp);
18870 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
18871 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
18872 			sd_return_failed_command(un, bp, EACCES);
18873 			return;
18874 		}
18875 	}
18876 
18877 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
18878 
18879 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
18880 		if (sd_failfast_enable != 0) {
18881 			/* By definition, we must panic here.... */
18882 			sd_panic_for_res_conflict(un);
18883 			/*NOTREACHED*/
18884 		}
18885 		SD_ERROR(SD_LOG_IO, un,
18886 		    "sd_handle_resv_conflict: Disk Reserved\n");
18887 		sd_return_failed_command(un, bp, EACCES);
18888 		return;
18889 	}
18890 
18891 	/*
18892 	 * 1147670: retry only if sd_retry_on_reservation_conflict
18893 	 * property is set (default is 1). Retries will not succeed
18894 	 * on a disk reserved by another initiator. HA systems
18895 	 * may reset this via sd.conf to avoid these retries.
18896 	 *
18897 	 * Note: The legacy return code for this failure is EIO, however EACCES
18898 	 * seems more appropriate for a reservation conflict.
18899 	 */
18900 	if (sd_retry_on_reservation_conflict == 0) {
18901 		SD_ERROR(SD_LOG_IO, un,
18902 		    "sd_handle_resv_conflict: Device Reserved\n");
18903 		sd_return_failed_command(un, bp, EIO);
18904 		return;
18905 	}
18906 
18907 	/*
18908 	 * Retry the command if we can.
18909 	 *
18910 	 * Note: The legacy return code for this failure is EIO, however EACCES
18911 	 * seems more appropriate for a reservation conflict.
18912 	 */
18913 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
18914 	    (clock_t)2, NULL);
18915 }
18916 
18917 
18918 
18919 /*
18920  *    Function: sd_pkt_status_qfull
18921  *
18922  * Description: Handle a QUEUE FULL condition from the target.  This can
18923  *		occur if the HBA does not handle the queue full condition.
18924  *		(Basically this means third-party HBAs as Sun HBAs will
18925  *		handle the queue full condition.)  Note that if there are
18926  *		some commands already in the transport, then the queue full
18927  *		has occurred because the queue for this nexus is actually
18928  *		full. If there are no commands in the transport, then the
18929  *		queue full is resulting from some other initiator or lun
18930  *		consuming all the resources at the target.
18931  *
18932  *     Context: May be called from interrupt context
18933  */
18934 
18935 static void
18936 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
18937 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18938 {
18939 	ASSERT(un != NULL);
18940 	ASSERT(mutex_owned(SD_MUTEX(un)));
18941 	ASSERT(bp != NULL);
18942 	ASSERT(xp != NULL);
18943 	ASSERT(pktp != NULL);
18944 
18945 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18946 	    "sd_pkt_status_qfull: entry\n");
18947 
18948 	/*
18949 	 * Just lower the QFULL throttle and retry the command.  Note that
18950 	 * we do not limit the number of retries here.
18951 	 */
18952 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
18953 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
18954 	    SD_RESTART_TIMEOUT, NULL);
18955 
18956 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18957 	    "sd_pkt_status_qfull: exit\n");
18958 }
18959 
18960 
18961 /*
18962  *    Function: sd_reset_target
18963  *
18964  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
18965  *		RESET_TARGET, or RESET_ALL.
18966  *
18967  *     Context: May be called under interrupt context.
18968  */
18969 
18970 static void
18971 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
18972 {
18973 	int rval = 0;
18974 
18975 	ASSERT(un != NULL);
18976 	ASSERT(mutex_owned(SD_MUTEX(un)));
18977 	ASSERT(pktp != NULL);
18978 
18979 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
18980 
18981 	/*
18982 	 * No need to reset if the transport layer has already done so.
18983 	 */
18984 	if ((pktp->pkt_statistics &
18985 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
18986 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18987 		    "sd_reset_target: no reset\n");
18988 		return;
18989 	}
18990 
18991 	mutex_exit(SD_MUTEX(un));
18992 
18993 	if (un->un_f_allow_bus_device_reset == TRUE) {
18994 		if (un->un_f_lun_reset_enabled == TRUE) {
18995 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18996 			    "sd_reset_target: RESET_LUN\n");
18997 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
18998 		}
18999 		if (rval == 0) {
19000 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19001 			    "sd_reset_target: RESET_TARGET\n");
19002 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19003 		}
19004 	}
19005 
19006 	if (rval == 0) {
19007 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19008 		    "sd_reset_target: RESET_ALL\n");
19009 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19010 	}
19011 
19012 	mutex_enter(SD_MUTEX(un));
19013 
19014 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19015 }
19016 
19017 
19018 /*
19019  *    Function: sd_media_change_task
19020  *
19021  * Description: Recovery action for CDROM to become available.
19022  *
19023  *     Context: Executes in a taskq() thread context
19024  */
19025 
19026 static void
19027 sd_media_change_task(void *arg)
19028 {
19029 	struct	scsi_pkt	*pktp = arg;
19030 	struct	sd_lun		*un;
19031 	struct	buf		*bp;
19032 	struct	sd_xbuf		*xp;
19033 	int	err		= 0;
19034 	int	retry_count	= 0;
19035 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
19036 	struct	sd_sense_info	si;
19037 
19038 	ASSERT(pktp != NULL);
19039 	bp = (struct buf *)pktp->pkt_private;
19040 	ASSERT(bp != NULL);
19041 	xp = SD_GET_XBUF(bp);
19042 	ASSERT(xp != NULL);
19043 	un = SD_GET_UN(bp);
19044 	ASSERT(un != NULL);
19045 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19046 	ASSERT(un->un_f_monitor_media_state);
19047 
19048 	si.ssi_severity = SCSI_ERR_INFO;
19049 	si.ssi_pfa_flag = FALSE;
19050 
19051 	/*
19052 	 * When a reset is issued on a CDROM, it takes a long time to
19053 	 * recover. First few attempts to read capacity and other things
19054 	 * related to handling unit attention fail (with a ASC 0x4 and
19055 	 * ASCQ 0x1). In that case we want to do enough retries and we want
19056 	 * to limit the retries in other cases of genuine failures like
19057 	 * no media in drive.
19058 	 */
19059 	while (retry_count++ < retry_limit) {
19060 		if ((err = sd_handle_mchange(un)) == 0) {
19061 			break;
19062 		}
19063 		if (err == EAGAIN) {
19064 			retry_limit = SD_UNIT_ATTENTION_RETRY;
19065 		}
19066 		/* Sleep for 0.5 sec. & try again */
19067 		delay(drv_usectohz(500000));
19068 	}
19069 
19070 	/*
19071 	 * Dispatch (retry or fail) the original command here,
19072 	 * along with appropriate console messages....
19073 	 *
19074 	 * Must grab the mutex before calling sd_retry_command,
19075 	 * sd_print_sense_msg and sd_return_failed_command.
19076 	 */
19077 	mutex_enter(SD_MUTEX(un));
19078 	if (err != SD_CMD_SUCCESS) {
19079 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
19080 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19081 		si.ssi_severity = SCSI_ERR_FATAL;
19082 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19083 		sd_return_failed_command(un, bp, EIO);
19084 	} else {
19085 		sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
19086 		    &si, EIO, (clock_t)0, NULL);
19087 	}
19088 	mutex_exit(SD_MUTEX(un));
19089 }
19090 
19091 
19092 
19093 /*
19094  *    Function: sd_handle_mchange
19095  *
19096  * Description: Perform geometry validation & other recovery when CDROM
19097  *		has been removed from drive.
19098  *
19099  * Return Code: 0 for success
19100  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
19101  *		sd_send_scsi_READ_CAPACITY()
19102  *
19103  *     Context: Executes in a taskq() thread context
19104  */
19105 
19106 static int
19107 sd_handle_mchange(struct sd_lun *un)
19108 {
19109 	uint64_t	capacity;
19110 	uint32_t	lbasize;
19111 	int		rval;
19112 
19113 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19114 	ASSERT(un->un_f_monitor_media_state);
19115 
19116 	if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
19117 	    SD_PATH_DIRECT_PRIORITY)) != 0) {
19118 		return (rval);
19119 	}
19120 
19121 	mutex_enter(SD_MUTEX(un));
19122 	sd_update_block_info(un, lbasize, capacity);
19123 
19124 	if (un->un_errstats != NULL) {
19125 		struct	sd_errstats *stp =
19126 		    (struct sd_errstats *)un->un_errstats->ks_data;
19127 		stp->sd_capacity.value.ui64 = (uint64_t)
19128 		    ((uint64_t)un->un_blockcount *
19129 		    (uint64_t)un->un_tgt_blocksize);
19130 	}
19131 
19132 	/*
19133 	 * Note: Maybe let the strategy/partitioning chain worry about getting
19134 	 * valid geometry.
19135 	 */
19136 	un->un_f_geometry_is_valid = FALSE;
19137 	(void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY);
19138 	if (un->un_f_geometry_is_valid == FALSE) {
19139 		mutex_exit(SD_MUTEX(un));
19140 		return (EIO);
19141 	}
19142 
19143 	mutex_exit(SD_MUTEX(un));
19144 
19145 	/*
19146 	 * Try to lock the door
19147 	 */
19148 	return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
19149 	    SD_PATH_DIRECT_PRIORITY));
19150 }
19151 
19152 
19153 /*
19154  *    Function: sd_send_scsi_DOORLOCK
19155  *
19156  * Description: Issue the scsi DOOR LOCK command
19157  *
19158  *   Arguments: un    - pointer to driver soft state (unit) structure for
19159  *			this target.
19160  *		flag  - SD_REMOVAL_ALLOW
19161  *			SD_REMOVAL_PREVENT
19162  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19163  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19164  *			to use the USCSI "direct" chain and bypass the normal
19165  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19166  *			command is issued as part of an error recovery action.
19167  *
19168  * Return Code: 0   - Success
19169  *		errno return code from sd_send_scsi_cmd()
19170  *
19171  *     Context: Can sleep.
19172  */
19173 
19174 static int
19175 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag)
19176 {
19177 	union scsi_cdb		cdb;
19178 	struct uscsi_cmd	ucmd_buf;
19179 	struct scsi_extended_sense	sense_buf;
19180 	int			status;
19181 
19182 	ASSERT(un != NULL);
19183 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19184 
19185 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19186 
19187 	/* already determined doorlock is not supported, fake success */
19188 	if (un->un_f_doorlock_supported == FALSE) {
19189 		return (0);
19190 	}
19191 
19192 	bzero(&cdb, sizeof (cdb));
19193 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19194 
19195 	cdb.scc_cmd = SCMD_DOORLOCK;
19196 	cdb.cdb_opaque[4] = (uchar_t)flag;
19197 
19198 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19199 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19200 	ucmd_buf.uscsi_bufaddr	= NULL;
19201 	ucmd_buf.uscsi_buflen	= 0;
19202 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19203 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19204 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19205 	ucmd_buf.uscsi_timeout	= 15;
19206 
19207 	SD_TRACE(SD_LOG_IO, un,
19208 	    "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n");
19209 
19210 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
19211 	    UIO_SYSSPACE, path_flag);
19212 
19213 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19214 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19215 	    (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19216 		/* fake success and skip subsequent doorlock commands */
19217 		un->un_f_doorlock_supported = FALSE;
19218 		return (0);
19219 	}
19220 
19221 	return (status);
19222 }
19223 
19224 /*
19225  *    Function: sd_send_scsi_READ_CAPACITY
19226  *
19227  * Description: This routine uses the scsi READ CAPACITY command to determine
19228  *		the device capacity in number of blocks and the device native
19229  *		block size. If this function returns a failure, then the
19230  *		values in *capp and *lbap are undefined.  If the capacity
19231  *		returned is 0xffffffff then the lun is too large for a
19232  *		normal READ CAPACITY command and the results of a
19233  *		READ CAPACITY 16 will be used instead.
19234  *
19235  *   Arguments: un   - ptr to soft state struct for the target
19236  *		capp - ptr to unsigned 64-bit variable to receive the
19237  *			capacity value from the command.
19238  *		lbap - ptr to unsigned 32-bit varaible to receive the
19239  *			block size value from the command
19240  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19241  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19242  *			to use the USCSI "direct" chain and bypass the normal
19243  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19244  *			command is issued as part of an error recovery action.
19245  *
19246  * Return Code: 0   - Success
19247  *		EIO - IO error
19248  *		EACCES - Reservation conflict detected
19249  *		EAGAIN - Device is becoming ready
19250  *		errno return code from sd_send_scsi_cmd()
19251  *
19252  *     Context: Can sleep.  Blocks until command completes.
19253  */
19254 
19255 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
19256 
19257 static int
19258 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap,
19259 	int path_flag)
19260 {
19261 	struct	scsi_extended_sense	sense_buf;
19262 	struct	uscsi_cmd	ucmd_buf;
19263 	union	scsi_cdb	cdb;
19264 	uint32_t		*capacity_buf;
19265 	uint64_t		capacity;
19266 	uint32_t		lbasize;
19267 	int			status;
19268 
19269 	ASSERT(un != NULL);
19270 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19271 	ASSERT(capp != NULL);
19272 	ASSERT(lbap != NULL);
19273 
19274 	SD_TRACE(SD_LOG_IO, un,
19275 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19276 
19277 	/*
19278 	 * First send a READ_CAPACITY command to the target.
19279 	 * (This command is mandatory under SCSI-2.)
19280 	 *
19281 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
19282 	 * Medium Indicator bit is cleared.  The address field must be
19283 	 * zero if the PMI bit is zero.
19284 	 */
19285 	bzero(&cdb, sizeof (cdb));
19286 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19287 
19288 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
19289 
19290 	cdb.scc_cmd = SCMD_READ_CAPACITY;
19291 
19292 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19293 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19294 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
19295 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
19296 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19297 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19298 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19299 	ucmd_buf.uscsi_timeout	= 60;
19300 
19301 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
19302 	    UIO_SYSSPACE, path_flag);
19303 
19304 	switch (status) {
19305 	case 0:
19306 		/* Return failure if we did not get valid capacity data. */
19307 		if (ucmd_buf.uscsi_resid != 0) {
19308 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19309 			return (EIO);
19310 		}
19311 
19312 		/*
19313 		 * Read capacity and block size from the READ CAPACITY 10 data.
19314 		 * This data may be adjusted later due to device specific
19315 		 * issues.
19316 		 *
19317 		 * According to the SCSI spec, the READ CAPACITY 10
19318 		 * command returns the following:
19319 		 *
19320 		 *  bytes 0-3: Maximum logical block address available.
19321 		 *		(MSB in byte:0 & LSB in byte:3)
19322 		 *
19323 		 *  bytes 4-7: Block length in bytes
19324 		 *		(MSB in byte:4 & LSB in byte:7)
19325 		 *
19326 		 */
19327 		capacity = BE_32(capacity_buf[0]);
19328 		lbasize = BE_32(capacity_buf[1]);
19329 
19330 		/*
19331 		 * Done with capacity_buf
19332 		 */
19333 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19334 
19335 		/*
19336 		 * if the reported capacity is set to all 0xf's, then
19337 		 * this disk is too large and requires SBC-2 commands.
19338 		 * Reissue the request using READ CAPACITY 16.
19339 		 */
19340 		if (capacity == 0xffffffff) {
19341 			status = sd_send_scsi_READ_CAPACITY_16(un, &capacity,
19342 			    &lbasize, path_flag);
19343 			if (status != 0) {
19344 				return (status);
19345 			}
19346 		}
19347 		break;	/* Success! */
19348 	case EIO:
19349 		switch (ucmd_buf.uscsi_status) {
19350 		case STATUS_RESERVATION_CONFLICT:
19351 			status = EACCES;
19352 			break;
19353 		case STATUS_CHECK:
19354 			/*
19355 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
19356 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19357 			 */
19358 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19359 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
19360 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
19361 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19362 				return (EAGAIN);
19363 			}
19364 			break;
19365 		default:
19366 			break;
19367 		}
19368 		/* FALLTHRU */
19369 	default:
19370 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19371 		return (status);
19372 	}
19373 
19374 	/*
19375 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
19376 	 * (2352 and 0 are common) so for these devices always force the value
19377 	 * to 2048 as required by the ATAPI specs.
19378 	 */
19379 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
19380 		lbasize = 2048;
19381 	}
19382 
19383 	/*
19384 	 * Get the maximum LBA value from the READ CAPACITY data.
19385 	 * Here we assume that the Partial Medium Indicator (PMI) bit
19386 	 * was cleared when issuing the command. This means that the LBA
19387 	 * returned from the device is the LBA of the last logical block
19388 	 * on the logical unit.  The actual logical block count will be
19389 	 * this value plus one.
19390 	 *
19391 	 * Currently the capacity is saved in terms of un->un_sys_blocksize,
19392 	 * so scale the capacity value to reflect this.
19393 	 */
19394 	capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize);
19395 
19396 #if defined(__i386) || defined(__amd64)
19397 	/*
19398 	 * Refer to comments related to off-by-1 at the
19399 	 * header of this file.
19400 	 * Treat 1TB disk as (1T - 512)B.
19401 	 */
19402 	if (un->un_f_capacity_adjusted == 1)
19403 	    capacity = DK_MAX_BLOCKS;
19404 #endif
19405 
19406 	/*
19407 	 * Copy the values from the READ CAPACITY command into the space
19408 	 * provided by the caller.
19409 	 */
19410 	*capp = capacity;
19411 	*lbap = lbasize;
19412 
19413 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
19414 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19415 
19416 	/*
19417 	 * Both the lbasize and capacity from the device must be nonzero,
19418 	 * otherwise we assume that the values are not valid and return
19419 	 * failure to the caller. (4203735)
19420 	 */
19421 	if ((capacity == 0) || (lbasize == 0)) {
19422 		return (EIO);
19423 	}
19424 
19425 	return (0);
19426 }
19427 
19428 /*
19429  *    Function: sd_send_scsi_READ_CAPACITY_16
19430  *
19431  * Description: This routine uses the scsi READ CAPACITY 16 command to
19432  *		determine the device capacity in number of blocks and the
19433  *		device native block size.  If this function returns a failure,
19434  *		then the values in *capp and *lbap are undefined.
19435  *		This routine should always be called by
19436  *		sd_send_scsi_READ_CAPACITY which will appy any device
19437  *		specific adjustments to capacity and lbasize.
19438  *
19439  *   Arguments: un   - ptr to soft state struct for the target
19440  *		capp - ptr to unsigned 64-bit variable to receive the
19441  *			capacity value from the command.
19442  *		lbap - ptr to unsigned 32-bit varaible to receive the
19443  *			block size value from the command
19444  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19445  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19446  *			to use the USCSI "direct" chain and bypass the normal
19447  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
19448  *			this command is issued as part of an error recovery
19449  *			action.
19450  *
19451  * Return Code: 0   - Success
19452  *		EIO - IO error
19453  *		EACCES - Reservation conflict detected
19454  *		EAGAIN - Device is becoming ready
19455  *		errno return code from sd_send_scsi_cmd()
19456  *
19457  *     Context: Can sleep.  Blocks until command completes.
19458  */
19459 
19460 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
19461 
19462 static int
19463 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
19464 	uint32_t *lbap, int path_flag)
19465 {
19466 	struct	scsi_extended_sense	sense_buf;
19467 	struct	uscsi_cmd	ucmd_buf;
19468 	union	scsi_cdb	cdb;
19469 	uint64_t		*capacity16_buf;
19470 	uint64_t		capacity;
19471 	uint32_t		lbasize;
19472 	int			status;
19473 
19474 	ASSERT(un != NULL);
19475 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19476 	ASSERT(capp != NULL);
19477 	ASSERT(lbap != NULL);
19478 
19479 	SD_TRACE(SD_LOG_IO, un,
19480 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19481 
19482 	/*
19483 	 * First send a READ_CAPACITY_16 command to the target.
19484 	 *
19485 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
19486 	 * Medium Indicator bit is cleared.  The address field must be
19487 	 * zero if the PMI bit is zero.
19488 	 */
19489 	bzero(&cdb, sizeof (cdb));
19490 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19491 
19492 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
19493 
19494 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19495 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
19496 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
19497 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
19498 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19499 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19500 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19501 	ucmd_buf.uscsi_timeout	= 60;
19502 
19503 	/*
19504 	 * Read Capacity (16) is a Service Action In command.  One
19505 	 * command byte (0x9E) is overloaded for multiple operations,
19506 	 * with the second CDB byte specifying the desired operation
19507 	 */
19508 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
19509 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
19510 
19511 	/*
19512 	 * Fill in allocation length field
19513 	 */
19514 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
19515 
19516 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
19517 	    UIO_SYSSPACE, path_flag);
19518 
19519 	switch (status) {
19520 	case 0:
19521 		/* Return failure if we did not get valid capacity data. */
19522 		if (ucmd_buf.uscsi_resid > 20) {
19523 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19524 			return (EIO);
19525 		}
19526 
19527 		/*
19528 		 * Read capacity and block size from the READ CAPACITY 10 data.
19529 		 * This data may be adjusted later due to device specific
19530 		 * issues.
19531 		 *
19532 		 * According to the SCSI spec, the READ CAPACITY 10
19533 		 * command returns the following:
19534 		 *
19535 		 *  bytes 0-7: Maximum logical block address available.
19536 		 *		(MSB in byte:0 & LSB in byte:7)
19537 		 *
19538 		 *  bytes 8-11: Block length in bytes
19539 		 *		(MSB in byte:8 & LSB in byte:11)
19540 		 *
19541 		 */
19542 		capacity = BE_64(capacity16_buf[0]);
19543 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
19544 
19545 		/*
19546 		 * Done with capacity16_buf
19547 		 */
19548 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19549 
19550 		/*
19551 		 * if the reported capacity is set to all 0xf's, then
19552 		 * this disk is too large.  This could only happen with
19553 		 * a device that supports LBAs larger than 64 bits which
19554 		 * are not defined by any current T10 standards.
19555 		 */
19556 		if (capacity == 0xffffffffffffffff) {
19557 			return (EIO);
19558 		}
19559 		break;	/* Success! */
19560 	case EIO:
19561 		switch (ucmd_buf.uscsi_status) {
19562 		case STATUS_RESERVATION_CONFLICT:
19563 			status = EACCES;
19564 			break;
19565 		case STATUS_CHECK:
19566 			/*
19567 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
19568 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19569 			 */
19570 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19571 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
19572 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
19573 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19574 				return (EAGAIN);
19575 			}
19576 			break;
19577 		default:
19578 			break;
19579 		}
19580 		/* FALLTHRU */
19581 	default:
19582 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19583 		return (status);
19584 	}
19585 
19586 	*capp = capacity;
19587 	*lbap = lbasize;
19588 
19589 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
19590 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19591 
19592 	return (0);
19593 }
19594 
19595 
19596 /*
19597  *    Function: sd_send_scsi_START_STOP_UNIT
19598  *
19599  * Description: Issue a scsi START STOP UNIT command to the target.
19600  *
19601  *   Arguments: un    - pointer to driver soft state (unit) structure for
19602  *			this target.
19603  *		flag  - SD_TARGET_START
19604  *			SD_TARGET_STOP
19605  *			SD_TARGET_EJECT
19606  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19607  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19608  *			to use the USCSI "direct" chain and bypass the normal
19609  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19610  *			command is issued as part of an error recovery action.
19611  *
19612  * Return Code: 0   - Success
19613  *		EIO - IO error
19614  *		EACCES - Reservation conflict detected
19615  *		ENXIO  - Not Ready, medium not present
19616  *		errno return code from sd_send_scsi_cmd()
19617  *
19618  *     Context: Can sleep.
19619  */
19620 
19621 static int
19622 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag)
19623 {
19624 	struct	scsi_extended_sense	sense_buf;
19625 	union scsi_cdb		cdb;
19626 	struct uscsi_cmd	ucmd_buf;
19627 	int			status;
19628 
19629 	ASSERT(un != NULL);
19630 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19631 
19632 	SD_TRACE(SD_LOG_IO, un,
19633 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
19634 
19635 	if (un->un_f_check_start_stop &&
19636 	    ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
19637 	    (un->un_f_start_stop_supported != TRUE)) {
19638 		return (0);
19639 	}
19640 
19641 	bzero(&cdb, sizeof (cdb));
19642 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19643 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19644 
19645 	cdb.scc_cmd = SCMD_START_STOP;
19646 	cdb.cdb_opaque[4] = (uchar_t)flag;
19647 
19648 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19649 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19650 	ucmd_buf.uscsi_bufaddr	= NULL;
19651 	ucmd_buf.uscsi_buflen	= 0;
19652 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19653 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19654 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19655 	ucmd_buf.uscsi_timeout	= 200;
19656 
19657 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
19658 	    UIO_SYSSPACE, path_flag);
19659 
19660 	switch (status) {
19661 	case 0:
19662 		break;	/* Success! */
19663 	case EIO:
19664 		switch (ucmd_buf.uscsi_status) {
19665 		case STATUS_RESERVATION_CONFLICT:
19666 			status = EACCES;
19667 			break;
19668 		case STATUS_CHECK:
19669 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
19670 				switch (scsi_sense_key(
19671 						(uint8_t *)&sense_buf)) {
19672 				case KEY_ILLEGAL_REQUEST:
19673 					status = ENOTSUP;
19674 					break;
19675 				case KEY_NOT_READY:
19676 					if (scsi_sense_asc(
19677 						    (uint8_t *)&sense_buf)
19678 					    == 0x3A) {
19679 						status = ENXIO;
19680 					}
19681 					break;
19682 				default:
19683 					break;
19684 				}
19685 			}
19686 			break;
19687 		default:
19688 			break;
19689 		}
19690 		break;
19691 	default:
19692 		break;
19693 	}
19694 
19695 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
19696 
19697 	return (status);
19698 }
19699 
19700 
19701 /*
19702  *    Function: sd_start_stop_unit_callback
19703  *
19704  * Description: timeout(9F) callback to begin recovery process for a
19705  *		device that has spun down.
19706  *
19707  *   Arguments: arg - pointer to associated softstate struct.
19708  *
19709  *     Context: Executes in a timeout(9F) thread context
19710  */
19711 
19712 static void
19713 sd_start_stop_unit_callback(void *arg)
19714 {
19715 	struct sd_lun	*un = arg;
19716 	ASSERT(un != NULL);
19717 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19718 
19719 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
19720 
19721 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
19722 }
19723 
19724 
19725 /*
19726  *    Function: sd_start_stop_unit_task
19727  *
19728  * Description: Recovery procedure when a drive is spun down.
19729  *
19730  *   Arguments: arg - pointer to associated softstate struct.
19731  *
19732  *     Context: Executes in a taskq() thread context
19733  */
19734 
19735 static void
19736 sd_start_stop_unit_task(void *arg)
19737 {
19738 	struct sd_lun	*un = arg;
19739 
19740 	ASSERT(un != NULL);
19741 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19742 
19743 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
19744 
19745 	/*
19746 	 * Some unformatted drives report not ready error, no need to
19747 	 * restart if format has been initiated.
19748 	 */
19749 	mutex_enter(SD_MUTEX(un));
19750 	if (un->un_f_format_in_progress == TRUE) {
19751 		mutex_exit(SD_MUTEX(un));
19752 		return;
19753 	}
19754 	mutex_exit(SD_MUTEX(un));
19755 
19756 	/*
19757 	 * When a START STOP command is issued from here, it is part of a
19758 	 * failure recovery operation and must be issued before any other
19759 	 * commands, including any pending retries. Thus it must be sent
19760 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
19761 	 * succeeds or not, we will start I/O after the attempt.
19762 	 */
19763 	(void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
19764 	    SD_PATH_DIRECT_PRIORITY);
19765 
19766 	/*
19767 	 * The above call blocks until the START_STOP_UNIT command completes.
19768 	 * Now that it has completed, we must re-try the original IO that
19769 	 * received the NOT READY condition in the first place. There are
19770 	 * three possible conditions here:
19771 	 *
19772 	 *  (1) The original IO is on un_retry_bp.
19773 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
19774 	 *	is NULL.
19775 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
19776 	 *	points to some other, unrelated bp.
19777 	 *
19778 	 * For each case, we must call sd_start_cmds() with un_retry_bp
19779 	 * as the argument. If un_retry_bp is NULL, this will initiate
19780 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
19781 	 * then this will process the bp on un_retry_bp. That may or may not
19782 	 * be the original IO, but that does not matter: the important thing
19783 	 * is to keep the IO processing going at this point.
19784 	 *
19785 	 * Note: This is a very specific error recovery sequence associated
19786 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
19787 	 * serialize the I/O with completion of the spin-up.
19788 	 */
19789 	mutex_enter(SD_MUTEX(un));
19790 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19791 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
19792 	    un, un->un_retry_bp);
19793 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
19794 	sd_start_cmds(un, un->un_retry_bp);
19795 	mutex_exit(SD_MUTEX(un));
19796 
19797 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
19798 }
19799 
19800 
19801 /*
19802  *    Function: sd_send_scsi_INQUIRY
19803  *
19804  * Description: Issue the scsi INQUIRY command.
19805  *
19806  *   Arguments: un
19807  *		bufaddr
19808  *		buflen
19809  *		evpd
19810  *		page_code
19811  *		page_length
19812  *
19813  * Return Code: 0   - Success
19814  *		errno return code from sd_send_scsi_cmd()
19815  *
19816  *     Context: Can sleep. Does not return until command is completed.
19817  */
19818 
19819 static int
19820 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen,
19821 	uchar_t evpd, uchar_t page_code, size_t *residp)
19822 {
19823 	union scsi_cdb		cdb;
19824 	struct uscsi_cmd	ucmd_buf;
19825 	int			status;
19826 
19827 	ASSERT(un != NULL);
19828 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19829 	ASSERT(bufaddr != NULL);
19830 
19831 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
19832 
19833 	bzero(&cdb, sizeof (cdb));
19834 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19835 	bzero(bufaddr, buflen);
19836 
19837 	cdb.scc_cmd = SCMD_INQUIRY;
19838 	cdb.cdb_opaque[1] = evpd;
19839 	cdb.cdb_opaque[2] = page_code;
19840 	FORMG0COUNT(&cdb, buflen);
19841 
19842 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19843 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19844 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
19845 	ucmd_buf.uscsi_buflen	= buflen;
19846 	ucmd_buf.uscsi_rqbuf	= NULL;
19847 	ucmd_buf.uscsi_rqlen	= 0;
19848 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
19849 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
19850 
19851 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
19852 	    UIO_SYSSPACE, SD_PATH_DIRECT);
19853 
19854 	if ((status == 0) && (residp != NULL)) {
19855 		*residp = ucmd_buf.uscsi_resid;
19856 	}
19857 
19858 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
19859 
19860 	return (status);
19861 }
19862 
19863 
19864 /*
19865  *    Function: sd_send_scsi_TEST_UNIT_READY
19866  *
19867  * Description: Issue the scsi TEST UNIT READY command.
19868  *		This routine can be told to set the flag USCSI_DIAGNOSE to
19869  *		prevent retrying failed commands. Use this when the intent
19870  *		is either to check for device readiness, to clear a Unit
19871  *		Attention, or to clear any outstanding sense data.
19872  *		However under specific conditions the expected behavior
19873  *		is for retries to bring a device ready, so use the flag
19874  *		with caution.
19875  *
19876  *   Arguments: un
19877  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
19878  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
19879  *			0: dont check for media present, do retries on cmd.
19880  *
19881  * Return Code: 0   - Success
19882  *		EIO - IO error
19883  *		EACCES - Reservation conflict detected
19884  *		ENXIO  - Not Ready, medium not present
19885  *		errno return code from sd_send_scsi_cmd()
19886  *
19887  *     Context: Can sleep. Does not return until command is completed.
19888  */
19889 
19890 static int
19891 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag)
19892 {
19893 	struct	scsi_extended_sense	sense_buf;
19894 	union scsi_cdb		cdb;
19895 	struct uscsi_cmd	ucmd_buf;
19896 	int			status;
19897 
19898 	ASSERT(un != NULL);
19899 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19900 
19901 	SD_TRACE(SD_LOG_IO, un,
19902 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
19903 
19904 	/*
19905 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
19906 	 * timeouts when they receive a TUR and the queue is not empty. Check
19907 	 * the configuration flag set during attach (indicating the drive has
19908 	 * this firmware bug) and un_ncmds_in_transport before issuing the
19909 	 * TUR. If there are
19910 	 * pending commands return success, this is a bit arbitrary but is ok
19911 	 * for non-removables (i.e. the eliteI disks) and non-clustering
19912 	 * configurations.
19913 	 */
19914 	if (un->un_f_cfg_tur_check == TRUE) {
19915 		mutex_enter(SD_MUTEX(un));
19916 		if (un->un_ncmds_in_transport != 0) {
19917 			mutex_exit(SD_MUTEX(un));
19918 			return (0);
19919 		}
19920 		mutex_exit(SD_MUTEX(un));
19921 	}
19922 
19923 	bzero(&cdb, sizeof (cdb));
19924 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19925 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19926 
19927 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
19928 
19929 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19930 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19931 	ucmd_buf.uscsi_bufaddr	= NULL;
19932 	ucmd_buf.uscsi_buflen	= 0;
19933 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19934 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19935 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19936 
19937 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
19938 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
19939 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
19940 	}
19941 	ucmd_buf.uscsi_timeout	= 60;
19942 
19943 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
19944 	    UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
19945 	    SD_PATH_STANDARD));
19946 
19947 	switch (status) {
19948 	case 0:
19949 		break;	/* Success! */
19950 	case EIO:
19951 		switch (ucmd_buf.uscsi_status) {
19952 		case STATUS_RESERVATION_CONFLICT:
19953 			status = EACCES;
19954 			break;
19955 		case STATUS_CHECK:
19956 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
19957 				break;
19958 			}
19959 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19960 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
19961 				KEY_NOT_READY) &&
19962 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
19963 				status = ENXIO;
19964 			}
19965 			break;
19966 		default:
19967 			break;
19968 		}
19969 		break;
19970 	default:
19971 		break;
19972 	}
19973 
19974 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
19975 
19976 	return (status);
19977 }
19978 
19979 
19980 /*
19981  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
19982  *
19983  * Description: Issue the scsi PERSISTENT RESERVE IN command.
19984  *
19985  *   Arguments: un
19986  *
19987  * Return Code: 0   - Success
19988  *		EACCES
19989  *		ENOTSUP
19990  *		errno return code from sd_send_scsi_cmd()
19991  *
19992  *     Context: Can sleep. Does not return until command is completed.
19993  */
19994 
19995 static int
19996 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t  usr_cmd,
19997 	uint16_t data_len, uchar_t *data_bufp)
19998 {
19999 	struct scsi_extended_sense	sense_buf;
20000 	union scsi_cdb		cdb;
20001 	struct uscsi_cmd	ucmd_buf;
20002 	int			status;
20003 	int			no_caller_buf = FALSE;
20004 
20005 	ASSERT(un != NULL);
20006 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20007 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20008 
20009 	SD_TRACE(SD_LOG_IO, un,
20010 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20011 
20012 	bzero(&cdb, sizeof (cdb));
20013 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20014 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20015 	if (data_bufp == NULL) {
20016 		/* Allocate a default buf if the caller did not give one */
20017 		ASSERT(data_len == 0);
20018 		data_len  = MHIOC_RESV_KEY_SIZE;
20019 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20020 		no_caller_buf = TRUE;
20021 	}
20022 
20023 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20024 	cdb.cdb_opaque[1] = usr_cmd;
20025 	FORMG1COUNT(&cdb, data_len);
20026 
20027 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20028 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20029 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
20030 	ucmd_buf.uscsi_buflen	= data_len;
20031 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20032 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20033 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20034 	ucmd_buf.uscsi_timeout	= 60;
20035 
20036 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
20037 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20038 
20039 	switch (status) {
20040 	case 0:
20041 		break;	/* Success! */
20042 	case EIO:
20043 		switch (ucmd_buf.uscsi_status) {
20044 		case STATUS_RESERVATION_CONFLICT:
20045 			status = EACCES;
20046 			break;
20047 		case STATUS_CHECK:
20048 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20049 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20050 				KEY_ILLEGAL_REQUEST)) {
20051 				status = ENOTSUP;
20052 			}
20053 			break;
20054 		default:
20055 			break;
20056 		}
20057 		break;
20058 	default:
20059 		break;
20060 	}
20061 
20062 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20063 
20064 	if (no_caller_buf == TRUE) {
20065 		kmem_free(data_bufp, data_len);
20066 	}
20067 
20068 	return (status);
20069 }
20070 
20071 
20072 /*
20073  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20074  *
20075  * Description: This routine is the driver entry point for handling CD-ROM
20076  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20077  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20078  *		device.
20079  *
20080  *   Arguments: un  -   Pointer to soft state struct for the target.
20081  *		usr_cmd SCSI-3 reservation facility command (one of
20082  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20083  *			SD_SCSI3_PREEMPTANDABORT)
20084  *		usr_bufp - user provided pointer register, reserve descriptor or
20085  *			preempt and abort structure (mhioc_register_t,
20086  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
20087  *
20088  * Return Code: 0   - Success
20089  *		EACCES
20090  *		ENOTSUP
20091  *		errno return code from sd_send_scsi_cmd()
20092  *
20093  *     Context: Can sleep. Does not return until command is completed.
20094  */
20095 
20096 static int
20097 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd,
20098 	uchar_t	*usr_bufp)
20099 {
20100 	struct scsi_extended_sense	sense_buf;
20101 	union scsi_cdb		cdb;
20102 	struct uscsi_cmd	ucmd_buf;
20103 	int			status;
20104 	uchar_t			data_len = sizeof (sd_prout_t);
20105 	sd_prout_t		*prp;
20106 
20107 	ASSERT(un != NULL);
20108 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20109 	ASSERT(data_len == 24);	/* required by scsi spec */
20110 
20111 	SD_TRACE(SD_LOG_IO, un,
20112 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
20113 
20114 	if (usr_bufp == NULL) {
20115 		return (EINVAL);
20116 	}
20117 
20118 	bzero(&cdb, sizeof (cdb));
20119 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20120 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20121 	prp = kmem_zalloc(data_len, KM_SLEEP);
20122 
20123 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
20124 	cdb.cdb_opaque[1] = usr_cmd;
20125 	FORMG1COUNT(&cdb, data_len);
20126 
20127 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20128 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20129 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
20130 	ucmd_buf.uscsi_buflen	= data_len;
20131 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20132 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20133 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20134 	ucmd_buf.uscsi_timeout	= 60;
20135 
20136 	switch (usr_cmd) {
20137 	case SD_SCSI3_REGISTER: {
20138 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
20139 
20140 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20141 		bcopy(ptr->newkey.key, prp->service_key,
20142 		    MHIOC_RESV_KEY_SIZE);
20143 		prp->aptpl = ptr->aptpl;
20144 		break;
20145 	}
20146 	case SD_SCSI3_RESERVE:
20147 	case SD_SCSI3_RELEASE: {
20148 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20149 
20150 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20151 		prp->scope_address = BE_32(ptr->scope_specific_addr);
20152 		cdb.cdb_opaque[2] = ptr->type;
20153 		break;
20154 	}
20155 	case SD_SCSI3_PREEMPTANDABORT: {
20156 		mhioc_preemptandabort_t *ptr =
20157 		    (mhioc_preemptandabort_t *)usr_bufp;
20158 
20159 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20160 		bcopy(ptr->victim_key.key, prp->service_key,
20161 		    MHIOC_RESV_KEY_SIZE);
20162 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
20163 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
20164 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
20165 		break;
20166 	}
20167 	case SD_SCSI3_REGISTERANDIGNOREKEY:
20168 	{
20169 		mhioc_registerandignorekey_t *ptr;
20170 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
20171 		bcopy(ptr->newkey.key,
20172 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
20173 		prp->aptpl = ptr->aptpl;
20174 		break;
20175 	}
20176 	default:
20177 		ASSERT(FALSE);
20178 		break;
20179 	}
20180 
20181 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
20182 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20183 
20184 	switch (status) {
20185 	case 0:
20186 		break;	/* Success! */
20187 	case EIO:
20188 		switch (ucmd_buf.uscsi_status) {
20189 		case STATUS_RESERVATION_CONFLICT:
20190 			status = EACCES;
20191 			break;
20192 		case STATUS_CHECK:
20193 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20194 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20195 				KEY_ILLEGAL_REQUEST)) {
20196 				status = ENOTSUP;
20197 			}
20198 			break;
20199 		default:
20200 			break;
20201 		}
20202 		break;
20203 	default:
20204 		break;
20205 	}
20206 
20207 	kmem_free(prp, data_len);
20208 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
20209 	return (status);
20210 }
20211 
20212 
20213 /*
20214  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
20215  *
20216  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
20217  *
20218  *   Arguments: un - pointer to the target's soft state struct
20219  *
20220  * Return Code: 0 - success
20221  *		errno-type error code
20222  *
20223  *     Context: kernel thread context only.
20224  */
20225 
20226 static int
20227 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
20228 {
20229 	struct sd_uscsi_info	*uip;
20230 	struct uscsi_cmd	*uscmd;
20231 	union scsi_cdb		*cdb;
20232 	struct buf		*bp;
20233 	int			rval = 0;
20234 
20235 	SD_TRACE(SD_LOG_IO, un,
20236 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
20237 
20238 	ASSERT(un != NULL);
20239 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20240 
20241 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
20242 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
20243 
20244 	/*
20245 	 * First get some memory for the uscsi_cmd struct and cdb
20246 	 * and initialize for SYNCHRONIZE_CACHE cmd.
20247 	 */
20248 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
20249 	uscmd->uscsi_cdblen = CDB_GROUP1;
20250 	uscmd->uscsi_cdb = (caddr_t)cdb;
20251 	uscmd->uscsi_bufaddr = NULL;
20252 	uscmd->uscsi_buflen = 0;
20253 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
20254 	uscmd->uscsi_rqlen = SENSE_LENGTH;
20255 	uscmd->uscsi_rqresid = SENSE_LENGTH;
20256 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20257 	uscmd->uscsi_timeout = sd_io_time;
20258 
20259 	/*
20260 	 * Allocate an sd_uscsi_info struct and fill it with the info
20261 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
20262 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
20263 	 * since we allocate the buf here in this function, we do not
20264 	 * need to preserve the prior contents of b_private.
20265 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
20266 	 */
20267 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
20268 	uip->ui_flags = SD_PATH_DIRECT;
20269 	uip->ui_cmdp  = uscmd;
20270 
20271 	bp = getrbuf(KM_SLEEP);
20272 	bp->b_private = uip;
20273 
20274 	/*
20275 	 * Setup buffer to carry uscsi request.
20276 	 */
20277 	bp->b_flags  = B_BUSY;
20278 	bp->b_bcount = 0;
20279 	bp->b_blkno  = 0;
20280 
20281 	if (dkc != NULL) {
20282 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
20283 		uip->ui_dkc = *dkc;
20284 	}
20285 
20286 	bp->b_edev = SD_GET_DEV(un);
20287 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
20288 
20289 	(void) sd_uscsi_strategy(bp);
20290 
20291 	/*
20292 	 * If synchronous request, wait for completion
20293 	 * If async just return and let b_iodone callback
20294 	 * cleanup.
20295 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
20296 	 * but it was also incremented in sd_uscsi_strategy(), so
20297 	 * we should be ok.
20298 	 */
20299 	if (dkc == NULL) {
20300 		(void) biowait(bp);
20301 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
20302 	}
20303 
20304 	return (rval);
20305 }
20306 
20307 
20308 static int
20309 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
20310 {
20311 	struct sd_uscsi_info *uip;
20312 	struct uscsi_cmd *uscmd;
20313 	uint8_t *sense_buf;
20314 	struct sd_lun *un;
20315 	int status;
20316 
20317 	uip = (struct sd_uscsi_info *)(bp->b_private);
20318 	ASSERT(uip != NULL);
20319 
20320 	uscmd = uip->ui_cmdp;
20321 	ASSERT(uscmd != NULL);
20322 
20323 	sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
20324 	ASSERT(sense_buf != NULL);
20325 
20326 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
20327 	ASSERT(un != NULL);
20328 
20329 	status = geterror(bp);
20330 	switch (status) {
20331 	case 0:
20332 		break;	/* Success! */
20333 	case EIO:
20334 		switch (uscmd->uscsi_status) {
20335 		case STATUS_RESERVATION_CONFLICT:
20336 			/* Ignore reservation conflict */
20337 			status = 0;
20338 			goto done;
20339 
20340 		case STATUS_CHECK:
20341 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
20342 			    (scsi_sense_key(sense_buf) ==
20343 				KEY_ILLEGAL_REQUEST)) {
20344 				/* Ignore Illegal Request error */
20345 				mutex_enter(SD_MUTEX(un));
20346 				un->un_f_sync_cache_supported = FALSE;
20347 				mutex_exit(SD_MUTEX(un));
20348 				status = ENOTSUP;
20349 				goto done;
20350 			}
20351 			break;
20352 		default:
20353 			break;
20354 		}
20355 		/* FALLTHRU */
20356 	default:
20357 		/*
20358 		 * Don't log an error message if this device
20359 		 * has removable media.
20360 		 */
20361 		if (!un->un_f_has_removable_media) {
20362 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
20363 			    "SYNCHRONIZE CACHE command failed (%d)\n", status);
20364 		}
20365 		break;
20366 	}
20367 
20368 done:
20369 	if (uip->ui_dkc.dkc_callback != NULL) {
20370 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
20371 	}
20372 
20373 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
20374 	freerbuf(bp);
20375 	kmem_free(uip, sizeof (struct sd_uscsi_info));
20376 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
20377 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
20378 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
20379 
20380 	return (status);
20381 }
20382 
20383 
20384 /*
20385  *    Function: sd_send_scsi_GET_CONFIGURATION
20386  *
20387  * Description: Issues the get configuration command to the device.
20388  *		Called from sd_check_for_writable_cd & sd_get_media_info
20389  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
20390  *   Arguments: un
20391  *		ucmdbuf
20392  *		rqbuf
20393  *		rqbuflen
20394  *		bufaddr
20395  *		buflen
20396  *
20397  * Return Code: 0   - Success
20398  *		errno return code from sd_send_scsi_cmd()
20399  *
20400  *     Context: Can sleep. Does not return until command is completed.
20401  *
20402  */
20403 
20404 static int
20405 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf,
20406 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen)
20407 {
20408 	char	cdb[CDB_GROUP1];
20409 	int	status;
20410 
20411 	ASSERT(un != NULL);
20412 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20413 	ASSERT(bufaddr != NULL);
20414 	ASSERT(ucmdbuf != NULL);
20415 	ASSERT(rqbuf != NULL);
20416 
20417 	SD_TRACE(SD_LOG_IO, un,
20418 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
20419 
20420 	bzero(cdb, sizeof (cdb));
20421 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
20422 	bzero(rqbuf, rqbuflen);
20423 	bzero(bufaddr, buflen);
20424 
20425 	/*
20426 	 * Set up cdb field for the get configuration command.
20427 	 */
20428 	cdb[0] = SCMD_GET_CONFIGURATION;
20429 	cdb[1] = 0x02;  /* Requested Type */
20430 	cdb[8] = SD_PROFILE_HEADER_LEN;
20431 	ucmdbuf->uscsi_cdb = cdb;
20432 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
20433 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
20434 	ucmdbuf->uscsi_buflen = buflen;
20435 	ucmdbuf->uscsi_timeout = sd_io_time;
20436 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
20437 	ucmdbuf->uscsi_rqlen = rqbuflen;
20438 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
20439 
20440 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL,
20441 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20442 
20443 	switch (status) {
20444 	case 0:
20445 		break;  /* Success! */
20446 	case EIO:
20447 		switch (ucmdbuf->uscsi_status) {
20448 		case STATUS_RESERVATION_CONFLICT:
20449 			status = EACCES;
20450 			break;
20451 		default:
20452 			break;
20453 		}
20454 		break;
20455 	default:
20456 		break;
20457 	}
20458 
20459 	if (status == 0) {
20460 		SD_DUMP_MEMORY(un, SD_LOG_IO,
20461 		    "sd_send_scsi_GET_CONFIGURATION: data",
20462 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
20463 	}
20464 
20465 	SD_TRACE(SD_LOG_IO, un,
20466 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
20467 
20468 	return (status);
20469 }
20470 
20471 /*
20472  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
20473  *
20474  * Description: Issues the get configuration command to the device to
20475  *              retrieve a specfic feature. Called from
20476  *		sd_check_for_writable_cd & sd_set_mmc_caps.
20477  *   Arguments: un
20478  *              ucmdbuf
20479  *              rqbuf
20480  *              rqbuflen
20481  *              bufaddr
20482  *              buflen
20483  *		feature
20484  *
20485  * Return Code: 0   - Success
20486  *              errno return code from sd_send_scsi_cmd()
20487  *
20488  *     Context: Can sleep. Does not return until command is completed.
20489  *
20490  */
20491 static int
20492 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
20493 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
20494 	uchar_t *bufaddr, uint_t buflen, char feature)
20495 {
20496 	char    cdb[CDB_GROUP1];
20497 	int	status;
20498 
20499 	ASSERT(un != NULL);
20500 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20501 	ASSERT(bufaddr != NULL);
20502 	ASSERT(ucmdbuf != NULL);
20503 	ASSERT(rqbuf != NULL);
20504 
20505 	SD_TRACE(SD_LOG_IO, un,
20506 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
20507 
20508 	bzero(cdb, sizeof (cdb));
20509 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
20510 	bzero(rqbuf, rqbuflen);
20511 	bzero(bufaddr, buflen);
20512 
20513 	/*
20514 	 * Set up cdb field for the get configuration command.
20515 	 */
20516 	cdb[0] = SCMD_GET_CONFIGURATION;
20517 	cdb[1] = 0x02;  /* Requested Type */
20518 	cdb[3] = feature;
20519 	cdb[8] = buflen;
20520 	ucmdbuf->uscsi_cdb = cdb;
20521 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
20522 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
20523 	ucmdbuf->uscsi_buflen = buflen;
20524 	ucmdbuf->uscsi_timeout = sd_io_time;
20525 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
20526 	ucmdbuf->uscsi_rqlen = rqbuflen;
20527 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
20528 
20529 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL,
20530 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20531 
20532 	switch (status) {
20533 	case 0:
20534 		break;  /* Success! */
20535 	case EIO:
20536 		switch (ucmdbuf->uscsi_status) {
20537 		case STATUS_RESERVATION_CONFLICT:
20538 			status = EACCES;
20539 			break;
20540 		default:
20541 			break;
20542 		}
20543 		break;
20544 	default:
20545 		break;
20546 	}
20547 
20548 	if (status == 0) {
20549 		SD_DUMP_MEMORY(un, SD_LOG_IO,
20550 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
20551 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
20552 	}
20553 
20554 	SD_TRACE(SD_LOG_IO, un,
20555 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
20556 
20557 	return (status);
20558 }
20559 
20560 
20561 /*
20562  *    Function: sd_send_scsi_MODE_SENSE
20563  *
20564  * Description: Utility function for issuing a scsi MODE SENSE command.
20565  *		Note: This routine uses a consistent implementation for Group0,
20566  *		Group1, and Group2 commands across all platforms. ATAPI devices
20567  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
20568  *
20569  *   Arguments: un - pointer to the softstate struct for the target.
20570  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
20571  *			  CDB_GROUP[1|2] (10 byte).
20572  *		bufaddr - buffer for page data retrieved from the target.
20573  *		buflen - size of page to be retrieved.
20574  *		page_code - page code of data to be retrieved from the target.
20575  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20576  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20577  *			to use the USCSI "direct" chain and bypass the normal
20578  *			command waitq.
20579  *
20580  * Return Code: 0   - Success
20581  *		errno return code from sd_send_scsi_cmd()
20582  *
20583  *     Context: Can sleep. Does not return until command is completed.
20584  */
20585 
20586 static int
20587 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
20588 	size_t buflen,  uchar_t page_code, int path_flag)
20589 {
20590 	struct	scsi_extended_sense	sense_buf;
20591 	union scsi_cdb		cdb;
20592 	struct uscsi_cmd	ucmd_buf;
20593 	int			status;
20594 	int			headlen;
20595 
20596 	ASSERT(un != NULL);
20597 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20598 	ASSERT(bufaddr != NULL);
20599 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
20600 	    (cdbsize == CDB_GROUP2));
20601 
20602 	SD_TRACE(SD_LOG_IO, un,
20603 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
20604 
20605 	bzero(&cdb, sizeof (cdb));
20606 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20607 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20608 	bzero(bufaddr, buflen);
20609 
20610 	if (cdbsize == CDB_GROUP0) {
20611 		cdb.scc_cmd = SCMD_MODE_SENSE;
20612 		cdb.cdb_opaque[2] = page_code;
20613 		FORMG0COUNT(&cdb, buflen);
20614 		headlen = MODE_HEADER_LENGTH;
20615 	} else {
20616 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
20617 		cdb.cdb_opaque[2] = page_code;
20618 		FORMG1COUNT(&cdb, buflen);
20619 		headlen = MODE_HEADER_LENGTH_GRP2;
20620 	}
20621 
20622 	ASSERT(headlen <= buflen);
20623 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20624 
20625 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20626 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20627 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20628 	ucmd_buf.uscsi_buflen	= buflen;
20629 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20630 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20631 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20632 	ucmd_buf.uscsi_timeout	= 60;
20633 
20634 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
20635 	    UIO_SYSSPACE, path_flag);
20636 
20637 	switch (status) {
20638 	case 0:
20639 		/*
20640 		 * sr_check_wp() uses 0x3f page code and check the header of
20641 		 * mode page to determine if target device is write-protected.
20642 		 * But some USB devices return 0 bytes for 0x3f page code. For
20643 		 * this case, make sure that mode page header is returned at
20644 		 * least.
20645 		 */
20646 		if (buflen - ucmd_buf.uscsi_resid <  headlen)
20647 			status = EIO;
20648 		break;	/* Success! */
20649 	case EIO:
20650 		switch (ucmd_buf.uscsi_status) {
20651 		case STATUS_RESERVATION_CONFLICT:
20652 			status = EACCES;
20653 			break;
20654 		default:
20655 			break;
20656 		}
20657 		break;
20658 	default:
20659 		break;
20660 	}
20661 
20662 	if (status == 0) {
20663 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
20664 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20665 	}
20666 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
20667 
20668 	return (status);
20669 }
20670 
20671 
20672 /*
20673  *    Function: sd_send_scsi_MODE_SELECT
20674  *
20675  * Description: Utility function for issuing a scsi MODE SELECT command.
20676  *		Note: This routine uses a consistent implementation for Group0,
20677  *		Group1, and Group2 commands across all platforms. ATAPI devices
20678  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
20679  *
20680  *   Arguments: un - pointer to the softstate struct for the target.
20681  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
20682  *			  CDB_GROUP[1|2] (10 byte).
20683  *		bufaddr - buffer for page data retrieved from the target.
20684  *		buflen - size of page to be retrieved.
20685  *		save_page - boolean to determin if SP bit should be set.
20686  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20687  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20688  *			to use the USCSI "direct" chain and bypass the normal
20689  *			command waitq.
20690  *
20691  * Return Code: 0   - Success
20692  *		errno return code from sd_send_scsi_cmd()
20693  *
20694  *     Context: Can sleep. Does not return until command is completed.
20695  */
20696 
20697 static int
20698 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
20699 	size_t buflen,  uchar_t save_page, int path_flag)
20700 {
20701 	struct	scsi_extended_sense	sense_buf;
20702 	union scsi_cdb		cdb;
20703 	struct uscsi_cmd	ucmd_buf;
20704 	int			status;
20705 
20706 	ASSERT(un != NULL);
20707 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20708 	ASSERT(bufaddr != NULL);
20709 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
20710 	    (cdbsize == CDB_GROUP2));
20711 
20712 	SD_TRACE(SD_LOG_IO, un,
20713 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
20714 
20715 	bzero(&cdb, sizeof (cdb));
20716 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20717 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20718 
20719 	/* Set the PF bit for many third party drives */
20720 	cdb.cdb_opaque[1] = 0x10;
20721 
20722 	/* Set the savepage(SP) bit if given */
20723 	if (save_page == SD_SAVE_PAGE) {
20724 		cdb.cdb_opaque[1] |= 0x01;
20725 	}
20726 
20727 	if (cdbsize == CDB_GROUP0) {
20728 		cdb.scc_cmd = SCMD_MODE_SELECT;
20729 		FORMG0COUNT(&cdb, buflen);
20730 	} else {
20731 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
20732 		FORMG1COUNT(&cdb, buflen);
20733 	}
20734 
20735 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20736 
20737 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20738 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20739 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20740 	ucmd_buf.uscsi_buflen	= buflen;
20741 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20742 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20743 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20744 	ucmd_buf.uscsi_timeout	= 60;
20745 
20746 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
20747 	    UIO_SYSSPACE, path_flag);
20748 
20749 	switch (status) {
20750 	case 0:
20751 		break;	/* Success! */
20752 	case EIO:
20753 		switch (ucmd_buf.uscsi_status) {
20754 		case STATUS_RESERVATION_CONFLICT:
20755 			status = EACCES;
20756 			break;
20757 		default:
20758 			break;
20759 		}
20760 		break;
20761 	default:
20762 		break;
20763 	}
20764 
20765 	if (status == 0) {
20766 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
20767 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20768 	}
20769 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
20770 
20771 	return (status);
20772 }
20773 
20774 
20775 /*
20776  *    Function: sd_send_scsi_RDWR
20777  *
20778  * Description: Issue a scsi READ or WRITE command with the given parameters.
20779  *
20780  *   Arguments: un:      Pointer to the sd_lun struct for the target.
20781  *		cmd:	 SCMD_READ or SCMD_WRITE
20782  *		bufaddr: Address of caller's buffer to receive the RDWR data
20783  *		buflen:  Length of caller's buffer receive the RDWR data.
20784  *		start_block: Block number for the start of the RDWR operation.
20785  *			 (Assumes target-native block size.)
20786  *		residp:  Pointer to variable to receive the redisual of the
20787  *			 RDWR operation (may be NULL of no residual requested).
20788  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20789  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20790  *			to use the USCSI "direct" chain and bypass the normal
20791  *			command waitq.
20792  *
20793  * Return Code: 0   - Success
20794  *		errno return code from sd_send_scsi_cmd()
20795  *
20796  *     Context: Can sleep. Does not return until command is completed.
20797  */
20798 
20799 static int
20800 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
20801 	size_t buflen, daddr_t start_block, int path_flag)
20802 {
20803 	struct	scsi_extended_sense	sense_buf;
20804 	union scsi_cdb		cdb;
20805 	struct uscsi_cmd	ucmd_buf;
20806 	uint32_t		block_count;
20807 	int			status;
20808 	int			cdbsize;
20809 	uchar_t			flag;
20810 
20811 	ASSERT(un != NULL);
20812 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20813 	ASSERT(bufaddr != NULL);
20814 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
20815 
20816 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
20817 
20818 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
20819 		return (EINVAL);
20820 	}
20821 
20822 	mutex_enter(SD_MUTEX(un));
20823 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
20824 	mutex_exit(SD_MUTEX(un));
20825 
20826 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
20827 
20828 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
20829 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
20830 	    bufaddr, buflen, start_block, block_count);
20831 
20832 	bzero(&cdb, sizeof (cdb));
20833 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20834 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20835 
20836 	/* Compute CDB size to use */
20837 	if (start_block > 0xffffffff)
20838 		cdbsize = CDB_GROUP4;
20839 	else if ((start_block & 0xFFE00000) ||
20840 	    (un->un_f_cfg_is_atapi == TRUE))
20841 		cdbsize = CDB_GROUP1;
20842 	else
20843 		cdbsize = CDB_GROUP0;
20844 
20845 	switch (cdbsize) {
20846 	case CDB_GROUP0:	/* 6-byte CDBs */
20847 		cdb.scc_cmd = cmd;
20848 		FORMG0ADDR(&cdb, start_block);
20849 		FORMG0COUNT(&cdb, block_count);
20850 		break;
20851 	case CDB_GROUP1:	/* 10-byte CDBs */
20852 		cdb.scc_cmd = cmd | SCMD_GROUP1;
20853 		FORMG1ADDR(&cdb, start_block);
20854 		FORMG1COUNT(&cdb, block_count);
20855 		break;
20856 	case CDB_GROUP4:	/* 16-byte CDBs */
20857 		cdb.scc_cmd = cmd | SCMD_GROUP4;
20858 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
20859 		FORMG4COUNT(&cdb, block_count);
20860 		break;
20861 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
20862 	default:
20863 		/* All others reserved */
20864 		return (EINVAL);
20865 	}
20866 
20867 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
20868 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20869 
20870 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20871 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20872 	ucmd_buf.uscsi_bufaddr	= bufaddr;
20873 	ucmd_buf.uscsi_buflen	= buflen;
20874 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20875 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20876 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
20877 	ucmd_buf.uscsi_timeout	= 60;
20878 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
20879 	    UIO_SYSSPACE, path_flag);
20880 	switch (status) {
20881 	case 0:
20882 		break;	/* Success! */
20883 	case EIO:
20884 		switch (ucmd_buf.uscsi_status) {
20885 		case STATUS_RESERVATION_CONFLICT:
20886 			status = EACCES;
20887 			break;
20888 		default:
20889 			break;
20890 		}
20891 		break;
20892 	default:
20893 		break;
20894 	}
20895 
20896 	if (status == 0) {
20897 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
20898 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20899 	}
20900 
20901 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
20902 
20903 	return (status);
20904 }
20905 
20906 
20907 /*
20908  *    Function: sd_send_scsi_LOG_SENSE
20909  *
20910  * Description: Issue a scsi LOG_SENSE command with the given parameters.
20911  *
20912  *   Arguments: un:      Pointer to the sd_lun struct for the target.
20913  *
20914  * Return Code: 0   - Success
20915  *		errno return code from sd_send_scsi_cmd()
20916  *
20917  *     Context: Can sleep. Does not return until command is completed.
20918  */
20919 
20920 static int
20921 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen,
20922 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
20923 	int path_flag)
20924 
20925 {
20926 	struct	scsi_extended_sense	sense_buf;
20927 	union scsi_cdb		cdb;
20928 	struct uscsi_cmd	ucmd_buf;
20929 	int			status;
20930 
20931 	ASSERT(un != NULL);
20932 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20933 
20934 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
20935 
20936 	bzero(&cdb, sizeof (cdb));
20937 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20938 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20939 
20940 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
20941 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
20942 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
20943 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
20944 	FORMG1COUNT(&cdb, buflen);
20945 
20946 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20947 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
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_READ | USCSI_SILENT;
20953 	ucmd_buf.uscsi_timeout	= 60;
20954 
20955 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
20956 	    UIO_SYSSPACE, path_flag);
20957 
20958 	switch (status) {
20959 	case 0:
20960 		break;
20961 	case EIO:
20962 		switch (ucmd_buf.uscsi_status) {
20963 		case STATUS_RESERVATION_CONFLICT:
20964 			status = EACCES;
20965 			break;
20966 		case STATUS_CHECK:
20967 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20968 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20969 				KEY_ILLEGAL_REQUEST) &&
20970 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
20971 				/*
20972 				 * ASC 0x24: INVALID FIELD IN CDB
20973 				 */
20974 				switch (page_code) {
20975 				case START_STOP_CYCLE_PAGE:
20976 					/*
20977 					 * The start stop cycle counter is
20978 					 * implemented as page 0x31 in earlier
20979 					 * generation disks. In new generation
20980 					 * disks the start stop cycle counter is
20981 					 * implemented as page 0xE. To properly
20982 					 * handle this case if an attempt for
20983 					 * log page 0xE is made and fails we
20984 					 * will try again using page 0x31.
20985 					 *
20986 					 * Network storage BU committed to
20987 					 * maintain the page 0x31 for this
20988 					 * purpose and will not have any other
20989 					 * page implemented with page code 0x31
20990 					 * until all disks transition to the
20991 					 * standard page.
20992 					 */
20993 					mutex_enter(SD_MUTEX(un));
20994 					un->un_start_stop_cycle_page =
20995 					    START_STOP_CYCLE_VU_PAGE;
20996 					cdb.cdb_opaque[2] =
20997 					    (char)(page_control << 6) |
20998 					    un->un_start_stop_cycle_page;
20999 					mutex_exit(SD_MUTEX(un));
21000 					status = sd_send_scsi_cmd(
21001 					    SD_GET_DEV(un), &ucmd_buf, FKIOCTL,
21002 					    UIO_SYSSPACE, path_flag);
21003 
21004 					break;
21005 				case TEMPERATURE_PAGE:
21006 					status = ENOTTY;
21007 					break;
21008 				default:
21009 					break;
21010 				}
21011 			}
21012 			break;
21013 		default:
21014 			break;
21015 		}
21016 		break;
21017 	default:
21018 		break;
21019 	}
21020 
21021 	if (status == 0) {
21022 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
21023 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21024 	}
21025 
21026 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
21027 
21028 	return (status);
21029 }
21030 
21031 
21032 /*
21033  *    Function: sdioctl
21034  *
21035  * Description: Driver's ioctl(9e) entry point function.
21036  *
21037  *   Arguments: dev     - device number
21038  *		cmd     - ioctl operation to be performed
21039  *		arg     - user argument, contains data to be set or reference
21040  *			  parameter for get
21041  *		flag    - bit flag, indicating open settings, 32/64 bit type
21042  *		cred_p  - user credential pointer
21043  *		rval_p  - calling process return value (OPT)
21044  *
21045  * Return Code: EINVAL
21046  *		ENOTTY
21047  *		ENXIO
21048  *		EIO
21049  *		EFAULT
21050  *		ENOTSUP
21051  *		EPERM
21052  *
21053  *     Context: Called from the device switch at normal priority.
21054  */
21055 
21056 static int
21057 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
21058 {
21059 	struct sd_lun	*un = NULL;
21060 	int		geom_validated = FALSE;
21061 	int		err = 0;
21062 	int		i = 0;
21063 	cred_t		*cr;
21064 
21065 	/*
21066 	 * All device accesses go thru sdstrategy where we check on suspend
21067 	 * status
21068 	 */
21069 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
21070 		return (ENXIO);
21071 	}
21072 
21073 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21074 
21075 	/*
21076 	 * Moved this wait from sd_uscsi_strategy to here for
21077 	 * reasons of deadlock prevention. Internal driver commands,
21078 	 * specifically those to change a devices power level, result
21079 	 * in a call to sd_uscsi_strategy.
21080 	 */
21081 	mutex_enter(SD_MUTEX(un));
21082 	while ((un->un_state == SD_STATE_SUSPENDED) ||
21083 	    (un->un_state == SD_STATE_PM_CHANGING)) {
21084 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
21085 	}
21086 	/*
21087 	 * Twiddling the counter here protects commands from now
21088 	 * through to the top of sd_uscsi_strategy. Without the
21089 	 * counter inc. a power down, for example, could get in
21090 	 * after the above check for state is made and before
21091 	 * execution gets to the top of sd_uscsi_strategy.
21092 	 * That would cause problems.
21093 	 */
21094 	un->un_ncmds_in_driver++;
21095 
21096 	if ((un->un_f_geometry_is_valid == FALSE) &&
21097 	    (flag & (FNDELAY | FNONBLOCK))) {
21098 		switch (cmd) {
21099 		case CDROMPAUSE:
21100 		case CDROMRESUME:
21101 		case CDROMPLAYMSF:
21102 		case CDROMPLAYTRKIND:
21103 		case CDROMREADTOCHDR:
21104 		case CDROMREADTOCENTRY:
21105 		case CDROMSTOP:
21106 		case CDROMSTART:
21107 		case CDROMVOLCTRL:
21108 		case CDROMSUBCHNL:
21109 		case CDROMREADMODE2:
21110 		case CDROMREADMODE1:
21111 		case CDROMREADOFFSET:
21112 		case CDROMSBLKMODE:
21113 		case CDROMGBLKMODE:
21114 		case CDROMGDRVSPEED:
21115 		case CDROMSDRVSPEED:
21116 		case CDROMCDDA:
21117 		case CDROMCDXA:
21118 		case CDROMSUBCODE:
21119 			if (!ISCD(un)) {
21120 				un->un_ncmds_in_driver--;
21121 				ASSERT(un->un_ncmds_in_driver >= 0);
21122 				mutex_exit(SD_MUTEX(un));
21123 				return (ENOTTY);
21124 			}
21125 			break;
21126 		case FDEJECT:
21127 		case DKIOCEJECT:
21128 		case CDROMEJECT:
21129 			if (!un->un_f_eject_media_supported) {
21130 				un->un_ncmds_in_driver--;
21131 				ASSERT(un->un_ncmds_in_driver >= 0);
21132 				mutex_exit(SD_MUTEX(un));
21133 				return (ENOTTY);
21134 			}
21135 			break;
21136 		case DKIOCSVTOC:
21137 		case DKIOCSETEFI:
21138 		case DKIOCSMBOOT:
21139 		case DKIOCFLUSHWRITECACHE:
21140 			mutex_exit(SD_MUTEX(un));
21141 			err = sd_send_scsi_TEST_UNIT_READY(un, 0);
21142 			if (err != 0) {
21143 				mutex_enter(SD_MUTEX(un));
21144 				un->un_ncmds_in_driver--;
21145 				ASSERT(un->un_ncmds_in_driver >= 0);
21146 				mutex_exit(SD_MUTEX(un));
21147 				return (EIO);
21148 			}
21149 			mutex_enter(SD_MUTEX(un));
21150 			/* FALLTHROUGH */
21151 		case DKIOCREMOVABLE:
21152 		case DKIOCHOTPLUGGABLE:
21153 		case DKIOCINFO:
21154 		case DKIOCGMEDIAINFO:
21155 		case MHIOCENFAILFAST:
21156 		case MHIOCSTATUS:
21157 		case MHIOCTKOWN:
21158 		case MHIOCRELEASE:
21159 		case MHIOCGRP_INKEYS:
21160 		case MHIOCGRP_INRESV:
21161 		case MHIOCGRP_REGISTER:
21162 		case MHIOCGRP_RESERVE:
21163 		case MHIOCGRP_PREEMPTANDABORT:
21164 		case MHIOCGRP_REGISTERANDIGNOREKEY:
21165 		case CDROMCLOSETRAY:
21166 		case USCSICMD:
21167 			goto skip_ready_valid;
21168 		default:
21169 			break;
21170 		}
21171 
21172 		mutex_exit(SD_MUTEX(un));
21173 		err = sd_ready_and_valid(un);
21174 		mutex_enter(SD_MUTEX(un));
21175 		if (err == SD_READY_NOT_VALID) {
21176 			switch (cmd) {
21177 			case DKIOCGAPART:
21178 			case DKIOCGGEOM:
21179 			case DKIOCSGEOM:
21180 			case DKIOCGVTOC:
21181 			case DKIOCSVTOC:
21182 			case DKIOCSAPART:
21183 			case DKIOCG_PHYGEOM:
21184 			case DKIOCG_VIRTGEOM:
21185 				err = ENOTSUP;
21186 				un->un_ncmds_in_driver--;
21187 				ASSERT(un->un_ncmds_in_driver >= 0);
21188 				mutex_exit(SD_MUTEX(un));
21189 				return (err);
21190 			}
21191 		}
21192 		if (err != SD_READY_VALID) {
21193 			switch (cmd) {
21194 			case DKIOCSTATE:
21195 			case CDROMGDRVSPEED:
21196 			case CDROMSDRVSPEED:
21197 			case FDEJECT:	/* for eject command */
21198 			case DKIOCEJECT:
21199 			case CDROMEJECT:
21200 			case DKIOCGETEFI:
21201 			case DKIOCSGEOM:
21202 			case DKIOCREMOVABLE:
21203 			case DKIOCHOTPLUGGABLE:
21204 			case DKIOCSAPART:
21205 			case DKIOCSETEFI:
21206 				break;
21207 			default:
21208 				if (un->un_f_has_removable_media) {
21209 					err = ENXIO;
21210 				} else {
21211 				/* Do not map SD_RESERVED_BY_OTHERS to EIO */
21212 					if (err == SD_RESERVED_BY_OTHERS) {
21213 						err = EACCES;
21214 					} else {
21215 						err = EIO;
21216 					}
21217 				}
21218 				un->un_ncmds_in_driver--;
21219 				ASSERT(un->un_ncmds_in_driver >= 0);
21220 				mutex_exit(SD_MUTEX(un));
21221 				return (err);
21222 			}
21223 		}
21224 		geom_validated = TRUE;
21225 	}
21226 	if ((un->un_f_geometry_is_valid == TRUE) &&
21227 	    (un->un_solaris_size > 0)) {
21228 		/*
21229 		 * the "geometry_is_valid" flag could be true if we
21230 		 * have an fdisk table but no Solaris partition
21231 		 */
21232 		if (un->un_vtoc.v_sanity != VTOC_SANE) {
21233 			/* it is EFI, so return ENOTSUP for these */
21234 			switch (cmd) {
21235 			case DKIOCGAPART:
21236 			case DKIOCGGEOM:
21237 			case DKIOCGVTOC:
21238 			case DKIOCSVTOC:
21239 			case DKIOCSAPART:
21240 				err = ENOTSUP;
21241 				un->un_ncmds_in_driver--;
21242 				ASSERT(un->un_ncmds_in_driver >= 0);
21243 				mutex_exit(SD_MUTEX(un));
21244 				return (err);
21245 			}
21246 		}
21247 	}
21248 
21249 skip_ready_valid:
21250 	mutex_exit(SD_MUTEX(un));
21251 
21252 	switch (cmd) {
21253 	case DKIOCINFO:
21254 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
21255 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
21256 		break;
21257 
21258 	case DKIOCGMEDIAINFO:
21259 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
21260 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
21261 		break;
21262 
21263 	case DKIOCGGEOM:
21264 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n");
21265 		err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag,
21266 		    geom_validated);
21267 		break;
21268 
21269 	case DKIOCSGEOM:
21270 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n");
21271 		err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag);
21272 		break;
21273 
21274 	case DKIOCGAPART:
21275 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n");
21276 		err = sd_dkio_get_partition(dev, (caddr_t)arg, flag,
21277 		    geom_validated);
21278 		break;
21279 
21280 	case DKIOCSAPART:
21281 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n");
21282 		err = sd_dkio_set_partition(dev, (caddr_t)arg, flag);
21283 		break;
21284 
21285 	case DKIOCGVTOC:
21286 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n");
21287 		err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag,
21288 		    geom_validated);
21289 		break;
21290 
21291 	case DKIOCGETEFI:
21292 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n");
21293 		err = sd_dkio_get_efi(dev, (caddr_t)arg, flag);
21294 		break;
21295 
21296 	case DKIOCPARTITION:
21297 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n");
21298 		err = sd_dkio_partition(dev, (caddr_t)arg, flag);
21299 		break;
21300 
21301 	case DKIOCSVTOC:
21302 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n");
21303 		err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag);
21304 		break;
21305 
21306 	case DKIOCSETEFI:
21307 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n");
21308 		err = sd_dkio_set_efi(dev, (caddr_t)arg, flag);
21309 		break;
21310 
21311 	case DKIOCGMBOOT:
21312 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n");
21313 		err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag);
21314 		break;
21315 
21316 	case DKIOCSMBOOT:
21317 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n");
21318 		err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag);
21319 		break;
21320 
21321 	case DKIOCLOCK:
21322 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
21323 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
21324 		    SD_PATH_STANDARD);
21325 		break;
21326 
21327 	case DKIOCUNLOCK:
21328 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
21329 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
21330 		    SD_PATH_STANDARD);
21331 		break;
21332 
21333 	case DKIOCSTATE: {
21334 		enum dkio_state		state;
21335 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
21336 
21337 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
21338 			err = EFAULT;
21339 		} else {
21340 			err = sd_check_media(dev, state);
21341 			if (err == 0) {
21342 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
21343 				    sizeof (int), flag) != 0)
21344 					err = EFAULT;
21345 			}
21346 		}
21347 		break;
21348 	}
21349 
21350 	case DKIOCREMOVABLE:
21351 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
21352 		i = un->un_f_has_removable_media ? 1 : 0;
21353 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
21354 			err = EFAULT;
21355 		} else {
21356 			err = 0;
21357 		}
21358 		break;
21359 
21360 	case DKIOCHOTPLUGGABLE:
21361 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
21362 		i = un->un_f_is_hotpluggable ? 1 : 0;
21363 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
21364 			err = EFAULT;
21365 		} else {
21366 			err = 0;
21367 		}
21368 		break;
21369 
21370 	case DKIOCGTEMPERATURE:
21371 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
21372 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
21373 		break;
21374 
21375 	case MHIOCENFAILFAST:
21376 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
21377 		if ((err = drv_priv(cred_p)) == 0) {
21378 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
21379 		}
21380 		break;
21381 
21382 	case MHIOCTKOWN:
21383 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
21384 		if ((err = drv_priv(cred_p)) == 0) {
21385 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
21386 		}
21387 		break;
21388 
21389 	case MHIOCRELEASE:
21390 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
21391 		if ((err = drv_priv(cred_p)) == 0) {
21392 			err = sd_mhdioc_release(dev);
21393 		}
21394 		break;
21395 
21396 	case MHIOCSTATUS:
21397 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
21398 		if ((err = drv_priv(cred_p)) == 0) {
21399 			switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) {
21400 			case 0:
21401 				err = 0;
21402 				break;
21403 			case EACCES:
21404 				*rval_p = 1;
21405 				err = 0;
21406 				break;
21407 			default:
21408 				err = EIO;
21409 				break;
21410 			}
21411 		}
21412 		break;
21413 
21414 	case MHIOCQRESERVE:
21415 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
21416 		if ((err = drv_priv(cred_p)) == 0) {
21417 			err = sd_reserve_release(dev, SD_RESERVE);
21418 		}
21419 		break;
21420 
21421 	case MHIOCREREGISTERDEVID:
21422 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
21423 		if (drv_priv(cred_p) == EPERM) {
21424 			err = EPERM;
21425 		} else if (!un->un_f_devid_supported) {
21426 			err = ENOTTY;
21427 		} else {
21428 			err = sd_mhdioc_register_devid(dev);
21429 		}
21430 		break;
21431 
21432 	case MHIOCGRP_INKEYS:
21433 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
21434 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
21435 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21436 				err = ENOTSUP;
21437 			} else {
21438 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
21439 				    flag);
21440 			}
21441 		}
21442 		break;
21443 
21444 	case MHIOCGRP_INRESV:
21445 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
21446 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
21447 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21448 				err = ENOTSUP;
21449 			} else {
21450 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
21451 			}
21452 		}
21453 		break;
21454 
21455 	case MHIOCGRP_REGISTER:
21456 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
21457 		if ((err = drv_priv(cred_p)) != EPERM) {
21458 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21459 				err = ENOTSUP;
21460 			} else if (arg != NULL) {
21461 				mhioc_register_t reg;
21462 				if (ddi_copyin((void *)arg, &reg,
21463 				    sizeof (mhioc_register_t), flag) != 0) {
21464 					err = EFAULT;
21465 				} else {
21466 					err =
21467 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21468 					    un, SD_SCSI3_REGISTER,
21469 					    (uchar_t *)&reg);
21470 				}
21471 			}
21472 		}
21473 		break;
21474 
21475 	case MHIOCGRP_RESERVE:
21476 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
21477 		if ((err = drv_priv(cred_p)) != EPERM) {
21478 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21479 				err = ENOTSUP;
21480 			} else if (arg != NULL) {
21481 				mhioc_resv_desc_t resv_desc;
21482 				if (ddi_copyin((void *)arg, &resv_desc,
21483 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
21484 					err = EFAULT;
21485 				} else {
21486 					err =
21487 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21488 					    un, SD_SCSI3_RESERVE,
21489 					    (uchar_t *)&resv_desc);
21490 				}
21491 			}
21492 		}
21493 		break;
21494 
21495 	case MHIOCGRP_PREEMPTANDABORT:
21496 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
21497 		if ((err = drv_priv(cred_p)) != EPERM) {
21498 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21499 				err = ENOTSUP;
21500 			} else if (arg != NULL) {
21501 				mhioc_preemptandabort_t preempt_abort;
21502 				if (ddi_copyin((void *)arg, &preempt_abort,
21503 				    sizeof (mhioc_preemptandabort_t),
21504 				    flag) != 0) {
21505 					err = EFAULT;
21506 				} else {
21507 					err =
21508 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21509 					    un, SD_SCSI3_PREEMPTANDABORT,
21510 					    (uchar_t *)&preempt_abort);
21511 				}
21512 			}
21513 		}
21514 		break;
21515 
21516 	case MHIOCGRP_REGISTERANDIGNOREKEY:
21517 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
21518 		if ((err = drv_priv(cred_p)) != EPERM) {
21519 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21520 				err = ENOTSUP;
21521 			} else if (arg != NULL) {
21522 				mhioc_registerandignorekey_t r_and_i;
21523 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
21524 				    sizeof (mhioc_registerandignorekey_t),
21525 				    flag) != 0) {
21526 					err = EFAULT;
21527 				} else {
21528 					err =
21529 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21530 					    un, SD_SCSI3_REGISTERANDIGNOREKEY,
21531 					    (uchar_t *)&r_and_i);
21532 				}
21533 			}
21534 		}
21535 		break;
21536 
21537 	case USCSICMD:
21538 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
21539 		cr = ddi_get_cred();
21540 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
21541 			err = EPERM;
21542 		} else {
21543 			enum uio_seg	uioseg;
21544 			uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
21545 			    UIO_USERSPACE;
21546 			if (un->un_f_format_in_progress == TRUE) {
21547 				err = EAGAIN;
21548 				break;
21549 			}
21550 			err = sd_send_scsi_cmd(dev, (struct uscsi_cmd *)arg,
21551 			    flag, uioseg, SD_PATH_STANDARD);
21552 		}
21553 		break;
21554 
21555 	case CDROMPAUSE:
21556 	case CDROMRESUME:
21557 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
21558 		if (!ISCD(un)) {
21559 			err = ENOTTY;
21560 		} else {
21561 			err = sr_pause_resume(dev, cmd);
21562 		}
21563 		break;
21564 
21565 	case CDROMPLAYMSF:
21566 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
21567 		if (!ISCD(un)) {
21568 			err = ENOTTY;
21569 		} else {
21570 			err = sr_play_msf(dev, (caddr_t)arg, flag);
21571 		}
21572 		break;
21573 
21574 	case CDROMPLAYTRKIND:
21575 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
21576 #if defined(__i386) || defined(__amd64)
21577 		/*
21578 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
21579 		 */
21580 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
21581 #else
21582 		if (!ISCD(un)) {
21583 #endif
21584 			err = ENOTTY;
21585 		} else {
21586 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
21587 		}
21588 		break;
21589 
21590 	case CDROMREADTOCHDR:
21591 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
21592 		if (!ISCD(un)) {
21593 			err = ENOTTY;
21594 		} else {
21595 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
21596 		}
21597 		break;
21598 
21599 	case CDROMREADTOCENTRY:
21600 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
21601 		if (!ISCD(un)) {
21602 			err = ENOTTY;
21603 		} else {
21604 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
21605 		}
21606 		break;
21607 
21608 	case CDROMSTOP:
21609 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
21610 		if (!ISCD(un)) {
21611 			err = ENOTTY;
21612 		} else {
21613 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP,
21614 			    SD_PATH_STANDARD);
21615 		}
21616 		break;
21617 
21618 	case CDROMSTART:
21619 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
21620 		if (!ISCD(un)) {
21621 			err = ENOTTY;
21622 		} else {
21623 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
21624 			    SD_PATH_STANDARD);
21625 		}
21626 		break;
21627 
21628 	case CDROMCLOSETRAY:
21629 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
21630 		if (!ISCD(un)) {
21631 			err = ENOTTY;
21632 		} else {
21633 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE,
21634 			    SD_PATH_STANDARD);
21635 		}
21636 		break;
21637 
21638 	case FDEJECT:	/* for eject command */
21639 	case DKIOCEJECT:
21640 	case CDROMEJECT:
21641 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
21642 		if (!un->un_f_eject_media_supported) {
21643 			err = ENOTTY;
21644 		} else {
21645 			err = sr_eject(dev);
21646 		}
21647 		break;
21648 
21649 	case CDROMVOLCTRL:
21650 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
21651 		if (!ISCD(un)) {
21652 			err = ENOTTY;
21653 		} else {
21654 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
21655 		}
21656 		break;
21657 
21658 	case CDROMSUBCHNL:
21659 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
21660 		if (!ISCD(un)) {
21661 			err = ENOTTY;
21662 		} else {
21663 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
21664 		}
21665 		break;
21666 
21667 	case CDROMREADMODE2:
21668 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
21669 		if (!ISCD(un)) {
21670 			err = ENOTTY;
21671 		} else if (un->un_f_cfg_is_atapi == TRUE) {
21672 			/*
21673 			 * If the drive supports READ CD, use that instead of
21674 			 * switching the LBA size via a MODE SELECT
21675 			 * Block Descriptor
21676 			 */
21677 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
21678 		} else {
21679 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
21680 		}
21681 		break;
21682 
21683 	case CDROMREADMODE1:
21684 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
21685 		if (!ISCD(un)) {
21686 			err = ENOTTY;
21687 		} else {
21688 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
21689 		}
21690 		break;
21691 
21692 	case CDROMREADOFFSET:
21693 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
21694 		if (!ISCD(un)) {
21695 			err = ENOTTY;
21696 		} else {
21697 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
21698 			    flag);
21699 		}
21700 		break;
21701 
21702 	case CDROMSBLKMODE:
21703 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
21704 		/*
21705 		 * There is no means of changing block size in case of atapi
21706 		 * drives, thus return ENOTTY if drive type is atapi
21707 		 */
21708 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
21709 			err = ENOTTY;
21710 		} else if (un->un_f_mmc_cap == TRUE) {
21711 
21712 			/*
21713 			 * MMC Devices do not support changing the
21714 			 * logical block size
21715 			 *
21716 			 * Note: EINVAL is being returned instead of ENOTTY to
21717 			 * maintain consistancy with the original mmc
21718 			 * driver update.
21719 			 */
21720 			err = EINVAL;
21721 		} else {
21722 			mutex_enter(SD_MUTEX(un));
21723 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
21724 			    (un->un_ncmds_in_transport > 0)) {
21725 				mutex_exit(SD_MUTEX(un));
21726 				err = EINVAL;
21727 			} else {
21728 				mutex_exit(SD_MUTEX(un));
21729 				err = sr_change_blkmode(dev, cmd, arg, flag);
21730 			}
21731 		}
21732 		break;
21733 
21734 	case CDROMGBLKMODE:
21735 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
21736 		if (!ISCD(un)) {
21737 			err = ENOTTY;
21738 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
21739 		    (un->un_f_blockcount_is_valid != FALSE)) {
21740 			/*
21741 			 * Drive is an ATAPI drive so return target block
21742 			 * size for ATAPI drives since we cannot change the
21743 			 * blocksize on ATAPI drives. Used primarily to detect
21744 			 * if an ATAPI cdrom is present.
21745 			 */
21746 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
21747 			    sizeof (int), flag) != 0) {
21748 				err = EFAULT;
21749 			} else {
21750 				err = 0;
21751 			}
21752 
21753 		} else {
21754 			/*
21755 			 * Drive supports changing block sizes via a Mode
21756 			 * Select.
21757 			 */
21758 			err = sr_change_blkmode(dev, cmd, arg, flag);
21759 		}
21760 		break;
21761 
21762 	case CDROMGDRVSPEED:
21763 	case CDROMSDRVSPEED:
21764 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
21765 		if (!ISCD(un)) {
21766 			err = ENOTTY;
21767 		} else if (un->un_f_mmc_cap == TRUE) {
21768 			/*
21769 			 * Note: In the future the driver implementation
21770 			 * for getting and
21771 			 * setting cd speed should entail:
21772 			 * 1) If non-mmc try the Toshiba mode page
21773 			 *    (sr_change_speed)
21774 			 * 2) If mmc but no support for Real Time Streaming try
21775 			 *    the SET CD SPEED (0xBB) command
21776 			 *   (sr_atapi_change_speed)
21777 			 * 3) If mmc and support for Real Time Streaming
21778 			 *    try the GET PERFORMANCE and SET STREAMING
21779 			 *    commands (not yet implemented, 4380808)
21780 			 */
21781 			/*
21782 			 * As per recent MMC spec, CD-ROM speed is variable
21783 			 * and changes with LBA. Since there is no such
21784 			 * things as drive speed now, fail this ioctl.
21785 			 *
21786 			 * Note: EINVAL is returned for consistancy of original
21787 			 * implementation which included support for getting
21788 			 * the drive speed of mmc devices but not setting
21789 			 * the drive speed. Thus EINVAL would be returned
21790 			 * if a set request was made for an mmc device.
21791 			 * We no longer support get or set speed for
21792 			 * mmc but need to remain consistant with regard
21793 			 * to the error code returned.
21794 			 */
21795 			err = EINVAL;
21796 		} else if (un->un_f_cfg_is_atapi == TRUE) {
21797 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
21798 		} else {
21799 			err = sr_change_speed(dev, cmd, arg, flag);
21800 		}
21801 		break;
21802 
21803 	case CDROMCDDA:
21804 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
21805 		if (!ISCD(un)) {
21806 			err = ENOTTY;
21807 		} else {
21808 			err = sr_read_cdda(dev, (void *)arg, flag);
21809 		}
21810 		break;
21811 
21812 	case CDROMCDXA:
21813 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
21814 		if (!ISCD(un)) {
21815 			err = ENOTTY;
21816 		} else {
21817 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
21818 		}
21819 		break;
21820 
21821 	case CDROMSUBCODE:
21822 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
21823 		if (!ISCD(un)) {
21824 			err = ENOTTY;
21825 		} else {
21826 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
21827 		}
21828 		break;
21829 
21830 	case DKIOCPARTINFO: {
21831 		/*
21832 		 * Return parameters describing the selected disk slice.
21833 		 * Note: this ioctl is for the intel platform only
21834 		 */
21835 #if defined(__i386) || defined(__amd64)
21836 		int part;
21837 
21838 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
21839 		part = SDPART(dev);
21840 
21841 		/* don't check un_solaris_size for pN */
21842 		if (part < P0_RAW_DISK && un->un_solaris_size == 0) {
21843 			err = EIO;
21844 		} else {
21845 			struct part_info p;
21846 
21847 			p.p_start = (daddr_t)un->un_offset[part];
21848 			p.p_length = (int)un->un_map[part].dkl_nblk;
21849 #ifdef _MULTI_DATAMODEL
21850 			switch (ddi_model_convert_from(flag & FMODELS)) {
21851 			case DDI_MODEL_ILP32:
21852 			{
21853 				struct part_info32 p32;
21854 
21855 				p32.p_start = (daddr32_t)p.p_start;
21856 				p32.p_length = p.p_length;
21857 				if (ddi_copyout(&p32, (void *)arg,
21858 				    sizeof (p32), flag))
21859 					err = EFAULT;
21860 				break;
21861 			}
21862 
21863 			case DDI_MODEL_NONE:
21864 			{
21865 				if (ddi_copyout(&p, (void *)arg, sizeof (p),
21866 				    flag))
21867 					err = EFAULT;
21868 				break;
21869 			}
21870 			}
21871 #else /* ! _MULTI_DATAMODEL */
21872 			if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
21873 				err = EFAULT;
21874 #endif /* _MULTI_DATAMODEL */
21875 		}
21876 #else
21877 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
21878 		err = ENOTTY;
21879 #endif
21880 		break;
21881 	}
21882 
21883 	case DKIOCG_PHYGEOM: {
21884 		/* Return the driver's notion of the media physical geometry */
21885 #if defined(__i386) || defined(__amd64)
21886 		uint64_t	capacity;
21887 		struct dk_geom	disk_geom;
21888 		struct dk_geom	*dkgp = &disk_geom;
21889 
21890 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
21891 		mutex_enter(SD_MUTEX(un));
21892 
21893 		if (un->un_g.dkg_nhead != 0 &&
21894 		    un->un_g.dkg_nsect != 0) {
21895 			/*
21896 			 * We succeeded in getting a geometry, but
21897 			 * right now it is being reported as just the
21898 			 * Solaris fdisk partition, just like for
21899 			 * DKIOCGGEOM. We need to change that to be
21900 			 * correct for the entire disk now.
21901 			 */
21902 			bcopy(&un->un_g, dkgp, sizeof (*dkgp));
21903 			dkgp->dkg_acyl = 0;
21904 			dkgp->dkg_ncyl = un->un_blockcount /
21905 			    (dkgp->dkg_nhead * dkgp->dkg_nsect);
21906 		} else {
21907 			bzero(dkgp, sizeof (struct dk_geom));
21908 			/*
21909 			 * This disk does not have a Solaris VTOC
21910 			 * so we must present a physical geometry
21911 			 * that will remain consistent regardless
21912 			 * of how the disk is used. This will ensure
21913 			 * that the geometry does not change regardless
21914 			 * of the fdisk partition type (ie. EFI, FAT32,
21915 			 * Solaris, etc).
21916 			 */
21917 			if (ISCD(un)) {
21918 				dkgp->dkg_nhead = un->un_pgeom.g_nhead;
21919 				dkgp->dkg_nsect = un->un_pgeom.g_nsect;
21920 				dkgp->dkg_ncyl = un->un_pgeom.g_ncyl;
21921 				dkgp->dkg_acyl = un->un_pgeom.g_acyl;
21922 			} else {
21923 				/*
21924 				 * Invalid un_blockcount can generate invalid
21925 				 * dk_geom and may result in division by zero
21926 				 * system failure. Should make sure blockcount
21927 				 * is valid before using it here.
21928 				 */
21929 				if (un->un_f_blockcount_is_valid == FALSE) {
21930 					mutex_exit(SD_MUTEX(un));
21931 					err = EIO;
21932 
21933 					break;
21934 				}
21935 
21936 				/*
21937 				 * Refer to comments related to off-by-1 at the
21938 				 * header of this file
21939 				 */
21940 				if (!un->un_f_capacity_adjusted &&
21941 					!un->un_f_has_removable_media &&
21942 				    !un->un_f_is_hotpluggable &&
21943 					(un->un_tgt_blocksize ==
21944 					un->un_sys_blocksize))
21945 					capacity = un->un_blockcount - 1;
21946 				else
21947 					capacity = un->un_blockcount;
21948 
21949 				sd_convert_geometry(capacity, dkgp);
21950 				dkgp->dkg_acyl = 0;
21951 				dkgp->dkg_ncyl = capacity /
21952 				    (dkgp->dkg_nhead * dkgp->dkg_nsect);
21953 			}
21954 		}
21955 		dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
21956 
21957 		if (ddi_copyout(dkgp, (void *)arg,
21958 		    sizeof (struct dk_geom), flag)) {
21959 			mutex_exit(SD_MUTEX(un));
21960 			err = EFAULT;
21961 		} else {
21962 			mutex_exit(SD_MUTEX(un));
21963 			err = 0;
21964 		}
21965 #else
21966 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
21967 		err = ENOTTY;
21968 #endif
21969 		break;
21970 	}
21971 
21972 	case DKIOCG_VIRTGEOM: {
21973 		/* Return the driver's notion of the media's logical geometry */
21974 #if defined(__i386) || defined(__amd64)
21975 		struct dk_geom	disk_geom;
21976 		struct dk_geom	*dkgp = &disk_geom;
21977 
21978 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
21979 		mutex_enter(SD_MUTEX(un));
21980 		/*
21981 		 * If there is no HBA geometry available, or
21982 		 * if the HBA returned us something that doesn't
21983 		 * really fit into an Int 13/function 8 geometry
21984 		 * result, just fail the ioctl.  See PSARC 1998/313.
21985 		 */
21986 		if (un->un_lgeom.g_nhead == 0 ||
21987 		    un->un_lgeom.g_nsect == 0 ||
21988 		    un->un_lgeom.g_ncyl > 1024) {
21989 			mutex_exit(SD_MUTEX(un));
21990 			err = EINVAL;
21991 		} else {
21992 			dkgp->dkg_ncyl	= un->un_lgeom.g_ncyl;
21993 			dkgp->dkg_acyl	= un->un_lgeom.g_acyl;
21994 			dkgp->dkg_pcyl	= dkgp->dkg_ncyl + dkgp->dkg_acyl;
21995 			dkgp->dkg_nhead	= un->un_lgeom.g_nhead;
21996 			dkgp->dkg_nsect	= un->un_lgeom.g_nsect;
21997 
21998 			if (ddi_copyout(dkgp, (void *)arg,
21999 			    sizeof (struct dk_geom), flag)) {
22000 				mutex_exit(SD_MUTEX(un));
22001 				err = EFAULT;
22002 			} else {
22003 				mutex_exit(SD_MUTEX(un));
22004 				err = 0;
22005 			}
22006 		}
22007 #else
22008 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
22009 		err = ENOTTY;
22010 #endif
22011 		break;
22012 	}
22013 #ifdef SDDEBUG
22014 /* RESET/ABORTS testing ioctls */
22015 	case DKIOCRESET: {
22016 		int	reset_level;
22017 
22018 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
22019 			err = EFAULT;
22020 		} else {
22021 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
22022 			    "reset_level = 0x%lx\n", reset_level);
22023 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
22024 				err = 0;
22025 			} else {
22026 				err = EIO;
22027 			}
22028 		}
22029 		break;
22030 	}
22031 
22032 	case DKIOCABORT:
22033 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
22034 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
22035 			err = 0;
22036 		} else {
22037 			err = EIO;
22038 		}
22039 		break;
22040 #endif
22041 
22042 #ifdef SD_FAULT_INJECTION
22043 /* SDIOC FaultInjection testing ioctls */
22044 	case SDIOCSTART:
22045 	case SDIOCSTOP:
22046 	case SDIOCINSERTPKT:
22047 	case SDIOCINSERTXB:
22048 	case SDIOCINSERTUN:
22049 	case SDIOCINSERTARQ:
22050 	case SDIOCPUSH:
22051 	case SDIOCRETRIEVE:
22052 	case SDIOCRUN:
22053 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
22054 		    "SDIOC detected cmd:0x%X:\n", cmd);
22055 		/* call error generator */
22056 		sd_faultinjection_ioctl(cmd, arg, un);
22057 		err = 0;
22058 		break;
22059 
22060 #endif /* SD_FAULT_INJECTION */
22061 
22062 	case DKIOCFLUSHWRITECACHE:
22063 		{
22064 			struct dk_callback *dkc = (struct dk_callback *)arg;
22065 
22066 			mutex_enter(SD_MUTEX(un));
22067 			if (!un->un_f_sync_cache_supported ||
22068 			    !un->un_f_write_cache_enabled) {
22069 				err = un->un_f_sync_cache_supported ?
22070 					0 : ENOTSUP;
22071 				mutex_exit(SD_MUTEX(un));
22072 				if ((flag & FKIOCTL) && dkc != NULL &&
22073 				    dkc->dkc_callback != NULL) {
22074 					(*dkc->dkc_callback)(dkc->dkc_cookie,
22075 					    err);
22076 					/*
22077 					 * Did callback and reported error.
22078 					 * Since we did a callback, ioctl
22079 					 * should return 0.
22080 					 */
22081 					err = 0;
22082 				}
22083 				break;
22084 			}
22085 			mutex_exit(SD_MUTEX(un));
22086 
22087 			if ((flag & FKIOCTL) && dkc != NULL &&
22088 			    dkc->dkc_callback != NULL) {
22089 				/* async SYNC CACHE request */
22090 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
22091 			} else {
22092 				/* synchronous SYNC CACHE request */
22093 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
22094 			}
22095 		}
22096 		break;
22097 
22098 	case DKIOCGETWCE: {
22099 
22100 		int wce;
22101 
22102 		if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) {
22103 			break;
22104 		}
22105 
22106 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
22107 			err = EFAULT;
22108 		}
22109 		break;
22110 	}
22111 
22112 	case DKIOCSETWCE: {
22113 
22114 		int wce, sync_supported;
22115 
22116 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
22117 			err = EFAULT;
22118 			break;
22119 		}
22120 
22121 		/*
22122 		 * Synchronize multiple threads trying to enable
22123 		 * or disable the cache via the un_f_wcc_cv
22124 		 * condition variable.
22125 		 */
22126 		mutex_enter(SD_MUTEX(un));
22127 
22128 		/*
22129 		 * Don't allow the cache to be enabled if the
22130 		 * config file has it disabled.
22131 		 */
22132 		if (un->un_f_opt_disable_cache && wce) {
22133 			mutex_exit(SD_MUTEX(un));
22134 			err = EINVAL;
22135 			break;
22136 		}
22137 
22138 		/*
22139 		 * Wait for write cache change in progress
22140 		 * bit to be clear before proceeding.
22141 		 */
22142 		while (un->un_f_wcc_inprog)
22143 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
22144 
22145 		un->un_f_wcc_inprog = 1;
22146 
22147 		if (un->un_f_write_cache_enabled && wce == 0) {
22148 			/*
22149 			 * Disable the write cache.  Don't clear
22150 			 * un_f_write_cache_enabled until after
22151 			 * the mode select and flush are complete.
22152 			 */
22153 			sync_supported = un->un_f_sync_cache_supported;
22154 			mutex_exit(SD_MUTEX(un));
22155 			if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE,
22156 			    SD_CACHE_DISABLE)) == 0 && sync_supported) {
22157 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
22158 			}
22159 
22160 			mutex_enter(SD_MUTEX(un));
22161 			if (err == 0) {
22162 				un->un_f_write_cache_enabled = 0;
22163 			}
22164 
22165 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
22166 			/*
22167 			 * Set un_f_write_cache_enabled first, so there is
22168 			 * no window where the cache is enabled, but the
22169 			 * bit says it isn't.
22170 			 */
22171 			un->un_f_write_cache_enabled = 1;
22172 			mutex_exit(SD_MUTEX(un));
22173 
22174 			err = sd_cache_control(un, SD_CACHE_NOCHANGE,
22175 				SD_CACHE_ENABLE);
22176 
22177 			mutex_enter(SD_MUTEX(un));
22178 
22179 			if (err) {
22180 				un->un_f_write_cache_enabled = 0;
22181 			}
22182 		}
22183 
22184 		un->un_f_wcc_inprog = 0;
22185 		cv_broadcast(&un->un_wcc_cv);
22186 		mutex_exit(SD_MUTEX(un));
22187 		break;
22188 	}
22189 
22190 	default:
22191 		err = ENOTTY;
22192 		break;
22193 	}
22194 	mutex_enter(SD_MUTEX(un));
22195 	un->un_ncmds_in_driver--;
22196 	ASSERT(un->un_ncmds_in_driver >= 0);
22197 	mutex_exit(SD_MUTEX(un));
22198 
22199 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
22200 	return (err);
22201 }
22202 
22203 
22204 /*
22205  *    Function: sd_dkio_ctrl_info
22206  *
22207  * Description: This routine is the driver entry point for handling controller
22208  *		information ioctl requests (DKIOCINFO).
22209  *
22210  *   Arguments: dev  - the device number
22211  *		arg  - pointer to user provided dk_cinfo structure
22212  *		       specifying the controller type and attributes.
22213  *		flag - this argument is a pass through to ddi_copyxxx()
22214  *		       directly from the mode argument of ioctl().
22215  *
22216  * Return Code: 0
22217  *		EFAULT
22218  *		ENXIO
22219  */
22220 
22221 static int
22222 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
22223 {
22224 	struct sd_lun	*un = NULL;
22225 	struct dk_cinfo	*info;
22226 	dev_info_t	*pdip;
22227 	int		lun, tgt;
22228 
22229 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22230 		return (ENXIO);
22231 	}
22232 
22233 	info = (struct dk_cinfo *)
22234 		kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
22235 
22236 	switch (un->un_ctype) {
22237 	case CTYPE_CDROM:
22238 		info->dki_ctype = DKC_CDROM;
22239 		break;
22240 	default:
22241 		info->dki_ctype = DKC_SCSI_CCS;
22242 		break;
22243 	}
22244 	pdip = ddi_get_parent(SD_DEVINFO(un));
22245 	info->dki_cnum = ddi_get_instance(pdip);
22246 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
22247 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
22248 	} else {
22249 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
22250 		    DK_DEVLEN - 1);
22251 	}
22252 
22253 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
22254 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
22255 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
22256 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
22257 
22258 	/* Unit Information */
22259 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
22260 	info->dki_slave = ((tgt << 3) | lun);
22261 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
22262 	    DK_DEVLEN - 1);
22263 	info->dki_flags = DKI_FMTVOL;
22264 	info->dki_partition = SDPART(dev);
22265 
22266 	/* Max Transfer size of this device in blocks */
22267 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
22268 	info->dki_addr = 0;
22269 	info->dki_space = 0;
22270 	info->dki_prio = 0;
22271 	info->dki_vec = 0;
22272 
22273 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
22274 		kmem_free(info, sizeof (struct dk_cinfo));
22275 		return (EFAULT);
22276 	} else {
22277 		kmem_free(info, sizeof (struct dk_cinfo));
22278 		return (0);
22279 	}
22280 }
22281 
22282 
22283 /*
22284  *    Function: sd_get_media_info
22285  *
22286  * Description: This routine is the driver entry point for handling ioctl
22287  *		requests for the media type or command set profile used by the
22288  *		drive to operate on the media (DKIOCGMEDIAINFO).
22289  *
22290  *   Arguments: dev	- the device number
22291  *		arg	- pointer to user provided dk_minfo structure
22292  *			  specifying the media type, logical block size and
22293  *			  drive capacity.
22294  *		flag	- this argument is a pass through to ddi_copyxxx()
22295  *			  directly from the mode argument of ioctl().
22296  *
22297  * Return Code: 0
22298  *		EACCESS
22299  *		EFAULT
22300  *		ENXIO
22301  *		EIO
22302  */
22303 
22304 static int
22305 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
22306 {
22307 	struct sd_lun		*un = NULL;
22308 	struct uscsi_cmd	com;
22309 	struct scsi_inquiry	*sinq;
22310 	struct dk_minfo		media_info;
22311 	u_longlong_t		media_capacity;
22312 	uint64_t		capacity;
22313 	uint_t			lbasize;
22314 	uchar_t			*out_data;
22315 	uchar_t			*rqbuf;
22316 	int			rval = 0;
22317 	int			rtn;
22318 
22319 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
22320 	    (un->un_state == SD_STATE_OFFLINE)) {
22321 		return (ENXIO);
22322 	}
22323 
22324 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n");
22325 
22326 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
22327 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
22328 
22329 	/* Issue a TUR to determine if the drive is ready with media present */
22330 	rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA);
22331 	if (rval == ENXIO) {
22332 		goto done;
22333 	}
22334 
22335 	/* Now get configuration data */
22336 	if (ISCD(un)) {
22337 		media_info.dki_media_type = DK_CDROM;
22338 
22339 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
22340 		if (un->un_f_mmc_cap == TRUE) {
22341 			rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf,
22342 				SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN);
22343 
22344 			if (rtn) {
22345 				/*
22346 				 * Failed for other than an illegal request
22347 				 * or command not supported
22348 				 */
22349 				if ((com.uscsi_status == STATUS_CHECK) &&
22350 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
22351 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
22352 					    (rqbuf[12] != 0x20)) {
22353 						rval = EIO;
22354 						goto done;
22355 					}
22356 				}
22357 			} else {
22358 				/*
22359 				 * The GET CONFIGURATION command succeeded
22360 				 * so set the media type according to the
22361 				 * returned data
22362 				 */
22363 				media_info.dki_media_type = out_data[6];
22364 				media_info.dki_media_type <<= 8;
22365 				media_info.dki_media_type |= out_data[7];
22366 			}
22367 		}
22368 	} else {
22369 		/*
22370 		 * The profile list is not available, so we attempt to identify
22371 		 * the media type based on the inquiry data
22372 		 */
22373 		sinq = un->un_sd->sd_inq;
22374 		if (sinq->inq_qual == 0) {
22375 			/* This is a direct access device */
22376 			media_info.dki_media_type = DK_FIXED_DISK;
22377 
22378 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
22379 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
22380 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
22381 					media_info.dki_media_type = DK_ZIP;
22382 				} else if (
22383 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
22384 					media_info.dki_media_type = DK_JAZ;
22385 				}
22386 			}
22387 		} else {
22388 			/* Not a CD or direct access so return unknown media */
22389 			media_info.dki_media_type = DK_UNKNOWN;
22390 		}
22391 	}
22392 
22393 	/* Now read the capacity so we can provide the lbasize and capacity */
22394 	switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
22395 	    SD_PATH_DIRECT)) {
22396 	case 0:
22397 		break;
22398 	case EACCES:
22399 		rval = EACCES;
22400 		goto done;
22401 	default:
22402 		rval = EIO;
22403 		goto done;
22404 	}
22405 
22406 	media_info.dki_lbsize = lbasize;
22407 	media_capacity = capacity;
22408 
22409 	/*
22410 	 * sd_send_scsi_READ_CAPACITY() reports capacity in
22411 	 * un->un_sys_blocksize chunks. So we need to convert it into
22412 	 * cap.lbasize chunks.
22413 	 */
22414 	media_capacity *= un->un_sys_blocksize;
22415 	media_capacity /= lbasize;
22416 	media_info.dki_capacity = media_capacity;
22417 
22418 	if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) {
22419 		rval = EFAULT;
22420 		/* Put goto. Anybody might add some code below in future */
22421 		goto done;
22422 	}
22423 done:
22424 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
22425 	kmem_free(rqbuf, SENSE_LENGTH);
22426 	return (rval);
22427 }
22428 
22429 
22430 /*
22431  *    Function: sd_dkio_get_geometry
22432  *
22433  * Description: This routine is the driver entry point for handling user
22434  *		requests to get the device geometry (DKIOCGGEOM).
22435  *
22436  *   Arguments: dev  - the device number
22437  *		arg  - pointer to user provided dk_geom structure specifying
22438  *			the controller's notion of the current geometry.
22439  *		flag - this argument is a pass through to ddi_copyxxx()
22440  *		       directly from the mode argument of ioctl().
22441  *		geom_validated - flag indicating if the device geometry has been
22442  *				 previously validated in the sdioctl routine.
22443  *
22444  * Return Code: 0
22445  *		EFAULT
22446  *		ENXIO
22447  *		EIO
22448  */
22449 
22450 static int
22451 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated)
22452 {
22453 	struct sd_lun	*un = NULL;
22454 	struct dk_geom	*tmp_geom = NULL;
22455 	int		rval = 0;
22456 
22457 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22458 		return (ENXIO);
22459 	}
22460 
22461 	if (geom_validated == FALSE) {
22462 		/*
22463 		 * sd_validate_geometry does not spin a disk up
22464 		 * if it was spun down. We need to make sure it
22465 		 * is ready.
22466 		 */
22467 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22468 			return (rval);
22469 		}
22470 		mutex_enter(SD_MUTEX(un));
22471 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
22472 		mutex_exit(SD_MUTEX(un));
22473 	}
22474 	if (rval)
22475 		return (rval);
22476 
22477 	/*
22478 	 * It is possible that un_solaris_size is 0(uninitialized)
22479 	 * after sd_unit_attach. Reservation conflict may cause the
22480 	 * above situation. Thus, the zero check of un_solaris_size
22481 	 * should occur after the sd_validate_geometry() call.
22482 	 */
22483 #if defined(__i386) || defined(__amd64)
22484 	if (un->un_solaris_size == 0) {
22485 		return (EIO);
22486 	}
22487 #endif
22488 
22489 	/*
22490 	 * Make a local copy of the soft state geometry to avoid some potential
22491 	 * race conditions associated with holding the mutex and updating the
22492 	 * write_reinstruct value
22493 	 */
22494 	tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
22495 	mutex_enter(SD_MUTEX(un));
22496 	bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom));
22497 	mutex_exit(SD_MUTEX(un));
22498 
22499 	if (tmp_geom->dkg_write_reinstruct == 0) {
22500 		tmp_geom->dkg_write_reinstruct =
22501 		    (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
22502 		    sd_rot_delay) / (int)60000);
22503 	}
22504 
22505 	rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
22506 	    flag);
22507 	if (rval != 0) {
22508 		rval = EFAULT;
22509 	}
22510 
22511 	kmem_free(tmp_geom, sizeof (struct dk_geom));
22512 	return (rval);
22513 
22514 }
22515 
22516 
22517 /*
22518  *    Function: sd_dkio_set_geometry
22519  *
22520  * Description: This routine is the driver entry point for handling user
22521  *		requests to set the device geometry (DKIOCSGEOM). The actual
22522  *		device geometry is not updated, just the driver "notion" of it.
22523  *
22524  *   Arguments: dev  - the device number
22525  *		arg  - pointer to user provided dk_geom structure used to set
22526  *			the controller's notion of the current geometry.
22527  *		flag - this argument is a pass through to ddi_copyxxx()
22528  *		       directly from the mode argument of ioctl().
22529  *
22530  * Return Code: 0
22531  *		EFAULT
22532  *		ENXIO
22533  *		EIO
22534  */
22535 
22536 static int
22537 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag)
22538 {
22539 	struct sd_lun	*un = NULL;
22540 	struct dk_geom	*tmp_geom;
22541 	struct dk_map	*lp;
22542 	int		rval = 0;
22543 	int		i;
22544 
22545 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22546 		return (ENXIO);
22547 	}
22548 
22549 	/*
22550 	 * Make sure there is no reservation conflict on the lun.
22551 	 */
22552 	if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) {
22553 		return (EACCES);
22554 	}
22555 
22556 #if defined(__i386) || defined(__amd64)
22557 	if (un->un_solaris_size == 0) {
22558 		return (EIO);
22559 	}
22560 #endif
22561 
22562 	/*
22563 	 * We need to copy the user specified geometry into local
22564 	 * storage and then update the softstate. We don't want to hold
22565 	 * the mutex and copyin directly from the user to the soft state
22566 	 */
22567 	tmp_geom = (struct dk_geom *)
22568 	    kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
22569 	rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
22570 	if (rval != 0) {
22571 		kmem_free(tmp_geom, sizeof (struct dk_geom));
22572 		return (EFAULT);
22573 	}
22574 
22575 	mutex_enter(SD_MUTEX(un));
22576 	bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom));
22577 	for (i = 0; i < NDKMAP; i++) {
22578 		lp  = &un->un_map[i];
22579 		un->un_offset[i] =
22580 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
22581 #if defined(__i386) || defined(__amd64)
22582 		un->un_offset[i] += un->un_solaris_offset;
22583 #endif
22584 	}
22585 	un->un_f_geometry_is_valid = FALSE;
22586 	mutex_exit(SD_MUTEX(un));
22587 	kmem_free(tmp_geom, sizeof (struct dk_geom));
22588 
22589 	return (rval);
22590 }
22591 
22592 
22593 /*
22594  *    Function: sd_dkio_get_partition
22595  *
22596  * Description: This routine is the driver entry point for handling user
22597  *		requests to get the partition table (DKIOCGAPART).
22598  *
22599  *   Arguments: dev  - the device number
22600  *		arg  - pointer to user provided dk_allmap structure specifying
22601  *			the controller's notion of the current partition table.
22602  *		flag - this argument is a pass through to ddi_copyxxx()
22603  *		       directly from the mode argument of ioctl().
22604  *		geom_validated - flag indicating if the device geometry has been
22605  *				 previously validated in the sdioctl routine.
22606  *
22607  * Return Code: 0
22608  *		EFAULT
22609  *		ENXIO
22610  *		EIO
22611  */
22612 
22613 static int
22614 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated)
22615 {
22616 	struct sd_lun	*un = NULL;
22617 	int		rval = 0;
22618 	int		size;
22619 
22620 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22621 		return (ENXIO);
22622 	}
22623 
22624 	/*
22625 	 * Make sure the geometry is valid before getting the partition
22626 	 * information.
22627 	 */
22628 	mutex_enter(SD_MUTEX(un));
22629 	if (geom_validated == FALSE) {
22630 		/*
22631 		 * sd_validate_geometry does not spin a disk up
22632 		 * if it was spun down. We need to make sure it
22633 		 * is ready before validating the geometry.
22634 		 */
22635 		mutex_exit(SD_MUTEX(un));
22636 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22637 			return (rval);
22638 		}
22639 		mutex_enter(SD_MUTEX(un));
22640 
22641 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
22642 			mutex_exit(SD_MUTEX(un));
22643 			return (rval);
22644 		}
22645 	}
22646 	mutex_exit(SD_MUTEX(un));
22647 
22648 	/*
22649 	 * It is possible that un_solaris_size is 0(uninitialized)
22650 	 * after sd_unit_attach. Reservation conflict may cause the
22651 	 * above situation. Thus, the zero check of un_solaris_size
22652 	 * should occur after the sd_validate_geometry() call.
22653 	 */
22654 #if defined(__i386) || defined(__amd64)
22655 	if (un->un_solaris_size == 0) {
22656 		return (EIO);
22657 	}
22658 #endif
22659 
22660 #ifdef _MULTI_DATAMODEL
22661 	switch (ddi_model_convert_from(flag & FMODELS)) {
22662 	case DDI_MODEL_ILP32: {
22663 		struct dk_map32 dk_map32[NDKMAP];
22664 		int		i;
22665 
22666 		for (i = 0; i < NDKMAP; i++) {
22667 			dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno;
22668 			dk_map32[i].dkl_nblk  = un->un_map[i].dkl_nblk;
22669 		}
22670 		size = NDKMAP * sizeof (struct dk_map32);
22671 		rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
22672 		if (rval != 0) {
22673 			rval = EFAULT;
22674 		}
22675 		break;
22676 	}
22677 	case DDI_MODEL_NONE:
22678 		size = NDKMAP * sizeof (struct dk_map);
22679 		rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
22680 		if (rval != 0) {
22681 			rval = EFAULT;
22682 		}
22683 		break;
22684 	}
22685 #else /* ! _MULTI_DATAMODEL */
22686 	size = NDKMAP * sizeof (struct dk_map);
22687 	rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
22688 	if (rval != 0) {
22689 		rval = EFAULT;
22690 	}
22691 #endif /* _MULTI_DATAMODEL */
22692 	return (rval);
22693 }
22694 
22695 
22696 /*
22697  *    Function: sd_dkio_set_partition
22698  *
22699  * Description: This routine is the driver entry point for handling user
22700  *		requests to set the partition table (DKIOCSAPART). The actual
22701  *		device partition is not updated.
22702  *
22703  *   Arguments: dev  - the device number
22704  *		arg  - pointer to user provided dk_allmap structure used to set
22705  *			the controller's notion of the partition table.
22706  *		flag - this argument is a pass through to ddi_copyxxx()
22707  *		       directly from the mode argument of ioctl().
22708  *
22709  * Return Code: 0
22710  *		EINVAL
22711  *		EFAULT
22712  *		ENXIO
22713  *		EIO
22714  */
22715 
22716 static int
22717 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag)
22718 {
22719 	struct sd_lun	*un = NULL;
22720 	struct dk_map	dk_map[NDKMAP];
22721 	struct dk_map	*lp;
22722 	int		rval = 0;
22723 	int		size;
22724 	int		i;
22725 #if defined(_SUNOS_VTOC_16)
22726 	struct dkl_partition	*vp;
22727 #endif
22728 
22729 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22730 		return (ENXIO);
22731 	}
22732 
22733 	/*
22734 	 * Set the map for all logical partitions.  We lock
22735 	 * the priority just to make sure an interrupt doesn't
22736 	 * come in while the map is half updated.
22737 	 */
22738 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size))
22739 	mutex_enter(SD_MUTEX(un));
22740 	if (un->un_blockcount > DK_MAX_BLOCKS) {
22741 		mutex_exit(SD_MUTEX(un));
22742 		return (ENOTSUP);
22743 	}
22744 	mutex_exit(SD_MUTEX(un));
22745 
22746 	/*
22747 	 * Make sure there is no reservation conflict on the lun.
22748 	 */
22749 	if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) {
22750 		return (EACCES);
22751 	}
22752 
22753 #if defined(__i386) || defined(__amd64)
22754 	if (un->un_solaris_size == 0) {
22755 		return (EIO);
22756 	}
22757 #endif
22758 
22759 #ifdef _MULTI_DATAMODEL
22760 	switch (ddi_model_convert_from(flag & FMODELS)) {
22761 	case DDI_MODEL_ILP32: {
22762 		struct dk_map32 dk_map32[NDKMAP];
22763 
22764 		size = NDKMAP * sizeof (struct dk_map32);
22765 		rval = ddi_copyin((void *)arg, dk_map32, size, flag);
22766 		if (rval != 0) {
22767 			return (EFAULT);
22768 		}
22769 		for (i = 0; i < NDKMAP; i++) {
22770 			dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
22771 			dk_map[i].dkl_nblk  = dk_map32[i].dkl_nblk;
22772 		}
22773 		break;
22774 	}
22775 	case DDI_MODEL_NONE:
22776 		size = NDKMAP * sizeof (struct dk_map);
22777 		rval = ddi_copyin((void *)arg, dk_map, size, flag);
22778 		if (rval != 0) {
22779 			return (EFAULT);
22780 		}
22781 		break;
22782 	}
22783 #else /* ! _MULTI_DATAMODEL */
22784 	size = NDKMAP * sizeof (struct dk_map);
22785 	rval = ddi_copyin((void *)arg, dk_map, size, flag);
22786 	if (rval != 0) {
22787 		return (EFAULT);
22788 	}
22789 #endif /* _MULTI_DATAMODEL */
22790 
22791 	mutex_enter(SD_MUTEX(un));
22792 	/* Note: The size used in this bcopy is set based upon the data model */
22793 	bcopy(dk_map, un->un_map, size);
22794 #if defined(_SUNOS_VTOC_16)
22795 	vp = (struct dkl_partition *)&(un->un_vtoc);
22796 #endif	/* defined(_SUNOS_VTOC_16) */
22797 	for (i = 0; i < NDKMAP; i++) {
22798 		lp  = &un->un_map[i];
22799 		un->un_offset[i] =
22800 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
22801 #if defined(_SUNOS_VTOC_16)
22802 		vp->p_start = un->un_offset[i];
22803 		vp->p_size = lp->dkl_nblk;
22804 		vp++;
22805 #endif	/* defined(_SUNOS_VTOC_16) */
22806 #if defined(__i386) || defined(__amd64)
22807 		un->un_offset[i] += un->un_solaris_offset;
22808 #endif
22809 	}
22810 	mutex_exit(SD_MUTEX(un));
22811 	return (rval);
22812 }
22813 
22814 
22815 /*
22816  *    Function: sd_dkio_get_vtoc
22817  *
22818  * Description: This routine is the driver entry point for handling user
22819  *		requests to get the current volume table of contents
22820  *		(DKIOCGVTOC).
22821  *
22822  *   Arguments: dev  - the device number
22823  *		arg  - pointer to user provided vtoc structure specifying
22824  *			the current vtoc.
22825  *		flag - this argument is a pass through to ddi_copyxxx()
22826  *		       directly from the mode argument of ioctl().
22827  *		geom_validated - flag indicating if the device geometry has been
22828  *				 previously validated in the sdioctl routine.
22829  *
22830  * Return Code: 0
22831  *		EFAULT
22832  *		ENXIO
22833  *		EIO
22834  */
22835 
22836 static int
22837 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated)
22838 {
22839 	struct sd_lun	*un = NULL;
22840 #if defined(_SUNOS_VTOC_8)
22841 	struct vtoc	user_vtoc;
22842 #endif	/* defined(_SUNOS_VTOC_8) */
22843 	int		rval = 0;
22844 
22845 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22846 		return (ENXIO);
22847 	}
22848 
22849 	mutex_enter(SD_MUTEX(un));
22850 	if (geom_validated == FALSE) {
22851 		/*
22852 		 * sd_validate_geometry does not spin a disk up
22853 		 * if it was spun down. We need to make sure it
22854 		 * is ready.
22855 		 */
22856 		mutex_exit(SD_MUTEX(un));
22857 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22858 			return (rval);
22859 		}
22860 		mutex_enter(SD_MUTEX(un));
22861 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
22862 			mutex_exit(SD_MUTEX(un));
22863 			return (rval);
22864 		}
22865 	}
22866 
22867 #if defined(_SUNOS_VTOC_8)
22868 	sd_build_user_vtoc(un, &user_vtoc);
22869 	mutex_exit(SD_MUTEX(un));
22870 
22871 #ifdef _MULTI_DATAMODEL
22872 	switch (ddi_model_convert_from(flag & FMODELS)) {
22873 	case DDI_MODEL_ILP32: {
22874 		struct vtoc32 user_vtoc32;
22875 
22876 		vtoctovtoc32(user_vtoc, user_vtoc32);
22877 		if (ddi_copyout(&user_vtoc32, (void *)arg,
22878 		    sizeof (struct vtoc32), flag)) {
22879 			return (EFAULT);
22880 		}
22881 		break;
22882 	}
22883 
22884 	case DDI_MODEL_NONE:
22885 		if (ddi_copyout(&user_vtoc, (void *)arg,
22886 		    sizeof (struct vtoc), flag)) {
22887 			return (EFAULT);
22888 		}
22889 		break;
22890 	}
22891 #else /* ! _MULTI_DATAMODEL */
22892 	if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
22893 		return (EFAULT);
22894 	}
22895 #endif /* _MULTI_DATAMODEL */
22896 
22897 #elif defined(_SUNOS_VTOC_16)
22898 	mutex_exit(SD_MUTEX(un));
22899 
22900 #ifdef _MULTI_DATAMODEL
22901 	/*
22902 	 * The un_vtoc structure is a "struct dk_vtoc"  which is always
22903 	 * 32-bit to maintain compatibility with existing on-disk
22904 	 * structures.  Thus, we need to convert the structure when copying
22905 	 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
22906 	 * program.  If the target is a 32-bit program, then no conversion
22907 	 * is necessary.
22908 	 */
22909 	/* LINTED: logical expression always true: op "||" */
22910 	ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32));
22911 	switch (ddi_model_convert_from(flag & FMODELS)) {
22912 	case DDI_MODEL_ILP32:
22913 		if (ddi_copyout(&(un->un_vtoc), (void *)arg,
22914 		    sizeof (un->un_vtoc), flag)) {
22915 			return (EFAULT);
22916 		}
22917 		break;
22918 
22919 	case DDI_MODEL_NONE: {
22920 		struct vtoc user_vtoc;
22921 
22922 		vtoc32tovtoc(un->un_vtoc, user_vtoc);
22923 		if (ddi_copyout(&user_vtoc, (void *)arg,
22924 		    sizeof (struct vtoc), flag)) {
22925 			return (EFAULT);
22926 		}
22927 		break;
22928 	}
22929 	}
22930 #else /* ! _MULTI_DATAMODEL */
22931 	if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc),
22932 	    flag)) {
22933 		return (EFAULT);
22934 	}
22935 #endif /* _MULTI_DATAMODEL */
22936 #else
22937 #error "No VTOC format defined."
22938 #endif
22939 
22940 	return (rval);
22941 }
22942 
22943 static int
22944 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag)
22945 {
22946 	struct sd_lun	*un = NULL;
22947 	dk_efi_t	user_efi;
22948 	int		rval = 0;
22949 	void		*buffer;
22950 
22951 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
22952 		return (ENXIO);
22953 
22954 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
22955 		return (EFAULT);
22956 
22957 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
22958 
22959 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
22960 	    (user_efi.dki_length > un->un_max_xfer_size))
22961 		return (EINVAL);
22962 
22963 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
22964 	rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length,
22965 	    user_efi.dki_lba, SD_PATH_DIRECT);
22966 	if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
22967 	    user_efi.dki_length, flag) != 0)
22968 		rval = EFAULT;
22969 
22970 	kmem_free(buffer, user_efi.dki_length);
22971 	return (rval);
22972 }
22973 
22974 #if defined(_SUNOS_VTOC_8)
22975 /*
22976  *    Function: sd_build_user_vtoc
22977  *
22978  * Description: This routine populates a pass by reference variable with the
22979  *		current volume table of contents.
22980  *
22981  *   Arguments: un - driver soft state (unit) structure
22982  *		user_vtoc - pointer to vtoc structure to be populated
22983  */
22984 
22985 static void
22986 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
22987 {
22988 	struct dk_map2		*lpart;
22989 	struct dk_map		*lmap;
22990 	struct partition	*vpart;
22991 	int			nblks;
22992 	int			i;
22993 
22994 	ASSERT(mutex_owned(SD_MUTEX(un)));
22995 
22996 	/*
22997 	 * Return vtoc structure fields in the provided VTOC area, addressed
22998 	 * by *vtoc.
22999 	 */
23000 	bzero(user_vtoc, sizeof (struct vtoc));
23001 	user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0];
23002 	user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1];
23003 	user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2];
23004 	user_vtoc->v_sanity	= VTOC_SANE;
23005 	user_vtoc->v_version	= un->un_vtoc.v_version;
23006 	bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
23007 	user_vtoc->v_sectorsz = un->un_sys_blocksize;
23008 	user_vtoc->v_nparts = un->un_vtoc.v_nparts;
23009 	bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved,
23010 	    sizeof (un->un_vtoc.v_reserved));
23011 	/*
23012 	 * Convert partitioning information.
23013 	 *
23014 	 * Note the conversion from starting cylinder number
23015 	 * to starting sector number.
23016 	 */
23017 	lmap = un->un_map;
23018 	lpart = (struct dk_map2 *)un->un_vtoc.v_part;
23019 	vpart = user_vtoc->v_part;
23020 
23021 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
23022 
23023 	for (i = 0; i < V_NUMPAR; i++) {
23024 		vpart->p_tag	= lpart->p_tag;
23025 		vpart->p_flag	= lpart->p_flag;
23026 		vpart->p_start	= lmap->dkl_cylno * nblks;
23027 		vpart->p_size	= lmap->dkl_nblk;
23028 		lmap++;
23029 		lpart++;
23030 		vpart++;
23031 
23032 		/* (4364927) */
23033 		user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i];
23034 	}
23035 
23036 	bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
23037 }
23038 #endif
23039 
23040 static int
23041 sd_dkio_partition(dev_t dev, caddr_t arg, int flag)
23042 {
23043 	struct sd_lun		*un = NULL;
23044 	struct partition64	p64;
23045 	int			rval = 0;
23046 	uint_t			nparts;
23047 	efi_gpe_t		*partitions;
23048 	efi_gpt_t		*buffer;
23049 	diskaddr_t		gpe_lba;
23050 
23051 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23052 		return (ENXIO);
23053 	}
23054 
23055 	if (ddi_copyin((const void *)arg, &p64,
23056 	    sizeof (struct partition64), flag)) {
23057 		return (EFAULT);
23058 	}
23059 
23060 	buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
23061 	rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE,
23062 		1, SD_PATH_DIRECT);
23063 	if (rval != 0)
23064 		goto done_error;
23065 
23066 	sd_swap_efi_gpt(buffer);
23067 
23068 	if ((rval = sd_validate_efi(buffer)) != 0)
23069 		goto done_error;
23070 
23071 	nparts = buffer->efi_gpt_NumberOfPartitionEntries;
23072 	gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
23073 	if (p64.p_partno > nparts) {
23074 		/* couldn't find it */
23075 		rval = ESRCH;
23076 		goto done_error;
23077 	}
23078 	/*
23079 	 * if we're dealing with a partition that's out of the normal
23080 	 * 16K block, adjust accordingly
23081 	 */
23082 	gpe_lba += p64.p_partno / sizeof (efi_gpe_t);
23083 	rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE,
23084 			gpe_lba, SD_PATH_DIRECT);
23085 	if (rval) {
23086 		goto done_error;
23087 	}
23088 	partitions = (efi_gpe_t *)buffer;
23089 
23090 	sd_swap_efi_gpe(nparts, partitions);
23091 
23092 	partitions += p64.p_partno;
23093 	bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
23094 	    sizeof (struct uuid));
23095 	p64.p_start = partitions->efi_gpe_StartingLBA;
23096 	p64.p_size = partitions->efi_gpe_EndingLBA -
23097 			p64.p_start + 1;
23098 
23099 	if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
23100 		rval = EFAULT;
23101 
23102 done_error:
23103 	kmem_free(buffer, EFI_MIN_ARRAY_SIZE);
23104 	return (rval);
23105 }
23106 
23107 
23108 /*
23109  *    Function: sd_dkio_set_vtoc
23110  *
23111  * Description: This routine is the driver entry point for handling user
23112  *		requests to set the current volume table of contents
23113  *		(DKIOCSVTOC).
23114  *
23115  *   Arguments: dev  - the device number
23116  *		arg  - pointer to user provided vtoc structure used to set the
23117  *			current vtoc.
23118  *		flag - this argument is a pass through to ddi_copyxxx()
23119  *		       directly from the mode argument of ioctl().
23120  *
23121  * Return Code: 0
23122  *		EFAULT
23123  *		ENXIO
23124  *		EINVAL
23125  *		ENOTSUP
23126  */
23127 
23128 static int
23129 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag)
23130 {
23131 	struct sd_lun	*un = NULL;
23132 	struct vtoc	user_vtoc;
23133 	int		rval = 0;
23134 
23135 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23136 		return (ENXIO);
23137 	}
23138 
23139 #if defined(__i386) || defined(__amd64)
23140 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
23141 		return (EINVAL);
23142 	}
23143 #endif
23144 
23145 #ifdef _MULTI_DATAMODEL
23146 	switch (ddi_model_convert_from(flag & FMODELS)) {
23147 	case DDI_MODEL_ILP32: {
23148 		struct vtoc32 user_vtoc32;
23149 
23150 		if (ddi_copyin((const void *)arg, &user_vtoc32,
23151 		    sizeof (struct vtoc32), flag)) {
23152 			return (EFAULT);
23153 		}
23154 		vtoc32tovtoc(user_vtoc32, user_vtoc);
23155 		break;
23156 	}
23157 
23158 	case DDI_MODEL_NONE:
23159 		if (ddi_copyin((const void *)arg, &user_vtoc,
23160 		    sizeof (struct vtoc), flag)) {
23161 			return (EFAULT);
23162 		}
23163 		break;
23164 	}
23165 #else /* ! _MULTI_DATAMODEL */
23166 	if (ddi_copyin((const void *)arg, &user_vtoc,
23167 	    sizeof (struct vtoc), flag)) {
23168 		return (EFAULT);
23169 	}
23170 #endif /* _MULTI_DATAMODEL */
23171 
23172 	mutex_enter(SD_MUTEX(un));
23173 	if (un->un_blockcount > DK_MAX_BLOCKS) {
23174 		mutex_exit(SD_MUTEX(un));
23175 		return (ENOTSUP);
23176 	}
23177 	if (un->un_g.dkg_ncyl == 0) {
23178 		mutex_exit(SD_MUTEX(un));
23179 		return (EINVAL);
23180 	}
23181 
23182 	mutex_exit(SD_MUTEX(un));
23183 	sd_clear_efi(un);
23184 	ddi_remove_minor_node(SD_DEVINFO(un), "wd");
23185 	ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw");
23186 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h",
23187 	    S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23188 	    un->un_node_type, NULL);
23189 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw",
23190 	    S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23191 	    un->un_node_type, NULL);
23192 	mutex_enter(SD_MUTEX(un));
23193 
23194 	if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) {
23195 		if ((rval = sd_write_label(dev)) == 0) {
23196 			if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT))
23197 			    != 0) {
23198 				SD_ERROR(SD_LOG_IOCTL_DKIO, un,
23199 				    "sd_dkio_set_vtoc: "
23200 				    "Failed validate geometry\n");
23201 			}
23202 		}
23203 	}
23204 
23205 	/*
23206 	 * If sd_build_label_vtoc, or sd_write_label failed above write the
23207 	 * devid anyway, what can it hurt? Also preserve the device id by
23208 	 * writing to the disk acyl for the case where a devid has been
23209 	 * fabricated.
23210 	 */
23211 	if (un->un_f_devid_supported &&
23212 	    (un->un_f_opt_fab_devid == TRUE)) {
23213 		if (un->un_devid == NULL) {
23214 			sd_register_devid(un, SD_DEVINFO(un),
23215 			    SD_TARGET_IS_UNRESERVED);
23216 		} else {
23217 			/*
23218 			 * The device id for this disk has been
23219 			 * fabricated. Fabricated device id's are
23220 			 * managed by storing them in the last 2
23221 			 * available sectors on the drive. The device
23222 			 * id must be preserved by writing it back out
23223 			 * to this location.
23224 			 */
23225 			if (sd_write_deviceid(un) != 0) {
23226 				ddi_devid_free(un->un_devid);
23227 				un->un_devid = NULL;
23228 			}
23229 		}
23230 	}
23231 	mutex_exit(SD_MUTEX(un));
23232 	return (rval);
23233 }
23234 
23235 
23236 /*
23237  *    Function: sd_build_label_vtoc
23238  *
23239  * Description: This routine updates the driver soft state current volume table
23240  *		of contents based on a user specified vtoc.
23241  *
23242  *   Arguments: un - driver soft state (unit) structure
23243  *		user_vtoc - pointer to vtoc structure specifying vtoc to be used
23244  *			    to update the driver soft state.
23245  *
23246  * Return Code: 0
23247  *		EINVAL
23248  */
23249 
23250 static int
23251 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
23252 {
23253 	struct dk_map		*lmap;
23254 	struct partition	*vpart;
23255 	int			nblks;
23256 #if defined(_SUNOS_VTOC_8)
23257 	int			ncyl;
23258 	struct dk_map2		*lpart;
23259 #endif	/* defined(_SUNOS_VTOC_8) */
23260 	int			i;
23261 
23262 	ASSERT(mutex_owned(SD_MUTEX(un)));
23263 
23264 	/* Sanity-check the vtoc */
23265 	if (user_vtoc->v_sanity != VTOC_SANE ||
23266 	    user_vtoc->v_sectorsz != un->un_sys_blocksize ||
23267 	    user_vtoc->v_nparts != V_NUMPAR) {
23268 		return (EINVAL);
23269 	}
23270 
23271 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
23272 	if (nblks == 0) {
23273 		return (EINVAL);
23274 	}
23275 
23276 #if defined(_SUNOS_VTOC_8)
23277 	vpart = user_vtoc->v_part;
23278 	for (i = 0; i < V_NUMPAR; i++) {
23279 		if ((vpart->p_start % nblks) != 0) {
23280 			return (EINVAL);
23281 		}
23282 		ncyl = vpart->p_start / nblks;
23283 		ncyl += vpart->p_size / nblks;
23284 		if ((vpart->p_size % nblks) != 0) {
23285 			ncyl++;
23286 		}
23287 		if (ncyl > (int)un->un_g.dkg_ncyl) {
23288 			return (EINVAL);
23289 		}
23290 		vpart++;
23291 	}
23292 #endif	/* defined(_SUNOS_VTOC_8) */
23293 
23294 	/* Put appropriate vtoc structure fields into the disk label */
23295 #if defined(_SUNOS_VTOC_16)
23296 	/*
23297 	 * The vtoc is always a 32bit data structure to maintain the
23298 	 * on-disk format. Convert "in place" instead of bcopying it.
23299 	 */
23300 	vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc))));
23301 
23302 	/*
23303 	 * in the 16-slice vtoc, starting sectors are expressed in
23304 	 * numbers *relative* to the start of the Solaris fdisk partition.
23305 	 */
23306 	lmap = un->un_map;
23307 	vpart = user_vtoc->v_part;
23308 
23309 	for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
23310 		lmap->dkl_cylno = vpart->p_start / nblks;
23311 		lmap->dkl_nblk = vpart->p_size;
23312 	}
23313 
23314 #elif defined(_SUNOS_VTOC_8)
23315 
23316 	un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
23317 	un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
23318 	un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
23319 
23320 	un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
23321 	un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version;
23322 
23323 	bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL);
23324 
23325 	un->un_vtoc.v_nparts = user_vtoc->v_nparts;
23326 
23327 	bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved,
23328 	    sizeof (un->un_vtoc.v_reserved));
23329 
23330 	/*
23331 	 * Note the conversion from starting sector number
23332 	 * to starting cylinder number.
23333 	 * Return error if division results in a remainder.
23334 	 */
23335 	lmap = un->un_map;
23336 	lpart = un->un_vtoc.v_part;
23337 	vpart = user_vtoc->v_part;
23338 
23339 	for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
23340 		lpart->p_tag  = vpart->p_tag;
23341 		lpart->p_flag = vpart->p_flag;
23342 		lmap->dkl_cylno = vpart->p_start / nblks;
23343 		lmap->dkl_nblk = vpart->p_size;
23344 
23345 		lmap++;
23346 		lpart++;
23347 		vpart++;
23348 
23349 		/* (4387723) */
23350 #ifdef _LP64
23351 		if (user_vtoc->timestamp[i] > TIME32_MAX) {
23352 			un->un_vtoc.v_timestamp[i] = TIME32_MAX;
23353 		} else {
23354 			un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
23355 		}
23356 #else
23357 		un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
23358 #endif
23359 	}
23360 
23361 	bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII);
23362 #else
23363 #error "No VTOC format defined."
23364 #endif
23365 	return (0);
23366 }
23367 
23368 /*
23369  *    Function: sd_clear_efi
23370  *
23371  * Description: This routine clears all EFI labels.
23372  *
23373  *   Arguments: un - driver soft state (unit) structure
23374  *
23375  * Return Code: void
23376  */
23377 
23378 static void
23379 sd_clear_efi(struct sd_lun *un)
23380 {
23381 	efi_gpt_t	*gpt;
23382 	uint_t		lbasize;
23383 	uint64_t	cap;
23384 	int rval;
23385 
23386 	ASSERT(!mutex_owned(SD_MUTEX(un)));
23387 
23388 	mutex_enter(SD_MUTEX(un));
23389 	un->un_reserved = -1;
23390 	mutex_exit(SD_MUTEX(un));
23391 	gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP);
23392 
23393 	if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) {
23394 		goto done;
23395 	}
23396 
23397 	sd_swap_efi_gpt(gpt);
23398 	rval = sd_validate_efi(gpt);
23399 	if (rval == 0) {
23400 		/* clear primary */
23401 		bzero(gpt, sizeof (efi_gpt_t));
23402 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1,
23403 			SD_PATH_DIRECT))) {
23404 			SD_INFO(SD_LOG_IO_PARTITION, un,
23405 				"sd_clear_efi: clear primary label failed\n");
23406 		}
23407 	}
23408 	/* the backup */
23409 	rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
23410 	    SD_PATH_DIRECT);
23411 	if (rval) {
23412 		goto done;
23413 	}
23414 	/*
23415 	 * The MMC standard allows READ CAPACITY to be
23416 	 * inaccurate by a bounded amount (in the interest of
23417 	 * response latency).  As a result, failed READs are
23418 	 * commonplace (due to the reading of metadata and not
23419 	 * data). Depending on the per-Vendor/drive Sense data,
23420 	 * the failed READ can cause many (unnecessary) retries.
23421 	 */
23422 	if ((rval = sd_send_scsi_READ(un, gpt, lbasize,
23423 	    cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY :
23424 		SD_PATH_DIRECT)) != 0) {
23425 		goto done;
23426 	}
23427 	sd_swap_efi_gpt(gpt);
23428 	rval = sd_validate_efi(gpt);
23429 	if (rval == 0) {
23430 		/* clear backup */
23431 		SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n",
23432 			cap-1);
23433 		bzero(gpt, sizeof (efi_gpt_t));
23434 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE,
23435 		    cap-1, SD_PATH_DIRECT))) {
23436 			SD_INFO(SD_LOG_IO_PARTITION, un,
23437 				"sd_clear_efi: clear backup label failed\n");
23438 		}
23439 	} else {
23440 		/*
23441 		 * Refer to comments related to off-by-1 at the
23442 		 * header of this file
23443 		 */
23444 		if ((rval = sd_send_scsi_READ(un, gpt, lbasize,
23445 		    cap - 2, ISCD(un) ? SD_PATH_DIRECT_PRIORITY :
23446 			SD_PATH_DIRECT)) != 0) {
23447 			goto done;
23448 		}
23449 		sd_swap_efi_gpt(gpt);
23450 		rval = sd_validate_efi(gpt);
23451 		if (rval == 0) {
23452 			/* clear legacy backup EFI label */
23453 			SD_TRACE(SD_LOG_IOCTL, un,
23454 			    "sd_clear_efi clear backup@%lu\n", cap-2);
23455 			bzero(gpt, sizeof (efi_gpt_t));
23456 			if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE,
23457 			    cap-2, SD_PATH_DIRECT))) {
23458 				SD_INFO(SD_LOG_IO_PARTITION,
23459 				    un, "sd_clear_efi: "
23460 				    " clear legacy backup label failed\n");
23461 			}
23462 		}
23463 	}
23464 
23465 done:
23466 	kmem_free(gpt, sizeof (efi_gpt_t));
23467 }
23468 
23469 /*
23470  *    Function: sd_set_vtoc
23471  *
23472  * Description: This routine writes data to the appropriate positions
23473  *
23474  *   Arguments: un - driver soft state (unit) structure
23475  *              dkl  - the data to be written
23476  *
23477  * Return: void
23478  */
23479 
23480 static int
23481 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl)
23482 {
23483 	void			*shadow_buf;
23484 	uint_t			label_addr;
23485 	int			sec;
23486 	int			blk;
23487 	int			head;
23488 	int			cyl;
23489 	int			rval;
23490 
23491 #if defined(__i386) || defined(__amd64)
23492 	label_addr = un->un_solaris_offset + DK_LABEL_LOC;
23493 #else
23494 	/* Write the primary label at block 0 of the solaris partition. */
23495 	label_addr = 0;
23496 #endif
23497 
23498 	if (NOT_DEVBSIZE(un)) {
23499 		shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP);
23500 		/*
23501 		 * Read the target's first block.
23502 		 */
23503 		if ((rval = sd_send_scsi_READ(un, shadow_buf,
23504 		    un->un_tgt_blocksize, label_addr,
23505 		    SD_PATH_STANDARD)) != 0) {
23506 			goto exit;
23507 		}
23508 		/*
23509 		 * Copy the contents of the label into the shadow buffer
23510 		 * which is of the size of target block size.
23511 		 */
23512 		bcopy(dkl, shadow_buf, sizeof (struct dk_label));
23513 	}
23514 
23515 	/* Write the primary label */
23516 	if (NOT_DEVBSIZE(un)) {
23517 		rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize,
23518 		    label_addr, SD_PATH_STANDARD);
23519 	} else {
23520 		rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
23521 		    label_addr, SD_PATH_STANDARD);
23522 	}
23523 	if (rval != 0) {
23524 		return (rval);
23525 	}
23526 
23527 	/*
23528 	 * Calculate where the backup labels go.  They are always on
23529 	 * the last alternate cylinder, but some older drives put them
23530 	 * on head 2 instead of the last head.	They are always on the
23531 	 * first 5 odd sectors of the appropriate track.
23532 	 *
23533 	 * We have no choice at this point, but to believe that the
23534 	 * disk label is valid.	 Use the geometry of the disk
23535 	 * as described in the label.
23536 	 */
23537 	cyl  = dkl->dkl_ncyl  + dkl->dkl_acyl - 1;
23538 	head = dkl->dkl_nhead - 1;
23539 
23540 	/*
23541 	 * Write and verify the backup labels. Make sure we don't try to
23542 	 * write past the last cylinder.
23543 	 */
23544 	for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
23545 		blk = (daddr_t)(
23546 		    (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
23547 		    (head * dkl->dkl_nsect) + sec);
23548 #if defined(__i386) || defined(__amd64)
23549 		blk += un->un_solaris_offset;
23550 #endif
23551 		if (NOT_DEVBSIZE(un)) {
23552 			uint64_t	tblk;
23553 			/*
23554 			 * Need to read the block first for read modify write.
23555 			 */
23556 			tblk = (uint64_t)blk;
23557 			blk = (int)((tblk * un->un_sys_blocksize) /
23558 			    un->un_tgt_blocksize);
23559 			if ((rval = sd_send_scsi_READ(un, shadow_buf,
23560 			    un->un_tgt_blocksize, blk,
23561 			    SD_PATH_STANDARD)) != 0) {
23562 				goto exit;
23563 			}
23564 			/*
23565 			 * Modify the shadow buffer with the label.
23566 			 */
23567 			bcopy(dkl, shadow_buf, sizeof (struct dk_label));
23568 			rval = sd_send_scsi_WRITE(un, shadow_buf,
23569 			    un->un_tgt_blocksize, blk, SD_PATH_STANDARD);
23570 		} else {
23571 			rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
23572 			    blk, SD_PATH_STANDARD);
23573 			SD_INFO(SD_LOG_IO_PARTITION, un,
23574 			"sd_set_vtoc: wrote backup label %d\n", blk);
23575 		}
23576 		if (rval != 0) {
23577 			goto exit;
23578 		}
23579 	}
23580 exit:
23581 	if (NOT_DEVBSIZE(un)) {
23582 		kmem_free(shadow_buf, un->un_tgt_blocksize);
23583 	}
23584 	return (rval);
23585 }
23586 
23587 /*
23588  *    Function: sd_clear_vtoc
23589  *
23590  * Description: This routine clears out the VTOC labels.
23591  *
23592  *   Arguments: un - driver soft state (unit) structure
23593  *
23594  * Return: void
23595  */
23596 
23597 static void
23598 sd_clear_vtoc(struct sd_lun *un)
23599 {
23600 	struct dk_label		*dkl;
23601 
23602 	mutex_exit(SD_MUTEX(un));
23603 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
23604 	mutex_enter(SD_MUTEX(un));
23605 	/*
23606 	 * sd_set_vtoc uses these fields in order to figure out
23607 	 * where to overwrite the backup labels
23608 	 */
23609 	dkl->dkl_apc    = un->un_g.dkg_apc;
23610 	dkl->dkl_ncyl   = un->un_g.dkg_ncyl;
23611 	dkl->dkl_acyl   = un->un_g.dkg_acyl;
23612 	dkl->dkl_nhead  = un->un_g.dkg_nhead;
23613 	dkl->dkl_nsect  = un->un_g.dkg_nsect;
23614 	mutex_exit(SD_MUTEX(un));
23615 	(void) sd_set_vtoc(un, dkl);
23616 	kmem_free(dkl, sizeof (struct dk_label));
23617 
23618 	mutex_enter(SD_MUTEX(un));
23619 }
23620 
23621 /*
23622  *    Function: sd_write_label
23623  *
23624  * Description: This routine will validate and write the driver soft state vtoc
23625  *		contents to the device.
23626  *
23627  *   Arguments: dev - the device number
23628  *
23629  * Return Code: the code returned by sd_send_scsi_cmd()
23630  *		0
23631  *		EINVAL
23632  *		ENXIO
23633  *		ENOMEM
23634  */
23635 
23636 static int
23637 sd_write_label(dev_t dev)
23638 {
23639 	struct sd_lun		*un;
23640 	struct dk_label		*dkl;
23641 	short			sum;
23642 	short			*sp;
23643 	int			i;
23644 	int			rval;
23645 
23646 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
23647 	    (un->un_state == SD_STATE_OFFLINE)) {
23648 		return (ENXIO);
23649 	}
23650 	ASSERT(mutex_owned(SD_MUTEX(un)));
23651 	mutex_exit(SD_MUTEX(un));
23652 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
23653 	mutex_enter(SD_MUTEX(un));
23654 
23655 	bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
23656 	dkl->dkl_rpm	= un->un_g.dkg_rpm;
23657 	dkl->dkl_pcyl	= un->un_g.dkg_pcyl;
23658 	dkl->dkl_apc	= un->un_g.dkg_apc;
23659 	dkl->dkl_intrlv = un->un_g.dkg_intrlv;
23660 	dkl->dkl_ncyl	= un->un_g.dkg_ncyl;
23661 	dkl->dkl_acyl	= un->un_g.dkg_acyl;
23662 	dkl->dkl_nhead	= un->un_g.dkg_nhead;
23663 	dkl->dkl_nsect	= un->un_g.dkg_nsect;
23664 
23665 #if defined(_SUNOS_VTOC_8)
23666 	dkl->dkl_obs1	= un->un_g.dkg_obs1;
23667 	dkl->dkl_obs2	= un->un_g.dkg_obs2;
23668 	dkl->dkl_obs3	= un->un_g.dkg_obs3;
23669 	for (i = 0; i < NDKMAP; i++) {
23670 		dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno;
23671 		dkl->dkl_map[i].dkl_nblk  = un->un_map[i].dkl_nblk;
23672 	}
23673 	bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
23674 #elif defined(_SUNOS_VTOC_16)
23675 	dkl->dkl_skew	= un->un_dkg_skew;
23676 #else
23677 #error "No VTOC format defined."
23678 #endif
23679 
23680 	dkl->dkl_magic			= DKL_MAGIC;
23681 	dkl->dkl_write_reinstruct	= un->un_g.dkg_write_reinstruct;
23682 	dkl->dkl_read_reinstruct	= un->un_g.dkg_read_reinstruct;
23683 
23684 	/* Construct checksum for the new disk label */
23685 	sum = 0;
23686 	sp = (short *)dkl;
23687 	i = sizeof (struct dk_label) / sizeof (short);
23688 	while (i--) {
23689 		sum ^= *sp++;
23690 	}
23691 	dkl->dkl_cksum = sum;
23692 
23693 	mutex_exit(SD_MUTEX(un));
23694 
23695 	rval = sd_set_vtoc(un, dkl);
23696 exit:
23697 	kmem_free(dkl, sizeof (struct dk_label));
23698 	mutex_enter(SD_MUTEX(un));
23699 	return (rval);
23700 }
23701 
23702 static int
23703 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag)
23704 {
23705 	struct sd_lun	*un = NULL;
23706 	dk_efi_t	user_efi;
23707 	int		rval = 0;
23708 	void		*buffer;
23709 	int		valid_efi;
23710 
23711 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
23712 		return (ENXIO);
23713 
23714 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
23715 		return (EFAULT);
23716 
23717 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
23718 
23719 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
23720 	    (user_efi.dki_length > un->un_max_xfer_size))
23721 		return (EINVAL);
23722 
23723 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
23724 	if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
23725 		rval = EFAULT;
23726 	} else {
23727 		/*
23728 		 * let's clear the vtoc labels and clear the softstate
23729 		 * vtoc.
23730 		 */
23731 		mutex_enter(SD_MUTEX(un));
23732 		if (un->un_vtoc.v_sanity == VTOC_SANE) {
23733 			SD_TRACE(SD_LOG_IO_PARTITION, un,
23734 				"sd_dkio_set_efi: CLEAR VTOC\n");
23735 			sd_clear_vtoc(un);
23736 			bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
23737 			mutex_exit(SD_MUTEX(un));
23738 			ddi_remove_minor_node(SD_DEVINFO(un), "h");
23739 			ddi_remove_minor_node(SD_DEVINFO(un), "h,raw");
23740 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd",
23741 			    S_IFBLK,
23742 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23743 			    un->un_node_type, NULL);
23744 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw",
23745 			    S_IFCHR,
23746 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23747 			    un->un_node_type, NULL);
23748 		} else
23749 			mutex_exit(SD_MUTEX(un));
23750 		rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length,
23751 		    user_efi.dki_lba, SD_PATH_DIRECT);
23752 		if (rval == 0) {
23753 			mutex_enter(SD_MUTEX(un));
23754 
23755 			/*
23756 			 * Set the un_reserved for valid efi label.
23757 			 * Function clear_efi in fdisk and efi_write in
23758 			 * libefi both change efi label on disk in 3 steps
23759 			 * 1. Change primary gpt and gpe
23760 			 * 2. Change backup gpe
23761 			 * 3. Change backup gpt, which is one block
23762 			 * We only reread the efi label after the 3rd step,
23763 			 * or there will be warning "primary label corrupt".
23764 			 */
23765 			if (user_efi.dki_length == un->un_tgt_blocksize) {
23766 				un->un_f_geometry_is_valid = FALSE;
23767 				valid_efi = sd_use_efi(un, SD_PATH_DIRECT);
23768 				if ((valid_efi == 0) &&
23769 				    un->un_f_devid_supported &&
23770 				    (un->un_f_opt_fab_devid == TRUE)) {
23771 					if (un->un_devid == NULL) {
23772 						sd_register_devid(un,
23773 						    SD_DEVINFO(un),
23774 						    SD_TARGET_IS_UNRESERVED);
23775 					} else {
23776 						/*
23777 						 * The device id for this disk
23778 						 * has been fabricated. The
23779 						 * device id must be preserved
23780 						 * by writing it back out to
23781 						 * disk.
23782 						 */
23783 						if (sd_write_deviceid(un)
23784 						    != 0) {
23785 							ddi_devid_free(
23786 							    un->un_devid);
23787 							un->un_devid = NULL;
23788 						}
23789 					}
23790 				}
23791 			}
23792 
23793 			mutex_exit(SD_MUTEX(un));
23794 		}
23795 	}
23796 	kmem_free(buffer, user_efi.dki_length);
23797 	return (rval);
23798 }
23799 
23800 /*
23801  *    Function: sd_dkio_get_mboot
23802  *
23803  * Description: This routine is the driver entry point for handling user
23804  *		requests to get the current device mboot (DKIOCGMBOOT)
23805  *
23806  *   Arguments: dev  - the device number
23807  *		arg  - pointer to user provided mboot structure specifying
23808  *			the current mboot.
23809  *		flag - this argument is a pass through to ddi_copyxxx()
23810  *		       directly from the mode argument of ioctl().
23811  *
23812  * Return Code: 0
23813  *		EINVAL
23814  *		EFAULT
23815  *		ENXIO
23816  */
23817 
23818 static int
23819 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag)
23820 {
23821 	struct sd_lun	*un;
23822 	struct mboot	*mboot;
23823 	int		rval;
23824 	size_t		buffer_size;
23825 
23826 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
23827 	    (un->un_state == SD_STATE_OFFLINE)) {
23828 		return (ENXIO);
23829 	}
23830 
23831 	if (!un->un_f_mboot_supported || arg == NULL) {
23832 		return (EINVAL);
23833 	}
23834 
23835 	/*
23836 	 * Read the mboot block, located at absolute block 0 on the target.
23837 	 */
23838 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot));
23839 
23840 	SD_TRACE(SD_LOG_IO_PARTITION, un,
23841 	    "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
23842 
23843 	mboot = kmem_zalloc(buffer_size, KM_SLEEP);
23844 	if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0,
23845 	    SD_PATH_STANDARD)) == 0) {
23846 		if (ddi_copyout(mboot, (void *)arg,
23847 		    sizeof (struct mboot), flag) != 0) {
23848 			rval = EFAULT;
23849 		}
23850 	}
23851 	kmem_free(mboot, buffer_size);
23852 	return (rval);
23853 }
23854 
23855 
23856 /*
23857  *    Function: sd_dkio_set_mboot
23858  *
23859  * Description: This routine is the driver entry point for handling user
23860  *		requests to validate and set the device master boot
23861  *		(DKIOCSMBOOT).
23862  *
23863  *   Arguments: dev  - the device number
23864  *		arg  - pointer to user provided mboot structure used to set the
23865  *			master boot.
23866  *		flag - this argument is a pass through to ddi_copyxxx()
23867  *		       directly from the mode argument of ioctl().
23868  *
23869  * Return Code: 0
23870  *		EINVAL
23871  *		EFAULT
23872  *		ENXIO
23873  */
23874 
23875 static int
23876 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag)
23877 {
23878 	struct sd_lun	*un = NULL;
23879 	struct mboot	*mboot = NULL;
23880 	int		rval;
23881 	ushort_t	magic;
23882 
23883 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23884 		return (ENXIO);
23885 	}
23886 
23887 	ASSERT(!mutex_owned(SD_MUTEX(un)));
23888 
23889 	if (!un->un_f_mboot_supported) {
23890 		return (EINVAL);
23891 	}
23892 
23893 	if (arg == NULL) {
23894 		return (EINVAL);
23895 	}
23896 
23897 	mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP);
23898 
23899 	if (ddi_copyin((const void *)arg, mboot,
23900 	    sizeof (struct mboot), flag) != 0) {
23901 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23902 		return (EFAULT);
23903 	}
23904 
23905 	/* Is this really a master boot record? */
23906 	magic = LE_16(mboot->signature);
23907 	if (magic != MBB_MAGIC) {
23908 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23909 		return (EINVAL);
23910 	}
23911 
23912 	rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0,
23913 	    SD_PATH_STANDARD);
23914 
23915 	mutex_enter(SD_MUTEX(un));
23916 #if defined(__i386) || defined(__amd64)
23917 	if (rval == 0) {
23918 		/*
23919 		 * mboot has been written successfully.
23920 		 * update the fdisk and vtoc tables in memory
23921 		 */
23922 		rval = sd_update_fdisk_and_vtoc(un);
23923 		if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) {
23924 			mutex_exit(SD_MUTEX(un));
23925 			kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23926 			return (rval);
23927 		}
23928 	}
23929 
23930 #ifdef __lock_lint
23931 	sd_setup_default_geometry(un);
23932 #endif
23933 
23934 #else
23935 	if (rval == 0) {
23936 		/*
23937 		 * mboot has been written successfully.
23938 		 * set up the default geometry and VTOC
23939 		 */
23940 		if (un->un_blockcount <= DK_MAX_BLOCKS)
23941 			sd_setup_default_geometry(un);
23942 	}
23943 #endif
23944 	mutex_exit(SD_MUTEX(un));
23945 	kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23946 	return (rval);
23947 }
23948 
23949 
23950 /*
23951  *    Function: sd_setup_default_geometry
23952  *
23953  * Description: This local utility routine sets the default geometry as part of
23954  *		setting the device mboot.
23955  *
23956  *   Arguments: un - driver soft state (unit) structure
23957  *
23958  * Note: This may be redundant with sd_build_default_label.
23959  */
23960 
23961 static void
23962 sd_setup_default_geometry(struct sd_lun *un)
23963 {
23964 	/* zero out the soft state geometry and partition table. */
23965 	bzero(&un->un_g, sizeof (struct dk_geom));
23966 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
23967 	bzero(un->un_map, NDKMAP * (sizeof (struct dk_map)));
23968 	un->un_asciilabel[0] = '\0';
23969 
23970 	/*
23971 	 * For the rpm, we use the minimum for the disk.
23972 	 * For the head, cyl and number of sector per track,
23973 	 * if the capacity <= 1GB, head = 64, sect = 32.
23974 	 * else head = 255, sect 63
23975 	 * Note: the capacity should be equal to C*H*S values.
23976 	 * This will cause some truncation of size due to
23977 	 * round off errors. For CD-ROMs, this truncation can
23978 	 * have adverse side effects, so returning ncyl and
23979 	 * nhead as 1. The nsect will overflow for most of
23980 	 * CD-ROMs as nsect is of type ushort.
23981 	 */
23982 	if (ISCD(un)) {
23983 		un->un_g.dkg_ncyl = 1;
23984 		un->un_g.dkg_nhead = 1;
23985 		un->un_g.dkg_nsect = un->un_blockcount;
23986 	} else {
23987 		if (un->un_blockcount <= 0x1000) {
23988 			/* Needed for unlabeled SCSI floppies. */
23989 			un->un_g.dkg_nhead = 2;
23990 			un->un_g.dkg_ncyl = 80;
23991 			un->un_g.dkg_pcyl = 80;
23992 			un->un_g.dkg_nsect = un->un_blockcount / (2 * 80);
23993 		} else if (un->un_blockcount <= 0x200000) {
23994 			un->un_g.dkg_nhead = 64;
23995 			un->un_g.dkg_nsect = 32;
23996 			un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32);
23997 		} else {
23998 			un->un_g.dkg_nhead = 255;
23999 			un->un_g.dkg_nsect = 63;
24000 			un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63);
24001 		}
24002 		un->un_blockcount = un->un_g.dkg_ncyl *
24003 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect;
24004 	}
24005 	un->un_g.dkg_acyl = 0;
24006 	un->un_g.dkg_bcyl = 0;
24007 	un->un_g.dkg_intrlv = 1;
24008 	un->un_g.dkg_rpm = 200;
24009 	un->un_g.dkg_read_reinstruct = 0;
24010 	un->un_g.dkg_write_reinstruct = 0;
24011 	if (un->un_g.dkg_pcyl == 0) {
24012 		un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl;
24013 	}
24014 
24015 	un->un_map['a'-'a'].dkl_cylno = 0;
24016 	un->un_map['a'-'a'].dkl_nblk = un->un_blockcount;
24017 	un->un_map['c'-'a'].dkl_cylno = 0;
24018 	un->un_map['c'-'a'].dkl_nblk = un->un_blockcount;
24019 	un->un_f_geometry_is_valid = FALSE;
24020 }
24021 
24022 
24023 #if defined(__i386) || defined(__amd64)
24024 /*
24025  *    Function: sd_update_fdisk_and_vtoc
24026  *
24027  * Description: This local utility routine updates the device fdisk and vtoc
24028  *		as part of setting the device mboot.
24029  *
24030  *   Arguments: un - driver soft state (unit) structure
24031  *
24032  * Return Code: 0 for success or errno-type return code.
24033  *
24034  *    Note:x86: This looks like a duplicate of sd_validate_geometry(), but
24035  *		these did exist seperately in x86 sd.c!!!
24036  */
24037 
24038 static int
24039 sd_update_fdisk_and_vtoc(struct sd_lun *un)
24040 {
24041 	static char	labelstring[128];
24042 	static char	buf[256];
24043 	char		*label = 0;
24044 	int		count;
24045 	int		label_rc = 0;
24046 	int		gvalid = un->un_f_geometry_is_valid;
24047 	int		fdisk_rval;
24048 	int		lbasize;
24049 	int		capacity;
24050 
24051 	ASSERT(mutex_owned(SD_MUTEX(un)));
24052 
24053 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
24054 		return (EINVAL);
24055 	}
24056 
24057 	if (un->un_f_blockcount_is_valid == FALSE) {
24058 		return (EINVAL);
24059 	}
24060 
24061 #if defined(_SUNOS_VTOC_16)
24062 	/*
24063 	 * Set up the "whole disk" fdisk partition; this should always
24064 	 * exist, regardless of whether the disk contains an fdisk table
24065 	 * or vtoc.
24066 	 */
24067 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
24068 	un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount;
24069 #endif	/* defined(_SUNOS_VTOC_16) */
24070 
24071 	/*
24072 	 * copy the lbasize and capacity so that if they're
24073 	 * reset while we're not holding the SD_MUTEX(un), we will
24074 	 * continue to use valid values after the SD_MUTEX(un) is
24075 	 * reacquired.
24076 	 */
24077 	lbasize  = un->un_tgt_blocksize;
24078 	capacity = un->un_blockcount;
24079 
24080 	/*
24081 	 * refresh the logical and physical geometry caches.
24082 	 * (data from mode sense format/rigid disk geometry pages,
24083 	 * and scsi_ifgetcap("geometry").
24084 	 */
24085 	sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT);
24086 
24087 	/*
24088 	 * Only DIRECT ACCESS devices will have Sun labels.
24089 	 * CD's supposedly have a Sun label, too
24090 	 */
24091 	if (un->un_f_vtoc_label_supported) {
24092 		fdisk_rval = sd_read_fdisk(un, capacity, lbasize,
24093 		    SD_PATH_DIRECT);
24094 		if (fdisk_rval == SD_CMD_FAILURE) {
24095 			ASSERT(mutex_owned(SD_MUTEX(un)));
24096 			return (EIO);
24097 		}
24098 
24099 		if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) {
24100 			ASSERT(mutex_owned(SD_MUTEX(un)));
24101 			return (EACCES);
24102 		}
24103 
24104 		if (un->un_solaris_size <= DK_LABEL_LOC) {
24105 			/*
24106 			 * Found fdisk table but no Solaris partition entry,
24107 			 * so don't call sd_uselabel() and don't create
24108 			 * a default label.
24109 			 */
24110 			label_rc = 0;
24111 			un->un_f_geometry_is_valid = TRUE;
24112 			goto no_solaris_partition;
24113 		}
24114 
24115 #if defined(_SUNOS_VTOC_8)
24116 		label = (char *)un->un_asciilabel;
24117 #elif defined(_SUNOS_VTOC_16)
24118 		label = (char *)un->un_vtoc.v_asciilabel;
24119 #else
24120 #error "No VTOC format defined."
24121 #endif
24122 	} else if (capacity < 0) {
24123 		ASSERT(mutex_owned(SD_MUTEX(un)));
24124 		return (EINVAL);
24125 	}
24126 
24127 	/*
24128 	 * For Removable media We reach here if we have found a
24129 	 * SOLARIS PARTITION.
24130 	 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS
24131 	 * PARTITION has changed from the previous one, hence we will setup a
24132 	 * default VTOC in this case.
24133 	 */
24134 	if (un->un_f_geometry_is_valid == FALSE) {
24135 		sd_build_default_label(un);
24136 		label_rc = 0;
24137 	}
24138 
24139 no_solaris_partition:
24140 	if ((!un->un_f_has_removable_media ||
24141 	    (un->un_f_has_removable_media &&
24142 	    un->un_mediastate == DKIO_EJECTED)) &&
24143 		(un->un_state == SD_STATE_NORMAL && !gvalid)) {
24144 		/*
24145 		 * Print out a message indicating who and what we are.
24146 		 * We do this only when we happen to really validate the
24147 		 * geometry. We may call sd_validate_geometry() at other
24148 		 * times, ioctl()'s like Get VTOC in which case we
24149 		 * don't want to print the label.
24150 		 * If the geometry is valid, print the label string,
24151 		 * else print vendor and product info, if available
24152 		 */
24153 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
24154 			SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label);
24155 		} else {
24156 			mutex_enter(&sd_label_mutex);
24157 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
24158 			    labelstring);
24159 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
24160 			    &labelstring[64]);
24161 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
24162 			    labelstring, &labelstring[64]);
24163 			if (un->un_f_blockcount_is_valid == TRUE) {
24164 				(void) sprintf(&buf[strlen(buf)],
24165 				    ", %" PRIu64 " %u byte blocks\n",
24166 				    un->un_blockcount,
24167 				    un->un_tgt_blocksize);
24168 			} else {
24169 				(void) sprintf(&buf[strlen(buf)],
24170 				    ", (unknown capacity)\n");
24171 			}
24172 			SD_INFO(SD_LOG_IOCTL_DKIO, un, buf);
24173 			mutex_exit(&sd_label_mutex);
24174 		}
24175 	}
24176 
24177 #if defined(_SUNOS_VTOC_16)
24178 	/*
24179 	 * If we have valid geometry, set up the remaining fdisk partitions.
24180 	 * Note that dkl_cylno is not used for the fdisk map entries, so
24181 	 * we set it to an entirely bogus value.
24182 	 */
24183 	for (count = 0; count < FD_NUMPART; count++) {
24184 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
24185 		un->un_map[FDISK_P1 + count].dkl_nblk =
24186 		    un->un_fmap[count].fmap_nblk;
24187 		un->un_offset[FDISK_P1 + count] =
24188 		    un->un_fmap[count].fmap_start;
24189 	}
24190 #endif
24191 
24192 	for (count = 0; count < NDKMAP; count++) {
24193 #if defined(_SUNOS_VTOC_8)
24194 		struct dk_map *lp  = &un->un_map[count];
24195 		un->un_offset[count] =
24196 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
24197 #elif defined(_SUNOS_VTOC_16)
24198 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
24199 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
24200 #else
24201 #error "No VTOC format defined."
24202 #endif
24203 	}
24204 
24205 	ASSERT(mutex_owned(SD_MUTEX(un)));
24206 	return (label_rc);
24207 }
24208 #endif
24209 
24210 
24211 /*
24212  *    Function: sd_check_media
24213  *
24214  * Description: This utility routine implements the functionality for the
24215  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
24216  *		driver state changes from that specified by the user
24217  *		(inserted or ejected). For example, if the user specifies
24218  *		DKIO_EJECTED and the current media state is inserted this
24219  *		routine will immediately return DKIO_INSERTED. However, if the
24220  *		current media state is not inserted the user thread will be
24221  *		blocked until the drive state changes. If DKIO_NONE is specified
24222  *		the user thread will block until a drive state change occurs.
24223  *
24224  *   Arguments: dev  - the device number
24225  *		state  - user pointer to a dkio_state, updated with the current
24226  *			drive state at return.
24227  *
24228  * Return Code: ENXIO
24229  *		EIO
24230  *		EAGAIN
24231  *		EINTR
24232  */
24233 
24234 static int
24235 sd_check_media(dev_t dev, enum dkio_state state)
24236 {
24237 	struct sd_lun		*un = NULL;
24238 	enum dkio_state		prev_state;
24239 	opaque_t		token = NULL;
24240 	int			rval = 0;
24241 
24242 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24243 		return (ENXIO);
24244 	}
24245 
24246 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
24247 
24248 	mutex_enter(SD_MUTEX(un));
24249 
24250 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
24251 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
24252 
24253 	prev_state = un->un_mediastate;
24254 
24255 	/* is there anything to do? */
24256 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
24257 		/*
24258 		 * submit the request to the scsi_watch service;
24259 		 * scsi_media_watch_cb() does the real work
24260 		 */
24261 		mutex_exit(SD_MUTEX(un));
24262 
24263 		/*
24264 		 * This change handles the case where a scsi watch request is
24265 		 * added to a device that is powered down. To accomplish this
24266 		 * we power up the device before adding the scsi watch request,
24267 		 * since the scsi watch sends a TUR directly to the device
24268 		 * which the device cannot handle if it is powered down.
24269 		 */
24270 		if (sd_pm_entry(un) != DDI_SUCCESS) {
24271 			mutex_enter(SD_MUTEX(un));
24272 			goto done;
24273 		}
24274 
24275 		token = scsi_watch_request_submit(SD_SCSI_DEVP(un),
24276 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
24277 		    (caddr_t)dev);
24278 
24279 		sd_pm_exit(un);
24280 
24281 		mutex_enter(SD_MUTEX(un));
24282 		if (token == NULL) {
24283 			rval = EAGAIN;
24284 			goto done;
24285 		}
24286 
24287 		/*
24288 		 * This is a special case IOCTL that doesn't return
24289 		 * until the media state changes. Routine sdpower
24290 		 * knows about and handles this so don't count it
24291 		 * as an active cmd in the driver, which would
24292 		 * keep the device busy to the pm framework.
24293 		 * If the count isn't decremented the device can't
24294 		 * be powered down.
24295 		 */
24296 		un->un_ncmds_in_driver--;
24297 		ASSERT(un->un_ncmds_in_driver >= 0);
24298 
24299 		/*
24300 		 * if a prior request had been made, this will be the same
24301 		 * token, as scsi_watch was designed that way.
24302 		 */
24303 		un->un_swr_token = token;
24304 		un->un_specified_mediastate = state;
24305 
24306 		/*
24307 		 * now wait for media change
24308 		 * we will not be signalled unless mediastate == state but it is
24309 		 * still better to test for this condition, since there is a
24310 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
24311 		 */
24312 		SD_TRACE(SD_LOG_COMMON, un,
24313 		    "sd_check_media: waiting for media state change\n");
24314 		while (un->un_mediastate == state) {
24315 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
24316 				SD_TRACE(SD_LOG_COMMON, un,
24317 				    "sd_check_media: waiting for media state "
24318 				    "was interrupted\n");
24319 				un->un_ncmds_in_driver++;
24320 				rval = EINTR;
24321 				goto done;
24322 			}
24323 			SD_TRACE(SD_LOG_COMMON, un,
24324 			    "sd_check_media: received signal, state=%x\n",
24325 			    un->un_mediastate);
24326 		}
24327 		/*
24328 		 * Inc the counter to indicate the device once again
24329 		 * has an active outstanding cmd.
24330 		 */
24331 		un->un_ncmds_in_driver++;
24332 	}
24333 
24334 	/* invalidate geometry */
24335 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
24336 		sr_ejected(un);
24337 	}
24338 
24339 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
24340 		uint64_t	capacity;
24341 		uint_t		lbasize;
24342 
24343 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
24344 		mutex_exit(SD_MUTEX(un));
24345 		/*
24346 		 * Since the following routines use SD_PATH_DIRECT, we must
24347 		 * call PM directly before the upcoming disk accesses. This
24348 		 * may cause the disk to be power/spin up.
24349 		 */
24350 
24351 		if (sd_pm_entry(un) == DDI_SUCCESS) {
24352 			rval = sd_send_scsi_READ_CAPACITY(un,
24353 			    &capacity,
24354 			    &lbasize, SD_PATH_DIRECT);
24355 			if (rval != 0) {
24356 				sd_pm_exit(un);
24357 				mutex_enter(SD_MUTEX(un));
24358 				goto done;
24359 			}
24360 		} else {
24361 			rval = EIO;
24362 			mutex_enter(SD_MUTEX(un));
24363 			goto done;
24364 		}
24365 		mutex_enter(SD_MUTEX(un));
24366 
24367 		sd_update_block_info(un, lbasize, capacity);
24368 
24369 		un->un_f_geometry_is_valid	= FALSE;
24370 		(void) sd_validate_geometry(un, SD_PATH_DIRECT);
24371 
24372 		mutex_exit(SD_MUTEX(un));
24373 		rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
24374 		    SD_PATH_DIRECT);
24375 		sd_pm_exit(un);
24376 
24377 		mutex_enter(SD_MUTEX(un));
24378 	}
24379 done:
24380 	un->un_f_watcht_stopped = FALSE;
24381 	if (un->un_swr_token) {
24382 		/*
24383 		 * Use of this local token and the mutex ensures that we avoid
24384 		 * some race conditions associated with terminating the
24385 		 * scsi watch.
24386 		 */
24387 		token = un->un_swr_token;
24388 		un->un_swr_token = (opaque_t)NULL;
24389 		mutex_exit(SD_MUTEX(un));
24390 		(void) scsi_watch_request_terminate(token,
24391 		    SCSI_WATCH_TERMINATE_WAIT);
24392 		mutex_enter(SD_MUTEX(un));
24393 	}
24394 
24395 	/*
24396 	 * Update the capacity kstat value, if no media previously
24397 	 * (capacity kstat is 0) and a media has been inserted
24398 	 * (un_f_blockcount_is_valid == TRUE)
24399 	 */
24400 	if (un->un_errstats) {
24401 		struct sd_errstats	*stp = NULL;
24402 
24403 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
24404 		if ((stp->sd_capacity.value.ui64 == 0) &&
24405 		    (un->un_f_blockcount_is_valid == TRUE)) {
24406 			stp->sd_capacity.value.ui64 =
24407 			    (uint64_t)((uint64_t)un->un_blockcount *
24408 			    un->un_sys_blocksize);
24409 		}
24410 	}
24411 	mutex_exit(SD_MUTEX(un));
24412 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
24413 	return (rval);
24414 }
24415 
24416 
24417 /*
24418  *    Function: sd_delayed_cv_broadcast
24419  *
24420  * Description: Delayed cv_broadcast to allow for target to recover from media
24421  *		insertion.
24422  *
24423  *   Arguments: arg - driver soft state (unit) structure
24424  */
24425 
24426 static void
24427 sd_delayed_cv_broadcast(void *arg)
24428 {
24429 	struct sd_lun *un = arg;
24430 
24431 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
24432 
24433 	mutex_enter(SD_MUTEX(un));
24434 	un->un_dcvb_timeid = NULL;
24435 	cv_broadcast(&un->un_state_cv);
24436 	mutex_exit(SD_MUTEX(un));
24437 }
24438 
24439 
24440 /*
24441  *    Function: sd_media_watch_cb
24442  *
24443  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
24444  *		routine processes the TUR sense data and updates the driver
24445  *		state if a transition has occurred. The user thread
24446  *		(sd_check_media) is then signalled.
24447  *
24448  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24449  *			among multiple watches that share this callback function
24450  *		resultp - scsi watch facility result packet containing scsi
24451  *			  packet, status byte and sense data
24452  *
24453  * Return Code: 0 for success, -1 for failure
24454  */
24455 
24456 static int
24457 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24458 {
24459 	struct sd_lun			*un;
24460 	struct scsi_status		*statusp = resultp->statusp;
24461 	uint8_t				*sensep = (uint8_t *)resultp->sensep;
24462 	enum dkio_state			state = DKIO_NONE;
24463 	dev_t				dev = (dev_t)arg;
24464 	uchar_t				actual_sense_length;
24465 	uint8_t				skey, asc, ascq;
24466 
24467 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24468 		return (-1);
24469 	}
24470 	actual_sense_length = resultp->actual_sense_length;
24471 
24472 	mutex_enter(SD_MUTEX(un));
24473 	SD_TRACE(SD_LOG_COMMON, un,
24474 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
24475 	    *((char *)statusp), (void *)sensep, actual_sense_length);
24476 
24477 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
24478 		un->un_mediastate = DKIO_DEV_GONE;
24479 		cv_broadcast(&un->un_state_cv);
24480 		mutex_exit(SD_MUTEX(un));
24481 
24482 		return (0);
24483 	}
24484 
24485 	/*
24486 	 * If there was a check condition then sensep points to valid sense data
24487 	 * If status was not a check condition but a reservation or busy status
24488 	 * then the new state is DKIO_NONE
24489 	 */
24490 	if (sensep != NULL) {
24491 		skey = scsi_sense_key(sensep);
24492 		asc = scsi_sense_asc(sensep);
24493 		ascq = scsi_sense_ascq(sensep);
24494 
24495 		SD_INFO(SD_LOG_COMMON, un,
24496 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
24497 		    skey, asc, ascq);
24498 		/* This routine only uses up to 13 bytes of sense data. */
24499 		if (actual_sense_length >= 13) {
24500 			if (skey == KEY_UNIT_ATTENTION) {
24501 				if (asc == 0x28) {
24502 					state = DKIO_INSERTED;
24503 				}
24504 			} else {
24505 				/*
24506 				 * if 02/04/02  means that the host
24507 				 * should send start command. Explicitly
24508 				 * leave the media state as is
24509 				 * (inserted) as the media is inserted
24510 				 * and host has stopped device for PM
24511 				 * reasons. Upon next true read/write
24512 				 * to this media will bring the
24513 				 * device to the right state good for
24514 				 * media access.
24515 				 */
24516 				if ((skey == KEY_NOT_READY) &&
24517 				    (asc == 0x3a)) {
24518 					state = DKIO_EJECTED;
24519 				}
24520 
24521 				/*
24522 				 * If the drivge is busy with an operation
24523 				 * or long write, keep the media in an
24524 				 * inserted state.
24525 				 */
24526 
24527 				if ((skey == KEY_NOT_READY) &&
24528 				    (asc == 0x04) &&
24529 				    ((ascq == 0x02) ||
24530 				    (ascq == 0x07) ||
24531 				    (ascq == 0x08))) {
24532 					state = DKIO_INSERTED;
24533 				}
24534 			}
24535 		}
24536 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
24537 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24538 		state = DKIO_INSERTED;
24539 	}
24540 
24541 	SD_TRACE(SD_LOG_COMMON, un,
24542 	    "sd_media_watch_cb: state=%x, specified=%x\n",
24543 	    state, un->un_specified_mediastate);
24544 
24545 	/*
24546 	 * now signal the waiting thread if this is *not* the specified state;
24547 	 * delay the signal if the state is DKIO_INSERTED to allow the target
24548 	 * to recover
24549 	 */
24550 	if (state != un->un_specified_mediastate) {
24551 		un->un_mediastate = state;
24552 		if (state == DKIO_INSERTED) {
24553 			/*
24554 			 * delay the signal to give the drive a chance
24555 			 * to do what it apparently needs to do
24556 			 */
24557 			SD_TRACE(SD_LOG_COMMON, un,
24558 			    "sd_media_watch_cb: delayed cv_broadcast\n");
24559 			if (un->un_dcvb_timeid == NULL) {
24560 				un->un_dcvb_timeid =
24561 				    timeout(sd_delayed_cv_broadcast, un,
24562 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24563 			}
24564 		} else {
24565 			SD_TRACE(SD_LOG_COMMON, un,
24566 			    "sd_media_watch_cb: immediate cv_broadcast\n");
24567 			cv_broadcast(&un->un_state_cv);
24568 		}
24569 	}
24570 	mutex_exit(SD_MUTEX(un));
24571 	return (0);
24572 }
24573 
24574 
24575 /*
24576  *    Function: sd_dkio_get_temp
24577  *
24578  * Description: This routine is the driver entry point for handling ioctl
24579  *		requests to get the disk temperature.
24580  *
24581  *   Arguments: dev  - the device number
24582  *		arg  - pointer to user provided dk_temperature structure.
24583  *		flag - this argument is a pass through to ddi_copyxxx()
24584  *		       directly from the mode argument of ioctl().
24585  *
24586  * Return Code: 0
24587  *		EFAULT
24588  *		ENXIO
24589  *		EAGAIN
24590  */
24591 
24592 static int
24593 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24594 {
24595 	struct sd_lun		*un = NULL;
24596 	struct dk_temperature	*dktemp = NULL;
24597 	uchar_t			*temperature_page;
24598 	int			rval = 0;
24599 	int			path_flag = SD_PATH_STANDARD;
24600 
24601 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24602 		return (ENXIO);
24603 	}
24604 
24605 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24606 
24607 	/* copyin the disk temp argument to get the user flags */
24608 	if (ddi_copyin((void *)arg, dktemp,
24609 	    sizeof (struct dk_temperature), flag) != 0) {
24610 		rval = EFAULT;
24611 		goto done;
24612 	}
24613 
24614 	/* Initialize the temperature to invalid. */
24615 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24616 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24617 
24618 	/*
24619 	 * Note: Investigate removing the "bypass pm" semantic.
24620 	 * Can we just bypass PM always?
24621 	 */
24622 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24623 		path_flag = SD_PATH_DIRECT;
24624 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24625 		mutex_enter(&un->un_pm_mutex);
24626 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24627 			/*
24628 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24629 			 * in low power mode, we can not wake it up, Need to
24630 			 * return EAGAIN.
24631 			 */
24632 			mutex_exit(&un->un_pm_mutex);
24633 			rval = EAGAIN;
24634 			goto done;
24635 		} else {
24636 			/*
24637 			 * Indicate to PM the device is busy. This is required
24638 			 * to avoid a race - i.e. the ioctl is issuing a
24639 			 * command and the pm framework brings down the device
24640 			 * to low power mode (possible power cut-off on some
24641 			 * platforms).
24642 			 */
24643 			mutex_exit(&un->un_pm_mutex);
24644 			if (sd_pm_entry(un) != DDI_SUCCESS) {
24645 				rval = EAGAIN;
24646 				goto done;
24647 			}
24648 		}
24649 	}
24650 
24651 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24652 
24653 	if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page,
24654 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) {
24655 		goto done2;
24656 	}
24657 
24658 	/*
24659 	 * For the current temperature verify that the parameter length is 0x02
24660 	 * and the parameter code is 0x00
24661 	 */
24662 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24663 	    (temperature_page[5] == 0x00)) {
24664 		if (temperature_page[9] == 0xFF) {
24665 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24666 		} else {
24667 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24668 		}
24669 	}
24670 
24671 	/*
24672 	 * For the reference temperature verify that the parameter
24673 	 * length is 0x02 and the parameter code is 0x01
24674 	 */
24675 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24676 	    (temperature_page[11] == 0x01)) {
24677 		if (temperature_page[15] == 0xFF) {
24678 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24679 		} else {
24680 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24681 		}
24682 	}
24683 
24684 	/* Do the copyout regardless of the temperature commands status. */
24685 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24686 	    flag) != 0) {
24687 		rval = EFAULT;
24688 	}
24689 
24690 done2:
24691 	if (path_flag == SD_PATH_DIRECT) {
24692 		sd_pm_exit(un);
24693 	}
24694 
24695 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24696 done:
24697 	if (dktemp != NULL) {
24698 		kmem_free(dktemp, sizeof (struct dk_temperature));
24699 	}
24700 
24701 	return (rval);
24702 }
24703 
24704 
24705 /*
24706  *    Function: sd_log_page_supported
24707  *
24708  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24709  *		supported log pages.
24710  *
24711  *   Arguments: un -
24712  *		log_page -
24713  *
24714  * Return Code: -1 - on error (log sense is optional and may not be supported).
24715  *		0  - log page not found.
24716  *  		1  - log page found.
24717  */
24718 
24719 static int
24720 sd_log_page_supported(struct sd_lun *un, int log_page)
24721 {
24722 	uchar_t *log_page_data;
24723 	int	i;
24724 	int	match = 0;
24725 	int	log_size;
24726 
24727 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24728 
24729 	if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0,
24730 	    SD_PATH_DIRECT) != 0) {
24731 		SD_ERROR(SD_LOG_COMMON, un,
24732 		    "sd_log_page_supported: failed log page retrieval\n");
24733 		kmem_free(log_page_data, 0xFF);
24734 		return (-1);
24735 	}
24736 	log_size = log_page_data[3];
24737 
24738 	/*
24739 	 * The list of supported log pages start from the fourth byte. Check
24740 	 * until we run out of log pages or a match is found.
24741 	 */
24742 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
24743 		if (log_page_data[i] == log_page) {
24744 			match++;
24745 		}
24746 	}
24747 	kmem_free(log_page_data, 0xFF);
24748 	return (match);
24749 }
24750 
24751 
24752 /*
24753  *    Function: sd_mhdioc_failfast
24754  *
24755  * Description: This routine is the driver entry point for handling ioctl
24756  *		requests to enable/disable the multihost failfast option.
24757  *		(MHIOCENFAILFAST)
24758  *
24759  *   Arguments: dev	- the device number
24760  *		arg	- user specified probing interval.
24761  *		flag	- this argument is a pass through to ddi_copyxxx()
24762  *			  directly from the mode argument of ioctl().
24763  *
24764  * Return Code: 0
24765  *		EFAULT
24766  *		ENXIO
24767  */
24768 
24769 static int
24770 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24771 {
24772 	struct sd_lun	*un = NULL;
24773 	int		mh_time;
24774 	int		rval = 0;
24775 
24776 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24777 		return (ENXIO);
24778 	}
24779 
24780 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24781 		return (EFAULT);
24782 
24783 	if (mh_time) {
24784 		mutex_enter(SD_MUTEX(un));
24785 		un->un_resvd_status |= SD_FAILFAST;
24786 		mutex_exit(SD_MUTEX(un));
24787 		/*
24788 		 * If mh_time is INT_MAX, then this ioctl is being used for
24789 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24790 		 */
24791 		if (mh_time != INT_MAX) {
24792 			rval = sd_check_mhd(dev, mh_time);
24793 		}
24794 	} else {
24795 		(void) sd_check_mhd(dev, 0);
24796 		mutex_enter(SD_MUTEX(un));
24797 		un->un_resvd_status &= ~SD_FAILFAST;
24798 		mutex_exit(SD_MUTEX(un));
24799 	}
24800 	return (rval);
24801 }
24802 
24803 
24804 /*
24805  *    Function: sd_mhdioc_takeown
24806  *
24807  * Description: This routine is the driver entry point for handling ioctl
24808  *		requests to forcefully acquire exclusive access rights to the
24809  *		multihost disk (MHIOCTKOWN).
24810  *
24811  *   Arguments: dev	- the device number
24812  *		arg	- user provided structure specifying the delay
24813  *			  parameters in milliseconds
24814  *		flag	- this argument is a pass through to ddi_copyxxx()
24815  *			  directly from the mode argument of ioctl().
24816  *
24817  * Return Code: 0
24818  *		EFAULT
24819  *		ENXIO
24820  */
24821 
24822 static int
24823 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24824 {
24825 	struct sd_lun		*un = NULL;
24826 	struct mhioctkown	*tkown = NULL;
24827 	int			rval = 0;
24828 
24829 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24830 		return (ENXIO);
24831 	}
24832 
24833 	if (arg != NULL) {
24834 		tkown = (struct mhioctkown *)
24835 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24836 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24837 		if (rval != 0) {
24838 			rval = EFAULT;
24839 			goto error;
24840 		}
24841 	}
24842 
24843 	rval = sd_take_ownership(dev, tkown);
24844 	mutex_enter(SD_MUTEX(un));
24845 	if (rval == 0) {
24846 		un->un_resvd_status |= SD_RESERVE;
24847 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24848 			sd_reinstate_resv_delay =
24849 			    tkown->reinstate_resv_delay * 1000;
24850 		} else {
24851 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24852 		}
24853 		/*
24854 		 * Give the scsi_watch routine interval set by
24855 		 * the MHIOCENFAILFAST ioctl precedence here.
24856 		 */
24857 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24858 			mutex_exit(SD_MUTEX(un));
24859 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24860 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24861 			    "sd_mhdioc_takeown : %d\n",
24862 			    sd_reinstate_resv_delay);
24863 		} else {
24864 			mutex_exit(SD_MUTEX(un));
24865 		}
24866 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24867 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24868 	} else {
24869 		un->un_resvd_status &= ~SD_RESERVE;
24870 		mutex_exit(SD_MUTEX(un));
24871 	}
24872 
24873 error:
24874 	if (tkown != NULL) {
24875 		kmem_free(tkown, sizeof (struct mhioctkown));
24876 	}
24877 	return (rval);
24878 }
24879 
24880 
24881 /*
24882  *    Function: sd_mhdioc_release
24883  *
24884  * Description: This routine is the driver entry point for handling ioctl
24885  *		requests to release exclusive access rights to the multihost
24886  *		disk (MHIOCRELEASE).
24887  *
24888  *   Arguments: dev	- the device number
24889  *
24890  * Return Code: 0
24891  *		ENXIO
24892  */
24893 
24894 static int
24895 sd_mhdioc_release(dev_t dev)
24896 {
24897 	struct sd_lun		*un = NULL;
24898 	timeout_id_t		resvd_timeid_save;
24899 	int			resvd_status_save;
24900 	int			rval = 0;
24901 
24902 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24903 		return (ENXIO);
24904 	}
24905 
24906 	mutex_enter(SD_MUTEX(un));
24907 	resvd_status_save = un->un_resvd_status;
24908 	un->un_resvd_status &=
24909 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24910 	if (un->un_resvd_timeid) {
24911 		resvd_timeid_save = un->un_resvd_timeid;
24912 		un->un_resvd_timeid = NULL;
24913 		mutex_exit(SD_MUTEX(un));
24914 		(void) untimeout(resvd_timeid_save);
24915 	} else {
24916 		mutex_exit(SD_MUTEX(un));
24917 	}
24918 
24919 	/*
24920 	 * destroy any pending timeout thread that may be attempting to
24921 	 * reinstate reservation on this device.
24922 	 */
24923 	sd_rmv_resv_reclaim_req(dev);
24924 
24925 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24926 		mutex_enter(SD_MUTEX(un));
24927 		if ((un->un_mhd_token) &&
24928 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24929 			mutex_exit(SD_MUTEX(un));
24930 			(void) sd_check_mhd(dev, 0);
24931 		} else {
24932 			mutex_exit(SD_MUTEX(un));
24933 		}
24934 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24935 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24936 	} else {
24937 		/*
24938 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24939 		 */
24940 		mutex_enter(SD_MUTEX(un));
24941 		un->un_resvd_status = resvd_status_save;
24942 		mutex_exit(SD_MUTEX(un));
24943 	}
24944 	return (rval);
24945 }
24946 
24947 
24948 /*
24949  *    Function: sd_mhdioc_register_devid
24950  *
24951  * Description: This routine is the driver entry point for handling ioctl
24952  *		requests to register the device id (MHIOCREREGISTERDEVID).
24953  *
24954  *		Note: The implementation for this ioctl has been updated to
24955  *		be consistent with the original PSARC case (1999/357)
24956  *		(4375899, 4241671, 4220005)
24957  *
24958  *   Arguments: dev	- the device number
24959  *
24960  * Return Code: 0
24961  *		ENXIO
24962  */
24963 
24964 static int
24965 sd_mhdioc_register_devid(dev_t dev)
24966 {
24967 	struct sd_lun	*un = NULL;
24968 	int		rval = 0;
24969 
24970 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24971 		return (ENXIO);
24972 	}
24973 
24974 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24975 
24976 	mutex_enter(SD_MUTEX(un));
24977 
24978 	/* If a devid already exists, de-register it */
24979 	if (un->un_devid != NULL) {
24980 		ddi_devid_unregister(SD_DEVINFO(un));
24981 		/*
24982 		 * After unregister devid, needs to free devid memory
24983 		 */
24984 		ddi_devid_free(un->un_devid);
24985 		un->un_devid = NULL;
24986 	}
24987 
24988 	/* Check for reservation conflict */
24989 	mutex_exit(SD_MUTEX(un));
24990 	rval = sd_send_scsi_TEST_UNIT_READY(un, 0);
24991 	mutex_enter(SD_MUTEX(un));
24992 
24993 	switch (rval) {
24994 	case 0:
24995 		sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24996 		break;
24997 	case EACCES:
24998 		break;
24999 	default:
25000 		rval = EIO;
25001 	}
25002 
25003 	mutex_exit(SD_MUTEX(un));
25004 	return (rval);
25005 }
25006 
25007 
25008 /*
25009  *    Function: sd_mhdioc_inkeys
25010  *
25011  * Description: This routine is the driver entry point for handling ioctl
25012  *		requests to issue the SCSI-3 Persistent In Read Keys command
25013  *		to the device (MHIOCGRP_INKEYS).
25014  *
25015  *   Arguments: dev	- the device number
25016  *		arg	- user provided in_keys structure
25017  *		flag	- this argument is a pass through to ddi_copyxxx()
25018  *			  directly from the mode argument of ioctl().
25019  *
25020  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
25021  *		ENXIO
25022  *		EFAULT
25023  */
25024 
25025 static int
25026 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
25027 {
25028 	struct sd_lun		*un;
25029 	mhioc_inkeys_t		inkeys;
25030 	int			rval = 0;
25031 
25032 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25033 		return (ENXIO);
25034 	}
25035 
25036 #ifdef _MULTI_DATAMODEL
25037 	switch (ddi_model_convert_from(flag & FMODELS)) {
25038 	case DDI_MODEL_ILP32: {
25039 		struct mhioc_inkeys32	inkeys32;
25040 
25041 		if (ddi_copyin(arg, &inkeys32,
25042 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
25043 			return (EFAULT);
25044 		}
25045 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
25046 		if ((rval = sd_persistent_reservation_in_read_keys(un,
25047 		    &inkeys, flag)) != 0) {
25048 			return (rval);
25049 		}
25050 		inkeys32.generation = inkeys.generation;
25051 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
25052 		    flag) != 0) {
25053 			return (EFAULT);
25054 		}
25055 		break;
25056 	}
25057 	case DDI_MODEL_NONE:
25058 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
25059 		    flag) != 0) {
25060 			return (EFAULT);
25061 		}
25062 		if ((rval = sd_persistent_reservation_in_read_keys(un,
25063 		    &inkeys, flag)) != 0) {
25064 			return (rval);
25065 		}
25066 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
25067 		    flag) != 0) {
25068 			return (EFAULT);
25069 		}
25070 		break;
25071 	}
25072 
25073 #else /* ! _MULTI_DATAMODEL */
25074 
25075 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
25076 		return (EFAULT);
25077 	}
25078 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
25079 	if (rval != 0) {
25080 		return (rval);
25081 	}
25082 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
25083 		return (EFAULT);
25084 	}
25085 
25086 #endif /* _MULTI_DATAMODEL */
25087 
25088 	return (rval);
25089 }
25090 
25091 
25092 /*
25093  *    Function: sd_mhdioc_inresv
25094  *
25095  * Description: This routine is the driver entry point for handling ioctl
25096  *		requests to issue the SCSI-3 Persistent In Read Reservations
25097  *		command to the device (MHIOCGRP_INKEYS).
25098  *
25099  *   Arguments: dev	- the device number
25100  *		arg	- user provided in_resv structure
25101  *		flag	- this argument is a pass through to ddi_copyxxx()
25102  *			  directly from the mode argument of ioctl().
25103  *
25104  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
25105  *		ENXIO
25106  *		EFAULT
25107  */
25108 
25109 static int
25110 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
25111 {
25112 	struct sd_lun		*un;
25113 	mhioc_inresvs_t		inresvs;
25114 	int			rval = 0;
25115 
25116 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25117 		return (ENXIO);
25118 	}
25119 
25120 #ifdef _MULTI_DATAMODEL
25121 
25122 	switch (ddi_model_convert_from(flag & FMODELS)) {
25123 	case DDI_MODEL_ILP32: {
25124 		struct mhioc_inresvs32	inresvs32;
25125 
25126 		if (ddi_copyin(arg, &inresvs32,
25127 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
25128 			return (EFAULT);
25129 		}
25130 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
25131 		if ((rval = sd_persistent_reservation_in_read_resv(un,
25132 		    &inresvs, flag)) != 0) {
25133 			return (rval);
25134 		}
25135 		inresvs32.generation = inresvs.generation;
25136 		if (ddi_copyout(&inresvs32, arg,
25137 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
25138 			return (EFAULT);
25139 		}
25140 		break;
25141 	}
25142 	case DDI_MODEL_NONE:
25143 		if (ddi_copyin(arg, &inresvs,
25144 		    sizeof (mhioc_inresvs_t), flag) != 0) {
25145 			return (EFAULT);
25146 		}
25147 		if ((rval = sd_persistent_reservation_in_read_resv(un,
25148 		    &inresvs, flag)) != 0) {
25149 			return (rval);
25150 		}
25151 		if (ddi_copyout(&inresvs, arg,
25152 		    sizeof (mhioc_inresvs_t), flag) != 0) {
25153 			return (EFAULT);
25154 		}
25155 		break;
25156 	}
25157 
25158 #else /* ! _MULTI_DATAMODEL */
25159 
25160 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
25161 		return (EFAULT);
25162 	}
25163 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
25164 	if (rval != 0) {
25165 		return (rval);
25166 	}
25167 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
25168 		return (EFAULT);
25169 	}
25170 
25171 #endif /* ! _MULTI_DATAMODEL */
25172 
25173 	return (rval);
25174 }
25175 
25176 
25177 /*
25178  * The following routines support the clustering functionality described below
25179  * and implement lost reservation reclaim functionality.
25180  *
25181  * Clustering
25182  * ----------
25183  * The clustering code uses two different, independent forms of SCSI
25184  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
25185  * Persistent Group Reservations. For any particular disk, it will use either
25186  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
25187  *
25188  * SCSI-2
25189  * The cluster software takes ownership of a multi-hosted disk by issuing the
25190  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
25191  * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster,
25192  * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues
25193  * the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the driver. The
25194  * meaning of failfast is that if the driver (on this host) ever encounters the
25195  * scsi error return code RESERVATION_CONFLICT from the device, it should
25196  * immediately panic the host. The motivation for this ioctl is that if this
25197  * host does encounter reservation conflict, the underlying cause is that some
25198  * other host of the cluster has decided that this host is no longer in the
25199  * cluster and has seized control of the disks for itself. Since this host is no
25200  * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl
25201  * does two things:
25202  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
25203  *      error to panic the host
25204  *      (b) it sets up a periodic timer to test whether this host still has
25205  *      "access" (in that no other host has reserved the device):  if the
25206  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
25207  *      purpose of that periodic timer is to handle scenarios where the host is
25208  *      otherwise temporarily quiescent, temporarily doing no real i/o.
25209  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
25210  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
25211  * the device itself.
25212  *
25213  * SCSI-3 PGR
25214  * A direct semantic implementation of the SCSI-3 Persistent Reservation
25215  * facility is supported through the shared multihost disk ioctls
25216  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
25217  * MHIOCGRP_PREEMPTANDABORT)
25218  *
25219  * Reservation Reclaim:
25220  * --------------------
25221  * To support the lost reservation reclaim operations this driver creates a
25222  * single thread to handle reinstating reservations on all devices that have
25223  * lost reservations sd_resv_reclaim_requests are logged for all devices that
25224  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
25225  * and the reservation reclaim thread loops through the requests to regain the
25226  * lost reservations.
25227  */
25228 
25229 /*
25230  *    Function: sd_check_mhd()
25231  *
25232  * Description: This function sets up and submits a scsi watch request or
25233  *		terminates an existing watch request. This routine is used in
25234  *		support of reservation reclaim.
25235  *
25236  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
25237  *			 among multiple watches that share the callback function
25238  *		interval - the number of microseconds specifying the watch
25239  *			   interval for issuing TEST UNIT READY commands. If
25240  *			   set to 0 the watch should be terminated. If the
25241  *			   interval is set to 0 and if the device is required
25242  *			   to hold reservation while disabling failfast, the
25243  *			   watch is restarted with an interval of
25244  *			   reinstate_resv_delay.
25245  *
25246  * Return Code: 0	   - Successful submit/terminate of scsi watch request
25247  *		ENXIO      - Indicates an invalid device was specified
25248  *		EAGAIN     - Unable to submit the scsi watch request
25249  */
25250 
25251 static int
25252 sd_check_mhd(dev_t dev, int interval)
25253 {
25254 	struct sd_lun	*un;
25255 	opaque_t	token;
25256 
25257 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25258 		return (ENXIO);
25259 	}
25260 
25261 	/* is this a watch termination request? */
25262 	if (interval == 0) {
25263 		mutex_enter(SD_MUTEX(un));
25264 		/* if there is an existing watch task then terminate it */
25265 		if (un->un_mhd_token) {
25266 			token = un->un_mhd_token;
25267 			un->un_mhd_token = NULL;
25268 			mutex_exit(SD_MUTEX(un));
25269 			(void) scsi_watch_request_terminate(token,
25270 			    SCSI_WATCH_TERMINATE_WAIT);
25271 			mutex_enter(SD_MUTEX(un));
25272 		} else {
25273 			mutex_exit(SD_MUTEX(un));
25274 			/*
25275 			 * Note: If we return here we don't check for the
25276 			 * failfast case. This is the original legacy
25277 			 * implementation but perhaps we should be checking
25278 			 * the failfast case.
25279 			 */
25280 			return (0);
25281 		}
25282 		/*
25283 		 * If the device is required to hold reservation while
25284 		 * disabling failfast, we need to restart the scsi_watch
25285 		 * routine with an interval of reinstate_resv_delay.
25286 		 */
25287 		if (un->un_resvd_status & SD_RESERVE) {
25288 			interval = sd_reinstate_resv_delay/1000;
25289 		} else {
25290 			/* no failfast so bail */
25291 			mutex_exit(SD_MUTEX(un));
25292 			return (0);
25293 		}
25294 		mutex_exit(SD_MUTEX(un));
25295 	}
25296 
25297 	/*
25298 	 * adjust minimum time interval to 1 second,
25299 	 * and convert from msecs to usecs
25300 	 */
25301 	if (interval > 0 && interval < 1000) {
25302 		interval = 1000;
25303 	}
25304 	interval *= 1000;
25305 
25306 	/*
25307 	 * submit the request to the scsi_watch service
25308 	 */
25309 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
25310 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
25311 	if (token == NULL) {
25312 		return (EAGAIN);
25313 	}
25314 
25315 	/*
25316 	 * save token for termination later on
25317 	 */
25318 	mutex_enter(SD_MUTEX(un));
25319 	un->un_mhd_token = token;
25320 	mutex_exit(SD_MUTEX(un));
25321 	return (0);
25322 }
25323 
25324 
25325 /*
25326  *    Function: sd_mhd_watch_cb()
25327  *
25328  * Description: This function is the call back function used by the scsi watch
25329  *		facility. The scsi watch facility sends the "Test Unit Ready"
25330  *		and processes the status. If applicable (i.e. a "Unit Attention"
25331  *		status and automatic "Request Sense" not used) the scsi watch
25332  *		facility will send a "Request Sense" and retrieve the sense data
25333  *		to be passed to this callback function. In either case the
25334  *		automatic "Request Sense" or the facility submitting one, this
25335  *		callback is passed the status and sense data.
25336  *
25337  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25338  *			among multiple watches that share this callback function
25339  *		resultp - scsi watch facility result packet containing scsi
25340  *			  packet, status byte and sense data
25341  *
25342  * Return Code: 0 - continue the watch task
25343  *		non-zero - terminate the watch task
25344  */
25345 
25346 static int
25347 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
25348 {
25349 	struct sd_lun			*un;
25350 	struct scsi_status		*statusp;
25351 	uint8_t				*sensep;
25352 	struct scsi_pkt			*pkt;
25353 	uchar_t				actual_sense_length;
25354 	dev_t  				dev = (dev_t)arg;
25355 
25356 	ASSERT(resultp != NULL);
25357 	statusp			= resultp->statusp;
25358 	sensep			= (uint8_t *)resultp->sensep;
25359 	pkt			= resultp->pkt;
25360 	actual_sense_length	= resultp->actual_sense_length;
25361 
25362 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25363 		return (ENXIO);
25364 	}
25365 
25366 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25367 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
25368 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
25369 
25370 	/* Begin processing of the status and/or sense data */
25371 	if (pkt->pkt_reason != CMD_CMPLT) {
25372 		/* Handle the incomplete packet */
25373 		sd_mhd_watch_incomplete(un, pkt);
25374 		return (0);
25375 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
25376 		if (*((unsigned char *)statusp)
25377 		    == STATUS_RESERVATION_CONFLICT) {
25378 			/*
25379 			 * Handle a reservation conflict by panicking if
25380 			 * configured for failfast or by logging the conflict
25381 			 * and updating the reservation status
25382 			 */
25383 			mutex_enter(SD_MUTEX(un));
25384 			if ((un->un_resvd_status & SD_FAILFAST) &&
25385 			    (sd_failfast_enable)) {
25386 				sd_panic_for_res_conflict(un);
25387 				/*NOTREACHED*/
25388 			}
25389 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25390 			    "sd_mhd_watch_cb: Reservation Conflict\n");
25391 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
25392 			mutex_exit(SD_MUTEX(un));
25393 		}
25394 	}
25395 
25396 	if (sensep != NULL) {
25397 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
25398 			mutex_enter(SD_MUTEX(un));
25399 			if ((scsi_sense_asc(sensep) ==
25400 			    SD_SCSI_RESET_SENSE_CODE) &&
25401 			    (un->un_resvd_status & SD_RESERVE)) {
25402 				/*
25403 				 * The additional sense code indicates a power
25404 				 * on or bus device reset has occurred; update
25405 				 * the reservation status.
25406 				 */
25407 				un->un_resvd_status |=
25408 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25409 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25410 				    "sd_mhd_watch_cb: Lost Reservation\n");
25411 			}
25412 		} else {
25413 			return (0);
25414 		}
25415 	} else {
25416 		mutex_enter(SD_MUTEX(un));
25417 	}
25418 
25419 	if ((un->un_resvd_status & SD_RESERVE) &&
25420 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
25421 		if (un->un_resvd_status & SD_WANT_RESERVE) {
25422 			/*
25423 			 * A reset occurred in between the last probe and this
25424 			 * one so if a timeout is pending cancel it.
25425 			 */
25426 			if (un->un_resvd_timeid) {
25427 				timeout_id_t temp_id = un->un_resvd_timeid;
25428 				un->un_resvd_timeid = NULL;
25429 				mutex_exit(SD_MUTEX(un));
25430 				(void) untimeout(temp_id);
25431 				mutex_enter(SD_MUTEX(un));
25432 			}
25433 			un->un_resvd_status &= ~SD_WANT_RESERVE;
25434 		}
25435 		if (un->un_resvd_timeid == 0) {
25436 			/* Schedule a timeout to handle the lost reservation */
25437 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25438 			    (void *)dev,
25439 			    drv_usectohz(sd_reinstate_resv_delay));
25440 		}
25441 	}
25442 	mutex_exit(SD_MUTEX(un));
25443 	return (0);
25444 }
25445 
25446 
25447 /*
25448  *    Function: sd_mhd_watch_incomplete()
25449  *
25450  * Description: This function is used to find out why a scsi pkt sent by the
25451  *		scsi watch facility was not completed. Under some scenarios this
25452  *		routine will return. Otherwise it will send a bus reset to see
25453  *		if the drive is still online.
25454  *
25455  *   Arguments: un  - driver soft state (unit) structure
25456  *		pkt - incomplete scsi pkt
25457  */
25458 
25459 static void
25460 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25461 {
25462 	int	be_chatty;
25463 	int	perr;
25464 
25465 	ASSERT(pkt != NULL);
25466 	ASSERT(un != NULL);
25467 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
25468 	perr		= (pkt->pkt_statistics & STAT_PERR);
25469 
25470 	mutex_enter(SD_MUTEX(un));
25471 	if (un->un_state == SD_STATE_DUMPING) {
25472 		mutex_exit(SD_MUTEX(un));
25473 		return;
25474 	}
25475 
25476 	switch (pkt->pkt_reason) {
25477 	case CMD_UNX_BUS_FREE:
25478 		/*
25479 		 * If we had a parity error that caused the target to drop BSY*,
25480 		 * don't be chatty about it.
25481 		 */
25482 		if (perr && be_chatty) {
25483 			be_chatty = 0;
25484 		}
25485 		break;
25486 	case CMD_TAG_REJECT:
25487 		/*
25488 		 * The SCSI-2 spec states that a tag reject will be sent by the
25489 		 * target if tagged queuing is not supported. A tag reject may
25490 		 * also be sent during certain initialization periods or to
25491 		 * control internal resources. For the latter case the target
25492 		 * may also return Queue Full.
25493 		 *
25494 		 * If this driver receives a tag reject from a target that is
25495 		 * going through an init period or controlling internal
25496 		 * resources tagged queuing will be disabled. This is a less
25497 		 * than optimal behavior but the driver is unable to determine
25498 		 * the target state and assumes tagged queueing is not supported
25499 		 */
25500 		pkt->pkt_flags = 0;
25501 		un->un_tagflags = 0;
25502 
25503 		if (un->un_f_opt_queueing == TRUE) {
25504 			un->un_throttle = min(un->un_throttle, 3);
25505 		} else {
25506 			un->un_throttle = 1;
25507 		}
25508 		mutex_exit(SD_MUTEX(un));
25509 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25510 		mutex_enter(SD_MUTEX(un));
25511 		break;
25512 	case CMD_INCOMPLETE:
25513 		/*
25514 		 * The transport stopped with an abnormal state, fallthrough and
25515 		 * reset the target and/or bus unless selection did not complete
25516 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
25517 		 * go through a target/bus reset
25518 		 */
25519 		if (pkt->pkt_state == STATE_GOT_BUS) {
25520 			break;
25521 		}
25522 		/*FALLTHROUGH*/
25523 
25524 	case CMD_TIMEOUT:
25525 	default:
25526 		/*
25527 		 * The lun may still be running the command, so a lun reset
25528 		 * should be attempted. If the lun reset fails or cannot be
25529 		 * issued, than try a target reset. Lastly try a bus reset.
25530 		 */
25531 		if ((pkt->pkt_statistics &
25532 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25533 			int reset_retval = 0;
25534 			mutex_exit(SD_MUTEX(un));
25535 			if (un->un_f_allow_bus_device_reset == TRUE) {
25536 				if (un->un_f_lun_reset_enabled == TRUE) {
25537 					reset_retval =
25538 					    scsi_reset(SD_ADDRESS(un),
25539 					    RESET_LUN);
25540 				}
25541 				if (reset_retval == 0) {
25542 					reset_retval =
25543 					    scsi_reset(SD_ADDRESS(un),
25544 					    RESET_TARGET);
25545 				}
25546 			}
25547 			if (reset_retval == 0) {
25548 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25549 			}
25550 			mutex_enter(SD_MUTEX(un));
25551 		}
25552 		break;
25553 	}
25554 
25555 	/* A device/bus reset has occurred; update the reservation status. */
25556 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25557 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25558 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25559 			un->un_resvd_status |=
25560 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25561 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25562 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25563 		}
25564 	}
25565 
25566 	/*
25567 	 * The disk has been turned off; Update the device state.
25568 	 *
25569 	 * Note: Should we be offlining the disk here?
25570 	 */
25571 	if (pkt->pkt_state == STATE_GOT_BUS) {
25572 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25573 		    "Disk not responding to selection\n");
25574 		if (un->un_state != SD_STATE_OFFLINE) {
25575 			New_state(un, SD_STATE_OFFLINE);
25576 		}
25577 	} else if (be_chatty) {
25578 		/*
25579 		 * suppress messages if they are all the same pkt reason;
25580 		 * with TQ, many (up to 256) are returned with the same
25581 		 * pkt_reason
25582 		 */
25583 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25584 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25585 			    "sd_mhd_watch_incomplete: "
25586 			    "SCSI transport failed: reason '%s'\n",
25587 			    scsi_rname(pkt->pkt_reason));
25588 		}
25589 	}
25590 	un->un_last_pkt_reason = pkt->pkt_reason;
25591 	mutex_exit(SD_MUTEX(un));
25592 }
25593 
25594 
25595 /*
25596  *    Function: sd_sname()
25597  *
25598  * Description: This is a simple little routine to return a string containing
25599  *		a printable description of command status byte for use in
25600  *		logging.
25601  *
25602  *   Arguments: status - pointer to a status byte
25603  *
25604  * Return Code: char * - string containing status description.
25605  */
25606 
25607 static char *
25608 sd_sname(uchar_t status)
25609 {
25610 	switch (status & STATUS_MASK) {
25611 	case STATUS_GOOD:
25612 		return ("good status");
25613 	case STATUS_CHECK:
25614 		return ("check condition");
25615 	case STATUS_MET:
25616 		return ("condition met");
25617 	case STATUS_BUSY:
25618 		return ("busy");
25619 	case STATUS_INTERMEDIATE:
25620 		return ("intermediate");
25621 	case STATUS_INTERMEDIATE_MET:
25622 		return ("intermediate - condition met");
25623 	case STATUS_RESERVATION_CONFLICT:
25624 		return ("reservation_conflict");
25625 	case STATUS_TERMINATED:
25626 		return ("command terminated");
25627 	case STATUS_QFULL:
25628 		return ("queue full");
25629 	default:
25630 		return ("<unknown status>");
25631 	}
25632 }
25633 
25634 
25635 /*
25636  *    Function: sd_mhd_resvd_recover()
25637  *
25638  * Description: This function adds a reservation entry to the
25639  *		sd_resv_reclaim_request list and signals the reservation
25640  *		reclaim thread that there is work pending. If the reservation
25641  *		reclaim thread has not been previously created this function
25642  *		will kick it off.
25643  *
25644  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25645  *			among multiple watches that share this callback function
25646  *
25647  *     Context: This routine is called by timeout() and is run in interrupt
25648  *		context. It must not sleep or call other functions which may
25649  *		sleep.
25650  */
25651 
25652 static void
25653 sd_mhd_resvd_recover(void *arg)
25654 {
25655 	dev_t			dev = (dev_t)arg;
25656 	struct sd_lun		*un;
25657 	struct sd_thr_request	*sd_treq = NULL;
25658 	struct sd_thr_request	*sd_cur = NULL;
25659 	struct sd_thr_request	*sd_prev = NULL;
25660 	int			already_there = 0;
25661 
25662 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25663 		return;
25664 	}
25665 
25666 	mutex_enter(SD_MUTEX(un));
25667 	un->un_resvd_timeid = NULL;
25668 	if (un->un_resvd_status & SD_WANT_RESERVE) {
25669 		/*
25670 		 * There was a reset so don't issue the reserve, allow the
25671 		 * sd_mhd_watch_cb callback function to notice this and
25672 		 * reschedule the timeout for reservation.
25673 		 */
25674 		mutex_exit(SD_MUTEX(un));
25675 		return;
25676 	}
25677 	mutex_exit(SD_MUTEX(un));
25678 
25679 	/*
25680 	 * Add this device to the sd_resv_reclaim_request list and the
25681 	 * sd_resv_reclaim_thread should take care of the rest.
25682 	 *
25683 	 * Note: We can't sleep in this context so if the memory allocation
25684 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
25685 	 * reschedule the timeout for reservation.  (4378460)
25686 	 */
25687 	sd_treq = (struct sd_thr_request *)
25688 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25689 	if (sd_treq == NULL) {
25690 		return;
25691 	}
25692 
25693 	sd_treq->sd_thr_req_next = NULL;
25694 	sd_treq->dev = dev;
25695 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25696 	if (sd_tr.srq_thr_req_head == NULL) {
25697 		sd_tr.srq_thr_req_head = sd_treq;
25698 	} else {
25699 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25700 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25701 			if (sd_cur->dev == dev) {
25702 				/*
25703 				 * already in Queue so don't log
25704 				 * another request for the device
25705 				 */
25706 				already_there = 1;
25707 				break;
25708 			}
25709 			sd_prev = sd_cur;
25710 		}
25711 		if (!already_there) {
25712 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25713 			    "logging request for %lx\n", dev);
25714 			sd_prev->sd_thr_req_next = sd_treq;
25715 		} else {
25716 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
25717 		}
25718 	}
25719 
25720 	/*
25721 	 * Create a kernel thread to do the reservation reclaim and free up this
25722 	 * thread. We cannot block this thread while we go away to do the
25723 	 * reservation reclaim
25724 	 */
25725 	if (sd_tr.srq_resv_reclaim_thread == NULL)
25726 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25727 		    sd_resv_reclaim_thread, NULL,
25728 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
25729 
25730 	/* Tell the reservation reclaim thread that it has work to do */
25731 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
25732 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25733 }
25734 
25735 /*
25736  *    Function: sd_resv_reclaim_thread()
25737  *
25738  * Description: This function implements the reservation reclaim operations
25739  *
25740  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25741  *		      among multiple watches that share this callback function
25742  */
25743 
25744 static void
25745 sd_resv_reclaim_thread()
25746 {
25747 	struct sd_lun		*un;
25748 	struct sd_thr_request	*sd_mhreq;
25749 
25750 	/* Wait for work */
25751 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25752 	if (sd_tr.srq_thr_req_head == NULL) {
25753 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
25754 		    &sd_tr.srq_resv_reclaim_mutex);
25755 	}
25756 
25757 	/* Loop while we have work */
25758 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25759 		un = ddi_get_soft_state(sd_state,
25760 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
25761 		if (un == NULL) {
25762 			/*
25763 			 * softstate structure is NULL so just
25764 			 * dequeue the request and continue
25765 			 */
25766 			sd_tr.srq_thr_req_head =
25767 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25768 			kmem_free(sd_tr.srq_thr_cur_req,
25769 			    sizeof (struct sd_thr_request));
25770 			continue;
25771 		}
25772 
25773 		/* dequeue the request */
25774 		sd_mhreq = sd_tr.srq_thr_cur_req;
25775 		sd_tr.srq_thr_req_head =
25776 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25777 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25778 
25779 		/*
25780 		 * Reclaim reservation only if SD_RESERVE is still set. There
25781 		 * may have been a call to MHIOCRELEASE before we got here.
25782 		 */
25783 		mutex_enter(SD_MUTEX(un));
25784 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25785 			/*
25786 			 * Note: The SD_LOST_RESERVE flag is cleared before
25787 			 * reclaiming the reservation. If this is done after the
25788 			 * call to sd_reserve_release a reservation loss in the
25789 			 * window between pkt completion of reserve cmd and
25790 			 * mutex_enter below may not be recognized
25791 			 */
25792 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25793 			mutex_exit(SD_MUTEX(un));
25794 
25795 			if (sd_reserve_release(sd_mhreq->dev,
25796 			    SD_RESERVE) == 0) {
25797 				mutex_enter(SD_MUTEX(un));
25798 				un->un_resvd_status |= SD_RESERVE;
25799 				mutex_exit(SD_MUTEX(un));
25800 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25801 				    "sd_resv_reclaim_thread: "
25802 				    "Reservation Recovered\n");
25803 			} else {
25804 				mutex_enter(SD_MUTEX(un));
25805 				un->un_resvd_status |= SD_LOST_RESERVE;
25806 				mutex_exit(SD_MUTEX(un));
25807 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25808 				    "sd_resv_reclaim_thread: Failed "
25809 				    "Reservation Recovery\n");
25810 			}
25811 		} else {
25812 			mutex_exit(SD_MUTEX(un));
25813 		}
25814 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25815 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25816 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25817 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25818 		/*
25819 		 * wakeup the destroy thread if anyone is waiting on
25820 		 * us to complete.
25821 		 */
25822 		cv_signal(&sd_tr.srq_inprocess_cv);
25823 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25824 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25825 	}
25826 
25827 	/*
25828 	 * cleanup the sd_tr structure now that this thread will not exist
25829 	 */
25830 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25831 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25832 	sd_tr.srq_resv_reclaim_thread = NULL;
25833 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25834 	thread_exit();
25835 }
25836 
25837 
25838 /*
25839  *    Function: sd_rmv_resv_reclaim_req()
25840  *
25841  * Description: This function removes any pending reservation reclaim requests
25842  *		for the specified device.
25843  *
25844  *   Arguments: dev - the device 'dev_t'
25845  */
25846 
25847 static void
25848 sd_rmv_resv_reclaim_req(dev_t dev)
25849 {
25850 	struct sd_thr_request *sd_mhreq;
25851 	struct sd_thr_request *sd_prev;
25852 
25853 	/* Remove a reservation reclaim request from the list */
25854 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25855 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25856 		/*
25857 		 * We are attempting to reinstate reservation for
25858 		 * this device. We wait for sd_reserve_release()
25859 		 * to return before we return.
25860 		 */
25861 		cv_wait(&sd_tr.srq_inprocess_cv,
25862 		    &sd_tr.srq_resv_reclaim_mutex);
25863 	} else {
25864 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25865 		if (sd_mhreq && sd_mhreq->dev == dev) {
25866 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25867 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25868 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25869 			return;
25870 		}
25871 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25872 			if (sd_mhreq && sd_mhreq->dev == dev) {
25873 				break;
25874 			}
25875 			sd_prev = sd_mhreq;
25876 		}
25877 		if (sd_mhreq != NULL) {
25878 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25879 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25880 		}
25881 	}
25882 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25883 }
25884 
25885 
25886 /*
25887  *    Function: sd_mhd_reset_notify_cb()
25888  *
25889  * Description: This is a call back function for scsi_reset_notify. This
25890  *		function updates the softstate reserved status and logs the
25891  *		reset. The driver scsi watch facility callback function
25892  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25893  *		will reclaim the reservation.
25894  *
25895  *   Arguments: arg  - driver soft state (unit) structure
25896  */
25897 
25898 static void
25899 sd_mhd_reset_notify_cb(caddr_t arg)
25900 {
25901 	struct sd_lun *un = (struct sd_lun *)arg;
25902 
25903 	mutex_enter(SD_MUTEX(un));
25904 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25905 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25906 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25907 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25908 	}
25909 	mutex_exit(SD_MUTEX(un));
25910 }
25911 
25912 
25913 /*
25914  *    Function: sd_take_ownership()
25915  *
25916  * Description: This routine implements an algorithm to achieve a stable
25917  *		reservation on disks which don't implement priority reserve,
25918  *		and makes sure that other host lose re-reservation attempts.
25919  *		This algorithm contains of a loop that keeps issuing the RESERVE
25920  *		for some period of time (min_ownership_delay, default 6 seconds)
25921  *		During that loop, it looks to see if there has been a bus device
25922  *		reset or bus reset (both of which cause an existing reservation
25923  *		to be lost). If the reservation is lost issue RESERVE until a
25924  *		period of min_ownership_delay with no resets has gone by, or
25925  *		until max_ownership_delay has expired. This loop ensures that
25926  *		the host really did manage to reserve the device, in spite of
25927  *		resets. The looping for min_ownership_delay (default six
25928  *		seconds) is important to early generation clustering products,
25929  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25930  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25931  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25932  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25933  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25934  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25935  *		no longer "owns" the disk and will have panicked itself.  Thus,
25936  *		the host issuing the MHIOCTKOWN is assured (with timing
25937  *		dependencies) that by the time it actually starts to use the
25938  *		disk for real work, the old owner is no longer accessing it.
25939  *
25940  *		min_ownership_delay is the minimum amount of time for which the
25941  *		disk must be reserved continuously devoid of resets before the
25942  *		MHIOCTKOWN ioctl will return success.
25943  *
25944  *		max_ownership_delay indicates the amount of time by which the
25945  *		take ownership should succeed or timeout with an error.
25946  *
25947  *   Arguments: dev - the device 'dev_t'
25948  *		*p  - struct containing timing info.
25949  *
25950  * Return Code: 0 for success or error code
25951  */
25952 
25953 static int
25954 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25955 {
25956 	struct sd_lun	*un;
25957 	int		rval;
25958 	int		err;
25959 	int		reservation_count   = 0;
25960 	int		min_ownership_delay =  6000000; /* in usec */
25961 	int		max_ownership_delay = 30000000; /* in usec */
25962 	clock_t		start_time;	/* starting time of this algorithm */
25963 	clock_t		end_time;	/* time limit for giving up */
25964 	clock_t		ownership_time;	/* time limit for stable ownership */
25965 	clock_t		current_time;
25966 	clock_t		previous_current_time;
25967 
25968 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25969 		return (ENXIO);
25970 	}
25971 
25972 	/*
25973 	 * Attempt a device reservation. A priority reservation is requested.
25974 	 */
25975 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25976 	    != SD_SUCCESS) {
25977 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25978 		    "sd_take_ownership: return(1)=%d\n", rval);
25979 		return (rval);
25980 	}
25981 
25982 	/* Update the softstate reserved status to indicate the reservation */
25983 	mutex_enter(SD_MUTEX(un));
25984 	un->un_resvd_status |= SD_RESERVE;
25985 	un->un_resvd_status &=
25986 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25987 	mutex_exit(SD_MUTEX(un));
25988 
25989 	if (p != NULL) {
25990 		if (p->min_ownership_delay != 0) {
25991 			min_ownership_delay = p->min_ownership_delay * 1000;
25992 		}
25993 		if (p->max_ownership_delay != 0) {
25994 			max_ownership_delay = p->max_ownership_delay * 1000;
25995 		}
25996 	}
25997 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25998 	    "sd_take_ownership: min, max delays: %d, %d\n",
25999 	    min_ownership_delay, max_ownership_delay);
26000 
26001 	start_time = ddi_get_lbolt();
26002 	current_time	= start_time;
26003 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
26004 	end_time	= start_time + drv_usectohz(max_ownership_delay);
26005 
26006 	while (current_time - end_time < 0) {
26007 		delay(drv_usectohz(500000));
26008 
26009 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
26010 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
26011 				mutex_enter(SD_MUTEX(un));
26012 				rval = (un->un_resvd_status &
26013 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
26014 				mutex_exit(SD_MUTEX(un));
26015 				break;
26016 			}
26017 		}
26018 		previous_current_time = current_time;
26019 		current_time = ddi_get_lbolt();
26020 		mutex_enter(SD_MUTEX(un));
26021 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
26022 			ownership_time = ddi_get_lbolt() +
26023 			    drv_usectohz(min_ownership_delay);
26024 			reservation_count = 0;
26025 		} else {
26026 			reservation_count++;
26027 		}
26028 		un->un_resvd_status |= SD_RESERVE;
26029 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
26030 		mutex_exit(SD_MUTEX(un));
26031 
26032 		SD_INFO(SD_LOG_IOCTL_MHD, un,
26033 		    "sd_take_ownership: ticks for loop iteration=%ld, "
26034 		    "reservation=%s\n", (current_time - previous_current_time),
26035 		    reservation_count ? "ok" : "reclaimed");
26036 
26037 		if (current_time - ownership_time >= 0 &&
26038 		    reservation_count >= 4) {
26039 			rval = 0; /* Achieved a stable ownership */
26040 			break;
26041 		}
26042 		if (current_time - end_time >= 0) {
26043 			rval = EACCES; /* No ownership in max possible time */
26044 			break;
26045 		}
26046 	}
26047 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
26048 	    "sd_take_ownership: return(2)=%d\n", rval);
26049 	return (rval);
26050 }
26051 
26052 
26053 /*
26054  *    Function: sd_reserve_release()
26055  *
26056  * Description: This function builds and sends scsi RESERVE, RELEASE, and
26057  *		PRIORITY RESERVE commands based on a user specified command type
26058  *
26059  *   Arguments: dev - the device 'dev_t'
26060  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
26061  *		      SD_RESERVE, SD_RELEASE
26062  *
26063  * Return Code: 0 or Error Code
26064  */
26065 
26066 static int
26067 sd_reserve_release(dev_t dev, int cmd)
26068 {
26069 	struct uscsi_cmd	*com = NULL;
26070 	struct sd_lun		*un = NULL;
26071 	char			cdb[CDB_GROUP0];
26072 	int			rval;
26073 
26074 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
26075 	    (cmd == SD_PRIORITY_RESERVE));
26076 
26077 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26078 		return (ENXIO);
26079 	}
26080 
26081 	/* instantiate and initialize the command and cdb */
26082 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
26083 	bzero(cdb, CDB_GROUP0);
26084 	com->uscsi_flags   = USCSI_SILENT;
26085 	com->uscsi_timeout = un->un_reserve_release_time;
26086 	com->uscsi_cdblen  = CDB_GROUP0;
26087 	com->uscsi_cdb	   = cdb;
26088 	if (cmd == SD_RELEASE) {
26089 		cdb[0] = SCMD_RELEASE;
26090 	} else {
26091 		cdb[0] = SCMD_RESERVE;
26092 	}
26093 
26094 	/* Send the command. */
26095 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
26096 	    SD_PATH_STANDARD);
26097 
26098 	/*
26099 	 * "break" a reservation that is held by another host, by issuing a
26100 	 * reset if priority reserve is desired, and we could not get the
26101 	 * device.
26102 	 */
26103 	if ((cmd == SD_PRIORITY_RESERVE) &&
26104 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
26105 		/*
26106 		 * First try to reset the LUN. If we cannot, then try a target
26107 		 * reset, followed by a bus reset if the target reset fails.
26108 		 */
26109 		int reset_retval = 0;
26110 		if (un->un_f_lun_reset_enabled == TRUE) {
26111 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
26112 		}
26113 		if (reset_retval == 0) {
26114 			/* The LUN reset either failed or was not issued */
26115 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26116 		}
26117 		if ((reset_retval == 0) &&
26118 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
26119 			rval = EIO;
26120 			kmem_free(com, sizeof (*com));
26121 			return (rval);
26122 		}
26123 
26124 		bzero(com, sizeof (struct uscsi_cmd));
26125 		com->uscsi_flags   = USCSI_SILENT;
26126 		com->uscsi_cdb	   = cdb;
26127 		com->uscsi_cdblen  = CDB_GROUP0;
26128 		com->uscsi_timeout = 5;
26129 
26130 		/*
26131 		 * Reissue the last reserve command, this time without request
26132 		 * sense.  Assume that it is just a regular reserve command.
26133 		 */
26134 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
26135 		    SD_PATH_STANDARD);
26136 	}
26137 
26138 	/* Return an error if still getting a reservation conflict. */
26139 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
26140 		rval = EACCES;
26141 	}
26142 
26143 	kmem_free(com, sizeof (*com));
26144 	return (rval);
26145 }
26146 
26147 
26148 #define	SD_NDUMP_RETRIES	12
26149 /*
26150  *	System Crash Dump routine
26151  */
26152 
26153 static int
26154 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
26155 {
26156 	int		instance;
26157 	int		partition;
26158 	int		i;
26159 	int		err;
26160 	struct sd_lun	*un;
26161 	struct dk_map	*lp;
26162 	struct scsi_pkt *wr_pktp;
26163 	struct buf	*wr_bp;
26164 	struct buf	wr_buf;
26165 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
26166 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
26167 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
26168 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
26169 	size_t		io_start_offset;
26170 	int		doing_rmw = FALSE;
26171 	int		rval;
26172 #if defined(__i386) || defined(__amd64)
26173 	ssize_t dma_resid;
26174 	daddr_t oblkno;
26175 #endif
26176 
26177 	instance = SDUNIT(dev);
26178 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
26179 	    (!un->un_f_geometry_is_valid) || ISCD(un)) {
26180 		return (ENXIO);
26181 	}
26182 
26183 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
26184 
26185 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
26186 
26187 	partition = SDPART(dev);
26188 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
26189 
26190 	/* Validate blocks to dump at against partition size. */
26191 	lp = &un->un_map[partition];
26192 	if ((blkno + nblk) > lp->dkl_nblk) {
26193 		SD_TRACE(SD_LOG_DUMP, un,
26194 		    "sddump: dump range larger than partition: "
26195 		    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
26196 		    blkno, nblk, lp->dkl_nblk);
26197 		return (EINVAL);
26198 	}
26199 
26200 	mutex_enter(&un->un_pm_mutex);
26201 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
26202 		struct scsi_pkt *start_pktp;
26203 
26204 		mutex_exit(&un->un_pm_mutex);
26205 
26206 		/*
26207 		 * use pm framework to power on HBA 1st
26208 		 */
26209 		(void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON);
26210 
26211 		/*
26212 		 * Dump no long uses sdpower to power on a device, it's
26213 		 * in-line here so it can be done in polled mode.
26214 		 */
26215 
26216 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
26217 
26218 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
26219 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
26220 
26221 		if (start_pktp == NULL) {
26222 			/* We were not given a SCSI packet, fail. */
26223 			return (EIO);
26224 		}
26225 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
26226 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
26227 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
26228 		start_pktp->pkt_flags = FLAG_NOINTR;
26229 
26230 		mutex_enter(SD_MUTEX(un));
26231 		SD_FILL_SCSI1_LUN(un, start_pktp);
26232 		mutex_exit(SD_MUTEX(un));
26233 		/*
26234 		 * Scsi_poll returns 0 (success) if the command completes and
26235 		 * the status block is STATUS_GOOD.
26236 		 */
26237 		if (sd_scsi_poll(un, start_pktp) != 0) {
26238 			scsi_destroy_pkt(start_pktp);
26239 			return (EIO);
26240 		}
26241 		scsi_destroy_pkt(start_pktp);
26242 		(void) sd_ddi_pm_resume(un);
26243 	} else {
26244 		mutex_exit(&un->un_pm_mutex);
26245 	}
26246 
26247 	mutex_enter(SD_MUTEX(un));
26248 	un->un_throttle = 0;
26249 
26250 	/*
26251 	 * The first time through, reset the specific target device.
26252 	 * However, when cpr calls sddump we know that sd is in a
26253 	 * a good state so no bus reset is required.
26254 	 * Clear sense data via Request Sense cmd.
26255 	 * In sddump we don't care about allow_bus_device_reset anymore
26256 	 */
26257 
26258 	if ((un->un_state != SD_STATE_SUSPENDED) &&
26259 	    (un->un_state != SD_STATE_DUMPING)) {
26260 
26261 		New_state(un, SD_STATE_DUMPING);
26262 
26263 		if (un->un_f_is_fibre == FALSE) {
26264 			mutex_exit(SD_MUTEX(un));
26265 			/*
26266 			 * Attempt a bus reset for parallel scsi.
26267 			 *
26268 			 * Note: A bus reset is required because on some host
26269 			 * systems (i.e. E420R) a bus device reset is
26270 			 * insufficient to reset the state of the target.
26271 			 *
26272 			 * Note: Don't issue the reset for fibre-channel,
26273 			 * because this tends to hang the bus (loop) for
26274 			 * too long while everyone is logging out and in
26275 			 * and the deadman timer for dumping will fire
26276 			 * before the dump is complete.
26277 			 */
26278 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
26279 				mutex_enter(SD_MUTEX(un));
26280 				Restore_state(un);
26281 				mutex_exit(SD_MUTEX(un));
26282 				return (EIO);
26283 			}
26284 
26285 			/* Delay to give the device some recovery time. */
26286 			drv_usecwait(10000);
26287 
26288 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
26289 				SD_INFO(SD_LOG_DUMP, un,
26290 					"sddump: sd_send_polled_RQS failed\n");
26291 			}
26292 			mutex_enter(SD_MUTEX(un));
26293 		}
26294 	}
26295 
26296 	/*
26297 	 * Convert the partition-relative block number to a
26298 	 * disk physical block number.
26299 	 */
26300 	blkno += un->un_offset[partition];
26301 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
26302 
26303 
26304 	/*
26305 	 * Check if the device has a non-512 block size.
26306 	 */
26307 	wr_bp = NULL;
26308 	if (NOT_DEVBSIZE(un)) {
26309 		tgt_byte_offset = blkno * un->un_sys_blocksize;
26310 		tgt_byte_count = nblk * un->un_sys_blocksize;
26311 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
26312 		    (tgt_byte_count % un->un_tgt_blocksize)) {
26313 			doing_rmw = TRUE;
26314 			/*
26315 			 * Calculate the block number and number of block
26316 			 * in terms of the media block size.
26317 			 */
26318 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26319 			tgt_nblk =
26320 			    ((tgt_byte_offset + tgt_byte_count +
26321 				(un->un_tgt_blocksize - 1)) /
26322 				un->un_tgt_blocksize) - tgt_blkno;
26323 
26324 			/*
26325 			 * Invoke the routine which is going to do read part
26326 			 * of read-modify-write.
26327 			 * Note that this routine returns a pointer to
26328 			 * a valid bp in wr_bp.
26329 			 */
26330 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
26331 			    &wr_bp);
26332 			if (err) {
26333 				mutex_exit(SD_MUTEX(un));
26334 				return (err);
26335 			}
26336 			/*
26337 			 * Offset is being calculated as -
26338 			 * (original block # * system block size) -
26339 			 * (new block # * target block size)
26340 			 */
26341 			io_start_offset =
26342 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
26343 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
26344 
26345 			ASSERT((io_start_offset >= 0) &&
26346 			    (io_start_offset < un->un_tgt_blocksize));
26347 			/*
26348 			 * Do the modify portion of read modify write.
26349 			 */
26350 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
26351 			    (size_t)nblk * un->un_sys_blocksize);
26352 		} else {
26353 			doing_rmw = FALSE;
26354 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26355 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
26356 		}
26357 
26358 		/* Convert blkno and nblk to target blocks */
26359 		blkno = tgt_blkno;
26360 		nblk = tgt_nblk;
26361 	} else {
26362 		wr_bp = &wr_buf;
26363 		bzero(wr_bp, sizeof (struct buf));
26364 		wr_bp->b_flags		= B_BUSY;
26365 		wr_bp->b_un.b_addr	= addr;
26366 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
26367 		wr_bp->b_resid		= 0;
26368 	}
26369 
26370 	mutex_exit(SD_MUTEX(un));
26371 
26372 	/*
26373 	 * Obtain a SCSI packet for the write command.
26374 	 * It should be safe to call the allocator here without
26375 	 * worrying about being locked for DVMA mapping because
26376 	 * the address we're passed is already a DVMA mapping
26377 	 *
26378 	 * We are also not going to worry about semaphore ownership
26379 	 * in the dump buffer. Dumping is single threaded at present.
26380 	 */
26381 
26382 	wr_pktp = NULL;
26383 
26384 #if defined(__i386) || defined(__amd64)
26385 	dma_resid = wr_bp->b_bcount;
26386 	oblkno = blkno;
26387 	while (dma_resid != 0) {
26388 #endif
26389 
26390 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26391 		wr_bp->b_flags &= ~B_ERROR;
26392 
26393 #if defined(__i386) || defined(__amd64)
26394 		blkno = oblkno +
26395 			((wr_bp->b_bcount - dma_resid) /
26396 			    un->un_tgt_blocksize);
26397 		nblk = dma_resid / un->un_tgt_blocksize;
26398 
26399 		if (wr_pktp) {
26400 			/* Partial DMA transfers after initial transfer */
26401 			rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26402 			    blkno, nblk);
26403 		} else {
26404 			/* Initial transfer */
26405 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26406 			    un->un_pkt_flags, NULL_FUNC, NULL,
26407 			    blkno, nblk);
26408 		}
26409 #else
26410 		rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26411 		    0, NULL_FUNC, NULL, blkno, nblk);
26412 #endif
26413 
26414 		if (rval == 0) {
26415 			/* We were given a SCSI packet, continue. */
26416 			break;
26417 		}
26418 
26419 		if (i == 0) {
26420 			if (wr_bp->b_flags & B_ERROR) {
26421 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26422 				    "no resources for dumping; "
26423 				    "error code: 0x%x, retrying",
26424 				    geterror(wr_bp));
26425 			} else {
26426 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26427 				    "no resources for dumping; retrying");
26428 			}
26429 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
26430 			if (wr_bp->b_flags & B_ERROR) {
26431 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26432 				    "no resources for dumping; error code: "
26433 				    "0x%x, retrying\n", geterror(wr_bp));
26434 			}
26435 		} else {
26436 			if (wr_bp->b_flags & B_ERROR) {
26437 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26438 				    "no resources for dumping; "
26439 				    "error code: 0x%x, retries failed, "
26440 				    "giving up.\n", geterror(wr_bp));
26441 			} else {
26442 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26443 				    "no resources for dumping; "
26444 				    "retries failed, giving up.\n");
26445 			}
26446 			mutex_enter(SD_MUTEX(un));
26447 			Restore_state(un);
26448 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26449 				mutex_exit(SD_MUTEX(un));
26450 				scsi_free_consistent_buf(wr_bp);
26451 			} else {
26452 				mutex_exit(SD_MUTEX(un));
26453 			}
26454 			return (EIO);
26455 		}
26456 		drv_usecwait(10000);
26457 	}
26458 
26459 #if defined(__i386) || defined(__amd64)
26460 	/*
26461 	 * save the resid from PARTIAL_DMA
26462 	 */
26463 	dma_resid = wr_pktp->pkt_resid;
26464 	if (dma_resid != 0)
26465 		nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26466 	wr_pktp->pkt_resid = 0;
26467 #endif
26468 
26469 	/* SunBug 1222170 */
26470 	wr_pktp->pkt_flags = FLAG_NOINTR;
26471 
26472 	err = EIO;
26473 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26474 
26475 		/*
26476 		 * Scsi_poll returns 0 (success) if the command completes and
26477 		 * the status block is STATUS_GOOD.  We should only check
26478 		 * errors if this condition is not true.  Even then we should
26479 		 * send our own request sense packet only if we have a check
26480 		 * condition and auto request sense has not been performed by
26481 		 * the hba.
26482 		 */
26483 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26484 
26485 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26486 		    (wr_pktp->pkt_resid == 0)) {
26487 			err = SD_SUCCESS;
26488 			break;
26489 		}
26490 
26491 		/*
26492 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
26493 		 */
26494 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26495 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26496 			    "Device is gone\n");
26497 			break;
26498 		}
26499 
26500 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26501 			SD_INFO(SD_LOG_DUMP, un,
26502 			    "sddump: write failed with CHECK, try # %d\n", i);
26503 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26504 				(void) sd_send_polled_RQS(un);
26505 			}
26506 
26507 			continue;
26508 		}
26509 
26510 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26511 			int reset_retval = 0;
26512 
26513 			SD_INFO(SD_LOG_DUMP, un,
26514 			    "sddump: write failed with BUSY, try # %d\n", i);
26515 
26516 			if (un->un_f_lun_reset_enabled == TRUE) {
26517 				reset_retval = scsi_reset(SD_ADDRESS(un),
26518 				    RESET_LUN);
26519 			}
26520 			if (reset_retval == 0) {
26521 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26522 			}
26523 			(void) sd_send_polled_RQS(un);
26524 
26525 		} else {
26526 			SD_INFO(SD_LOG_DUMP, un,
26527 			    "sddump: write failed with 0x%x, try # %d\n",
26528 			    SD_GET_PKT_STATUS(wr_pktp), i);
26529 			mutex_enter(SD_MUTEX(un));
26530 			sd_reset_target(un, wr_pktp);
26531 			mutex_exit(SD_MUTEX(un));
26532 		}
26533 
26534 		/*
26535 		 * If we are not getting anywhere with lun/target resets,
26536 		 * let's reset the bus.
26537 		 */
26538 		if (i == SD_NDUMP_RETRIES/2) {
26539 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26540 			(void) sd_send_polled_RQS(un);
26541 		}
26542 
26543 	}
26544 #if defined(__i386) || defined(__amd64)
26545 	}	/* dma_resid */
26546 #endif
26547 
26548 	scsi_destroy_pkt(wr_pktp);
26549 	mutex_enter(SD_MUTEX(un));
26550 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26551 		mutex_exit(SD_MUTEX(un));
26552 		scsi_free_consistent_buf(wr_bp);
26553 	} else {
26554 		mutex_exit(SD_MUTEX(un));
26555 	}
26556 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26557 	return (err);
26558 }
26559 
26560 /*
26561  *    Function: sd_scsi_poll()
26562  *
26563  * Description: This is a wrapper for the scsi_poll call.
26564  *
26565  *   Arguments: sd_lun - The unit structure
26566  *              scsi_pkt - The scsi packet being sent to the device.
26567  *
26568  * Return Code: 0 - Command completed successfully with good status
26569  *             -1 - Command failed.  This could indicate a check condition
26570  *                  or other status value requiring recovery action.
26571  *
26572  */
26573 
26574 static int
26575 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26576 {
26577 	int status;
26578 
26579 	ASSERT(un != NULL);
26580 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26581 	ASSERT(pktp != NULL);
26582 
26583 	status = SD_SUCCESS;
26584 
26585 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26586 		pktp->pkt_flags |= un->un_tagflags;
26587 		pktp->pkt_flags &= ~FLAG_NODISCON;
26588 	}
26589 
26590 	status = sd_ddi_scsi_poll(pktp);
26591 	/*
26592 	 * Scsi_poll returns 0 (success) if the command completes and the
26593 	 * status block is STATUS_GOOD.  We should only check errors if this
26594 	 * condition is not true.  Even then we should send our own request
26595 	 * sense packet only if we have a check condition and auto
26596 	 * request sense has not been performed by the hba.
26597 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26598 	 */
26599 	if ((status != SD_SUCCESS) &&
26600 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26601 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26602 	    (pktp->pkt_reason != CMD_DEV_GONE))
26603 		(void) sd_send_polled_RQS(un);
26604 
26605 	return (status);
26606 }
26607 
26608 /*
26609  *    Function: sd_send_polled_RQS()
26610  *
26611  * Description: This sends the request sense command to a device.
26612  *
26613  *   Arguments: sd_lun - The unit structure
26614  *
26615  * Return Code: 0 - Command completed successfully with good status
26616  *             -1 - Command failed.
26617  *
26618  */
26619 
26620 static int
26621 sd_send_polled_RQS(struct sd_lun *un)
26622 {
26623 	int	ret_val;
26624 	struct	scsi_pkt	*rqs_pktp;
26625 	struct	buf		*rqs_bp;
26626 
26627 	ASSERT(un != NULL);
26628 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26629 
26630 	ret_val = SD_SUCCESS;
26631 
26632 	rqs_pktp = un->un_rqs_pktp;
26633 	rqs_bp	 = un->un_rqs_bp;
26634 
26635 	mutex_enter(SD_MUTEX(un));
26636 
26637 	if (un->un_sense_isbusy) {
26638 		ret_val = SD_FAILURE;
26639 		mutex_exit(SD_MUTEX(un));
26640 		return (ret_val);
26641 	}
26642 
26643 	/*
26644 	 * If the request sense buffer (and packet) is not in use,
26645 	 * let's set the un_sense_isbusy and send our packet
26646 	 */
26647 	un->un_sense_isbusy 	= 1;
26648 	rqs_pktp->pkt_resid  	= 0;
26649 	rqs_pktp->pkt_reason 	= 0;
26650 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
26651 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26652 
26653 	mutex_exit(SD_MUTEX(un));
26654 
26655 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26656 	    " 0x%p\n", rqs_bp->b_un.b_addr);
26657 
26658 	/*
26659 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26660 	 * axle - it has a call into us!
26661 	 */
26662 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26663 		SD_INFO(SD_LOG_COMMON, un,
26664 		    "sd_send_polled_RQS: RQS failed\n");
26665 	}
26666 
26667 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26668 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26669 
26670 	mutex_enter(SD_MUTEX(un));
26671 	un->un_sense_isbusy = 0;
26672 	mutex_exit(SD_MUTEX(un));
26673 
26674 	return (ret_val);
26675 }
26676 
26677 /*
26678  * Defines needed for localized version of the scsi_poll routine.
26679  */
26680 #define	SD_CSEC		10000			/* usecs */
26681 #define	SD_SEC_TO_CSEC	(1000000/SD_CSEC)
26682 
26683 
26684 /*
26685  *    Function: sd_ddi_scsi_poll()
26686  *
26687  * Description: Localized version of the scsi_poll routine.  The purpose is to
26688  *		send a scsi_pkt to a device as a polled command.  This version
26689  *		is to ensure more robust handling of transport errors.
26690  *		Specifically this routine cures not ready, coming ready
26691  *		transition for power up and reset of sonoma's.  This can take
26692  *		up to 45 seconds for power-on and 20 seconds for reset of a
26693  * 		sonoma lun.
26694  *
26695  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26696  *
26697  * Return Code: 0 - Command completed successfully with good status
26698  *             -1 - Command failed.
26699  *
26700  */
26701 
26702 static int
26703 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26704 {
26705 	int busy_count;
26706 	int timeout;
26707 	int rval = SD_FAILURE;
26708 	int savef;
26709 	uint8_t *sensep;
26710 	long savet;
26711 	void (*savec)();
26712 	/*
26713 	 * The following is defined in machdep.c and is used in determining if
26714 	 * the scsi transport system will do polled I/O instead of interrupt
26715 	 * I/O when called from xx_dump().
26716 	 */
26717 	extern int do_polled_io;
26718 
26719 	/*
26720 	 * save old flags in pkt, to restore at end
26721 	 */
26722 	savef = pkt->pkt_flags;
26723 	savec = pkt->pkt_comp;
26724 	savet = pkt->pkt_time;
26725 
26726 	pkt->pkt_flags |= FLAG_NOINTR;
26727 
26728 	/*
26729 	 * XXX there is nothing in the SCSA spec that states that we should not
26730 	 * do a callback for polled cmds; however, removing this will break sd
26731 	 * and probably other target drivers
26732 	 */
26733 	pkt->pkt_comp = NULL;
26734 
26735 	/*
26736 	 * we don't like a polled command without timeout.
26737 	 * 60 seconds seems long enough.
26738 	 */
26739 	if (pkt->pkt_time == 0) {
26740 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
26741 	}
26742 
26743 	/*
26744 	 * Send polled cmd.
26745 	 *
26746 	 * We do some error recovery for various errors.  Tran_busy,
26747 	 * queue full, and non-dispatched commands are retried every 10 msec.
26748 	 * as they are typically transient failures.  Busy status and Not
26749 	 * Ready are retried every second as this status takes a while to
26750 	 * change.  Unit attention is retried for pkt_time (60) times
26751 	 * with no delay.
26752 	 */
26753 	timeout = pkt->pkt_time * SD_SEC_TO_CSEC;
26754 
26755 	for (busy_count = 0; busy_count < timeout; busy_count++) {
26756 		int rc;
26757 		int poll_delay;
26758 
26759 		/*
26760 		 * Initialize pkt status variables.
26761 		 */
26762 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26763 
26764 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26765 			if (rc != TRAN_BUSY) {
26766 				/* Transport failed - give up. */
26767 				break;
26768 			} else {
26769 				/* Transport busy - try again. */
26770 				poll_delay = 1 * SD_CSEC; /* 10 msec */
26771 			}
26772 		} else {
26773 			/*
26774 			 * Transport accepted - check pkt status.
26775 			 */
26776 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
26777 			if (pkt->pkt_reason == CMD_CMPLT &&
26778 			    rc == STATUS_CHECK &&
26779 			    pkt->pkt_state & STATE_ARQ_DONE) {
26780 				struct scsi_arq_status *arqstat =
26781 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26782 
26783 				sensep = (uint8_t *)&arqstat->sts_sensedata;
26784 			} else {
26785 				sensep = NULL;
26786 			}
26787 
26788 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26789 			    (rc == STATUS_GOOD)) {
26790 				/* No error - we're done */
26791 				rval = SD_SUCCESS;
26792 				break;
26793 
26794 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26795 				/* Lost connection - give up */
26796 				break;
26797 
26798 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26799 			    (pkt->pkt_state == 0)) {
26800 				/* Pkt not dispatched - try again. */
26801 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
26802 
26803 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26804 			    (rc == STATUS_QFULL)) {
26805 				/* Queue full - try again. */
26806 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
26807 
26808 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26809 			    (rc == STATUS_BUSY)) {
26810 				/* Busy - try again. */
26811 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
26812 				busy_count += (SD_SEC_TO_CSEC - 1);
26813 
26814 			} else if ((sensep != NULL) &&
26815 			    (scsi_sense_key(sensep) ==
26816 				KEY_UNIT_ATTENTION)) {
26817 				/* Unit Attention - try again */
26818 				busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */
26819 				continue;
26820 
26821 			} else if ((sensep != NULL) &&
26822 			    (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26823 			    (scsi_sense_asc(sensep) == 0x04) &&
26824 			    (scsi_sense_ascq(sensep) == 0x01)) {
26825 				/* Not ready -> ready - try again. */
26826 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
26827 				busy_count += (SD_SEC_TO_CSEC - 1);
26828 
26829 			} else {
26830 				/* BAD status - give up. */
26831 				break;
26832 			}
26833 		}
26834 
26835 		if ((curthread->t_flag & T_INTR_THREAD) == 0 &&
26836 		    !do_polled_io) {
26837 			delay(drv_usectohz(poll_delay));
26838 		} else {
26839 			/* we busy wait during cpr_dump or interrupt threads */
26840 			drv_usecwait(poll_delay);
26841 		}
26842 	}
26843 
26844 	pkt->pkt_flags = savef;
26845 	pkt->pkt_comp = savec;
26846 	pkt->pkt_time = savet;
26847 	return (rval);
26848 }
26849 
26850 
26851 /*
26852  *    Function: sd_persistent_reservation_in_read_keys
26853  *
26854  * Description: This routine is the driver entry point for handling CD-ROM
26855  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26856  *		by sending the SCSI-3 PRIN commands to the device.
26857  *		Processes the read keys command response by copying the
26858  *		reservation key information into the user provided buffer.
26859  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26860  *
26861  *   Arguments: un   -  Pointer to soft state struct for the target.
26862  *		usrp -	user provided pointer to multihost Persistent In Read
26863  *			Keys structure (mhioc_inkeys_t)
26864  *		flag -	this argument is a pass through to ddi_copyxxx()
26865  *			directly from the mode argument of ioctl().
26866  *
26867  * Return Code: 0   - Success
26868  *		EACCES
26869  *		ENOTSUP
26870  *		errno return code from sd_send_scsi_cmd()
26871  *
26872  *     Context: Can sleep. Does not return until command is completed.
26873  */
26874 
26875 static int
26876 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26877     mhioc_inkeys_t *usrp, int flag)
26878 {
26879 #ifdef _MULTI_DATAMODEL
26880 	struct mhioc_key_list32	li32;
26881 #endif
26882 	sd_prin_readkeys_t	*in;
26883 	mhioc_inkeys_t		*ptr;
26884 	mhioc_key_list_t	li;
26885 	uchar_t			*data_bufp;
26886 	int 			data_len;
26887 	int			rval;
26888 	size_t			copysz;
26889 
26890 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26891 		return (EINVAL);
26892 	}
26893 	bzero(&li, sizeof (mhioc_key_list_t));
26894 
26895 	/*
26896 	 * Get the listsize from user
26897 	 */
26898 #ifdef _MULTI_DATAMODEL
26899 
26900 	switch (ddi_model_convert_from(flag & FMODELS)) {
26901 	case DDI_MODEL_ILP32:
26902 		copysz = sizeof (struct mhioc_key_list32);
26903 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26904 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26905 			    "sd_persistent_reservation_in_read_keys: "
26906 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26907 			rval = EFAULT;
26908 			goto done;
26909 		}
26910 		li.listsize = li32.listsize;
26911 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26912 		break;
26913 
26914 	case DDI_MODEL_NONE:
26915 		copysz = sizeof (mhioc_key_list_t);
26916 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26917 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26918 			    "sd_persistent_reservation_in_read_keys: "
26919 			    "failed ddi_copyin: mhioc_key_list_t\n");
26920 			rval = EFAULT;
26921 			goto done;
26922 		}
26923 		break;
26924 	}
26925 
26926 #else /* ! _MULTI_DATAMODEL */
26927 	copysz = sizeof (mhioc_key_list_t);
26928 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26929 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26930 		    "sd_persistent_reservation_in_read_keys: "
26931 		    "failed ddi_copyin: mhioc_key_list_t\n");
26932 		rval = EFAULT;
26933 		goto done;
26934 	}
26935 #endif
26936 
26937 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26938 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26939 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26940 
26941 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS,
26942 	    data_len, data_bufp)) != 0) {
26943 		goto done;
26944 	}
26945 	in = (sd_prin_readkeys_t *)data_bufp;
26946 	ptr->generation = BE_32(in->generation);
26947 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26948 
26949 	/*
26950 	 * Return the min(listsize, listlen) keys
26951 	 */
26952 #ifdef _MULTI_DATAMODEL
26953 
26954 	switch (ddi_model_convert_from(flag & FMODELS)) {
26955 	case DDI_MODEL_ILP32:
26956 		li32.listlen = li.listlen;
26957 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26958 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26959 			    "sd_persistent_reservation_in_read_keys: "
26960 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26961 			rval = EFAULT;
26962 			goto done;
26963 		}
26964 		break;
26965 
26966 	case DDI_MODEL_NONE:
26967 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26968 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26969 			    "sd_persistent_reservation_in_read_keys: "
26970 			    "failed ddi_copyout: mhioc_key_list_t\n");
26971 			rval = EFAULT;
26972 			goto done;
26973 		}
26974 		break;
26975 	}
26976 
26977 #else /* ! _MULTI_DATAMODEL */
26978 
26979 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26980 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26981 		    "sd_persistent_reservation_in_read_keys: "
26982 		    "failed ddi_copyout: mhioc_key_list_t\n");
26983 		rval = EFAULT;
26984 		goto done;
26985 	}
26986 
26987 #endif /* _MULTI_DATAMODEL */
26988 
26989 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26990 	    li.listsize * MHIOC_RESV_KEY_SIZE);
26991 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26992 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26993 		    "sd_persistent_reservation_in_read_keys: "
26994 		    "failed ddi_copyout: keylist\n");
26995 		rval = EFAULT;
26996 	}
26997 done:
26998 	kmem_free(data_bufp, data_len);
26999 	return (rval);
27000 }
27001 
27002 
27003 /*
27004  *    Function: sd_persistent_reservation_in_read_resv
27005  *
27006  * Description: This routine is the driver entry point for handling CD-ROM
27007  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
27008  *		by sending the SCSI-3 PRIN commands to the device.
27009  *		Process the read persistent reservations command response by
27010  *		copying the reservation information into the user provided
27011  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
27012  *
27013  *   Arguments: un   -  Pointer to soft state struct for the target.
27014  *		usrp -	user provided pointer to multihost Persistent In Read
27015  *			Keys structure (mhioc_inkeys_t)
27016  *		flag -	this argument is a pass through to ddi_copyxxx()
27017  *			directly from the mode argument of ioctl().
27018  *
27019  * Return Code: 0   - Success
27020  *		EACCES
27021  *		ENOTSUP
27022  *		errno return code from sd_send_scsi_cmd()
27023  *
27024  *     Context: Can sleep. Does not return until command is completed.
27025  */
27026 
27027 static int
27028 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
27029     mhioc_inresvs_t *usrp, int flag)
27030 {
27031 #ifdef _MULTI_DATAMODEL
27032 	struct mhioc_resv_desc_list32 resvlist32;
27033 #endif
27034 	sd_prin_readresv_t	*in;
27035 	mhioc_inresvs_t		*ptr;
27036 	sd_readresv_desc_t	*readresv_ptr;
27037 	mhioc_resv_desc_list_t	resvlist;
27038 	mhioc_resv_desc_t 	resvdesc;
27039 	uchar_t			*data_bufp;
27040 	int 			data_len;
27041 	int			rval;
27042 	int			i;
27043 	size_t			copysz;
27044 	mhioc_resv_desc_t	*bufp;
27045 
27046 	if ((ptr = usrp) == NULL) {
27047 		return (EINVAL);
27048 	}
27049 
27050 	/*
27051 	 * Get the listsize from user
27052 	 */
27053 #ifdef _MULTI_DATAMODEL
27054 	switch (ddi_model_convert_from(flag & FMODELS)) {
27055 	case DDI_MODEL_ILP32:
27056 		copysz = sizeof (struct mhioc_resv_desc_list32);
27057 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
27058 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27059 			    "sd_persistent_reservation_in_read_resv: "
27060 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
27061 			rval = EFAULT;
27062 			goto done;
27063 		}
27064 		resvlist.listsize = resvlist32.listsize;
27065 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
27066 		break;
27067 
27068 	case DDI_MODEL_NONE:
27069 		copysz = sizeof (mhioc_resv_desc_list_t);
27070 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
27071 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27072 			    "sd_persistent_reservation_in_read_resv: "
27073 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
27074 			rval = EFAULT;
27075 			goto done;
27076 		}
27077 		break;
27078 	}
27079 #else /* ! _MULTI_DATAMODEL */
27080 	copysz = sizeof (mhioc_resv_desc_list_t);
27081 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
27082 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27083 		    "sd_persistent_reservation_in_read_resv: "
27084 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
27085 		rval = EFAULT;
27086 		goto done;
27087 	}
27088 #endif /* ! _MULTI_DATAMODEL */
27089 
27090 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
27091 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
27092 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
27093 
27094 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV,
27095 	    data_len, data_bufp)) != 0) {
27096 		goto done;
27097 	}
27098 	in = (sd_prin_readresv_t *)data_bufp;
27099 	ptr->generation = BE_32(in->generation);
27100 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
27101 
27102 	/*
27103 	 * Return the min(listsize, listlen( keys
27104 	 */
27105 #ifdef _MULTI_DATAMODEL
27106 
27107 	switch (ddi_model_convert_from(flag & FMODELS)) {
27108 	case DDI_MODEL_ILP32:
27109 		resvlist32.listlen = resvlist.listlen;
27110 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
27111 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27112 			    "sd_persistent_reservation_in_read_resv: "
27113 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27114 			rval = EFAULT;
27115 			goto done;
27116 		}
27117 		break;
27118 
27119 	case DDI_MODEL_NONE:
27120 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
27121 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27122 			    "sd_persistent_reservation_in_read_resv: "
27123 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27124 			rval = EFAULT;
27125 			goto done;
27126 		}
27127 		break;
27128 	}
27129 
27130 #else /* ! _MULTI_DATAMODEL */
27131 
27132 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
27133 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27134 		    "sd_persistent_reservation_in_read_resv: "
27135 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27136 		rval = EFAULT;
27137 		goto done;
27138 	}
27139 
27140 #endif /* ! _MULTI_DATAMODEL */
27141 
27142 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
27143 	bufp = resvlist.list;
27144 	copysz = sizeof (mhioc_resv_desc_t);
27145 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
27146 	    i++, readresv_ptr++, bufp++) {
27147 
27148 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
27149 		    MHIOC_RESV_KEY_SIZE);
27150 		resvdesc.type  = readresv_ptr->type;
27151 		resvdesc.scope = readresv_ptr->scope;
27152 		resvdesc.scope_specific_addr =
27153 		    BE_32(readresv_ptr->scope_specific_addr);
27154 
27155 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
27156 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27157 			    "sd_persistent_reservation_in_read_resv: "
27158 			    "failed ddi_copyout: resvlist\n");
27159 			rval = EFAULT;
27160 			goto done;
27161 		}
27162 	}
27163 done:
27164 	kmem_free(data_bufp, data_len);
27165 	return (rval);
27166 }
27167 
27168 
27169 /*
27170  *    Function: sr_change_blkmode()
27171  *
27172  * Description: This routine is the driver entry point for handling CD-ROM
27173  *		block mode ioctl requests. Support for returning and changing
27174  *		the current block size in use by the device is implemented. The
27175  *		LBA size is changed via a MODE SELECT Block Descriptor.
27176  *
27177  *		This routine issues a mode sense with an allocation length of
27178  *		12 bytes for the mode page header and a single block descriptor.
27179  *
27180  *   Arguments: dev - the device 'dev_t'
27181  *		cmd - the request type; one of CDROMGBLKMODE (get) or
27182  *		      CDROMSBLKMODE (set)
27183  *		data - current block size or requested block size
27184  *		flag - this argument is a pass through to ddi_copyxxx() directly
27185  *		       from the mode argument of ioctl().
27186  *
27187  * Return Code: the code returned by sd_send_scsi_cmd()
27188  *		EINVAL if invalid arguments are provided
27189  *		EFAULT if ddi_copyxxx() fails
27190  *		ENXIO if fail ddi_get_soft_state
27191  *		EIO if invalid mode sense block descriptor length
27192  *
27193  */
27194 
27195 static int
27196 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
27197 {
27198 	struct sd_lun			*un = NULL;
27199 	struct mode_header		*sense_mhp, *select_mhp;
27200 	struct block_descriptor		*sense_desc, *select_desc;
27201 	int				current_bsize;
27202 	int				rval = EINVAL;
27203 	uchar_t				*sense = NULL;
27204 	uchar_t				*select = NULL;
27205 
27206 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
27207 
27208 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27209 		return (ENXIO);
27210 	}
27211 
27212 	/*
27213 	 * The block length is changed via the Mode Select block descriptor, the
27214 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
27215 	 * required as part of this routine. Therefore the mode sense allocation
27216 	 * length is specified to be the length of a mode page header and a
27217 	 * block descriptor.
27218 	 */
27219 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27220 
27221 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
27222 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) {
27223 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27224 		    "sr_change_blkmode: Mode Sense Failed\n");
27225 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27226 		return (rval);
27227 	}
27228 
27229 	/* Check the block descriptor len to handle only 1 block descriptor */
27230 	sense_mhp = (struct mode_header *)sense;
27231 	if ((sense_mhp->bdesc_length == 0) ||
27232 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
27233 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27234 		    "sr_change_blkmode: Mode Sense returned invalid block"
27235 		    " descriptor length\n");
27236 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27237 		return (EIO);
27238 	}
27239 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
27240 	current_bsize = ((sense_desc->blksize_hi << 16) |
27241 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
27242 
27243 	/* Process command */
27244 	switch (cmd) {
27245 	case CDROMGBLKMODE:
27246 		/* Return the block size obtained during the mode sense */
27247 		if (ddi_copyout(&current_bsize, (void *)data,
27248 		    sizeof (int), flag) != 0)
27249 			rval = EFAULT;
27250 		break;
27251 	case CDROMSBLKMODE:
27252 		/* Validate the requested block size */
27253 		switch (data) {
27254 		case CDROM_BLK_512:
27255 		case CDROM_BLK_1024:
27256 		case CDROM_BLK_2048:
27257 		case CDROM_BLK_2056:
27258 		case CDROM_BLK_2336:
27259 		case CDROM_BLK_2340:
27260 		case CDROM_BLK_2352:
27261 		case CDROM_BLK_2368:
27262 		case CDROM_BLK_2448:
27263 		case CDROM_BLK_2646:
27264 		case CDROM_BLK_2647:
27265 			break;
27266 		default:
27267 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27268 			    "sr_change_blkmode: "
27269 			    "Block Size '%ld' Not Supported\n", data);
27270 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27271 			return (EINVAL);
27272 		}
27273 
27274 		/*
27275 		 * The current block size matches the requested block size so
27276 		 * there is no need to send the mode select to change the size
27277 		 */
27278 		if (current_bsize == data) {
27279 			break;
27280 		}
27281 
27282 		/* Build the select data for the requested block size */
27283 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27284 		select_mhp = (struct mode_header *)select;
27285 		select_desc =
27286 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
27287 		/*
27288 		 * The LBA size is changed via the block descriptor, so the
27289 		 * descriptor is built according to the user data
27290 		 */
27291 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
27292 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
27293 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
27294 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
27295 
27296 		/* Send the mode select for the requested block size */
27297 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
27298 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27299 		    SD_PATH_STANDARD)) != 0) {
27300 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27301 			    "sr_change_blkmode: Mode Select Failed\n");
27302 			/*
27303 			 * The mode select failed for the requested block size,
27304 			 * so reset the data for the original block size and
27305 			 * send it to the target. The error is indicated by the
27306 			 * return value for the failed mode select.
27307 			 */
27308 			select_desc->blksize_hi  = sense_desc->blksize_hi;
27309 			select_desc->blksize_mid = sense_desc->blksize_mid;
27310 			select_desc->blksize_lo  = sense_desc->blksize_lo;
27311 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
27312 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27313 			    SD_PATH_STANDARD);
27314 		} else {
27315 			ASSERT(!mutex_owned(SD_MUTEX(un)));
27316 			mutex_enter(SD_MUTEX(un));
27317 			sd_update_block_info(un, (uint32_t)data, 0);
27318 
27319 			mutex_exit(SD_MUTEX(un));
27320 		}
27321 		break;
27322 	default:
27323 		/* should not reach here, but check anyway */
27324 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27325 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
27326 		rval = EINVAL;
27327 		break;
27328 	}
27329 
27330 	if (select) {
27331 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
27332 	}
27333 	if (sense) {
27334 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27335 	}
27336 	return (rval);
27337 }
27338 
27339 
27340 /*
27341  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27342  * implement driver support for getting and setting the CD speed. The command
27343  * set used will be based on the device type. If the device has not been
27344  * identified as MMC the Toshiba vendor specific mode page will be used. If
27345  * the device is MMC but does not support the Real Time Streaming feature
27346  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27347  * be used to read the speed.
27348  */
27349 
27350 /*
27351  *    Function: sr_change_speed()
27352  *
27353  * Description: This routine is the driver entry point for handling CD-ROM
27354  *		drive speed ioctl requests for devices supporting the Toshiba
27355  *		vendor specific drive speed mode page. Support for returning
27356  *		and changing the current drive speed in use by the device is
27357  *		implemented.
27358  *
27359  *   Arguments: dev - the device 'dev_t'
27360  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
27361  *		      CDROMSDRVSPEED (set)
27362  *		data - current drive speed or requested drive speed
27363  *		flag - this argument is a pass through to ddi_copyxxx() directly
27364  *		       from the mode argument of ioctl().
27365  *
27366  * Return Code: the code returned by sd_send_scsi_cmd()
27367  *		EINVAL if invalid arguments are provided
27368  *		EFAULT if ddi_copyxxx() fails
27369  *		ENXIO if fail ddi_get_soft_state
27370  *		EIO if invalid mode sense block descriptor length
27371  */
27372 
27373 static int
27374 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27375 {
27376 	struct sd_lun			*un = NULL;
27377 	struct mode_header		*sense_mhp, *select_mhp;
27378 	struct mode_speed		*sense_page, *select_page;
27379 	int				current_speed;
27380 	int				rval = EINVAL;
27381 	int				bd_len;
27382 	uchar_t				*sense = NULL;
27383 	uchar_t				*select = NULL;
27384 
27385 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27386 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27387 		return (ENXIO);
27388 	}
27389 
27390 	/*
27391 	 * Note: The drive speed is being modified here according to a Toshiba
27392 	 * vendor specific mode page (0x31).
27393 	 */
27394 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27395 
27396 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
27397 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27398 		SD_PATH_STANDARD)) != 0) {
27399 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27400 		    "sr_change_speed: Mode Sense Failed\n");
27401 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27402 		return (rval);
27403 	}
27404 	sense_mhp  = (struct mode_header *)sense;
27405 
27406 	/* Check the block descriptor len to handle only 1 block descriptor */
27407 	bd_len = sense_mhp->bdesc_length;
27408 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27409 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27410 		    "sr_change_speed: Mode Sense returned invalid block "
27411 		    "descriptor length\n");
27412 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27413 		return (EIO);
27414 	}
27415 
27416 	sense_page = (struct mode_speed *)
27417 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27418 	current_speed = sense_page->speed;
27419 
27420 	/* Process command */
27421 	switch (cmd) {
27422 	case CDROMGDRVSPEED:
27423 		/* Return the drive speed obtained during the mode sense */
27424 		if (current_speed == 0x2) {
27425 			current_speed = CDROM_TWELVE_SPEED;
27426 		}
27427 		if (ddi_copyout(&current_speed, (void *)data,
27428 		    sizeof (int), flag) != 0) {
27429 			rval = EFAULT;
27430 		}
27431 		break;
27432 	case CDROMSDRVSPEED:
27433 		/* Validate the requested drive speed */
27434 		switch ((uchar_t)data) {
27435 		case CDROM_TWELVE_SPEED:
27436 			data = 0x2;
27437 			/*FALLTHROUGH*/
27438 		case CDROM_NORMAL_SPEED:
27439 		case CDROM_DOUBLE_SPEED:
27440 		case CDROM_QUAD_SPEED:
27441 		case CDROM_MAXIMUM_SPEED:
27442 			break;
27443 		default:
27444 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27445 			    "sr_change_speed: "
27446 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27447 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27448 			return (EINVAL);
27449 		}
27450 
27451 		/*
27452 		 * The current drive speed matches the requested drive speed so
27453 		 * there is no need to send the mode select to change the speed
27454 		 */
27455 		if (current_speed == data) {
27456 			break;
27457 		}
27458 
27459 		/* Build the select data for the requested drive speed */
27460 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27461 		select_mhp = (struct mode_header *)select;
27462 		select_mhp->bdesc_length = 0;
27463 		select_page =
27464 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27465 		select_page =
27466 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27467 		select_page->mode_page.code = CDROM_MODE_SPEED;
27468 		select_page->mode_page.length = 2;
27469 		select_page->speed = (uchar_t)data;
27470 
27471 		/* Send the mode select for the requested block size */
27472 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
27473 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27474 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
27475 			/*
27476 			 * The mode select failed for the requested drive speed,
27477 			 * so reset the data for the original drive speed and
27478 			 * send it to the target. The error is indicated by the
27479 			 * return value for the failed mode select.
27480 			 */
27481 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27482 			    "sr_drive_speed: Mode Select Failed\n");
27483 			select_page->speed = sense_page->speed;
27484 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
27485 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27486 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27487 		}
27488 		break;
27489 	default:
27490 		/* should not reach here, but check anyway */
27491 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27492 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27493 		rval = EINVAL;
27494 		break;
27495 	}
27496 
27497 	if (select) {
27498 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27499 	}
27500 	if (sense) {
27501 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27502 	}
27503 
27504 	return (rval);
27505 }
27506 
27507 
27508 /*
27509  *    Function: sr_atapi_change_speed()
27510  *
27511  * Description: This routine is the driver entry point for handling CD-ROM
27512  *		drive speed ioctl requests for MMC devices that do not support
27513  *		the Real Time Streaming feature (0x107).
27514  *
27515  *		Note: This routine will use the SET SPEED command which may not
27516  *		be supported by all devices.
27517  *
27518  *   Arguments: dev- the device 'dev_t'
27519  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27520  *		     CDROMSDRVSPEED (set)
27521  *		data- current drive speed or requested drive speed
27522  *		flag- this argument is a pass through to ddi_copyxxx() directly
27523  *		      from the mode argument of ioctl().
27524  *
27525  * Return Code: the code returned by sd_send_scsi_cmd()
27526  *		EINVAL if invalid arguments are provided
27527  *		EFAULT if ddi_copyxxx() fails
27528  *		ENXIO if fail ddi_get_soft_state
27529  *		EIO if invalid mode sense block descriptor length
27530  */
27531 
27532 static int
27533 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27534 {
27535 	struct sd_lun			*un;
27536 	struct uscsi_cmd		*com = NULL;
27537 	struct mode_header_grp2		*sense_mhp;
27538 	uchar_t				*sense_page;
27539 	uchar_t				*sense = NULL;
27540 	char				cdb[CDB_GROUP5];
27541 	int				bd_len;
27542 	int				current_speed = 0;
27543 	int				max_speed = 0;
27544 	int				rval;
27545 
27546 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27547 
27548 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27549 		return (ENXIO);
27550 	}
27551 
27552 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27553 
27554 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
27555 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27556 	    SD_PATH_STANDARD)) != 0) {
27557 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27558 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27559 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27560 		return (rval);
27561 	}
27562 
27563 	/* Check the block descriptor len to handle only 1 block descriptor */
27564 	sense_mhp = (struct mode_header_grp2 *)sense;
27565 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27566 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27567 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27568 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27569 		    "block descriptor length\n");
27570 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27571 		return (EIO);
27572 	}
27573 
27574 	/* Calculate the current and maximum drive speeds */
27575 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27576 	current_speed = (sense_page[14] << 8) | sense_page[15];
27577 	max_speed = (sense_page[8] << 8) | sense_page[9];
27578 
27579 	/* Process the command */
27580 	switch (cmd) {
27581 	case CDROMGDRVSPEED:
27582 		current_speed /= SD_SPEED_1X;
27583 		if (ddi_copyout(&current_speed, (void *)data,
27584 		    sizeof (int), flag) != 0)
27585 			rval = EFAULT;
27586 		break;
27587 	case CDROMSDRVSPEED:
27588 		/* Convert the speed code to KB/sec */
27589 		switch ((uchar_t)data) {
27590 		case CDROM_NORMAL_SPEED:
27591 			current_speed = SD_SPEED_1X;
27592 			break;
27593 		case CDROM_DOUBLE_SPEED:
27594 			current_speed = 2 * SD_SPEED_1X;
27595 			break;
27596 		case CDROM_QUAD_SPEED:
27597 			current_speed = 4 * SD_SPEED_1X;
27598 			break;
27599 		case CDROM_TWELVE_SPEED:
27600 			current_speed = 12 * SD_SPEED_1X;
27601 			break;
27602 		case CDROM_MAXIMUM_SPEED:
27603 			current_speed = 0xffff;
27604 			break;
27605 		default:
27606 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27607 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27608 			    (uchar_t)data);
27609 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27610 			return (EINVAL);
27611 		}
27612 
27613 		/* Check the request against the drive's max speed. */
27614 		if (current_speed != 0xffff) {
27615 			if (current_speed > max_speed) {
27616 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27617 				return (EINVAL);
27618 			}
27619 		}
27620 
27621 		/*
27622 		 * Build and send the SET SPEED command
27623 		 *
27624 		 * Note: The SET SPEED (0xBB) command used in this routine is
27625 		 * obsolete per the SCSI MMC spec but still supported in the
27626 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27627 		 * therefore the command is still implemented in this routine.
27628 		 */
27629 		bzero(cdb, sizeof (cdb));
27630 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27631 		cdb[2] = (uchar_t)(current_speed >> 8);
27632 		cdb[3] = (uchar_t)current_speed;
27633 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27634 		com->uscsi_cdb	   = (caddr_t)cdb;
27635 		com->uscsi_cdblen  = CDB_GROUP5;
27636 		com->uscsi_bufaddr = NULL;
27637 		com->uscsi_buflen  = 0;
27638 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27639 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27640 		break;
27641 	default:
27642 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27643 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27644 		rval = EINVAL;
27645 	}
27646 
27647 	if (sense) {
27648 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27649 	}
27650 	if (com) {
27651 		kmem_free(com, sizeof (*com));
27652 	}
27653 	return (rval);
27654 }
27655 
27656 
27657 /*
27658  *    Function: sr_pause_resume()
27659  *
27660  * Description: This routine is the driver entry point for handling CD-ROM
27661  *		pause/resume ioctl requests. This only affects the audio play
27662  *		operation.
27663  *
27664  *   Arguments: dev - the device 'dev_t'
27665  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27666  *		      for setting the resume bit of the cdb.
27667  *
27668  * Return Code: the code returned by sd_send_scsi_cmd()
27669  *		EINVAL if invalid mode specified
27670  *
27671  */
27672 
27673 static int
27674 sr_pause_resume(dev_t dev, int cmd)
27675 {
27676 	struct sd_lun		*un;
27677 	struct uscsi_cmd	*com;
27678 	char			cdb[CDB_GROUP1];
27679 	int			rval;
27680 
27681 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27682 		return (ENXIO);
27683 	}
27684 
27685 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27686 	bzero(cdb, CDB_GROUP1);
27687 	cdb[0] = SCMD_PAUSE_RESUME;
27688 	switch (cmd) {
27689 	case CDROMRESUME:
27690 		cdb[8] = 1;
27691 		break;
27692 	case CDROMPAUSE:
27693 		cdb[8] = 0;
27694 		break;
27695 	default:
27696 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27697 		    " Command '%x' Not Supported\n", cmd);
27698 		rval = EINVAL;
27699 		goto done;
27700 	}
27701 
27702 	com->uscsi_cdb    = cdb;
27703 	com->uscsi_cdblen = CDB_GROUP1;
27704 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27705 
27706 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27707 	    SD_PATH_STANDARD);
27708 
27709 done:
27710 	kmem_free(com, sizeof (*com));
27711 	return (rval);
27712 }
27713 
27714 
27715 /*
27716  *    Function: sr_play_msf()
27717  *
27718  * Description: This routine is the driver entry point for handling CD-ROM
27719  *		ioctl requests to output the audio signals at the specified
27720  *		starting address and continue the audio play until the specified
27721  *		ending address (CDROMPLAYMSF) The address is in Minute Second
27722  *		Frame (MSF) format.
27723  *
27724  *   Arguments: dev	- the device 'dev_t'
27725  *		data	- pointer to user provided audio msf structure,
27726  *		          specifying start/end addresses.
27727  *		flag	- this argument is a pass through to ddi_copyxxx()
27728  *		          directly from the mode argument of ioctl().
27729  *
27730  * Return Code: the code returned by sd_send_scsi_cmd()
27731  *		EFAULT if ddi_copyxxx() fails
27732  *		ENXIO if fail ddi_get_soft_state
27733  *		EINVAL if data pointer is NULL
27734  */
27735 
27736 static int
27737 sr_play_msf(dev_t dev, caddr_t data, int flag)
27738 {
27739 	struct sd_lun		*un;
27740 	struct uscsi_cmd	*com;
27741 	struct cdrom_msf	msf_struct;
27742 	struct cdrom_msf	*msf = &msf_struct;
27743 	char			cdb[CDB_GROUP1];
27744 	int			rval;
27745 
27746 	if (data == NULL) {
27747 		return (EINVAL);
27748 	}
27749 
27750 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27751 		return (ENXIO);
27752 	}
27753 
27754 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27755 		return (EFAULT);
27756 	}
27757 
27758 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27759 	bzero(cdb, CDB_GROUP1);
27760 	cdb[0] = SCMD_PLAYAUDIO_MSF;
27761 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
27762 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27763 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27764 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27765 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27766 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27767 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27768 	} else {
27769 		cdb[3] = msf->cdmsf_min0;
27770 		cdb[4] = msf->cdmsf_sec0;
27771 		cdb[5] = msf->cdmsf_frame0;
27772 		cdb[6] = msf->cdmsf_min1;
27773 		cdb[7] = msf->cdmsf_sec1;
27774 		cdb[8] = msf->cdmsf_frame1;
27775 	}
27776 	com->uscsi_cdb    = cdb;
27777 	com->uscsi_cdblen = CDB_GROUP1;
27778 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27779 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27780 	    SD_PATH_STANDARD);
27781 	kmem_free(com, sizeof (*com));
27782 	return (rval);
27783 }
27784 
27785 
27786 /*
27787  *    Function: sr_play_trkind()
27788  *
27789  * Description: This routine is the driver entry point for handling CD-ROM
27790  *		ioctl requests to output the audio signals at the specified
27791  *		starting address and continue the audio play until the specified
27792  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27793  *		format.
27794  *
27795  *   Arguments: dev	- the device 'dev_t'
27796  *		data	- pointer to user provided audio track/index structure,
27797  *		          specifying start/end addresses.
27798  *		flag	- this argument is a pass through to ddi_copyxxx()
27799  *		          directly from the mode argument of ioctl().
27800  *
27801  * Return Code: the code returned by sd_send_scsi_cmd()
27802  *		EFAULT if ddi_copyxxx() fails
27803  *		ENXIO if fail ddi_get_soft_state
27804  *		EINVAL if data pointer is NULL
27805  */
27806 
27807 static int
27808 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27809 {
27810 	struct cdrom_ti		ti_struct;
27811 	struct cdrom_ti		*ti = &ti_struct;
27812 	struct uscsi_cmd	*com = NULL;
27813 	char			cdb[CDB_GROUP1];
27814 	int			rval;
27815 
27816 	if (data == NULL) {
27817 		return (EINVAL);
27818 	}
27819 
27820 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27821 		return (EFAULT);
27822 	}
27823 
27824 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27825 	bzero(cdb, CDB_GROUP1);
27826 	cdb[0] = SCMD_PLAYAUDIO_TI;
27827 	cdb[4] = ti->cdti_trk0;
27828 	cdb[5] = ti->cdti_ind0;
27829 	cdb[7] = ti->cdti_trk1;
27830 	cdb[8] = ti->cdti_ind1;
27831 	com->uscsi_cdb    = cdb;
27832 	com->uscsi_cdblen = CDB_GROUP1;
27833 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27834 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27835 	    SD_PATH_STANDARD);
27836 	kmem_free(com, sizeof (*com));
27837 	return (rval);
27838 }
27839 
27840 
27841 /*
27842  *    Function: sr_read_all_subcodes()
27843  *
27844  * Description: This routine is the driver entry point for handling CD-ROM
27845  *		ioctl requests to return raw subcode data while the target is
27846  *		playing audio (CDROMSUBCODE).
27847  *
27848  *   Arguments: dev	- the device 'dev_t'
27849  *		data	- pointer to user provided cdrom subcode structure,
27850  *		          specifying the transfer length and address.
27851  *		flag	- this argument is a pass through to ddi_copyxxx()
27852  *		          directly from the mode argument of ioctl().
27853  *
27854  * Return Code: the code returned by sd_send_scsi_cmd()
27855  *		EFAULT if ddi_copyxxx() fails
27856  *		ENXIO if fail ddi_get_soft_state
27857  *		EINVAL if data pointer is NULL
27858  */
27859 
27860 static int
27861 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27862 {
27863 	struct sd_lun		*un = NULL;
27864 	struct uscsi_cmd	*com = NULL;
27865 	struct cdrom_subcode	*subcode = NULL;
27866 	int			rval;
27867 	size_t			buflen;
27868 	char			cdb[CDB_GROUP5];
27869 
27870 #ifdef _MULTI_DATAMODEL
27871 	/* To support ILP32 applications in an LP64 world */
27872 	struct cdrom_subcode32		cdrom_subcode32;
27873 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27874 #endif
27875 	if (data == NULL) {
27876 		return (EINVAL);
27877 	}
27878 
27879 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27880 		return (ENXIO);
27881 	}
27882 
27883 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27884 
27885 #ifdef _MULTI_DATAMODEL
27886 	switch (ddi_model_convert_from(flag & FMODELS)) {
27887 	case DDI_MODEL_ILP32:
27888 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27889 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27890 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27891 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27892 			return (EFAULT);
27893 		}
27894 		/* Convert the ILP32 uscsi data from the application to LP64 */
27895 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27896 		break;
27897 	case DDI_MODEL_NONE:
27898 		if (ddi_copyin(data, subcode,
27899 		    sizeof (struct cdrom_subcode), flag)) {
27900 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27901 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27902 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27903 			return (EFAULT);
27904 		}
27905 		break;
27906 	}
27907 #else /* ! _MULTI_DATAMODEL */
27908 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27909 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27910 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27911 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27912 		return (EFAULT);
27913 	}
27914 #endif /* _MULTI_DATAMODEL */
27915 
27916 	/*
27917 	 * Since MMC-2 expects max 3 bytes for length, check if the
27918 	 * length input is greater than 3 bytes
27919 	 */
27920 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27921 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27922 		    "sr_read_all_subcodes: "
27923 		    "cdrom transfer length too large: %d (limit %d)\n",
27924 		    subcode->cdsc_length, 0xFFFFFF);
27925 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27926 		return (EINVAL);
27927 	}
27928 
27929 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27930 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27931 	bzero(cdb, CDB_GROUP5);
27932 
27933 	if (un->un_f_mmc_cap == TRUE) {
27934 		cdb[0] = (char)SCMD_READ_CD;
27935 		cdb[2] = (char)0xff;
27936 		cdb[3] = (char)0xff;
27937 		cdb[4] = (char)0xff;
27938 		cdb[5] = (char)0xff;
27939 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27940 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27941 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27942 		cdb[10] = 1;
27943 	} else {
27944 		/*
27945 		 * Note: A vendor specific command (0xDF) is being used her to
27946 		 * request a read of all subcodes.
27947 		 */
27948 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27949 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27950 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27951 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27952 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27953 	}
27954 	com->uscsi_cdb	   = cdb;
27955 	com->uscsi_cdblen  = CDB_GROUP5;
27956 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27957 	com->uscsi_buflen  = buflen;
27958 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27959 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27960 	    SD_PATH_STANDARD);
27961 	kmem_free(subcode, sizeof (struct cdrom_subcode));
27962 	kmem_free(com, sizeof (*com));
27963 	return (rval);
27964 }
27965 
27966 
27967 /*
27968  *    Function: sr_read_subchannel()
27969  *
27970  * Description: This routine is the driver entry point for handling CD-ROM
27971  *		ioctl requests to return the Q sub-channel data of the CD
27972  *		current position block. (CDROMSUBCHNL) The data includes the
27973  *		track number, index number, absolute CD-ROM address (LBA or MSF
27974  *		format per the user) , track relative CD-ROM address (LBA or MSF
27975  *		format per the user), control data and audio status.
27976  *
27977  *   Arguments: dev	- the device 'dev_t'
27978  *		data	- pointer to user provided cdrom sub-channel structure
27979  *		flag	- this argument is a pass through to ddi_copyxxx()
27980  *		          directly from the mode argument of ioctl().
27981  *
27982  * Return Code: the code returned by sd_send_scsi_cmd()
27983  *		EFAULT if ddi_copyxxx() fails
27984  *		ENXIO if fail ddi_get_soft_state
27985  *		EINVAL if data pointer is NULL
27986  */
27987 
27988 static int
27989 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27990 {
27991 	struct sd_lun		*un;
27992 	struct uscsi_cmd	*com;
27993 	struct cdrom_subchnl	subchanel;
27994 	struct cdrom_subchnl	*subchnl = &subchanel;
27995 	char			cdb[CDB_GROUP1];
27996 	caddr_t			buffer;
27997 	int			rval;
27998 
27999 	if (data == NULL) {
28000 		return (EINVAL);
28001 	}
28002 
28003 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28004 	    (un->un_state == SD_STATE_OFFLINE)) {
28005 		return (ENXIO);
28006 	}
28007 
28008 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
28009 		return (EFAULT);
28010 	}
28011 
28012 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
28013 	bzero(cdb, CDB_GROUP1);
28014 	cdb[0] = SCMD_READ_SUBCHANNEL;
28015 	/* Set the MSF bit based on the user requested address format */
28016 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
28017 	/*
28018 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
28019 	 * returned
28020 	 */
28021 	cdb[2] = 0x40;
28022 	/*
28023 	 * Set byte 3 to specify the return data format. A value of 0x01
28024 	 * indicates that the CD-ROM current position should be returned.
28025 	 */
28026 	cdb[3] = 0x01;
28027 	cdb[8] = 0x10;
28028 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28029 	com->uscsi_cdb	   = cdb;
28030 	com->uscsi_cdblen  = CDB_GROUP1;
28031 	com->uscsi_bufaddr = buffer;
28032 	com->uscsi_buflen  = 16;
28033 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28034 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28035 	    SD_PATH_STANDARD);
28036 	if (rval != 0) {
28037 		kmem_free(buffer, 16);
28038 		kmem_free(com, sizeof (*com));
28039 		return (rval);
28040 	}
28041 
28042 	/* Process the returned Q sub-channel data */
28043 	subchnl->cdsc_audiostatus = buffer[1];
28044 	subchnl->cdsc_adr	= (buffer[5] & 0xF0);
28045 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
28046 	subchnl->cdsc_trk	= buffer[6];
28047 	subchnl->cdsc_ind	= buffer[7];
28048 	if (subchnl->cdsc_format & CDROM_LBA) {
28049 		subchnl->cdsc_absaddr.lba =
28050 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
28051 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
28052 		subchnl->cdsc_reladdr.lba =
28053 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
28054 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
28055 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
28056 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
28057 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
28058 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
28059 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
28060 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
28061 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
28062 	} else {
28063 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
28064 		subchnl->cdsc_absaddr.msf.second = buffer[10];
28065 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
28066 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
28067 		subchnl->cdsc_reladdr.msf.second = buffer[14];
28068 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
28069 	}
28070 	kmem_free(buffer, 16);
28071 	kmem_free(com, sizeof (*com));
28072 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
28073 	    != 0) {
28074 		return (EFAULT);
28075 	}
28076 	return (rval);
28077 }
28078 
28079 
28080 /*
28081  *    Function: sr_read_tocentry()
28082  *
28083  * Description: This routine is the driver entry point for handling CD-ROM
28084  *		ioctl requests to read from the Table of Contents (TOC)
28085  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
28086  *		fields, the starting address (LBA or MSF format per the user)
28087  *		and the data mode if the user specified track is a data track.
28088  *
28089  *		Note: The READ HEADER (0x44) command used in this routine is
28090  *		obsolete per the SCSI MMC spec but still supported in the
28091  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
28092  *		therefore the command is still implemented in this routine.
28093  *
28094  *   Arguments: dev	- the device 'dev_t'
28095  *		data	- pointer to user provided toc entry structure,
28096  *			  specifying the track # and the address format
28097  *			  (LBA or MSF).
28098  *		flag	- this argument is a pass through to ddi_copyxxx()
28099  *		          directly from the mode argument of ioctl().
28100  *
28101  * Return Code: the code returned by sd_send_scsi_cmd()
28102  *		EFAULT if ddi_copyxxx() fails
28103  *		ENXIO if fail ddi_get_soft_state
28104  *		EINVAL if data pointer is NULL
28105  */
28106 
28107 static int
28108 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
28109 {
28110 	struct sd_lun		*un = NULL;
28111 	struct uscsi_cmd	*com;
28112 	struct cdrom_tocentry	toc_entry;
28113 	struct cdrom_tocentry	*entry = &toc_entry;
28114 	caddr_t			buffer;
28115 	int			rval;
28116 	char			cdb[CDB_GROUP1];
28117 
28118 	if (data == NULL) {
28119 		return (EINVAL);
28120 	}
28121 
28122 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28123 	    (un->un_state == SD_STATE_OFFLINE)) {
28124 		return (ENXIO);
28125 	}
28126 
28127 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
28128 		return (EFAULT);
28129 	}
28130 
28131 	/* Validate the requested track and address format */
28132 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
28133 		return (EINVAL);
28134 	}
28135 
28136 	if (entry->cdte_track == 0) {
28137 		return (EINVAL);
28138 	}
28139 
28140 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
28141 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28142 	bzero(cdb, CDB_GROUP1);
28143 
28144 	cdb[0] = SCMD_READ_TOC;
28145 	/* Set the MSF bit based on the user requested address format  */
28146 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
28147 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28148 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
28149 	} else {
28150 		cdb[6] = entry->cdte_track;
28151 	}
28152 
28153 	/*
28154 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
28155 	 * (4 byte TOC response header + 8 byte track descriptor)
28156 	 */
28157 	cdb[8] = 12;
28158 	com->uscsi_cdb	   = cdb;
28159 	com->uscsi_cdblen  = CDB_GROUP1;
28160 	com->uscsi_bufaddr = buffer;
28161 	com->uscsi_buflen  = 0x0C;
28162 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
28163 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28164 	    SD_PATH_STANDARD);
28165 	if (rval != 0) {
28166 		kmem_free(buffer, 12);
28167 		kmem_free(com, sizeof (*com));
28168 		return (rval);
28169 	}
28170 
28171 	/* Process the toc entry */
28172 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
28173 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
28174 	if (entry->cdte_format & CDROM_LBA) {
28175 		entry->cdte_addr.lba =
28176 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
28177 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
28178 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
28179 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
28180 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
28181 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
28182 		/*
28183 		 * Send a READ TOC command using the LBA address format to get
28184 		 * the LBA for the track requested so it can be used in the
28185 		 * READ HEADER request
28186 		 *
28187 		 * Note: The MSF bit of the READ HEADER command specifies the
28188 		 * output format. The block address specified in that command
28189 		 * must be in LBA format.
28190 		 */
28191 		cdb[1] = 0;
28192 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28193 		    SD_PATH_STANDARD);
28194 		if (rval != 0) {
28195 			kmem_free(buffer, 12);
28196 			kmem_free(com, sizeof (*com));
28197 			return (rval);
28198 		}
28199 	} else {
28200 		entry->cdte_addr.msf.minute	= buffer[9];
28201 		entry->cdte_addr.msf.second	= buffer[10];
28202 		entry->cdte_addr.msf.frame	= buffer[11];
28203 		/*
28204 		 * Send a READ TOC command using the LBA address format to get
28205 		 * the LBA for the track requested so it can be used in the
28206 		 * READ HEADER request
28207 		 *
28208 		 * Note: The MSF bit of the READ HEADER command specifies the
28209 		 * output format. The block address specified in that command
28210 		 * must be in LBA format.
28211 		 */
28212 		cdb[1] = 0;
28213 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28214 		    SD_PATH_STANDARD);
28215 		if (rval != 0) {
28216 			kmem_free(buffer, 12);
28217 			kmem_free(com, sizeof (*com));
28218 			return (rval);
28219 		}
28220 	}
28221 
28222 	/*
28223 	 * Build and send the READ HEADER command to determine the data mode of
28224 	 * the user specified track.
28225 	 */
28226 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
28227 	    (entry->cdte_track != CDROM_LEADOUT)) {
28228 		bzero(cdb, CDB_GROUP1);
28229 		cdb[0] = SCMD_READ_HEADER;
28230 		cdb[2] = buffer[8];
28231 		cdb[3] = buffer[9];
28232 		cdb[4] = buffer[10];
28233 		cdb[5] = buffer[11];
28234 		cdb[8] = 0x08;
28235 		com->uscsi_buflen = 0x08;
28236 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28237 		    SD_PATH_STANDARD);
28238 		if (rval == 0) {
28239 			entry->cdte_datamode = buffer[0];
28240 		} else {
28241 			/*
28242 			 * READ HEADER command failed, since this is
28243 			 * obsoleted in one spec, its better to return
28244 			 * -1 for an invlid track so that we can still
28245 			 * recieve the rest of the TOC data.
28246 			 */
28247 			entry->cdte_datamode = (uchar_t)-1;
28248 		}
28249 	} else {
28250 		entry->cdte_datamode = (uchar_t)-1;
28251 	}
28252 
28253 	kmem_free(buffer, 12);
28254 	kmem_free(com, sizeof (*com));
28255 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
28256 		return (EFAULT);
28257 
28258 	return (rval);
28259 }
28260 
28261 
28262 /*
28263  *    Function: sr_read_tochdr()
28264  *
28265  * Description: This routine is the driver entry point for handling CD-ROM
28266  * 		ioctl requests to read the Table of Contents (TOC) header
28267  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
28268  *		and ending track numbers
28269  *
28270  *   Arguments: dev	- the device 'dev_t'
28271  *		data	- pointer to user provided toc header structure,
28272  *			  specifying the starting and ending track numbers.
28273  *		flag	- this argument is a pass through to ddi_copyxxx()
28274  *			  directly from the mode argument of ioctl().
28275  *
28276  * Return Code: the code returned by sd_send_scsi_cmd()
28277  *		EFAULT if ddi_copyxxx() fails
28278  *		ENXIO if fail ddi_get_soft_state
28279  *		EINVAL if data pointer is NULL
28280  */
28281 
28282 static int
28283 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
28284 {
28285 	struct sd_lun		*un;
28286 	struct uscsi_cmd	*com;
28287 	struct cdrom_tochdr	toc_header;
28288 	struct cdrom_tochdr	*hdr = &toc_header;
28289 	char			cdb[CDB_GROUP1];
28290 	int			rval;
28291 	caddr_t			buffer;
28292 
28293 	if (data == NULL) {
28294 		return (EINVAL);
28295 	}
28296 
28297 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28298 	    (un->un_state == SD_STATE_OFFLINE)) {
28299 		return (ENXIO);
28300 	}
28301 
28302 	buffer = kmem_zalloc(4, KM_SLEEP);
28303 	bzero(cdb, CDB_GROUP1);
28304 	cdb[0] = SCMD_READ_TOC;
28305 	/*
28306 	 * Specifying a track number of 0x00 in the READ TOC command indicates
28307 	 * that the TOC header should be returned
28308 	 */
28309 	cdb[6] = 0x00;
28310 	/*
28311 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28312 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28313 	 */
28314 	cdb[8] = 0x04;
28315 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28316 	com->uscsi_cdb	   = cdb;
28317 	com->uscsi_cdblen  = CDB_GROUP1;
28318 	com->uscsi_bufaddr = buffer;
28319 	com->uscsi_buflen  = 0x04;
28320 	com->uscsi_timeout = 300;
28321 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28322 
28323 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28324 	    SD_PATH_STANDARD);
28325 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28326 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28327 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28328 	} else {
28329 		hdr->cdth_trk0 = buffer[2];
28330 		hdr->cdth_trk1 = buffer[3];
28331 	}
28332 	kmem_free(buffer, 4);
28333 	kmem_free(com, sizeof (*com));
28334 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28335 		return (EFAULT);
28336 	}
28337 	return (rval);
28338 }
28339 
28340 
28341 /*
28342  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28343  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28344  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28345  * digital audio and extended architecture digital audio. These modes are
28346  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28347  * MMC specs.
28348  *
28349  * In addition to support for the various data formats these routines also
28350  * include support for devices that implement only the direct access READ
28351  * commands (0x08, 0x28), devices that implement the READ_CD commands
28352  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28353  * READ CDXA commands (0xD8, 0xDB)
28354  */
28355 
28356 /*
28357  *    Function: sr_read_mode1()
28358  *
28359  * Description: This routine is the driver entry point for handling CD-ROM
28360  *		ioctl read mode1 requests (CDROMREADMODE1).
28361  *
28362  *   Arguments: dev	- the device 'dev_t'
28363  *		data	- pointer to user provided cd read structure specifying
28364  *			  the lba buffer address and length.
28365  *		flag	- this argument is a pass through to ddi_copyxxx()
28366  *			  directly from the mode argument of ioctl().
28367  *
28368  * Return Code: the code returned by sd_send_scsi_cmd()
28369  *		EFAULT if ddi_copyxxx() fails
28370  *		ENXIO if fail ddi_get_soft_state
28371  *		EINVAL if data pointer is NULL
28372  */
28373 
28374 static int
28375 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28376 {
28377 	struct sd_lun		*un;
28378 	struct cdrom_read	mode1_struct;
28379 	struct cdrom_read	*mode1 = &mode1_struct;
28380 	int			rval;
28381 #ifdef _MULTI_DATAMODEL
28382 	/* To support ILP32 applications in an LP64 world */
28383 	struct cdrom_read32	cdrom_read32;
28384 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28385 #endif /* _MULTI_DATAMODEL */
28386 
28387 	if (data == NULL) {
28388 		return (EINVAL);
28389 	}
28390 
28391 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28392 	    (un->un_state == SD_STATE_OFFLINE)) {
28393 		return (ENXIO);
28394 	}
28395 
28396 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28397 	    "sd_read_mode1: entry: un:0x%p\n", un);
28398 
28399 #ifdef _MULTI_DATAMODEL
28400 	switch (ddi_model_convert_from(flag & FMODELS)) {
28401 	case DDI_MODEL_ILP32:
28402 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28403 			return (EFAULT);
28404 		}
28405 		/* Convert the ILP32 uscsi data from the application to LP64 */
28406 		cdrom_read32tocdrom_read(cdrd32, mode1);
28407 		break;
28408 	case DDI_MODEL_NONE:
28409 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28410 			return (EFAULT);
28411 		}
28412 	}
28413 #else /* ! _MULTI_DATAMODEL */
28414 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28415 		return (EFAULT);
28416 	}
28417 #endif /* _MULTI_DATAMODEL */
28418 
28419 	rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr,
28420 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28421 
28422 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28423 	    "sd_read_mode1: exit: un:0x%p\n", un);
28424 
28425 	return (rval);
28426 }
28427 
28428 
28429 /*
28430  *    Function: sr_read_cd_mode2()
28431  *
28432  * Description: This routine is the driver entry point for handling CD-ROM
28433  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28434  *		support the READ CD (0xBE) command or the 1st generation
28435  *		READ CD (0xD4) command.
28436  *
28437  *   Arguments: dev	- the device 'dev_t'
28438  *		data	- pointer to user provided cd read structure specifying
28439  *			  the lba buffer address and length.
28440  *		flag	- this argument is a pass through to ddi_copyxxx()
28441  *			  directly from the mode argument of ioctl().
28442  *
28443  * Return Code: the code returned by sd_send_scsi_cmd()
28444  *		EFAULT if ddi_copyxxx() fails
28445  *		ENXIO if fail ddi_get_soft_state
28446  *		EINVAL if data pointer is NULL
28447  */
28448 
28449 static int
28450 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28451 {
28452 	struct sd_lun		*un;
28453 	struct uscsi_cmd	*com;
28454 	struct cdrom_read	mode2_struct;
28455 	struct cdrom_read	*mode2 = &mode2_struct;
28456 	uchar_t			cdb[CDB_GROUP5];
28457 	int			nblocks;
28458 	int			rval;
28459 #ifdef _MULTI_DATAMODEL
28460 	/*  To support ILP32 applications in an LP64 world */
28461 	struct cdrom_read32	cdrom_read32;
28462 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28463 #endif /* _MULTI_DATAMODEL */
28464 
28465 	if (data == NULL) {
28466 		return (EINVAL);
28467 	}
28468 
28469 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28470 	    (un->un_state == SD_STATE_OFFLINE)) {
28471 		return (ENXIO);
28472 	}
28473 
28474 #ifdef _MULTI_DATAMODEL
28475 	switch (ddi_model_convert_from(flag & FMODELS)) {
28476 	case DDI_MODEL_ILP32:
28477 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28478 			return (EFAULT);
28479 		}
28480 		/* Convert the ILP32 uscsi data from the application to LP64 */
28481 		cdrom_read32tocdrom_read(cdrd32, mode2);
28482 		break;
28483 	case DDI_MODEL_NONE:
28484 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28485 			return (EFAULT);
28486 		}
28487 		break;
28488 	}
28489 
28490 #else /* ! _MULTI_DATAMODEL */
28491 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28492 		return (EFAULT);
28493 	}
28494 #endif /* _MULTI_DATAMODEL */
28495 
28496 	bzero(cdb, sizeof (cdb));
28497 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28498 		/* Read command supported by 1st generation atapi drives */
28499 		cdb[0] = SCMD_READ_CDD4;
28500 	} else {
28501 		/* Universal CD Access Command */
28502 		cdb[0] = SCMD_READ_CD;
28503 	}
28504 
28505 	/*
28506 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28507 	 */
28508 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28509 
28510 	/* set the start address */
28511 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28512 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28513 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28514 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28515 
28516 	/* set the transfer length */
28517 	nblocks = mode2->cdread_buflen / 2336;
28518 	cdb[6] = (uchar_t)(nblocks >> 16);
28519 	cdb[7] = (uchar_t)(nblocks >> 8);
28520 	cdb[8] = (uchar_t)nblocks;
28521 
28522 	/* set the filter bits */
28523 	cdb[9] = CDROM_READ_CD_USERDATA;
28524 
28525 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28526 	com->uscsi_cdb = (caddr_t)cdb;
28527 	com->uscsi_cdblen = sizeof (cdb);
28528 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28529 	com->uscsi_buflen = mode2->cdread_buflen;
28530 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28531 
28532 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28533 	    SD_PATH_STANDARD);
28534 	kmem_free(com, sizeof (*com));
28535 	return (rval);
28536 }
28537 
28538 
28539 /*
28540  *    Function: sr_read_mode2()
28541  *
28542  * Description: This routine is the driver entry point for handling CD-ROM
28543  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28544  *		do not support the READ CD (0xBE) command.
28545  *
28546  *   Arguments: dev	- the device 'dev_t'
28547  *		data	- pointer to user provided cd read structure specifying
28548  *			  the lba buffer address and length.
28549  *		flag	- this argument is a pass through to ddi_copyxxx()
28550  *			  directly from the mode argument of ioctl().
28551  *
28552  * Return Code: the code returned by sd_send_scsi_cmd()
28553  *		EFAULT if ddi_copyxxx() fails
28554  *		ENXIO if fail ddi_get_soft_state
28555  *		EINVAL if data pointer is NULL
28556  *		EIO if fail to reset block size
28557  *		EAGAIN if commands are in progress in the driver
28558  */
28559 
28560 static int
28561 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28562 {
28563 	struct sd_lun		*un;
28564 	struct cdrom_read	mode2_struct;
28565 	struct cdrom_read	*mode2 = &mode2_struct;
28566 	int			rval;
28567 	uint32_t		restore_blksize;
28568 	struct uscsi_cmd	*com;
28569 	uchar_t			cdb[CDB_GROUP0];
28570 	int			nblocks;
28571 
28572 #ifdef _MULTI_DATAMODEL
28573 	/* To support ILP32 applications in an LP64 world */
28574 	struct cdrom_read32	cdrom_read32;
28575 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28576 #endif /* _MULTI_DATAMODEL */
28577 
28578 	if (data == NULL) {
28579 		return (EINVAL);
28580 	}
28581 
28582 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28583 	    (un->un_state == SD_STATE_OFFLINE)) {
28584 		return (ENXIO);
28585 	}
28586 
28587 	/*
28588 	 * Because this routine will update the device and driver block size
28589 	 * being used we want to make sure there are no commands in progress.
28590 	 * If commands are in progress the user will have to try again.
28591 	 *
28592 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28593 	 * in sdioctl to protect commands from sdioctl through to the top of
28594 	 * sd_uscsi_strategy. See sdioctl for details.
28595 	 */
28596 	mutex_enter(SD_MUTEX(un));
28597 	if (un->un_ncmds_in_driver != 1) {
28598 		mutex_exit(SD_MUTEX(un));
28599 		return (EAGAIN);
28600 	}
28601 	mutex_exit(SD_MUTEX(un));
28602 
28603 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28604 	    "sd_read_mode2: entry: un:0x%p\n", un);
28605 
28606 #ifdef _MULTI_DATAMODEL
28607 	switch (ddi_model_convert_from(flag & FMODELS)) {
28608 	case DDI_MODEL_ILP32:
28609 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28610 			return (EFAULT);
28611 		}
28612 		/* Convert the ILP32 uscsi data from the application to LP64 */
28613 		cdrom_read32tocdrom_read(cdrd32, mode2);
28614 		break;
28615 	case DDI_MODEL_NONE:
28616 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28617 			return (EFAULT);
28618 		}
28619 		break;
28620 	}
28621 #else /* ! _MULTI_DATAMODEL */
28622 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28623 		return (EFAULT);
28624 	}
28625 #endif /* _MULTI_DATAMODEL */
28626 
28627 	/* Store the current target block size for restoration later */
28628 	restore_blksize = un->un_tgt_blocksize;
28629 
28630 	/* Change the device and soft state target block size to 2336 */
28631 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28632 		rval = EIO;
28633 		goto done;
28634 	}
28635 
28636 
28637 	bzero(cdb, sizeof (cdb));
28638 
28639 	/* set READ operation */
28640 	cdb[0] = SCMD_READ;
28641 
28642 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
28643 	mode2->cdread_lba >>= 2;
28644 
28645 	/* set the start address */
28646 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28647 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28648 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28649 
28650 	/* set the transfer length */
28651 	nblocks = mode2->cdread_buflen / 2336;
28652 	cdb[4] = (uchar_t)nblocks & 0xFF;
28653 
28654 	/* build command */
28655 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28656 	com->uscsi_cdb = (caddr_t)cdb;
28657 	com->uscsi_cdblen = sizeof (cdb);
28658 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28659 	com->uscsi_buflen = mode2->cdread_buflen;
28660 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28661 
28662 	/*
28663 	 * Issue SCSI command with user space address for read buffer.
28664 	 *
28665 	 * This sends the command through main channel in the driver.
28666 	 *
28667 	 * Since this is accessed via an IOCTL call, we go through the
28668 	 * standard path, so that if the device was powered down, then
28669 	 * it would be 'awakened' to handle the command.
28670 	 */
28671 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28672 	    SD_PATH_STANDARD);
28673 
28674 	kmem_free(com, sizeof (*com));
28675 
28676 	/* Restore the device and soft state target block size */
28677 	if (sr_sector_mode(dev, restore_blksize) != 0) {
28678 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28679 		    "can't do switch back to mode 1\n");
28680 		/*
28681 		 * If sd_send_scsi_READ succeeded we still need to report
28682 		 * an error because we failed to reset the block size
28683 		 */
28684 		if (rval == 0) {
28685 			rval = EIO;
28686 		}
28687 	}
28688 
28689 done:
28690 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28691 	    "sd_read_mode2: exit: un:0x%p\n", un);
28692 
28693 	return (rval);
28694 }
28695 
28696 
28697 /*
28698  *    Function: sr_sector_mode()
28699  *
28700  * Description: This utility function is used by sr_read_mode2 to set the target
28701  *		block size based on the user specified size. This is a legacy
28702  *		implementation based upon a vendor specific mode page
28703  *
28704  *   Arguments: dev	- the device 'dev_t'
28705  *		data	- flag indicating if block size is being set to 2336 or
28706  *			  512.
28707  *
28708  * Return Code: the code returned by sd_send_scsi_cmd()
28709  *		EFAULT if ddi_copyxxx() fails
28710  *		ENXIO if fail ddi_get_soft_state
28711  *		EINVAL if data pointer is NULL
28712  */
28713 
28714 static int
28715 sr_sector_mode(dev_t dev, uint32_t blksize)
28716 {
28717 	struct sd_lun	*un;
28718 	uchar_t		*sense;
28719 	uchar_t		*select;
28720 	int		rval;
28721 
28722 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28723 	    (un->un_state == SD_STATE_OFFLINE)) {
28724 		return (ENXIO);
28725 	}
28726 
28727 	sense = kmem_zalloc(20, KM_SLEEP);
28728 
28729 	/* Note: This is a vendor specific mode page (0x81) */
28730 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81,
28731 	    SD_PATH_STANDARD)) != 0) {
28732 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28733 		    "sr_sector_mode: Mode Sense failed\n");
28734 		kmem_free(sense, 20);
28735 		return (rval);
28736 	}
28737 	select = kmem_zalloc(20, KM_SLEEP);
28738 	select[3] = 0x08;
28739 	select[10] = ((blksize >> 8) & 0xff);
28740 	select[11] = (blksize & 0xff);
28741 	select[12] = 0x01;
28742 	select[13] = 0x06;
28743 	select[14] = sense[14];
28744 	select[15] = sense[15];
28745 	if (blksize == SD_MODE2_BLKSIZE) {
28746 		select[14] |= 0x01;
28747 	}
28748 
28749 	if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20,
28750 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
28751 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28752 		    "sr_sector_mode: Mode Select failed\n");
28753 	} else {
28754 		/*
28755 		 * Only update the softstate block size if we successfully
28756 		 * changed the device block mode.
28757 		 */
28758 		mutex_enter(SD_MUTEX(un));
28759 		sd_update_block_info(un, blksize, 0);
28760 		mutex_exit(SD_MUTEX(un));
28761 	}
28762 	kmem_free(sense, 20);
28763 	kmem_free(select, 20);
28764 	return (rval);
28765 }
28766 
28767 
28768 /*
28769  *    Function: sr_read_cdda()
28770  *
28771  * Description: This routine is the driver entry point for handling CD-ROM
28772  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28773  *		the target supports CDDA these requests are handled via a vendor
28774  *		specific command (0xD8) If the target does not support CDDA
28775  *		these requests are handled via the READ CD command (0xBE).
28776  *
28777  *   Arguments: dev	- the device 'dev_t'
28778  *		data	- pointer to user provided CD-DA structure specifying
28779  *			  the track starting address, transfer length, and
28780  *			  subcode options.
28781  *		flag	- this argument is a pass through to ddi_copyxxx()
28782  *			  directly from the mode argument of ioctl().
28783  *
28784  * Return Code: the code returned by sd_send_scsi_cmd()
28785  *		EFAULT if ddi_copyxxx() fails
28786  *		ENXIO if fail ddi_get_soft_state
28787  *		EINVAL if invalid arguments are provided
28788  *		ENOTTY
28789  */
28790 
28791 static int
28792 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28793 {
28794 	struct sd_lun			*un;
28795 	struct uscsi_cmd		*com;
28796 	struct cdrom_cdda		*cdda;
28797 	int				rval;
28798 	size_t				buflen;
28799 	char				cdb[CDB_GROUP5];
28800 
28801 #ifdef _MULTI_DATAMODEL
28802 	/* To support ILP32 applications in an LP64 world */
28803 	struct cdrom_cdda32	cdrom_cdda32;
28804 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28805 #endif /* _MULTI_DATAMODEL */
28806 
28807 	if (data == NULL) {
28808 		return (EINVAL);
28809 	}
28810 
28811 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28812 		return (ENXIO);
28813 	}
28814 
28815 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28816 
28817 #ifdef _MULTI_DATAMODEL
28818 	switch (ddi_model_convert_from(flag & FMODELS)) {
28819 	case DDI_MODEL_ILP32:
28820 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28821 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28822 			    "sr_read_cdda: ddi_copyin Failed\n");
28823 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28824 			return (EFAULT);
28825 		}
28826 		/* Convert the ILP32 uscsi data from the application to LP64 */
28827 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28828 		break;
28829 	case DDI_MODEL_NONE:
28830 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28831 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28832 			    "sr_read_cdda: ddi_copyin Failed\n");
28833 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28834 			return (EFAULT);
28835 		}
28836 		break;
28837 	}
28838 #else /* ! _MULTI_DATAMODEL */
28839 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28840 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28841 		    "sr_read_cdda: ddi_copyin Failed\n");
28842 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28843 		return (EFAULT);
28844 	}
28845 #endif /* _MULTI_DATAMODEL */
28846 
28847 	/*
28848 	 * Since MMC-2 expects max 3 bytes for length, check if the
28849 	 * length input is greater than 3 bytes
28850 	 */
28851 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28852 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28853 		    "cdrom transfer length too large: %d (limit %d)\n",
28854 		    cdda->cdda_length, 0xFFFFFF);
28855 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28856 		return (EINVAL);
28857 	}
28858 
28859 	switch (cdda->cdda_subcode) {
28860 	case CDROM_DA_NO_SUBCODE:
28861 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28862 		break;
28863 	case CDROM_DA_SUBQ:
28864 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28865 		break;
28866 	case CDROM_DA_ALL_SUBCODE:
28867 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28868 		break;
28869 	case CDROM_DA_SUBCODE_ONLY:
28870 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28871 		break;
28872 	default:
28873 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28874 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28875 		    cdda->cdda_subcode);
28876 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28877 		return (EINVAL);
28878 	}
28879 
28880 	/* Build and send the command */
28881 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28882 	bzero(cdb, CDB_GROUP5);
28883 
28884 	if (un->un_f_cfg_cdda == TRUE) {
28885 		cdb[0] = (char)SCMD_READ_CD;
28886 		cdb[1] = 0x04;
28887 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28888 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28889 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28890 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28891 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28892 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28893 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28894 		cdb[9] = 0x10;
28895 		switch (cdda->cdda_subcode) {
28896 		case CDROM_DA_NO_SUBCODE :
28897 			cdb[10] = 0x0;
28898 			break;
28899 		case CDROM_DA_SUBQ :
28900 			cdb[10] = 0x2;
28901 			break;
28902 		case CDROM_DA_ALL_SUBCODE :
28903 			cdb[10] = 0x1;
28904 			break;
28905 		case CDROM_DA_SUBCODE_ONLY :
28906 			/* FALLTHROUGH */
28907 		default :
28908 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28909 			kmem_free(com, sizeof (*com));
28910 			return (ENOTTY);
28911 		}
28912 	} else {
28913 		cdb[0] = (char)SCMD_READ_CDDA;
28914 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28915 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28916 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28917 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28918 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28919 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28920 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28921 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28922 		cdb[10] = cdda->cdda_subcode;
28923 	}
28924 
28925 	com->uscsi_cdb = cdb;
28926 	com->uscsi_cdblen = CDB_GROUP5;
28927 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28928 	com->uscsi_buflen = buflen;
28929 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28930 
28931 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28932 	    SD_PATH_STANDARD);
28933 
28934 	kmem_free(cdda, sizeof (struct cdrom_cdda));
28935 	kmem_free(com, sizeof (*com));
28936 	return (rval);
28937 }
28938 
28939 
28940 /*
28941  *    Function: sr_read_cdxa()
28942  *
28943  * Description: This routine is the driver entry point for handling CD-ROM
28944  *		ioctl requests to return CD-XA (Extended Architecture) data.
28945  *		(CDROMCDXA).
28946  *
28947  *   Arguments: dev	- the device 'dev_t'
28948  *		data	- pointer to user provided CD-XA structure specifying
28949  *			  the data starting address, transfer length, and format
28950  *		flag	- this argument is a pass through to ddi_copyxxx()
28951  *			  directly from the mode argument of ioctl().
28952  *
28953  * Return Code: the code returned by sd_send_scsi_cmd()
28954  *		EFAULT if ddi_copyxxx() fails
28955  *		ENXIO if fail ddi_get_soft_state
28956  *		EINVAL if data pointer is NULL
28957  */
28958 
28959 static int
28960 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28961 {
28962 	struct sd_lun		*un;
28963 	struct uscsi_cmd	*com;
28964 	struct cdrom_cdxa	*cdxa;
28965 	int			rval;
28966 	size_t			buflen;
28967 	char			cdb[CDB_GROUP5];
28968 	uchar_t			read_flags;
28969 
28970 #ifdef _MULTI_DATAMODEL
28971 	/* To support ILP32 applications in an LP64 world */
28972 	struct cdrom_cdxa32		cdrom_cdxa32;
28973 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
28974 #endif /* _MULTI_DATAMODEL */
28975 
28976 	if (data == NULL) {
28977 		return (EINVAL);
28978 	}
28979 
28980 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28981 		return (ENXIO);
28982 	}
28983 
28984 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28985 
28986 #ifdef _MULTI_DATAMODEL
28987 	switch (ddi_model_convert_from(flag & FMODELS)) {
28988 	case DDI_MODEL_ILP32:
28989 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28990 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28991 			return (EFAULT);
28992 		}
28993 		/*
28994 		 * Convert the ILP32 uscsi data from the
28995 		 * application to LP64 for internal use.
28996 		 */
28997 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28998 		break;
28999 	case DDI_MODEL_NONE:
29000 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
29001 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29002 			return (EFAULT);
29003 		}
29004 		break;
29005 	}
29006 #else /* ! _MULTI_DATAMODEL */
29007 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
29008 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29009 		return (EFAULT);
29010 	}
29011 #endif /* _MULTI_DATAMODEL */
29012 
29013 	/*
29014 	 * Since MMC-2 expects max 3 bytes for length, check if the
29015 	 * length input is greater than 3 bytes
29016 	 */
29017 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
29018 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
29019 		    "cdrom transfer length too large: %d (limit %d)\n",
29020 		    cdxa->cdxa_length, 0xFFFFFF);
29021 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29022 		return (EINVAL);
29023 	}
29024 
29025 	switch (cdxa->cdxa_format) {
29026 	case CDROM_XA_DATA:
29027 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
29028 		read_flags = 0x10;
29029 		break;
29030 	case CDROM_XA_SECTOR_DATA:
29031 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
29032 		read_flags = 0xf8;
29033 		break;
29034 	case CDROM_XA_DATA_W_ERROR:
29035 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
29036 		read_flags = 0xfc;
29037 		break;
29038 	default:
29039 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29040 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
29041 		    cdxa->cdxa_format);
29042 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29043 		return (EINVAL);
29044 	}
29045 
29046 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29047 	bzero(cdb, CDB_GROUP5);
29048 	if (un->un_f_mmc_cap == TRUE) {
29049 		cdb[0] = (char)SCMD_READ_CD;
29050 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
29051 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
29052 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
29053 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
29054 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
29055 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
29056 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
29057 		cdb[9] = (char)read_flags;
29058 	} else {
29059 		/*
29060 		 * Note: A vendor specific command (0xDB) is being used her to
29061 		 * request a read of all subcodes.
29062 		 */
29063 		cdb[0] = (char)SCMD_READ_CDXA;
29064 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
29065 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
29066 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
29067 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
29068 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
29069 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
29070 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
29071 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
29072 		cdb[10] = cdxa->cdxa_format;
29073 	}
29074 	com->uscsi_cdb	   = cdb;
29075 	com->uscsi_cdblen  = CDB_GROUP5;
29076 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
29077 	com->uscsi_buflen  = buflen;
29078 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29079 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
29080 	    SD_PATH_STANDARD);
29081 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
29082 	kmem_free(com, sizeof (*com));
29083 	return (rval);
29084 }
29085 
29086 
29087 /*
29088  *    Function: sr_eject()
29089  *
29090  * Description: This routine is the driver entry point for handling CD-ROM
29091  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
29092  *
29093  *   Arguments: dev	- the device 'dev_t'
29094  *
29095  * Return Code: the code returned by sd_send_scsi_cmd()
29096  */
29097 
29098 static int
29099 sr_eject(dev_t dev)
29100 {
29101 	struct sd_lun	*un;
29102 	int		rval;
29103 
29104 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29105 	    (un->un_state == SD_STATE_OFFLINE)) {
29106 		return (ENXIO);
29107 	}
29108 	if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
29109 	    SD_PATH_STANDARD)) != 0) {
29110 		return (rval);
29111 	}
29112 
29113 	rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT,
29114 	    SD_PATH_STANDARD);
29115 
29116 	if (rval == 0) {
29117 		mutex_enter(SD_MUTEX(un));
29118 		sr_ejected(un);
29119 		un->un_mediastate = DKIO_EJECTED;
29120 		cv_broadcast(&un->un_state_cv);
29121 		mutex_exit(SD_MUTEX(un));
29122 	}
29123 	return (rval);
29124 }
29125 
29126 
29127 /*
29128  *    Function: sr_ejected()
29129  *
29130  * Description: This routine updates the soft state structure to invalidate the
29131  *		geometry information after the media has been ejected or a
29132  *		media eject has been detected.
29133  *
29134  *   Arguments: un - driver soft state (unit) structure
29135  */
29136 
29137 static void
29138 sr_ejected(struct sd_lun *un)
29139 {
29140 	struct sd_errstats *stp;
29141 
29142 	ASSERT(un != NULL);
29143 	ASSERT(mutex_owned(SD_MUTEX(un)));
29144 
29145 	un->un_f_blockcount_is_valid	= FALSE;
29146 	un->un_f_tgt_blocksize_is_valid	= FALSE;
29147 	un->un_f_geometry_is_valid	= FALSE;
29148 
29149 	if (un->un_errstats != NULL) {
29150 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
29151 		stp->sd_capacity.value.ui64 = 0;
29152 	}
29153 }
29154 
29155 
29156 /*
29157  *    Function: sr_check_wp()
29158  *
29159  * Description: This routine checks the write protection of a removable
29160  *      media disk and hotpluggable devices via the write protect bit of
29161  *      the Mode Page Header device specific field. Some devices choke
29162  *      on unsupported mode page. In order to workaround this issue,
29163  *      this routine has been implemented to use 0x3f mode page(request
29164  *      for all pages) for all device types.
29165  *
29166  *   Arguments: dev		- the device 'dev_t'
29167  *
29168  * Return Code: int indicating if the device is write protected (1) or not (0)
29169  *
29170  *     Context: Kernel thread.
29171  *
29172  */
29173 
29174 static int
29175 sr_check_wp(dev_t dev)
29176 {
29177 	struct sd_lun	*un;
29178 	uchar_t		device_specific;
29179 	uchar_t		*sense;
29180 	int		hdrlen;
29181 	int		rval = FALSE;
29182 
29183 	/*
29184 	 * Note: The return codes for this routine should be reworked to
29185 	 * properly handle the case of a NULL softstate.
29186 	 */
29187 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
29188 		return (FALSE);
29189 	}
29190 
29191 	if (un->un_f_cfg_is_atapi == TRUE) {
29192 		/*
29193 		 * The mode page contents are not required; set the allocation
29194 		 * length for the mode page header only
29195 		 */
29196 		hdrlen = MODE_HEADER_LENGTH_GRP2;
29197 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29198 		if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen,
29199 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0)
29200 			goto err_exit;
29201 		device_specific =
29202 		    ((struct mode_header_grp2 *)sense)->device_specific;
29203 	} else {
29204 		hdrlen = MODE_HEADER_LENGTH;
29205 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29206 		if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen,
29207 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0)
29208 			goto err_exit;
29209 		device_specific =
29210 		    ((struct mode_header *)sense)->device_specific;
29211 	}
29212 
29213 	/*
29214 	 * Write protect mode sense failed; not all disks
29215 	 * understand this query. Return FALSE assuming that
29216 	 * these devices are not writable.
29217 	 */
29218 	if (device_specific & WRITE_PROTECT) {
29219 		rval = TRUE;
29220 	}
29221 
29222 err_exit:
29223 	kmem_free(sense, hdrlen);
29224 	return (rval);
29225 }
29226 
29227 /*
29228  *    Function: sr_volume_ctrl()
29229  *
29230  * Description: This routine is the driver entry point for handling CD-ROM
29231  *		audio output volume ioctl requests. (CDROMVOLCTRL)
29232  *
29233  *   Arguments: dev	- the device 'dev_t'
29234  *		data	- pointer to user audio volume control structure
29235  *		flag	- this argument is a pass through to ddi_copyxxx()
29236  *			  directly from the mode argument of ioctl().
29237  *
29238  * Return Code: the code returned by sd_send_scsi_cmd()
29239  *		EFAULT if ddi_copyxxx() fails
29240  *		ENXIO if fail ddi_get_soft_state
29241  *		EINVAL if data pointer is NULL
29242  *
29243  */
29244 
29245 static int
29246 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
29247 {
29248 	struct sd_lun		*un;
29249 	struct cdrom_volctrl    volume;
29250 	struct cdrom_volctrl    *vol = &volume;
29251 	uchar_t			*sense_page;
29252 	uchar_t			*select_page;
29253 	uchar_t			*sense;
29254 	uchar_t			*select;
29255 	int			sense_buflen;
29256 	int			select_buflen;
29257 	int			rval;
29258 
29259 	if (data == NULL) {
29260 		return (EINVAL);
29261 	}
29262 
29263 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29264 	    (un->un_state == SD_STATE_OFFLINE)) {
29265 		return (ENXIO);
29266 	}
29267 
29268 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
29269 		return (EFAULT);
29270 	}
29271 
29272 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29273 		struct mode_header_grp2		*sense_mhp;
29274 		struct mode_header_grp2		*select_mhp;
29275 		int				bd_len;
29276 
29277 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29278 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
29279 		    MODEPAGE_AUDIO_CTRL_LEN;
29280 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29281 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29282 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
29283 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29284 		    SD_PATH_STANDARD)) != 0) {
29285 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29286 			    "sr_volume_ctrl: Mode Sense Failed\n");
29287 			kmem_free(sense, sense_buflen);
29288 			kmem_free(select, select_buflen);
29289 			return (rval);
29290 		}
29291 		sense_mhp = (struct mode_header_grp2 *)sense;
29292 		select_mhp = (struct mode_header_grp2 *)select;
29293 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
29294 		    sense_mhp->bdesc_length_lo;
29295 		if (bd_len > MODE_BLK_DESC_LENGTH) {
29296 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29297 			    "sr_volume_ctrl: Mode Sense returned invalid "
29298 			    "block descriptor length\n");
29299 			kmem_free(sense, sense_buflen);
29300 			kmem_free(select, select_buflen);
29301 			return (EIO);
29302 		}
29303 		sense_page = (uchar_t *)
29304 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29305 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29306 		select_mhp->length_msb = 0;
29307 		select_mhp->length_lsb = 0;
29308 		select_mhp->bdesc_length_hi = 0;
29309 		select_mhp->bdesc_length_lo = 0;
29310 	} else {
29311 		struct mode_header		*sense_mhp, *select_mhp;
29312 
29313 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29314 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29315 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29316 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29317 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
29318 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29319 		    SD_PATH_STANDARD)) != 0) {
29320 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29321 			    "sr_volume_ctrl: Mode Sense Failed\n");
29322 			kmem_free(sense, sense_buflen);
29323 			kmem_free(select, select_buflen);
29324 			return (rval);
29325 		}
29326 		sense_mhp  = (struct mode_header *)sense;
29327 		select_mhp = (struct mode_header *)select;
29328 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29329 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29330 			    "sr_volume_ctrl: Mode Sense returned invalid "
29331 			    "block descriptor length\n");
29332 			kmem_free(sense, sense_buflen);
29333 			kmem_free(select, select_buflen);
29334 			return (EIO);
29335 		}
29336 		sense_page = (uchar_t *)
29337 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29338 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29339 		select_mhp->length = 0;
29340 		select_mhp->bdesc_length = 0;
29341 	}
29342 	/*
29343 	 * Note: An audio control data structure could be created and overlayed
29344 	 * on the following in place of the array indexing method implemented.
29345 	 */
29346 
29347 	/* Build the select data for the user volume data */
29348 	select_page[0] = MODEPAGE_AUDIO_CTRL;
29349 	select_page[1] = 0xE;
29350 	/* Set the immediate bit */
29351 	select_page[2] = 0x04;
29352 	/* Zero out reserved fields */
29353 	select_page[3] = 0x00;
29354 	select_page[4] = 0x00;
29355 	/* Return sense data for fields not to be modified */
29356 	select_page[5] = sense_page[5];
29357 	select_page[6] = sense_page[6];
29358 	select_page[7] = sense_page[7];
29359 	/* Set the user specified volume levels for channel 0 and 1 */
29360 	select_page[8] = 0x01;
29361 	select_page[9] = vol->channel0;
29362 	select_page[10] = 0x02;
29363 	select_page[11] = vol->channel1;
29364 	/* Channel 2 and 3 are currently unsupported so return the sense data */
29365 	select_page[12] = sense_page[12];
29366 	select_page[13] = sense_page[13];
29367 	select_page[14] = sense_page[14];
29368 	select_page[15] = sense_page[15];
29369 
29370 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29371 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select,
29372 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29373 	} else {
29374 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
29375 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29376 	}
29377 
29378 	kmem_free(sense, sense_buflen);
29379 	kmem_free(select, select_buflen);
29380 	return (rval);
29381 }
29382 
29383 
29384 /*
29385  *    Function: sr_read_sony_session_offset()
29386  *
29387  * Description: This routine is the driver entry point for handling CD-ROM
29388  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29389  *		The address of the first track in the last session of a
29390  *		multi-session CD-ROM is returned
29391  *
29392  *		Note: This routine uses a vendor specific key value in the
29393  *		command control field without implementing any vendor check here
29394  *		or in the ioctl routine.
29395  *
29396  *   Arguments: dev	- the device 'dev_t'
29397  *		data	- pointer to an int to hold the requested address
29398  *		flag	- this argument is a pass through to ddi_copyxxx()
29399  *			  directly from the mode argument of ioctl().
29400  *
29401  * Return Code: the code returned by sd_send_scsi_cmd()
29402  *		EFAULT if ddi_copyxxx() fails
29403  *		ENXIO if fail ddi_get_soft_state
29404  *		EINVAL if data pointer is NULL
29405  */
29406 
29407 static int
29408 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29409 {
29410 	struct sd_lun		*un;
29411 	struct uscsi_cmd	*com;
29412 	caddr_t			buffer;
29413 	char			cdb[CDB_GROUP1];
29414 	int			session_offset = 0;
29415 	int			rval;
29416 
29417 	if (data == NULL) {
29418 		return (EINVAL);
29419 	}
29420 
29421 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29422 	    (un->un_state == SD_STATE_OFFLINE)) {
29423 		return (ENXIO);
29424 	}
29425 
29426 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29427 	bzero(cdb, CDB_GROUP1);
29428 	cdb[0] = SCMD_READ_TOC;
29429 	/*
29430 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29431 	 * (4 byte TOC response header + 8 byte response data)
29432 	 */
29433 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29434 	/* Byte 9 is the control byte. A vendor specific value is used */
29435 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29436 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29437 	com->uscsi_cdb = cdb;
29438 	com->uscsi_cdblen = CDB_GROUP1;
29439 	com->uscsi_bufaddr = buffer;
29440 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29441 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29442 
29443 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29444 	    SD_PATH_STANDARD);
29445 	if (rval != 0) {
29446 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29447 		kmem_free(com, sizeof (*com));
29448 		return (rval);
29449 	}
29450 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29451 		session_offset =
29452 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29453 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29454 		/*
29455 		 * Offset returned offset in current lbasize block's. Convert to
29456 		 * 2k block's to return to the user
29457 		 */
29458 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29459 			session_offset >>= 2;
29460 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29461 			session_offset >>= 1;
29462 		}
29463 	}
29464 
29465 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29466 		rval = EFAULT;
29467 	}
29468 
29469 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29470 	kmem_free(com, sizeof (*com));
29471 	return (rval);
29472 }
29473 
29474 
29475 /*
29476  *    Function: sd_wm_cache_constructor()
29477  *
29478  * Description: Cache Constructor for the wmap cache for the read/modify/write
29479  * 		devices.
29480  *
29481  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29482  *		un	- sd_lun structure for the device.
29483  *		flag	- the km flags passed to constructor
29484  *
29485  * Return Code: 0 on success.
29486  *		-1 on failure.
29487  */
29488 
29489 /*ARGSUSED*/
29490 static int
29491 sd_wm_cache_constructor(void *wm, void *un, int flags)
29492 {
29493 	bzero(wm, sizeof (struct sd_w_map));
29494 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29495 	return (0);
29496 }
29497 
29498 
29499 /*
29500  *    Function: sd_wm_cache_destructor()
29501  *
29502  * Description: Cache destructor for the wmap cache for the read/modify/write
29503  * 		devices.
29504  *
29505  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29506  *		un	- sd_lun structure for the device.
29507  */
29508 /*ARGSUSED*/
29509 static void
29510 sd_wm_cache_destructor(void *wm, void *un)
29511 {
29512 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29513 }
29514 
29515 
29516 /*
29517  *    Function: sd_range_lock()
29518  *
29519  * Description: Lock the range of blocks specified as parameter to ensure
29520  *		that read, modify write is atomic and no other i/o writes
29521  *		to the same location. The range is specified in terms
29522  *		of start and end blocks. Block numbers are the actual
29523  *		media block numbers and not system.
29524  *
29525  *   Arguments: un	- sd_lun structure for the device.
29526  *		startb - The starting block number
29527  *		endb - The end block number
29528  *		typ - type of i/o - simple/read_modify_write
29529  *
29530  * Return Code: wm  - pointer to the wmap structure.
29531  *
29532  *     Context: This routine can sleep.
29533  */
29534 
29535 static struct sd_w_map *
29536 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29537 {
29538 	struct sd_w_map *wmp = NULL;
29539 	struct sd_w_map *sl_wmp = NULL;
29540 	struct sd_w_map *tmp_wmp;
29541 	wm_state state = SD_WM_CHK_LIST;
29542 
29543 
29544 	ASSERT(un != NULL);
29545 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29546 
29547 	mutex_enter(SD_MUTEX(un));
29548 
29549 	while (state != SD_WM_DONE) {
29550 
29551 		switch (state) {
29552 		case SD_WM_CHK_LIST:
29553 			/*
29554 			 * This is the starting state. Check the wmap list
29555 			 * to see if the range is currently available.
29556 			 */
29557 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29558 				/*
29559 				 * If this is a simple write and no rmw
29560 				 * i/o is pending then try to lock the
29561 				 * range as the range should be available.
29562 				 */
29563 				state = SD_WM_LOCK_RANGE;
29564 			} else {
29565 				tmp_wmp = sd_get_range(un, startb, endb);
29566 				if (tmp_wmp != NULL) {
29567 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29568 						/*
29569 						 * Should not keep onlist wmps
29570 						 * while waiting this macro
29571 						 * will also do wmp = NULL;
29572 						 */
29573 						FREE_ONLIST_WMAP(un, wmp);
29574 					}
29575 					/*
29576 					 * sl_wmp is the wmap on which wait
29577 					 * is done, since the tmp_wmp points
29578 					 * to the inuse wmap, set sl_wmp to
29579 					 * tmp_wmp and change the state to sleep
29580 					 */
29581 					sl_wmp = tmp_wmp;
29582 					state = SD_WM_WAIT_MAP;
29583 				} else {
29584 					state = SD_WM_LOCK_RANGE;
29585 				}
29586 
29587 			}
29588 			break;
29589 
29590 		case SD_WM_LOCK_RANGE:
29591 			ASSERT(un->un_wm_cache);
29592 			/*
29593 			 * The range need to be locked, try to get a wmap.
29594 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29595 			 * if possible as we will have to release the sd mutex
29596 			 * if we have to sleep.
29597 			 */
29598 			if (wmp == NULL)
29599 				wmp = kmem_cache_alloc(un->un_wm_cache,
29600 				    KM_NOSLEEP);
29601 			if (wmp == NULL) {
29602 				mutex_exit(SD_MUTEX(un));
29603 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29604 				    (sd_lun::un_wm_cache))
29605 				wmp = kmem_cache_alloc(un->un_wm_cache,
29606 				    KM_SLEEP);
29607 				mutex_enter(SD_MUTEX(un));
29608 				/*
29609 				 * we released the mutex so recheck and go to
29610 				 * check list state.
29611 				 */
29612 				state = SD_WM_CHK_LIST;
29613 			} else {
29614 				/*
29615 				 * We exit out of state machine since we
29616 				 * have the wmap. Do the housekeeping first.
29617 				 * place the wmap on the wmap list if it is not
29618 				 * on it already and then set the state to done.
29619 				 */
29620 				wmp->wm_start = startb;
29621 				wmp->wm_end = endb;
29622 				wmp->wm_flags = typ | SD_WM_BUSY;
29623 				if (typ & SD_WTYPE_RMW) {
29624 					un->un_rmw_count++;
29625 				}
29626 				/*
29627 				 * If not already on the list then link
29628 				 */
29629 				if (!ONLIST(un, wmp)) {
29630 					wmp->wm_next = un->un_wm;
29631 					wmp->wm_prev = NULL;
29632 					if (wmp->wm_next)
29633 						wmp->wm_next->wm_prev = wmp;
29634 					un->un_wm = wmp;
29635 				}
29636 				state = SD_WM_DONE;
29637 			}
29638 			break;
29639 
29640 		case SD_WM_WAIT_MAP:
29641 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29642 			/*
29643 			 * Wait is done on sl_wmp, which is set in the
29644 			 * check_list state.
29645 			 */
29646 			sl_wmp->wm_wanted_count++;
29647 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29648 			sl_wmp->wm_wanted_count--;
29649 			/*
29650 			 * We can reuse the memory from the completed sl_wmp
29651 			 * lock range for our new lock, but only if noone is
29652 			 * waiting for it.
29653 			 */
29654 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29655 			if (sl_wmp->wm_wanted_count == 0) {
29656 				if (wmp != NULL)
29657 					CHK_N_FREEWMP(un, wmp);
29658 				wmp = sl_wmp;
29659 			}
29660 			sl_wmp = NULL;
29661 			/*
29662 			 * After waking up, need to recheck for availability of
29663 			 * range.
29664 			 */
29665 			state = SD_WM_CHK_LIST;
29666 			break;
29667 
29668 		default:
29669 			panic("sd_range_lock: "
29670 			    "Unknown state %d in sd_range_lock", state);
29671 			/*NOTREACHED*/
29672 		} /* switch(state) */
29673 
29674 	} /* while(state != SD_WM_DONE) */
29675 
29676 	mutex_exit(SD_MUTEX(un));
29677 
29678 	ASSERT(wmp != NULL);
29679 
29680 	return (wmp);
29681 }
29682 
29683 
29684 /*
29685  *    Function: sd_get_range()
29686  *
29687  * Description: Find if there any overlapping I/O to this one
29688  *		Returns the write-map of 1st such I/O, NULL otherwise.
29689  *
29690  *   Arguments: un	- sd_lun structure for the device.
29691  *		startb - The starting block number
29692  *		endb - The end block number
29693  *
29694  * Return Code: wm  - pointer to the wmap structure.
29695  */
29696 
29697 static struct sd_w_map *
29698 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29699 {
29700 	struct sd_w_map *wmp;
29701 
29702 	ASSERT(un != NULL);
29703 
29704 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29705 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
29706 			continue;
29707 		}
29708 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29709 			break;
29710 		}
29711 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29712 			break;
29713 		}
29714 	}
29715 
29716 	return (wmp);
29717 }
29718 
29719 
29720 /*
29721  *    Function: sd_free_inlist_wmap()
29722  *
29723  * Description: Unlink and free a write map struct.
29724  *
29725  *   Arguments: un      - sd_lun structure for the device.
29726  *		wmp	- sd_w_map which needs to be unlinked.
29727  */
29728 
29729 static void
29730 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29731 {
29732 	ASSERT(un != NULL);
29733 
29734 	if (un->un_wm == wmp) {
29735 		un->un_wm = wmp->wm_next;
29736 	} else {
29737 		wmp->wm_prev->wm_next = wmp->wm_next;
29738 	}
29739 
29740 	if (wmp->wm_next) {
29741 		wmp->wm_next->wm_prev = wmp->wm_prev;
29742 	}
29743 
29744 	wmp->wm_next = wmp->wm_prev = NULL;
29745 
29746 	kmem_cache_free(un->un_wm_cache, wmp);
29747 }
29748 
29749 
29750 /*
29751  *    Function: sd_range_unlock()
29752  *
29753  * Description: Unlock the range locked by wm.
29754  *		Free write map if nobody else is waiting on it.
29755  *
29756  *   Arguments: un      - sd_lun structure for the device.
29757  *              wmp     - sd_w_map which needs to be unlinked.
29758  */
29759 
29760 static void
29761 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29762 {
29763 	ASSERT(un != NULL);
29764 	ASSERT(wm != NULL);
29765 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29766 
29767 	mutex_enter(SD_MUTEX(un));
29768 
29769 	if (wm->wm_flags & SD_WTYPE_RMW) {
29770 		un->un_rmw_count--;
29771 	}
29772 
29773 	if (wm->wm_wanted_count) {
29774 		wm->wm_flags = 0;
29775 		/*
29776 		 * Broadcast that the wmap is available now.
29777 		 */
29778 		cv_broadcast(&wm->wm_avail);
29779 	} else {
29780 		/*
29781 		 * If no one is waiting on the map, it should be free'ed.
29782 		 */
29783 		sd_free_inlist_wmap(un, wm);
29784 	}
29785 
29786 	mutex_exit(SD_MUTEX(un));
29787 }
29788 
29789 
29790 /*
29791  *    Function: sd_read_modify_write_task
29792  *
29793  * Description: Called from a taskq thread to initiate the write phase of
29794  *		a read-modify-write request.  This is used for targets where
29795  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29796  *
29797  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29798  *
29799  *     Context: Called under taskq thread context.
29800  */
29801 
29802 static void
29803 sd_read_modify_write_task(void *arg)
29804 {
29805 	struct sd_mapblocksize_info	*bsp;
29806 	struct buf	*bp;
29807 	struct sd_xbuf	*xp;
29808 	struct sd_lun	*un;
29809 
29810 	bp = arg;	/* The bp is given in arg */
29811 	ASSERT(bp != NULL);
29812 
29813 	/* Get the pointer to the layer-private data struct */
29814 	xp = SD_GET_XBUF(bp);
29815 	ASSERT(xp != NULL);
29816 	bsp = xp->xb_private;
29817 	ASSERT(bsp != NULL);
29818 
29819 	un = SD_GET_UN(bp);
29820 	ASSERT(un != NULL);
29821 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29822 
29823 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29824 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29825 
29826 	/*
29827 	 * This is the write phase of a read-modify-write request, called
29828 	 * under the context of a taskq thread in response to the completion
29829 	 * of the read portion of the rmw request completing under interrupt
29830 	 * context. The write request must be sent from here down the iostart
29831 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29832 	 * we use the layer index saved in the layer-private data area.
29833 	 */
29834 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29835 
29836 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29837 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29838 }
29839 
29840 
29841 /*
29842  *    Function: sddump_do_read_of_rmw()
29843  *
29844  * Description: This routine will be called from sddump, If sddump is called
29845  *		with an I/O which not aligned on device blocksize boundary
29846  *		then the write has to be converted to read-modify-write.
29847  *		Do the read part here in order to keep sddump simple.
29848  *		Note - That the sd_mutex is held across the call to this
29849  *		routine.
29850  *
29851  *   Arguments: un	- sd_lun
29852  *		blkno	- block number in terms of media block size.
29853  *		nblk	- number of blocks.
29854  *		bpp	- pointer to pointer to the buf structure. On return
29855  *			from this function, *bpp points to the valid buffer
29856  *			to which the write has to be done.
29857  *
29858  * Return Code: 0 for success or errno-type return code
29859  */
29860 
29861 static int
29862 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29863 	struct buf **bpp)
29864 {
29865 	int err;
29866 	int i;
29867 	int rval;
29868 	struct buf *bp;
29869 	struct scsi_pkt *pkt = NULL;
29870 	uint32_t target_blocksize;
29871 
29872 	ASSERT(un != NULL);
29873 	ASSERT(mutex_owned(SD_MUTEX(un)));
29874 
29875 	target_blocksize = un->un_tgt_blocksize;
29876 
29877 	mutex_exit(SD_MUTEX(un));
29878 
29879 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29880 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29881 	if (bp == NULL) {
29882 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29883 		    "no resources for dumping; giving up");
29884 		err = ENOMEM;
29885 		goto done;
29886 	}
29887 
29888 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29889 	    blkno, nblk);
29890 	if (rval != 0) {
29891 		scsi_free_consistent_buf(bp);
29892 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29893 		    "no resources for dumping; giving up");
29894 		err = ENOMEM;
29895 		goto done;
29896 	}
29897 
29898 	pkt->pkt_flags |= FLAG_NOINTR;
29899 
29900 	err = EIO;
29901 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29902 
29903 		/*
29904 		 * Scsi_poll returns 0 (success) if the command completes and
29905 		 * the status block is STATUS_GOOD.  We should only check
29906 		 * errors if this condition is not true.  Even then we should
29907 		 * send our own request sense packet only if we have a check
29908 		 * condition and auto request sense has not been performed by
29909 		 * the hba.
29910 		 */
29911 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29912 
29913 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29914 			err = 0;
29915 			break;
29916 		}
29917 
29918 		/*
29919 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
29920 		 * no need to read RQS data.
29921 		 */
29922 		if (pkt->pkt_reason == CMD_DEV_GONE) {
29923 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29924 			    "Device is gone\n");
29925 			break;
29926 		}
29927 
29928 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29929 			SD_INFO(SD_LOG_DUMP, un,
29930 			    "sddump: read failed with CHECK, try # %d\n", i);
29931 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29932 				(void) sd_send_polled_RQS(un);
29933 			}
29934 
29935 			continue;
29936 		}
29937 
29938 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29939 			int reset_retval = 0;
29940 
29941 			SD_INFO(SD_LOG_DUMP, un,
29942 			    "sddump: read failed with BUSY, try # %d\n", i);
29943 
29944 			if (un->un_f_lun_reset_enabled == TRUE) {
29945 				reset_retval = scsi_reset(SD_ADDRESS(un),
29946 				    RESET_LUN);
29947 			}
29948 			if (reset_retval == 0) {
29949 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29950 			}
29951 			(void) sd_send_polled_RQS(un);
29952 
29953 		} else {
29954 			SD_INFO(SD_LOG_DUMP, un,
29955 			    "sddump: read failed with 0x%x, try # %d\n",
29956 			    SD_GET_PKT_STATUS(pkt), i);
29957 			mutex_enter(SD_MUTEX(un));
29958 			sd_reset_target(un, pkt);
29959 			mutex_exit(SD_MUTEX(un));
29960 		}
29961 
29962 		/*
29963 		 * If we are not getting anywhere with lun/target resets,
29964 		 * let's reset the bus.
29965 		 */
29966 		if (i > SD_NDUMP_RETRIES/2) {
29967 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29968 			(void) sd_send_polled_RQS(un);
29969 		}
29970 
29971 	}
29972 	scsi_destroy_pkt(pkt);
29973 
29974 	if (err != 0) {
29975 		scsi_free_consistent_buf(bp);
29976 		*bpp = NULL;
29977 	} else {
29978 		*bpp = bp;
29979 	}
29980 
29981 done:
29982 	mutex_enter(SD_MUTEX(un));
29983 	return (err);
29984 }
29985 
29986 
29987 /*
29988  *    Function: sd_failfast_flushq
29989  *
29990  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29991  *		in b_flags and move them onto the failfast queue, then kick
29992  *		off a thread to return all bp's on the failfast queue to
29993  *		their owners with an error set.
29994  *
29995  *   Arguments: un - pointer to the soft state struct for the instance.
29996  *
29997  *     Context: may execute in interrupt context.
29998  */
29999 
30000 static void
30001 sd_failfast_flushq(struct sd_lun *un)
30002 {
30003 	struct buf *bp;
30004 	struct buf *next_waitq_bp;
30005 	struct buf *prev_waitq_bp = NULL;
30006 
30007 	ASSERT(un != NULL);
30008 	ASSERT(mutex_owned(SD_MUTEX(un)));
30009 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
30010 	ASSERT(un->un_failfast_bp == NULL);
30011 
30012 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
30013 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
30014 
30015 	/*
30016 	 * Check if we should flush all bufs when entering failfast state, or
30017 	 * just those with B_FAILFAST set.
30018 	 */
30019 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
30020 		/*
30021 		 * Move *all* bp's on the wait queue to the failfast flush
30022 		 * queue, including those that do NOT have B_FAILFAST set.
30023 		 */
30024 		if (un->un_failfast_headp == NULL) {
30025 			ASSERT(un->un_failfast_tailp == NULL);
30026 			un->un_failfast_headp = un->un_waitq_headp;
30027 		} else {
30028 			ASSERT(un->un_failfast_tailp != NULL);
30029 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
30030 		}
30031 
30032 		un->un_failfast_tailp = un->un_waitq_tailp;
30033 
30034 		/* update kstat for each bp moved out of the waitq */
30035 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
30036 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
30037 		}
30038 
30039 		/* empty the waitq */
30040 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
30041 
30042 	} else {
30043 		/*
30044 		 * Go thru the wait queue, pick off all entries with
30045 		 * B_FAILFAST set, and move these onto the failfast queue.
30046 		 */
30047 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
30048 			/*
30049 			 * Save the pointer to the next bp on the wait queue,
30050 			 * so we get to it on the next iteration of this loop.
30051 			 */
30052 			next_waitq_bp = bp->av_forw;
30053 
30054 			/*
30055 			 * If this bp from the wait queue does NOT have
30056 			 * B_FAILFAST set, just move on to the next element
30057 			 * in the wait queue. Note, this is the only place
30058 			 * where it is correct to set prev_waitq_bp.
30059 			 */
30060 			if ((bp->b_flags & B_FAILFAST) == 0) {
30061 				prev_waitq_bp = bp;
30062 				continue;
30063 			}
30064 
30065 			/*
30066 			 * Remove the bp from the wait queue.
30067 			 */
30068 			if (bp == un->un_waitq_headp) {
30069 				/* The bp is the first element of the waitq. */
30070 				un->un_waitq_headp = next_waitq_bp;
30071 				if (un->un_waitq_headp == NULL) {
30072 					/* The wait queue is now empty */
30073 					un->un_waitq_tailp = NULL;
30074 				}
30075 			} else {
30076 				/*
30077 				 * The bp is either somewhere in the middle
30078 				 * or at the end of the wait queue.
30079 				 */
30080 				ASSERT(un->un_waitq_headp != NULL);
30081 				ASSERT(prev_waitq_bp != NULL);
30082 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
30083 				    == 0);
30084 				if (bp == un->un_waitq_tailp) {
30085 					/* bp is the last entry on the waitq. */
30086 					ASSERT(next_waitq_bp == NULL);
30087 					un->un_waitq_tailp = prev_waitq_bp;
30088 				}
30089 				prev_waitq_bp->av_forw = next_waitq_bp;
30090 			}
30091 			bp->av_forw = NULL;
30092 
30093 			/*
30094 			 * update kstat since the bp is moved out of
30095 			 * the waitq
30096 			 */
30097 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
30098 
30099 			/*
30100 			 * Now put the bp onto the failfast queue.
30101 			 */
30102 			if (un->un_failfast_headp == NULL) {
30103 				/* failfast queue is currently empty */
30104 				ASSERT(un->un_failfast_tailp == NULL);
30105 				un->un_failfast_headp =
30106 				    un->un_failfast_tailp = bp;
30107 			} else {
30108 				/* Add the bp to the end of the failfast q */
30109 				ASSERT(un->un_failfast_tailp != NULL);
30110 				ASSERT(un->un_failfast_tailp->b_flags &
30111 				    B_FAILFAST);
30112 				un->un_failfast_tailp->av_forw = bp;
30113 				un->un_failfast_tailp = bp;
30114 			}
30115 		}
30116 	}
30117 
30118 	/*
30119 	 * Now return all bp's on the failfast queue to their owners.
30120 	 */
30121 	while ((bp = un->un_failfast_headp) != NULL) {
30122 
30123 		un->un_failfast_headp = bp->av_forw;
30124 		if (un->un_failfast_headp == NULL) {
30125 			un->un_failfast_tailp = NULL;
30126 		}
30127 
30128 		/*
30129 		 * We want to return the bp with a failure error code, but
30130 		 * we do not want a call to sd_start_cmds() to occur here,
30131 		 * so use sd_return_failed_command_no_restart() instead of
30132 		 * sd_return_failed_command().
30133 		 */
30134 		sd_return_failed_command_no_restart(un, bp, EIO);
30135 	}
30136 
30137 	/* Flush the xbuf queues if required. */
30138 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
30139 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
30140 	}
30141 
30142 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
30143 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
30144 }
30145 
30146 
30147 /*
30148  *    Function: sd_failfast_flushq_callback
30149  *
30150  * Description: Return TRUE if the given bp meets the criteria for failfast
30151  *		flushing. Used with ddi_xbuf_flushq(9F).
30152  *
30153  *   Arguments: bp - ptr to buf struct to be examined.
30154  *
30155  *     Context: Any
30156  */
30157 
30158 static int
30159 sd_failfast_flushq_callback(struct buf *bp)
30160 {
30161 	/*
30162 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
30163 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
30164 	 */
30165 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
30166 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
30167 }
30168 
30169 
30170 
30171 #if defined(__i386) || defined(__amd64)
30172 /*
30173  * Function: sd_setup_next_xfer
30174  *
30175  * Description: Prepare next I/O operation using DMA_PARTIAL
30176  *
30177  */
30178 
30179 static int
30180 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
30181     struct scsi_pkt *pkt, struct sd_xbuf *xp)
30182 {
30183 	ssize_t	num_blks_not_xfered;
30184 	daddr_t	strt_blk_num;
30185 	ssize_t	bytes_not_xfered;
30186 	int	rval;
30187 
30188 	ASSERT(pkt->pkt_resid == 0);
30189 
30190 	/*
30191 	 * Calculate next block number and amount to be transferred.
30192 	 *
30193 	 * How much data NOT transfered to the HBA yet.
30194 	 */
30195 	bytes_not_xfered = xp->xb_dma_resid;
30196 
30197 	/*
30198 	 * figure how many blocks NOT transfered to the HBA yet.
30199 	 */
30200 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
30201 
30202 	/*
30203 	 * set starting block number to the end of what WAS transfered.
30204 	 */
30205 	strt_blk_num = xp->xb_blkno +
30206 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
30207 
30208 	/*
30209 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
30210 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
30211 	 * the disk mutex here.
30212 	 */
30213 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
30214 	    strt_blk_num, num_blks_not_xfered);
30215 
30216 	if (rval == 0) {
30217 
30218 		/*
30219 		 * Success.
30220 		 *
30221 		 * Adjust things if there are still more blocks to be
30222 		 * transfered.
30223 		 */
30224 		xp->xb_dma_resid = pkt->pkt_resid;
30225 		pkt->pkt_resid = 0;
30226 
30227 		return (1);
30228 	}
30229 
30230 	/*
30231 	 * There's really only one possible return value from
30232 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
30233 	 * returns NULL.
30234 	 */
30235 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
30236 
30237 	bp->b_resid = bp->b_bcount;
30238 	bp->b_flags |= B_ERROR;
30239 
30240 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30241 	    "Error setting up next portion of DMA transfer\n");
30242 
30243 	return (0);
30244 }
30245 #endif
30246 
30247 /*
30248  *    Function: sd_panic_for_res_conflict
30249  *
30250  * Description: Call panic with a string formated with "Reservation Conflict"
30251  *		and a human readable identifier indicating the SD instance
30252  *		that experienced the reservation conflict.
30253  *
30254  *   Arguments: un - pointer to the soft state struct for the instance.
30255  *
30256  *     Context: may execute in interrupt context.
30257  */
30258 
30259 #define	SD_RESV_CONFLICT_FMT_LEN 40
30260 void
30261 sd_panic_for_res_conflict(struct sd_lun *un)
30262 {
30263 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30264 	char path_str[MAXPATHLEN];
30265 
30266 	(void) snprintf(panic_str, sizeof (panic_str),
30267 	    "Reservation Conflict\nDisk: %s",
30268 	    ddi_pathname(SD_DEVINFO(un), path_str));
30269 
30270 	panic(panic_str);
30271 }
30272 
30273 /*
30274  * Note: The following sd_faultinjection_ioctl( ) routines implement
30275  * driver support for handling fault injection for error analysis
30276  * causing faults in multiple layers of the driver.
30277  *
30278  */
30279 
30280 #ifdef SD_FAULT_INJECTION
30281 static uint_t   sd_fault_injection_on = 0;
30282 
30283 /*
30284  *    Function: sd_faultinjection_ioctl()
30285  *
30286  * Description: This routine is the driver entry point for handling
30287  *              faultinjection ioctls to inject errors into the
30288  *              layer model
30289  *
30290  *   Arguments: cmd	- the ioctl cmd recieved
30291  *		arg	- the arguments from user and returns
30292  */
30293 
30294 static void
30295 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
30296 
30297 	uint_t i;
30298 	uint_t rval;
30299 
30300 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30301 
30302 	mutex_enter(SD_MUTEX(un));
30303 
30304 	switch (cmd) {
30305 	case SDIOCRUN:
30306 		/* Allow pushed faults to be injected */
30307 		SD_INFO(SD_LOG_SDTEST, un,
30308 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
30309 
30310 		sd_fault_injection_on = 1;
30311 
30312 		SD_INFO(SD_LOG_IOERR, un,
30313 		    "sd_faultinjection_ioctl: run finished\n");
30314 		break;
30315 
30316 	case SDIOCSTART:
30317 		/* Start Injection Session */
30318 		SD_INFO(SD_LOG_SDTEST, un,
30319 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
30320 
30321 		sd_fault_injection_on = 0;
30322 		un->sd_injection_mask = 0xFFFFFFFF;
30323 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30324 			un->sd_fi_fifo_pkt[i] = NULL;
30325 			un->sd_fi_fifo_xb[i] = NULL;
30326 			un->sd_fi_fifo_un[i] = NULL;
30327 			un->sd_fi_fifo_arq[i] = NULL;
30328 		}
30329 		un->sd_fi_fifo_start = 0;
30330 		un->sd_fi_fifo_end = 0;
30331 
30332 		mutex_enter(&(un->un_fi_mutex));
30333 		un->sd_fi_log[0] = '\0';
30334 		un->sd_fi_buf_len = 0;
30335 		mutex_exit(&(un->un_fi_mutex));
30336 
30337 		SD_INFO(SD_LOG_IOERR, un,
30338 		    "sd_faultinjection_ioctl: start finished\n");
30339 		break;
30340 
30341 	case SDIOCSTOP:
30342 		/* Stop Injection Session */
30343 		SD_INFO(SD_LOG_SDTEST, un,
30344 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30345 		sd_fault_injection_on = 0;
30346 		un->sd_injection_mask = 0x0;
30347 
30348 		/* Empty stray or unuseds structs from fifo */
30349 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30350 			if (un->sd_fi_fifo_pkt[i] != NULL) {
30351 				kmem_free(un->sd_fi_fifo_pkt[i],
30352 				    sizeof (struct sd_fi_pkt));
30353 			}
30354 			if (un->sd_fi_fifo_xb[i] != NULL) {
30355 				kmem_free(un->sd_fi_fifo_xb[i],
30356 				    sizeof (struct sd_fi_xb));
30357 			}
30358 			if (un->sd_fi_fifo_un[i] != NULL) {
30359 				kmem_free(un->sd_fi_fifo_un[i],
30360 				    sizeof (struct sd_fi_un));
30361 			}
30362 			if (un->sd_fi_fifo_arq[i] != NULL) {
30363 				kmem_free(un->sd_fi_fifo_arq[i],
30364 				    sizeof (struct sd_fi_arq));
30365 			}
30366 			un->sd_fi_fifo_pkt[i] = NULL;
30367 			un->sd_fi_fifo_un[i] = NULL;
30368 			un->sd_fi_fifo_xb[i] = NULL;
30369 			un->sd_fi_fifo_arq[i] = NULL;
30370 		}
30371 		un->sd_fi_fifo_start = 0;
30372 		un->sd_fi_fifo_end = 0;
30373 
30374 		SD_INFO(SD_LOG_IOERR, un,
30375 		    "sd_faultinjection_ioctl: stop finished\n");
30376 		break;
30377 
30378 	case SDIOCINSERTPKT:
30379 		/* Store a packet struct to be pushed onto fifo */
30380 		SD_INFO(SD_LOG_SDTEST, un,
30381 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30382 
30383 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30384 
30385 		sd_fault_injection_on = 0;
30386 
30387 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30388 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30389 			kmem_free(un->sd_fi_fifo_pkt[i],
30390 			    sizeof (struct sd_fi_pkt));
30391 		}
30392 		if (arg != NULL) {
30393 			un->sd_fi_fifo_pkt[i] =
30394 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30395 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30396 				/* Alloc failed don't store anything */
30397 				break;
30398 			}
30399 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30400 			    sizeof (struct sd_fi_pkt), 0);
30401 			if (rval == -1) {
30402 				kmem_free(un->sd_fi_fifo_pkt[i],
30403 				    sizeof (struct sd_fi_pkt));
30404 				un->sd_fi_fifo_pkt[i] = NULL;
30405 			}
30406 		} else {
30407 			SD_INFO(SD_LOG_IOERR, un,
30408 			    "sd_faultinjection_ioctl: pkt null\n");
30409 		}
30410 		break;
30411 
30412 	case SDIOCINSERTXB:
30413 		/* Store a xb struct to be pushed onto fifo */
30414 		SD_INFO(SD_LOG_SDTEST, un,
30415 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30416 
30417 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30418 
30419 		sd_fault_injection_on = 0;
30420 
30421 		if (un->sd_fi_fifo_xb[i] != NULL) {
30422 			kmem_free(un->sd_fi_fifo_xb[i],
30423 			    sizeof (struct sd_fi_xb));
30424 			un->sd_fi_fifo_xb[i] = NULL;
30425 		}
30426 		if (arg != NULL) {
30427 			un->sd_fi_fifo_xb[i] =
30428 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30429 			if (un->sd_fi_fifo_xb[i] == NULL) {
30430 				/* Alloc failed don't store anything */
30431 				break;
30432 			}
30433 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30434 			    sizeof (struct sd_fi_xb), 0);
30435 
30436 			if (rval == -1) {
30437 				kmem_free(un->sd_fi_fifo_xb[i],
30438 				    sizeof (struct sd_fi_xb));
30439 				un->sd_fi_fifo_xb[i] = NULL;
30440 			}
30441 		} else {
30442 			SD_INFO(SD_LOG_IOERR, un,
30443 			    "sd_faultinjection_ioctl: xb null\n");
30444 		}
30445 		break;
30446 
30447 	case SDIOCINSERTUN:
30448 		/* Store a un struct to be pushed onto fifo */
30449 		SD_INFO(SD_LOG_SDTEST, un,
30450 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30451 
30452 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30453 
30454 		sd_fault_injection_on = 0;
30455 
30456 		if (un->sd_fi_fifo_un[i] != NULL) {
30457 			kmem_free(un->sd_fi_fifo_un[i],
30458 			    sizeof (struct sd_fi_un));
30459 			un->sd_fi_fifo_un[i] = NULL;
30460 		}
30461 		if (arg != NULL) {
30462 			un->sd_fi_fifo_un[i] =
30463 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30464 			if (un->sd_fi_fifo_un[i] == NULL) {
30465 				/* Alloc failed don't store anything */
30466 				break;
30467 			}
30468 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30469 			    sizeof (struct sd_fi_un), 0);
30470 			if (rval == -1) {
30471 				kmem_free(un->sd_fi_fifo_un[i],
30472 				    sizeof (struct sd_fi_un));
30473 				un->sd_fi_fifo_un[i] = NULL;
30474 			}
30475 
30476 		} else {
30477 			SD_INFO(SD_LOG_IOERR, un,
30478 			    "sd_faultinjection_ioctl: un null\n");
30479 		}
30480 
30481 		break;
30482 
30483 	case SDIOCINSERTARQ:
30484 		/* Store a arq struct to be pushed onto fifo */
30485 		SD_INFO(SD_LOG_SDTEST, un,
30486 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30487 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30488 
30489 		sd_fault_injection_on = 0;
30490 
30491 		if (un->sd_fi_fifo_arq[i] != NULL) {
30492 			kmem_free(un->sd_fi_fifo_arq[i],
30493 			    sizeof (struct sd_fi_arq));
30494 			un->sd_fi_fifo_arq[i] = NULL;
30495 		}
30496 		if (arg != NULL) {
30497 			un->sd_fi_fifo_arq[i] =
30498 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30499 			if (un->sd_fi_fifo_arq[i] == NULL) {
30500 				/* Alloc failed don't store anything */
30501 				break;
30502 			}
30503 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30504 			    sizeof (struct sd_fi_arq), 0);
30505 			if (rval == -1) {
30506 				kmem_free(un->sd_fi_fifo_arq[i],
30507 				    sizeof (struct sd_fi_arq));
30508 				un->sd_fi_fifo_arq[i] = NULL;
30509 			}
30510 
30511 		} else {
30512 			SD_INFO(SD_LOG_IOERR, un,
30513 			    "sd_faultinjection_ioctl: arq null\n");
30514 		}
30515 
30516 		break;
30517 
30518 	case SDIOCPUSH:
30519 		/* Push stored xb, pkt, un, and arq onto fifo */
30520 		sd_fault_injection_on = 0;
30521 
30522 		if (arg != NULL) {
30523 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30524 			if (rval != -1 &&
30525 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30526 				un->sd_fi_fifo_end += i;
30527 			}
30528 		} else {
30529 			SD_INFO(SD_LOG_IOERR, un,
30530 			    "sd_faultinjection_ioctl: push arg null\n");
30531 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30532 				un->sd_fi_fifo_end++;
30533 			}
30534 		}
30535 		SD_INFO(SD_LOG_IOERR, un,
30536 		    "sd_faultinjection_ioctl: push to end=%d\n",
30537 		    un->sd_fi_fifo_end);
30538 		break;
30539 
30540 	case SDIOCRETRIEVE:
30541 		/* Return buffer of log from Injection session */
30542 		SD_INFO(SD_LOG_SDTEST, un,
30543 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30544 
30545 		sd_fault_injection_on = 0;
30546 
30547 		mutex_enter(&(un->un_fi_mutex));
30548 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30549 		    un->sd_fi_buf_len+1, 0);
30550 		mutex_exit(&(un->un_fi_mutex));
30551 
30552 		if (rval == -1) {
30553 			/*
30554 			 * arg is possibly invalid setting
30555 			 * it to NULL for return
30556 			 */
30557 			arg = NULL;
30558 		}
30559 		break;
30560 	}
30561 
30562 	mutex_exit(SD_MUTEX(un));
30563 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30564 			    " exit\n");
30565 }
30566 
30567 
30568 /*
30569  *    Function: sd_injection_log()
30570  *
30571  * Description: This routine adds buff to the already existing injection log
30572  *              for retrieval via faultinjection_ioctl for use in fault
30573  *              detection and recovery
30574  *
30575  *   Arguments: buf - the string to add to the log
30576  */
30577 
30578 static void
30579 sd_injection_log(char *buf, struct sd_lun *un)
30580 {
30581 	uint_t len;
30582 
30583 	ASSERT(un != NULL);
30584 	ASSERT(buf != NULL);
30585 
30586 	mutex_enter(&(un->un_fi_mutex));
30587 
30588 	len = min(strlen(buf), 255);
30589 	/* Add logged value to Injection log to be returned later */
30590 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30591 		uint_t	offset = strlen((char *)un->sd_fi_log);
30592 		char *destp = (char *)un->sd_fi_log + offset;
30593 		int i;
30594 		for (i = 0; i < len; i++) {
30595 			*destp++ = *buf++;
30596 		}
30597 		un->sd_fi_buf_len += len;
30598 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30599 	}
30600 
30601 	mutex_exit(&(un->un_fi_mutex));
30602 }
30603 
30604 
30605 /*
30606  *    Function: sd_faultinjection()
30607  *
30608  * Description: This routine takes the pkt and changes its
30609  *		content based on error injection scenerio.
30610  *
30611  *   Arguments: pktp	- packet to be changed
30612  */
30613 
30614 static void
30615 sd_faultinjection(struct scsi_pkt *pktp)
30616 {
30617 	uint_t i;
30618 	struct sd_fi_pkt *fi_pkt;
30619 	struct sd_fi_xb *fi_xb;
30620 	struct sd_fi_un *fi_un;
30621 	struct sd_fi_arq *fi_arq;
30622 	struct buf *bp;
30623 	struct sd_xbuf *xb;
30624 	struct sd_lun *un;
30625 
30626 	ASSERT(pktp != NULL);
30627 
30628 	/* pull bp xb and un from pktp */
30629 	bp = (struct buf *)pktp->pkt_private;
30630 	xb = SD_GET_XBUF(bp);
30631 	un = SD_GET_UN(bp);
30632 
30633 	ASSERT(un != NULL);
30634 
30635 	mutex_enter(SD_MUTEX(un));
30636 
30637 	SD_TRACE(SD_LOG_SDTEST, un,
30638 	    "sd_faultinjection: entry Injection from sdintr\n");
30639 
30640 	/* if injection is off return */
30641 	if (sd_fault_injection_on == 0 ||
30642 		un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30643 		mutex_exit(SD_MUTEX(un));
30644 		return;
30645 	}
30646 
30647 
30648 	/* take next set off fifo */
30649 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30650 
30651 	fi_pkt = un->sd_fi_fifo_pkt[i];
30652 	fi_xb = un->sd_fi_fifo_xb[i];
30653 	fi_un = un->sd_fi_fifo_un[i];
30654 	fi_arq = un->sd_fi_fifo_arq[i];
30655 
30656 
30657 	/* set variables accordingly */
30658 	/* set pkt if it was on fifo */
30659 	if (fi_pkt != NULL) {
30660 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30661 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30662 		SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30663 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30664 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30665 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30666 
30667 	}
30668 
30669 	/* set xb if it was on fifo */
30670 	if (fi_xb != NULL) {
30671 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30672 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30673 		SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30674 		SD_CONDSET(xb, xb, xb_victim_retry_count,
30675 		    "xb_victim_retry_count");
30676 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30677 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30678 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30679 
30680 		/* copy in block data from sense */
30681 		if (fi_xb->xb_sense_data[0] != -1) {
30682 			bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30683 			    SENSE_LENGTH);
30684 		}
30685 
30686 		/* copy in extended sense codes */
30687 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code,
30688 		    "es_code");
30689 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key,
30690 		    "es_key");
30691 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code,
30692 		    "es_add_code");
30693 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb,
30694 		    es_qual_code, "es_qual_code");
30695 	}
30696 
30697 	/* set un if it was on fifo */
30698 	if (fi_un != NULL) {
30699 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30700 		SD_CONDSET(un, un, un_ctype, "un_ctype");
30701 		SD_CONDSET(un, un, un_reset_retry_count,
30702 		    "un_reset_retry_count");
30703 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30704 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30705 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30706 		SD_CONDSET(un, un, un_f_geometry_is_valid,
30707 		    "un_f_geometry_is_valid");
30708 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30709 		    "un_f_allow_bus_device_reset");
30710 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30711 
30712 	}
30713 
30714 	/* copy in auto request sense if it was on fifo */
30715 	if (fi_arq != NULL) {
30716 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30717 	}
30718 
30719 	/* free structs */
30720 	if (un->sd_fi_fifo_pkt[i] != NULL) {
30721 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30722 	}
30723 	if (un->sd_fi_fifo_xb[i] != NULL) {
30724 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30725 	}
30726 	if (un->sd_fi_fifo_un[i] != NULL) {
30727 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30728 	}
30729 	if (un->sd_fi_fifo_arq[i] != NULL) {
30730 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30731 	}
30732 
30733 	/*
30734 	 * kmem_free does not gurantee to set to NULL
30735 	 * since we uses these to determine if we set
30736 	 * values or not lets confirm they are always
30737 	 * NULL after free
30738 	 */
30739 	un->sd_fi_fifo_pkt[i] = NULL;
30740 	un->sd_fi_fifo_un[i] = NULL;
30741 	un->sd_fi_fifo_xb[i] = NULL;
30742 	un->sd_fi_fifo_arq[i] = NULL;
30743 
30744 	un->sd_fi_fifo_start++;
30745 
30746 	mutex_exit(SD_MUTEX(un));
30747 
30748 	SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30749 }
30750 
30751 #endif /* SD_FAULT_INJECTION */
30752 
30753 /*
30754  * This routine is invoked in sd_unit_attach(). Before calling it, the
30755  * properties in conf file should be processed already, and "hotpluggable"
30756  * property was processed also.
30757  *
30758  * The sd driver distinguishes 3 different type of devices: removable media,
30759  * non-removable media, and hotpluggable. Below the differences are defined:
30760  *
30761  * 1. Device ID
30762  *
30763  *     The device ID of a device is used to identify this device. Refer to
30764  *     ddi_devid_register(9F).
30765  *
30766  *     For a non-removable media disk device which can provide 0x80 or 0x83
30767  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30768  *     device ID is created to identify this device. For other non-removable
30769  *     media devices, a default device ID is created only if this device has
30770  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30771  *
30772  *     -------------------------------------------------------
30773  *     removable media   hotpluggable  | Can Have Device ID
30774  *     -------------------------------------------------------
30775  *         false             false     |     Yes
30776  *         false             true      |     Yes
30777  *         true                x       |     No
30778  *     ------------------------------------------------------
30779  *
30780  *
30781  * 2. SCSI group 4 commands
30782  *
30783  *     In SCSI specs, only some commands in group 4 command set can use
30784  *     8-byte addresses that can be used to access >2TB storage spaces.
30785  *     Other commands have no such capability. Without supporting group4,
30786  *     it is impossible to make full use of storage spaces of a disk with
30787  *     capacity larger than 2TB.
30788  *
30789  *     -----------------------------------------------
30790  *     removable media   hotpluggable   LP64  |  Group
30791  *     -----------------------------------------------
30792  *           false          false       false |   1
30793  *           false          false       true  |   4
30794  *           false          true        false |   1
30795  *           false          true        true  |   4
30796  *           true             x           x   |   5
30797  *     -----------------------------------------------
30798  *
30799  *
30800  * 3. Check for VTOC Label
30801  *
30802  *     If a direct-access disk has no EFI label, sd will check if it has a
30803  *     valid VTOC label. Now, sd also does that check for removable media
30804  *     and hotpluggable devices.
30805  *
30806  *     --------------------------------------------------------------
30807  *     Direct-Access   removable media    hotpluggable |  Check Label
30808  *     -------------------------------------------------------------
30809  *         false          false           false        |   No
30810  *         false          false           true         |   No
30811  *         false          true            false        |   Yes
30812  *         false          true            true         |   Yes
30813  *         true            x                x          |   Yes
30814  *     --------------------------------------------------------------
30815  *
30816  *
30817  * 4. Building default VTOC label
30818  *
30819  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30820  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30821  *     create default VTOC for them. Currently sd creates default VTOC label
30822  *     for all devices on x86 platform (VTOC_16), but only for removable
30823  *     media devices on SPARC (VTOC_8).
30824  *
30825  *     -----------------------------------------------------------
30826  *       removable media hotpluggable platform   |   Default Label
30827  *     -----------------------------------------------------------
30828  *             false          false    sparc     |     No
30829  *             false          true      x86      |     Yes
30830  *             false          true     sparc     |     Yes
30831  *             true             x        x       |     Yes
30832  *     ----------------------------------------------------------
30833  *
30834  *
30835  * 5. Supported blocksizes of target devices
30836  *
30837  *     Sd supports non-512-byte blocksize for removable media devices only.
30838  *     For other devices, only 512-byte blocksize is supported. This may be
30839  *     changed in near future because some RAID devices require non-512-byte
30840  *     blocksize
30841  *
30842  *     -----------------------------------------------------------
30843  *     removable media    hotpluggable    | non-512-byte blocksize
30844  *     -----------------------------------------------------------
30845  *           false          false         |   No
30846  *           false          true          |   No
30847  *           true             x           |   Yes
30848  *     -----------------------------------------------------------
30849  *
30850  *
30851  * 6. Automatic mount & unmount
30852  *
30853  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30854  *     if a device is removable media device. It return 1 for removable media
30855  *     devices, and 0 for others.
30856  *
30857  *     The automatic mounting subsystem should distinguish between the types
30858  *     of devices and apply automounting policies to each.
30859  *
30860  *
30861  * 7. fdisk partition management
30862  *
30863  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30864  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
30865  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30866  *     fdisk partitions on both x86 and SPARC platform.
30867  *
30868  *     -----------------------------------------------------------
30869  *       platform   removable media  USB/1394  |  fdisk supported
30870  *     -----------------------------------------------------------
30871  *        x86         X               X        |       true
30872  *     ------------------------------------------------------------
30873  *        sparc       X               X        |       false
30874  *     ------------------------------------------------------------
30875  *
30876  *
30877  * 8. MBOOT/MBR
30878  *
30879  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30880  *     read/write mboot for removable media devices on sparc platform.
30881  *
30882  *     -----------------------------------------------------------
30883  *       platform   removable media  USB/1394  |  mboot supported
30884  *     -----------------------------------------------------------
30885  *        x86         X               X        |       true
30886  *     ------------------------------------------------------------
30887  *        sparc      false           false     |       false
30888  *        sparc      false           true      |       true
30889  *        sparc      true            false     |       true
30890  *        sparc      true            true      |       true
30891  *     ------------------------------------------------------------
30892  *
30893  *
30894  * 9.  error handling during opening device
30895  *
30896  *     If failed to open a disk device, an errno is returned. For some kinds
30897  *     of errors, different errno is returned depending on if this device is
30898  *     a removable media device. This brings USB/1394 hard disks in line with
30899  *     expected hard disk behavior. It is not expected that this breaks any
30900  *     application.
30901  *
30902  *     ------------------------------------------------------
30903  *       removable media    hotpluggable   |  errno
30904  *     ------------------------------------------------------
30905  *             false          false        |   EIO
30906  *             false          true         |   EIO
30907  *             true             x          |   ENXIO
30908  *     ------------------------------------------------------
30909  *
30910  *
30911  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30912  *
30913  *     These IOCTLs are applicable only to removable media devices.
30914  *
30915  *     -----------------------------------------------------------
30916  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30917  *     -----------------------------------------------------------
30918  *             false          false        |     No
30919  *             false          true         |     No
30920  *             true            x           |     Yes
30921  *     -----------------------------------------------------------
30922  *
30923  *
30924  * 12. Kstats for partitions
30925  *
30926  *     sd creates partition kstat for non-removable media devices. USB and
30927  *     Firewire hard disks now have partition kstats
30928  *
30929  *      ------------------------------------------------------
30930  *       removable media    hotplugable    |   kstat
30931  *      ------------------------------------------------------
30932  *             false          false        |    Yes
30933  *             false          true         |    Yes
30934  *             true             x          |    No
30935  *       ------------------------------------------------------
30936  *
30937  *
30938  * 13. Removable media & hotpluggable properties
30939  *
30940  *     Sd driver creates a "removable-media" property for removable media
30941  *     devices. Parent nexus drivers create a "hotpluggable" property if
30942  *     it supports hotplugging.
30943  *
30944  *     ---------------------------------------------------------------------
30945  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30946  *     ---------------------------------------------------------------------
30947  *       false            false       |    No                   No
30948  *       false            true        |    No                   Yes
30949  *       true             false       |    Yes                  No
30950  *       true             true        |    Yes                  Yes
30951  *     ---------------------------------------------------------------------
30952  *
30953  *
30954  * 14. Power Management
30955  *
30956  *     sd only power manages removable media devices or devices that support
30957  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30958  *
30959  *     A parent nexus that supports hotplugging can also set "pm-capable"
30960  *     if the disk can be power managed.
30961  *
30962  *     ------------------------------------------------------------
30963  *       removable media hotpluggable pm-capable  |   power manage
30964  *     ------------------------------------------------------------
30965  *             false          false     false     |     No
30966  *             false          false     true      |     Yes
30967  *             false          true      false     |     No
30968  *             false          true      true      |     Yes
30969  *             true             x        x        |     Yes
30970  *     ------------------------------------------------------------
30971  *
30972  *      USB and firewire hard disks can now be power managed independently
30973  *      of the framebuffer
30974  *
30975  *
30976  * 15. Support for USB disks with capacity larger than 1TB
30977  *
30978  *     Currently, sd doesn't permit a fixed disk device with capacity
30979  *     larger than 1TB to be used in a 32-bit operating system environment.
30980  *     However, sd doesn't do that for removable media devices. Instead, it
30981  *     assumes that removable media devices cannot have a capacity larger
30982  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30983  *     supported, which can cause some unexpected results.
30984  *
30985  *     ---------------------------------------------------------------------
30986  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30987  *     ---------------------------------------------------------------------
30988  *             false          false  |   true         |     no
30989  *             false          true   |   true         |     no
30990  *             true           false  |   true         |     Yes
30991  *             true           true   |   true         |     Yes
30992  *     ---------------------------------------------------------------------
30993  *
30994  *
30995  * 16. Check write-protection at open time
30996  *
30997  *     When a removable media device is being opened for writing without NDELAY
30998  *     flag, sd will check if this device is writable. If attempting to open
30999  *     without NDELAY flag a write-protected device, this operation will abort.
31000  *
31001  *     ------------------------------------------------------------
31002  *       removable media    USB/1394   |   WP Check
31003  *     ------------------------------------------------------------
31004  *             false          false    |     No
31005  *             false          true     |     No
31006  *             true           false    |     Yes
31007  *             true           true     |     Yes
31008  *     ------------------------------------------------------------
31009  *
31010  *
31011  * 17. syslog when corrupted VTOC is encountered
31012  *
31013  *      Currently, if an invalid VTOC is encountered, sd only print syslog
31014  *      for fixed SCSI disks.
31015  *     ------------------------------------------------------------
31016  *       removable media    USB/1394   |   print syslog
31017  *     ------------------------------------------------------------
31018  *             false          false    |     Yes
31019  *             false          true     |     No
31020  *             true           false    |     No
31021  *             true           true     |     No
31022  *     ------------------------------------------------------------
31023  */
31024 static void
31025 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
31026 {
31027 	int	pm_capable_prop;
31028 
31029 	ASSERT(un->un_sd);
31030 	ASSERT(un->un_sd->sd_inq);
31031 
31032 #if defined(_SUNOS_VTOC_16)
31033 	/*
31034 	 * For VTOC_16 devices, the default label will be created for all
31035 	 * devices. (see sd_build_default_label)
31036 	 */
31037 	un->un_f_default_vtoc_supported = TRUE;
31038 #endif
31039 
31040 	/*
31041 	 * Enable SYNC CACHE support for all devices.
31042 	 */
31043 	un->un_f_sync_cache_supported = TRUE;
31044 
31045 	if (un->un_sd->sd_inq->inq_rmb) {
31046 		/*
31047 		 * The media of this device is removable. And for this kind
31048 		 * of devices, it is possible to change medium after opening
31049 		 * devices. Thus we should support this operation.
31050 		 */
31051 		un->un_f_has_removable_media = TRUE;
31052 
31053 #if defined(_SUNOS_VTOC_8)
31054 		/*
31055 		 * Note: currently, for VTOC_8 devices, default label is
31056 		 * created for removable and hotpluggable devices only.
31057 		 */
31058 		un->un_f_default_vtoc_supported = TRUE;
31059 #endif
31060 		/*
31061 		 * support non-512-byte blocksize of removable media devices
31062 		 */
31063 		un->un_f_non_devbsize_supported = TRUE;
31064 
31065 		/*
31066 		 * Assume that all removable media devices support DOOR_LOCK
31067 		 */
31068 		un->un_f_doorlock_supported = TRUE;
31069 
31070 		/*
31071 		 * For a removable media device, it is possible to be opened
31072 		 * with NDELAY flag when there is no media in drive, in this
31073 		 * case we don't care if device is writable. But if without
31074 		 * NDELAY flag, we need to check if media is write-protected.
31075 		 */
31076 		un->un_f_chk_wp_open = TRUE;
31077 
31078 		/*
31079 		 * need to start a SCSI watch thread to monitor media state,
31080 		 * when media is being inserted or ejected, notify syseventd.
31081 		 */
31082 		un->un_f_monitor_media_state = TRUE;
31083 
31084 		/*
31085 		 * Some devices don't support START_STOP_UNIT command.
31086 		 * Therefore, we'd better check if a device supports it
31087 		 * before sending it.
31088 		 */
31089 		un->un_f_check_start_stop = TRUE;
31090 
31091 		/*
31092 		 * support eject media ioctl:
31093 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
31094 		 */
31095 		un->un_f_eject_media_supported = TRUE;
31096 
31097 		/*
31098 		 * Because many removable-media devices don't support
31099 		 * LOG_SENSE, we couldn't use this command to check if
31100 		 * a removable media device support power-management.
31101 		 * We assume that they support power-management via
31102 		 * START_STOP_UNIT command and can be spun up and down
31103 		 * without limitations.
31104 		 */
31105 		un->un_f_pm_supported = TRUE;
31106 
31107 		/*
31108 		 * Need to create a zero length (Boolean) property
31109 		 * removable-media for the removable media devices.
31110 		 * Note that the return value of the property is not being
31111 		 * checked, since if unable to create the property
31112 		 * then do not want the attach to fail altogether. Consistent
31113 		 * with other property creation in attach.
31114 		 */
31115 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
31116 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
31117 
31118 	} else {
31119 		/*
31120 		 * create device ID for device
31121 		 */
31122 		un->un_f_devid_supported = TRUE;
31123 
31124 		/*
31125 		 * Spin up non-removable-media devices once it is attached
31126 		 */
31127 		un->un_f_attach_spinup = TRUE;
31128 
31129 		/*
31130 		 * According to SCSI specification, Sense data has two kinds of
31131 		 * format: fixed format, and descriptor format. At present, we
31132 		 * don't support descriptor format sense data for removable
31133 		 * media.
31134 		 */
31135 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
31136 			un->un_f_descr_format_supported = TRUE;
31137 		}
31138 
31139 		/*
31140 		 * kstats are created only for non-removable media devices.
31141 		 *
31142 		 * Set this in sd.conf to 0 in order to disable kstats.  The
31143 		 * default is 1, so they are enabled by default.
31144 		 */
31145 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
31146 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
31147 			"enable-partition-kstats", 1));
31148 
31149 		/*
31150 		 * Check if HBA has set the "pm-capable" property.
31151 		 * If "pm-capable" exists and is non-zero then we can
31152 		 * power manage the device without checking the start/stop
31153 		 * cycle count log sense page.
31154 		 *
31155 		 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0)
31156 		 * then we should not power manage the device.
31157 		 *
31158 		 * If "pm-capable" doesn't exist then pm_capable_prop will
31159 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
31160 		 * sd will check the start/stop cycle count log sense page
31161 		 * and power manage the device if the cycle count limit has
31162 		 * not been exceeded.
31163 		 */
31164 		pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
31165 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
31166 		if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) {
31167 			un->un_f_log_sense_supported = TRUE;
31168 		} else {
31169 			/*
31170 			 * pm-capable property exists.
31171 			 *
31172 			 * Convert "TRUE" values for pm_capable_prop to
31173 			 * SD_PM_CAPABLE_TRUE (1) to make it easier to check
31174 			 * later. "TRUE" values are any values except
31175 			 * SD_PM_CAPABLE_FALSE (0) and
31176 			 * SD_PM_CAPABLE_UNDEFINED (-1)
31177 			 */
31178 			if (pm_capable_prop == SD_PM_CAPABLE_FALSE) {
31179 				un->un_f_log_sense_supported = FALSE;
31180 			} else {
31181 				un->un_f_pm_supported = TRUE;
31182 			}
31183 
31184 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
31185 			    "sd_unit_attach: un:0x%p pm-capable "
31186 			    "property set to %d.\n", un, un->un_f_pm_supported);
31187 		}
31188 	}
31189 
31190 	if (un->un_f_is_hotpluggable) {
31191 #if defined(_SUNOS_VTOC_8)
31192 		/*
31193 		 * Note: currently, for VTOC_8 devices, default label is
31194 		 * created for removable and hotpluggable devices only.
31195 		 */
31196 		un->un_f_default_vtoc_supported = TRUE;
31197 #endif
31198 
31199 		/*
31200 		 * Have to watch hotpluggable devices as well, since
31201 		 * that's the only way for userland applications to
31202 		 * detect hot removal while device is busy/mounted.
31203 		 */
31204 		un->un_f_monitor_media_state = TRUE;
31205 
31206 		un->un_f_check_start_stop = TRUE;
31207 
31208 	}
31209 
31210 	/*
31211 	 * By default, only DIRECT ACCESS devices and CDs will have Sun
31212 	 * labels.
31213 	 */
31214 	if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) ||
31215 	    (un->un_sd->sd_inq->inq_rmb)) {
31216 		/*
31217 		 * Direct access devices have disk label
31218 		 */
31219 		un->un_f_vtoc_label_supported = TRUE;
31220 	}
31221 
31222 	/*
31223 	 * Fdisk partitions are supported for all direct access devices on
31224 	 * x86 platform, and just for removable media and hotpluggable
31225 	 * devices on SPARC platform. Later, we will set the following flag
31226 	 * to FALSE if current device is not removable media or hotpluggable
31227 	 * device and if sd works on SAPRC platform.
31228 	 */
31229 	if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
31230 		un->un_f_mboot_supported = TRUE;
31231 	}
31232 
31233 	if (!un->un_f_is_hotpluggable &&
31234 	    !un->un_sd->sd_inq->inq_rmb) {
31235 
31236 #if defined(_SUNOS_VTOC_8)
31237 		/*
31238 		 * Don't support fdisk on fixed disk
31239 		 */
31240 		un->un_f_mboot_supported = FALSE;
31241 #endif
31242 
31243 		/*
31244 		 * For fixed disk, if its VTOC is not valid, we will write
31245 		 * errlog into system log
31246 		 */
31247 		if (un->un_f_vtoc_label_supported)
31248 			un->un_f_vtoc_errlog_supported = TRUE;
31249 	}
31250 }
31251